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
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export function unflatten (target, opts) {
target = Object.keys(target).reduce(function (result, key) {
const type = Object.prototype.toString.call(target[key])
const isObject = (type === '[object Object]' || type === '[object Array]')
if (!isObject || isEmpty(target[key])) {
// a `safe`-preserved array is a leaf value here, not a nested object to
// re-flatten; pass it through so `object: true` doesn't rebuild it as an
// object and break the `safe` contract of keeping arrays intact
if (!isObject || isEmpty(target[key]) || (opts.safe && Array.isArray(target[key]))) {
result[key] = target[key]
return result
} else {
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ describe('Unflatten', function () {
bar: {}
}), { foo: [], bar: {} })
})

test('Should keep arrays when both safe and object are set', function () {
const opts = { safe: true, object: true }
const flat = flatten({ tags: ['red', 'green', 'blue'] }, opts)
assert.deepStrictEqual(unflatten(flat, opts), { tags: ['red', 'green', 'blue'] })
})
})

describe('.object', function () {
Expand Down