diff --git a/index.js b/index.js index 42e1ddc..9c32520 100644 --- a/index.js +++ b/index.js @@ -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 { diff --git a/test/test.js b/test/test.js index ef01c9b..954c2f2 100644 --- a/test/test.js +++ b/test/test.js @@ -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 () {