flatulence
Advanced tools
Comparing version 0.2.10 to 0.2.12
const deflate = { | ||
deflate(subject, prefix = '', result = {}) { | ||
flatten(subject, prefix = '', result = {}, keepEmptyObjects = false) { | ||
if(subject !== null && typeof subject === 'object') { | ||
const keys = Object.keys(subject) | ||
if(!keys.length && keepEmptyObjects) { | ||
result[prefix] = subject | ||
return result | ||
} | ||
for(var key in subject) { | ||
deflate.deflate( | ||
subject[key], | ||
appendKey(prefix, key, Array.isArray(subject)), | ||
result | ||
) | ||
} | ||
keys.forEach(key => deflate.flatten( | ||
subject[key], | ||
appendKey(prefix, key, Array.isArray(subject)), | ||
result, | ||
keepEmptyObjects, | ||
)) | ||
return result | ||
@@ -20,6 +27,12 @@ } | ||
return result | ||
} | ||
}, | ||
} | ||
deflate.flatten.keepEmpty = (subject, prefix = '', result = {}) => { | ||
const keepEmptyObjects = true | ||
return deflate.flatten(subject, prefix, result, keepEmptyObjects) | ||
} | ||
const last = string => string.charAt(string.length - 1) | ||
@@ -43,2 +56,2 @@ | ||
module.exports = deflate.deflate | ||
module.exports = deflate.flatten |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.flatulence=e():t.flatulence=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";t.exports=n(2)},function(t,e){"use strict";var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r={deflate:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(null!==t&&"object"===("undefined"==typeof t?"undefined":n(t))){for(var f in t)r.deflate(t[f],i(e,f,Array.isArray(t)),o);return o}return o[u(e)]=t,o}},o=function(t){return t.charAt(t.length-1)},u=function(t){var e=t.length-1;return"."===o(t)?t.substring(0,e):t},i=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return n?u(t)+"["+e+"]":t?t+"."+e:e};t.exports=r.deflate},function(t,e,n){"use strict";var r=n(3),o=n(1),u={toFlatArray:function(){throw new Error("not implemented yet")},deflate:o,flatten:o,inflate:r,unflatten:r};t.exports=u},function(t,e){"use strict";var n=!1,r={inflate:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=arguments[2];r||(r=o(t,e)?[]:{});var f=u(t,e);return n&&console.log("subMap",e,f),Object.keys(f).forEach(function(t){return i(t,f[t],r)}),r}},o=function(t,e){var n=Object.keys(t).find(function(t){return t.startsWith(e)});return n&&"["===n.charAt(e.length)},u=function(t,e){return Object.keys(t).reduce(function(n,r){return r.startsWith(e)&&(n[r.substring(e.length)]=t[r]),n},{})},i=function t(e,n,r){var o=e.split(t.regex).filter(function(t){return!!t});return f(o,n,r)};i.regex=/\.|(\[\d\])/;var f=function t(e,r,o){if(n&&console.log("setRec",e,r),e.length){var u=e.shift(),i=c(u),f=o[i];if(!f){var l=e[0];o[i]=s(i,l,r)}return t(e,r,o[i])}return o},c=function(t){return t.startsWith("[")?t.substring(1,t.length-1):t},s=function(t,e,n){return e?e.startsWith("[")?[]:{}:n};t.exports=r.inflate}])}); | ||
!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.flatulence=n():t.flatulence=n()}(this,function(){return function(t){function n(r){if(e[r])return e[r].exports;var o=e[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,n),o.loaded=!0,o.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n,e){"use strict";t.exports=e(2)},function(t,n){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r={flatten:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},f=arguments.length>3&&void 0!==arguments[3]&&arguments[3];if(null!==t&&"object"===("undefined"==typeof t?"undefined":e(t))){var c=Object.keys(t);return!c.length&&f?(o[n]=t,o):(c.forEach(function(e){return r.flatten(t[e],i(n,e,Array.isArray(t)),o,f)}),o)}return o[u(n)]=t,o}};r.flatten.keepEmpty=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},o=!0;return r.flatten(t,n,e,o)};var o=function(t){return t.charAt(t.length-1)},u=function(t){var n=t.length-1;return"."===o(t)?t.substring(0,n):t},i=function(t,n){var e=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return e?u(t)+"["+n+"]":t?t+"."+n:n};t.exports=r.flatten},function(t,n,e){"use strict";var r=e(3),o=e(1),u={toFlatArray:function(){throw new Error("not implemented yet")},deflate:o,flatten:o,inflate:r,unflatten:r};t.exports=u},function(t,n){"use strict";var e=!1,r={inflate:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=arguments[2];r||(r=o(t,n)?[]:{});var f=u(t,n);return e&&console.log("subMap",n,f),Object.keys(f).forEach(function(t){return i(t,f[t],r)}),r}},o=function(t,n){var e=Object.keys(t).find(function(t){return t.startsWith(n)});return e&&"["===e.charAt(n.length)},u=function(t,n){return Object.keys(t).reduce(function(e,r){return r.startsWith(n)&&(e[r.substring(n.length)]=t[r]),e},{})},i=function t(n,e,r){var o=n.split(t.regex).filter(function(t){return!!t});return f(o,e,r)};i.regex=/\.|(\[\d\])/;var f=function t(n,r,o){if(e&&console.log("setRec",n,r),n.length){var u=n.shift(),i=c(u),f=o[i];if(!f){var s=n[0];o[i]=l(i,s,r)}return t(n,r,o[i])}return o},c=function(t){return t.startsWith("[")?t.substring(1,t.length-1):t},l=function(t,n,e){return n?n.startsWith("[")?[]:{}:e};t.exports=r.inflate}])}); |
{ | ||
"name": "flatulence", | ||
"version": "0.2.10", | ||
"version": "0.2.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 JS objects again.", | ||
@@ -8,3 +8,4 @@ "main": "lib/main.js", | ||
"build": "webpack -p --out-dir lib", | ||
"test": "mocha test" | ||
"test": "mocha test", | ||
"test:build": "npm run build && npm run test" | ||
}, | ||
@@ -11,0 +12,0 @@ "maintainers": [ |
# flatulence | ||
Flatten objects and arrays so that each key is the full path to a primitive value. Paths are lodash-compatible. Unflatten flattened objects and make them regular JS objects again. | ||
Flatten objects and arrays into non-nested objects and arrays. | ||
A flattened object consists of key paths pointing to primitive values. | ||
Paths are lodash-compatible so they can be used with _.set() and _.get(). | ||
Unflatten flattened objects and make them regular, nested JS objects again. | ||
```javascript | ||
@@ -17,4 +21,27 @@ const flatulence = require('flatulence') | ||
}) | ||
// { a: 1, 'b.c': 1, 'b.d[0]': 1, 'b.d[1]': 2, 'b.d[2]': 3 } | ||
// { | ||
// a: 1, | ||
// 'b.c': 1, | ||
// 'b.d[0]': 1, | ||
// 'b.d[1]': 2, | ||
// 'b.d[2]': 3 | ||
// } | ||
// Optionally you can decide to keep empty arrays and objects | ||
const flattened = fl.flatten.keepEmpty({ | ||
"name": "Mega Group 2", | ||
"loginId": "admin_mega_merchant_2", | ||
"description": "again, some description here", | ||
"merchantUsers": [], | ||
"map": {} | ||
}) | ||
// | ||
// { | ||
// name: 'Mega Group 2', | ||
// loginId: 'admin_mega_merchant_2', | ||
// description: 'again, some description here', | ||
// merchantUsers: [], | ||
// map: {} | ||
// } | ||
// Create a flat object and provide a prefix to each key | ||
@@ -26,7 +53,9 @@ var flattened2 = flatulence.flatten( | ||
//{ 'my.prefix.key.a': 1, | ||
//{ | ||
// 'my.prefix.key.a': 1, | ||
// 'my.prefix.key.b.c': 1, | ||
// 'my.prefix.key.b.d[0]': 1, | ||
// 'my.prefix.key.b.d[1]': 2, | ||
// 'my.prefix.key.b.d[2]': 3 } | ||
// 'my.prefix.key.b.d[2]': 3 | ||
//} | ||
@@ -33,0 +62,0 @@ |
@@ -12,3 +12,3 @@ const assert = require('assert') | ||
console.log(flattened) | ||
// console.log(flattened) | ||
@@ -18,2 +18,19 @@ assert.equal(flattened['array[0][1]'], 1) | ||
it('should keep empty arrays and objects', () => { | ||
const flattened = fl.flatten.keepEmpty({ | ||
"name": "Mega Group 2", | ||
"loginId": "admin_mega_merchant_2", | ||
"description": "again, some description here", | ||
"merchantUsers": [], | ||
"map": {} | ||
}) | ||
console.log('flattened', flattened) | ||
assert.ok(Array.isArray(flattened.merchantUsers)) | ||
assert.ok(typeof flattened.map === 'object') | ||
assert.ok(!Array.isArray(flattened.map)) | ||
}) | ||
it('should use prefix arg if provided', () => { | ||
@@ -29,3 +46,3 @@ const prefix = 'this.is.my.custom.prefix' | ||
console.log(flattened) | ||
// console.log(flattened) | ||
@@ -56,4 +73,6 @@ assert.equal(flattened[prefix + '.id'], 'anid') | ||
const flattened = fl.flatten(data) | ||
const unflattened = fl.unflatten(flattened, 'cc.contract.multiContractSetup') | ||
console.log('flattened', flattened) | ||
console.log('unflattened', unflattened) | ||
@@ -60,0 +79,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
21495
370
75
79