flatulence
Advanced tools
Comparing version 0.1.12 to 0.2.0
const deflate = { | ||
deflate(subject, prefix = '', result = {}) { | ||
if(subject !== null && typeof subject === 'object') { | ||
@@ -10,3 +10,3 @@ | ||
subject[key], | ||
extendPrefix(prefix, key, Array.isArray(subject)), | ||
appendKey(prefix, key, Array.isArray(subject)), | ||
result | ||
@@ -17,3 +17,5 @@ ) | ||
} | ||
result[removeTrailingDot(prefix)] = subject | ||
return result | ||
@@ -24,5 +26,7 @@ } | ||
const last = string => string.charAt(string.length - 1) | ||
const removeTrailingDot = prefix => { | ||
const lastIdx = prefix.length - 1 | ||
return prefix.charAt(lastIdx) === '.' | ||
return last(prefix) === '.' | ||
? prefix.substring(0, lastIdx) | ||
@@ -32,8 +36,11 @@ : prefix | ||
const extendPrefix = (prefix, key, isArrayElement = false) => { | ||
return isArrayElement | ||
? removeTrailingDot(prefix) + '[' + key + '].' | ||
: prefix + key + '.' | ||
const appendKey = (prefix, key, isArrayElement = false) => { | ||
if(isArrayElement) { | ||
return removeTrailingDot(prefix) + '[' + key + ']' | ||
} | ||
return !!prefix ? (prefix + '.' + key) : key | ||
} | ||
module.exports = deflate.deflate |
@@ -7,6 +7,8 @@ const regex = /\.|(\[\d\])/ | ||
inflate(map, prefix = '') { | ||
inflate(map, prefix = '', result = {}) { | ||
const subMap = buildSubMap(map, prefix) | ||
const result = {} | ||
Object.keys(subMap).forEach(path => setAtPath(path, subMap[path], result)) | ||
Object.keys(subMap).forEach( | ||
path => setAtPath(path, subMap[path], result) | ||
) | ||
@@ -33,3 +35,3 @@ return result | ||
// [ 'contract', 'currencies', '[0]' ] | ||
const setRec = (keys, finalValue, result) => { | ||
@@ -36,0 +38,0 @@ |
{ | ||
"name": "flatulence", | ||
"version": "0.1.12", | ||
"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.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
const assert = require('assert') | ||
const fl = require('../lib') | ||
const data = require('./data') | ||
const fp = require('lodash/fp') | ||
const fl = require('../lib') | ||
const data = require('./data') | ||
const fp = require('lodash/fp') | ||
@@ -11,5 +11,23 @@ describe('flatulence', () => { | ||
const flattened = fl.deflate(data) | ||
console.log(flattened) | ||
assert.equal(flattened['array[0][1]'], 1) | ||
}) | ||
it('should use prefix arg if provided', () => { | ||
const prefix = 'this.is.my.custom.prefix' | ||
const flattened = fl.deflate( | ||
{ | ||
id: 'anid', | ||
map: { a:1, b:2, c:3} | ||
}, | ||
prefix | ||
) | ||
console.log(flattened) | ||
assert.equal(flattened[prefix + '.id'], 'anid') | ||
assert.equal(flattened[prefix + '.map.a'], 1) | ||
}) | ||
}) | ||
@@ -16,0 +34,0 @@ |
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
7693
202