Comparing version 0.2.0 to 1.0.0
48
index.js
@@ -34,33 +34,41 @@ var flat = module.exports = {} | ||
var unflatten = flat.unflatten = function (target, opts) { | ||
if (typeof target !== 'object') return target | ||
var opts = opts || {} | ||
, delimiter = opts.delimiter || '.' | ||
, result = {} | ||
if (opts.safe && Array.isArray(target)) { | ||
return target.map(function(value) { | ||
return unflatten(value, opts); | ||
}); | ||
if (Object.prototype.toString.call(target) !== '[object Object]') { | ||
return target | ||
} | ||
target = flatten(target, opts) | ||
function getkey(key) { | ||
var parsedKey = parseInt(key) | ||
return (isNaN(parsedKey) ? key : parsedKey) | ||
}; | ||
var unflat = Object.keys(target).reduce(function (memo, key) { | ||
Object.keys(target).forEach(function(key) { | ||
var split = key.split(delimiter) | ||
, first = split.shift() | ||
, firstNibble | ||
, secondNibble | ||
, recipient = result | ||
if (split.length < 1) { | ||
memo[key] = target[key] | ||
return memo | ||
} | ||
firstNibble = getkey(split.shift()) | ||
secondNibble = getkey(split[0]) | ||
memo[first] = memo[first] || {} | ||
memo[first][split.join(delimiter)] = target[key] | ||
while (secondNibble !== undefined) { | ||
if (recipient[firstNibble] === undefined) { | ||
recipient[firstNibble] = ((typeof secondNibble === 'number') ? [] : {}) | ||
} | ||
memo[first] = unflatten(memo[first], opts) | ||
recipient = recipient[firstNibble] | ||
if (split.length > 0) { | ||
firstNibble = getkey(split.shift()) | ||
secondNibble = getkey(split[0]) | ||
} | ||
} | ||
return memo; | ||
}, {}) | ||
// unflatten again for 'messy objects' | ||
recipient[firstNibble] = unflatten(target[key]) | ||
}); | ||
return unflat | ||
}; | ||
return result | ||
}; |
{ | ||
"name": "flat", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -234,2 +234,18 @@ var assert = require('assert') | ||
}) | ||
}) | ||
}); | ||
suite('Arrays', function() { | ||
var object, flatObject; | ||
object = { "a": ["foo", "bar"] }; | ||
flatObject = { "a.0": "foo", "a.1": "bar"}; | ||
test('Should be able to flatten arrays properly', function() { | ||
assert.deepEqual(flatObject, flatten(object)); | ||
}); | ||
test('Should be able to revert and reverse array serialization via unflatten', function() { | ||
assert.deepEqual(object, unflatten(flatObject)); | ||
}); | ||
test('Array typed objects should be restored by unflatten', function () { | ||
assert.equal(Object.prototype.toString.call(object.a), Object.prototype.toString.call(unflatten(flatObject).a)); | ||
}) | ||
}); |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11938
299
1
0
1
1