Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to filter values that should be skipped #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,27 @@ flatten({
// 'key3.a': { b: { c: 2 } }
// }
```

### filter

Decide if a value should be flattened any further

```javascript
var flatten = require('flat')

flatten({
key1: {
keyA: 'valueI'
},
key2: {
keyB: 'valueII'
}
}, { filter: (value) => !value.keyA }) // skip key1

// {
// key1: {
// keyA: 'valueI'
// },
// 'key2.keyB': 'valueII'
// }
```
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function flatten(target, opts) {

var delimiter = opts.delimiter || '.'
var maxDepth = opts.maxDepth
var filter = opts.filter || function() { return true }
var currentDepth = 1
var output = {}

Expand All @@ -31,7 +32,7 @@ function flatten(target, opts) {
maxDepth = currentDepth + 1;
}

if (!isarray && !isbuffer && isobject && Object.keys(value).length && currentDepth < maxDepth) {
if (!isarray && !isbuffer && isobject && Object.keys(value).length && currentDepth < maxDepth && filter(value)) {
++currentDepth
return step(value, newKey)
}
Expand Down
50 changes: 50 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,56 @@ suite('Unflatten', function() {
})
})

suite('.filter', function() {
var everything = function() { return false }
var nothing = function() { return true }
var ifHasName = function(obj) { return !obj.name }

test('Should let a custom check decide if object should be flattened', function () {
var fixture = {
hello: {
world: 'hello',
good: {
bye: 'my friend'
},
something: {
nested: {
pretty: {
deep: true
},
name: 'hello'
}
},
array: ['of', 'values']
}
}

assert.deepEqual(flat(fixture, { filter: everything }), fixture)

assert.deepEqual(flat(fixture, { filter: nothing }), {
'hello.world': 'hello',
'hello.good.bye': 'my friend',
'hello.something.nested.pretty.deep': true,
'hello.something.nested.name': 'hello',
'hello.array.0': 'of',
'hello.array.1': 'values'
})

assert.deepEqual(flat(fixture, { filter: ifHasName }), {
'hello.world': 'hello',
'hello.good.bye': 'my friend',
'hello.something.nested': {
pretty: {
deep: true
},
name: 'hello'
},
'hello.array.0': 'of',
'hello.array.1': 'values'
})
})
})

suite('.safe', function() {
test('Should protect arrays when true', function() {
assert.deepEqual(flatten({
Expand Down