Comparing version 1.4.0 to 1.5.0
@@ -42,2 +42,3 @@ var flat = module.exports = flatten | ||
var delimiter = opts.delimiter || '.' | ||
var overwrite = opts.overwrite || false | ||
var result = {} | ||
@@ -68,3 +69,9 @@ | ||
while (key2 !== undefined) { | ||
if (recipient[key1] === undefined) { | ||
var type = Object.prototype.toString.call(recipient[key1]) | ||
var isobject = ( | ||
type === "[object Object]" || | ||
type === "[object Array]" | ||
) | ||
if ((overwrite && !isobject) || (!overwrite && recipient[key1] === undefined)) { | ||
recipient[key1] = ( | ||
@@ -71,0 +78,0 @@ typeof key2 === 'number' && |
{ | ||
"name": "flat", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -118,1 +118,21 @@ # flat [![Build Status](https://secure.travis-ci.org/hughsk/flat.png?branch=master)](http://travis-ci.org/hughsk/flat) | ||
``` | ||
### overwrite | ||
When enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value: | ||
```javascript | ||
unflatten({ | ||
'TRAVIS': 'true', | ||
'TRAVIS_DIR': '/home/travis/build/kvz/environmental' | ||
}, { overwrite: true }) | ||
// TRAVIS: { | ||
// DIR: '/home/travis/build/kvz/environmental' | ||
// } | ||
``` | ||
Without `overwrite` set to `true`, the `TRAVIS` key would already have been set to a string, thus could not accept the nested `DIR` element. | ||
This only makes sense on ordered arrays, and since we're overwriting data, should be used with care. | ||
@@ -178,2 +178,18 @@ var assert = require('assert') | ||
test('Overwrite', function() { | ||
assert.deepEqual({ | ||
travis: { | ||
build: { | ||
dir: "/home/travis/build/kvz/environmental" | ||
} | ||
} | ||
}, unflatten({ | ||
travis: "true", | ||
travis_build_dir: "/home/travis/build/kvz/environmental" | ||
}, { | ||
delimiter: '_', | ||
overwrite: true, | ||
})) | ||
}) | ||
test('Messy', function() { | ||
@@ -180,0 +196,0 @@ assert.deepEqual({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14644
415
138