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

Prefix option #36

Open
wants to merge 6 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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ unflatten({

Use a custom delimiter for (un)flattening your objects, instead of `.`.

### keyname

Use a custom `function` to flatten the keyname. By default, the `delimiter` is inserted between `prev` and `next`

Here's an example that uses a colon (':') to prefix and delimit the keyname

````javascript
var o = {
hello: {
world: {
again: 'good morning'
}}}

flatten(o, { keyname: function(prev, next) {
return prev
? prev + ':' + next
: ':' + next
}})

// {
// ':hello:world:again': 'good morning'
// }
````

### keynames

Use a custom `function` to unflatten the keyname. It returns an array of key names. This is the inverse of [keyname](#keyname). By default, the `delimiter` is used to [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) the name.

### safe

When enabled, both `flat` and `unflatten` will preserve arrays and their
Expand Down
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ function flatten(target, opts) {

var delimiter = opts.delimiter || '.'
var maxDepth = opts.maxDepth

function flattenKeyname(prev, key) {
return prev
? prev + delimiter + key
: key
}

var keyname = opts.keyname || flattenKeyname;
var output = {}

function step(object, prev, currentDepth) {
Expand All @@ -23,9 +31,7 @@ function flatten(target, opts) {
type === "[object Array]"
)

var newKey = prev
? prev + delimiter + key
: key
var newKey = keyname(prev, key)

if (!isarray && !isbuffer && isobject && Object.keys(value).length &&
(!opts.maxDepth || currentDepth < maxDepth)) {
Expand Down Expand Up @@ -53,6 +59,12 @@ function unflatten(target, opts) {
return target
}

function unflattenKeyname(key) {
return key.split(delimiter)
}

var keynames = opts.keynames || unflattenKeyname;

// safely ensure that the key is
// an integer.
function getkey(key) {
Expand All @@ -66,7 +78,7 @@ function unflatten(target, opts) {
}

Object.keys(target).forEach(function(key) {
var split = key.split(delimiter)
var split = keynames(key)
var key1 = getkey(split.shift())
var key2 = getkey(split[0])
var recipient = result
Expand Down
32 changes: 32 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ suite('Flatten', function() {
})
})

test('Custom keyname', function() {
assert.deepEqual(flatten({
hello: {
world: {
again: 'good morning'
}
}
}, {
keyname: function(prev, key) {
return prev ? prev + ':' + key : ':' + key
}
}), {
':hello:world:again': 'good morning'
})
})

test('Empty Objects', function() {
assert.deepEqual(flatten({
hello: {
Expand Down Expand Up @@ -223,6 +239,22 @@ suite('Unflatten', function() {
}))
})

test('Custom keynames', function() {
assert.deepEqual({
hello: {
world: {
again: 'good morning'
}
}
}, unflatten({
':hello:world:again': 'good morning'
}, {
keynames: function(key) {
return key.substr(1).split(':')
}
}))
})

test('Overwrite', function() {
assert.deepEqual({
travis: {
Expand Down