flatulence
Advanced tools
Comparing version 0.2.0 to 0.2.2
@@ -9,4 +9,6 @@ const inflate = require('./inflate') | ||
deflate, | ||
flatten: deflate, | ||
inflate, | ||
unflatten: inflate, | ||
@@ -13,0 +15,0 @@ } |
@@ -1,3 +0,3 @@ | ||
const regex = /\.|(\[\d\])/ | ||
const DEBUG = false | ||
@@ -8,3 +8,5 @@ | ||
inflate(map, prefix = '', result = {}) { | ||
const subMap = buildSubMap(map, prefix) | ||
const subMap = buildMatchingMap(map, prefix) | ||
DEBUG && console.log('subMap', prefix, subMap) | ||
@@ -17,9 +19,11 @@ Object.keys(subMap).forEach( | ||
} | ||
} | ||
const buildSubMap = (map, prefix) => { | ||
// Returns an object with all key-value pairs | ||
// matching the given prefix | ||
const buildMatchingMap = (map, prefix) => { | ||
return Object.keys(map).reduce((result, key) => { | ||
if(key.startsWith(prefix)) { | ||
result[key.substring(prefix.length + 1)] = map[key] | ||
result[key.substring(prefix.length)] = map[key] | ||
} | ||
@@ -30,9 +34,13 @@ return result | ||
// Sets the value on result. Location of value is defined by the | ||
// provided path. | ||
const setAtPath = (path, value, result) => { | ||
const keys = path.split(regex).filter(k => !!k) | ||
const keys = path.split(setAtPath.regex).filter(k => !!k) | ||
return setRec(keys, value, result) | ||
} | ||
setAtPath.regex = /\.|(\[\d\])/ | ||
// | ||
const setRec = (keys, finalValue, result) => { | ||
@@ -39,0 +47,0 @@ |
{ | ||
"name": "flatulence", | ||
"version": "0.2.0", | ||
"description": "Flatten objects and arrays so that each key is the full path to a primitive value. Paths are lodash-compatible. Inflate flattened objects and make them regular JSON objects again.", | ||
"version": "0.2.2", | ||
"description": "Flatten objects and arrays so that each key is the full path to a primitive value. Paths are lodash-compatible. Inflate flattened objects and make them regular JS objects again.", | ||
"main": "index.js", | ||
@@ -11,5 +11,6 @@ "scripts": { | ||
"utility", | ||
"flatten", | ||
"unflatten", | ||
"js", | ||
"paths", | ||
"json" | ||
"paths" | ||
], | ||
@@ -16,0 +17,0 @@ "author": "Kolja Kirchner", |
@@ -1,2 +0,18 @@ | ||
# flattenObject | ||
Flatten objects so that each key is the full path to a primitive value | ||
# flatulence | ||
Flatten objects and arrays so that each key is the full path to a primitive value. Paths are lodash-compatible. Inflate flattened objects and make them regular JSON objects again. | ||
```javascript | ||
const flatu = require('flatulence') | ||
const flattened = flatu.deflate({ | ||
a: 1, | ||
b: { | ||
c: 1, | ||
d: [1,2,3] | ||
} | ||
}) | ||
const regular = flatu.inflate(flattened) | ||
``` |
@@ -36,2 +36,10 @@ const assert = require('assert') | ||
it('should reverse flattened object to an inflated one', () => { | ||
const flattened = fl.flatten(data) | ||
const inflated = fl.unflatten(flattened) | ||
assert.deepEqual(data, inflated) | ||
}) | ||
it('should reverse flattened object to an inflated one at a prefix', () => { | ||
const flattened = fl.deflate(data) | ||
@@ -38,0 +46,0 @@ const inflated = fl.inflate(flattened, 'cc') |
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
8495
85
215
19