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

Unflatten dot-notation keys nested in an array #82

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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ Use a custom delimiter for (un)flattening your objects, instead of `.`.

### safe

When enabled, both `flat` and `unflatten` will preserve arrays and their
contents. This is disabled by default.
When enabled `flatten` will preserve arrays and their contents. This is disabled by default.

``` javascript
var flatten = require('flat')
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ function unflatten (target, opts) {
var result = {}

var isbuffer = isBuffer(target)
if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
if (Array.isArray(target)) {
return target.map(function (item) {
return unflatten(item, opts)
})
} else if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
return target
}

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

suite('.safe', function () {
test('Should not protect arrays when false', function () {
assert.deepEqual(flatten({
hello: [
{ world: { again: 'foo' } },
{ lorem: 'ipsum' }
]
}, {
safe: false
}), {
'hello.0.world.again': 'foo',
'hello.1.lorem': 'ipsum'
})
})

test('Should protect arrays when true', function () {
assert.deepEqual(flatten({
hello: [
{ world: { again: 'foo' } },
{ lorem: 'ipsum' }
],
another: {
nested: [{ array: { too: 'deep' } }]
},
lorem: {
ipsum: 'whoop'
}
}, {
safe: true
}), {
hello: [
{ world: { again: 'foo' } },
{ lorem: 'ipsum' }
],
'lorem.ipsum': 'whoop',
'another.nested': [{ array: { too: 'deep' } }]
})
})
})
})

suite('Unflatten', function () {
Expand Down Expand Up @@ -327,41 +367,17 @@ suite('Unflatten', function () {
})

suite('.safe', function () {
test('Should protect arrays when true', function () {
assert.deepEqual(flatten({
hello: [
{ world: { again: 'foo' } },
{ lorem: 'ipsum' }
],
another: {
nested: [{ array: { too: 'deep' } }]
},
lorem: {
ipsum: 'whoop'
}
}, {
safe: true
}), {
hello: [
{ world: { again: 'foo' } },
{ lorem: 'ipsum' }
],
'lorem.ipsum': 'whoop',
'another.nested': [{ array: { too: 'deep' } }]
})
})

test('Should not protect arrays when false', function () {
assert.deepEqual(flatten({
test('Should unflatten dot notation keys nested in array', function () {
assert.deepEqual(unflatten({
hello: [
{ world: { again: 'foo' } },
{ lorem: 'ipsum' }
{ 'something.nested': true }
]
}, {
safe: false
}), {
'hello.0.world.again': 'foo',
'hello.1.lorem': 'ipsum'
hello: [
{ something: { nested: true } }
]
})
})
})
Expand Down