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

107 add transformfirst option #108

Open
wants to merge 4 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.idea
.idea
*.iml
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ flatten({

### transformKey

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

```javascript
var flatten = require('flat')
Expand Down Expand Up @@ -208,6 +208,59 @@ unflatten({
// }
```

### transformFirst

Transforms the top-level key before and after flattening. It has two arguments:

1. The key to transform
1. An optional delimiter. This is not required, but allows the transformation to
add a new key to the transformation.

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

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

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

unflatten({
'foo.key1.keyA': 'valueI',
'foo.key2.keyB': 'valueII',
'foo.key3.a.b.c': 2
}, {
transformFirst: function(key, delim){
return key.substring((key+delim).length)
}
})

// {
// 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
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function flatten (target, opts) {
const delimiter = opts.delimiter || '.'
const maxDepth = opts.maxDepth
const transformKey = opts.transformKey || keyIdentity
const transformFirst = opts.transformFirst || keyIdentity
const output = {}

function step (object, prev, currentDepth) {
Expand All @@ -30,18 +31,18 @@ function flatten (target, opts) {

const newKey = prev
? prev + delimiter + transformKey(key)
: transformKey(key)
: transformFirst(transformKey(key), delimiter)

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

output[newKey] = value
})
}

step(target)
step(target, false, 1)

return output
}
Expand All @@ -52,6 +53,7 @@ function unflatten (target, opts) {
const delimiter = opts.delimiter || '.'
const overwrite = opts.overwrite || false
const transformKey = opts.transformKey || keyIdentity
const transformFirst = opts.transformFirst || keyIdentity
const result = {}

const isbuffer = isBuffer(target)
Expand Down Expand Up @@ -110,7 +112,7 @@ function unflatten (target, opts) {
}, {})

Object.keys(target).forEach(function (key) {
const split = key.split(delimiter).map(transformKey)
const split = transformFirst(key, delimiter).split(delimiter).map(transformKey)
let key1 = getkey(split.shift())
let key2 = getkey(split[0])
let recipient = result
Expand Down
Loading