flatulence
Advanced tools
Comparing version 0.1.10 to 0.1.12
@@ -0,38 +1,15 @@ | ||
const inflate = require('./inflate') | ||
const deflate = require('./deflate') | ||
const flatulence = { | ||
toFlatArray() { | ||
throw new Error('not implemented yet') | ||
}, | ||
toFlatArray() { throw new Error('not implemented yet') }, | ||
flatten(subject, prefix = '', result = {}) { | ||
deflate, | ||
if(subject !== null && typeof subject === 'object') { | ||
for(var key in subject) { | ||
flatulence.flatten( | ||
subject[key], | ||
extendPrefix(prefix, key, Array.isArray(subject)), | ||
result | ||
) | ||
} | ||
return result | ||
} | ||
result[removeTrailingDot(prefix)] = subject | ||
return result | ||
} | ||
inflate, | ||
} | ||
const removeTrailingDot = prefix => { | ||
const lastIdx = prefix.length - 1 | ||
return prefix.charAt(lastIdx) === '.' | ||
? prefix.substring(0, lastIdx) | ||
: prefix | ||
} | ||
const extendPrefix = (prefix, key, isArrayElement = false) => { | ||
return isArrayElement | ||
? removeTrailingDot(prefix) + '[' + key + '].' | ||
: prefix + key + '.' | ||
} | ||
@@ -42,2 +19,4 @@ | ||
module.exports = flatulence |
{ | ||
"name": "flatulence", | ||
"version": "0.1.10", | ||
"description": "Flatten objects and arrays so that each key is the full path to a primitive value. Paths are lodash-compatible.", | ||
"version": "0.1.12", | ||
"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.", | ||
"main": "index.js", | ||
@@ -19,4 +19,5 @@ "scripts": { | ||
"assert": "^1.4.1", | ||
"lodash": "^4.16.6", | ||
"mocha": "^3.1.2" | ||
} | ||
} |
@@ -1,13 +0,25 @@ | ||
const mocha = require('mocha') | ||
const assert = require('assert') | ||
const fl = require('../lib') | ||
const data = require('./data') | ||
const fp = require('lodash/fp') | ||
describe('flatulence', () => { | ||
describe('#flatten()', () => { | ||
describe('#deflate()', () => { | ||
it('should flatten object', () => { | ||
const flattened = fl.flatten(data) | ||
console.log(flattened) | ||
const flattened = fl.deflate(data) | ||
assert.equal(flattened['array[0][1]'], 1) | ||
}) | ||
}) | ||
describe('#inflate()', () => { | ||
it('should reverse flattened object to an inflated one', () => { | ||
const flattened = fl.deflate(data) | ||
const inflated = fl.inflate(flattened, 'cc') | ||
assert.deepEqual(data.cc, inflated) | ||
}) | ||
}) | ||
}) |
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
7209
11
186
3
79