Skip to content
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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,36 @@ unflatten({
// }
```

### ignoreValue

Ignore specific values from flattening.

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

flatten({
key1: {
keyA: 'valueI'
},
key2: {
keyB: 'valueII'
},
key3: { a: { b: { c: 2 } } }
}, {
ignoreValue: function(value){
return !!value.c;
}
})

// {
// 'key1.keyA': 'valueI',
// 'key2.keyB__': 'valueII',
// 'key3.a.b': {'c':2},
// }

```

## Command Line Usage

`flat` is also available as a command line tool. You can run it with
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ function flatten (target, opts) {
: transformKey(key)

if (!isarray && !isbuffer && isobject && Object.keys(value).length &&
(!opts.maxDepth || currentDepth < maxDepth)) {
(!opts.maxDepth || currentDepth < maxDepth) &&
(!opts.ignoreValue || !opts.ignoreValue(value))) {
return step(value, newKey, currentDepth + 1)
}

Expand Down
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ suite('Flatten', function () {
'hello.0500': 'darkness my old friend'
})
})

test('Ignore Values', function () {
assert.deepStrictEqual(flatten({
hello: {
world: {
again: 'good morning'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
}, {
ignoreValue: function (value) {
return !!value.again;
}
}), {
'hello.world': {
again: 'good morning'
},
'lorem.ipsum.dolor': 'good evening'
})
});
})

suite('Unflatten', function () {
Expand Down