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

feat: (flatten, unflatten) Add the transformKey opt. #88

Merged
merged 1 commit into from
Oct 22, 2019
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,55 @@ flatten({
// }
```

### transformKey

Transform each part of a flat key before and after flattening.

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

flatten({
key1: {
keyA: 'valueI'
},
key2: {
keyB: 'valueII'
},
key3: { a: { b: { c: 2 } } }
}, {
transformKey: function(key){
return '__' + key + '__';
}
})

// {
// '__key1__.__keyA__': 'valueI',
// '__key2__.__keyB__': 'valueII',
// '__key3__.__a__.__b__.__c__': 2
// }

unflatten({
'__key1__.__keyA__': 'valueI',
'__key2__.__keyB__': 'valueII',
'__key3__.__a__.__b__.__c__': 2
}, {
transformKey: function(key){
return key.substring(2, key.length - 2)
}
})

// {
// 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
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ module.exports = flatten
flatten.flatten = flatten
flatten.unflatten = unflatten

function keyIdentity (key) {
return key
}

function flatten (target, opts) {
opts = opts || {}

var delimiter = opts.delimiter || '.'
var maxDepth = opts.maxDepth
var transformKey = opts.transformKey || keyIdentity
var output = {}

function step (object, prev, currentDepth) {
Expand All @@ -24,8 +29,8 @@ function flatten (target, opts) {
)

var newKey = prev
? prev + delimiter + key
: key
? prev + delimiter + transformKey(key)
: transformKey(key)

if (!isarray && !isbuffer && isobject && Object.keys(value).length &&
(!opts.maxDepth || currentDepth < maxDepth)) {
Expand All @@ -46,6 +51,7 @@ function unflatten (target, opts) {

var delimiter = opts.delimiter || '.'
var overwrite = opts.overwrite || false
var transformKey = opts.transformKey || keyIdentity
var result = {}

var isbuffer = isBuffer(target)
Expand All @@ -71,7 +77,7 @@ function unflatten (target, opts) {
})

sortedKeys.forEach(function (key) {
var split = key.split(delimiter)
var split = key.split(delimiter).map(transformKey)
var key1 = getkey(split.shift())
var key2 = getkey(split[0])
var recipient = result
Expand Down
Loading