Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cdxoo/flat

Package Overview
Dependencies
Maintainers
0
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cdxoo/flat - npm Package Compare versions

Comparing version 0.0.11 to 0.1.0

2

package.json
{
"name": "@cdxoo/flat",
"version": "0.0.11",
"version": "0.1.0",
"description": "does not flatten cats",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -13,3 +13,7 @@ 'use strict';

var out = {};
if (Array.isArray(that) && !traverseArrays) {
return that;
}
traverse(that, (context) => {

@@ -49,2 +53,18 @@ var { isLeaf, path, value, parentNode } = context;

var setValue = (that, path = [], override) => {
//console.log('getValue()')
//console.log('=>', that, path);
var target = that;
var lasttoken = undefined;
for (var [ix, token] of path.entries()) {
if (ix < path.length - 1) {
target = target[token];
}
else {
lasttoken = token;
}
}
target[lasttoken] = override;
}
var unflatten = (that, options = {}) => {

@@ -54,8 +74,15 @@ var {

handlePropertiesOnNonObjects = 'throw',
initArrayOnNumericToken = false,
} = options;
var out = {};
var wrapper = { ROOT: undefined };
for (var key of Object.keys(that)) {
var path = key.split(delimiter);
var route = [ { token: '#ROOT#', getValue: () => out }];
var route = [{
token: '#ROOT#',
getValue: () => wrapper.ROOT,
setValue: (override) => { wrapper.ROOT = override },
}];
// NOTE: theese "let" initializaions are important with for

@@ -71,2 +98,11 @@ path.forEach((token, i) => {

if (container.getValue() === undefined) {
if (initArrayOnNumericToken && /^\d+$/.test(String(token))) {
container.setValue([]);
}
else {
container.setValue({});
}
}
var precheckValue = container.getValue();

@@ -81,4 +117,7 @@ if (precheckValue === null || precheckValue === undefined) {

var value = (
isLast ? that[key] : container.getValue()[token] || {}
isLast
? that[key]
: container.getValue()[token]
);

@@ -105,3 +144,3 @@

(path: '${path.join('.')}'
in ${JSON.stringify(out)})
in ${JSON.stringify(wrapper.ROOT)})
`))

@@ -116,3 +155,3 @@ }

(path: '${path.join('.')}'
in ${JSON.stringify(out)})
in ${JSON.stringify(wrapper.ROOT)})
`))

@@ -129,3 +168,6 @@ }

//console.log({ i })
return getValue(out, currentPath)
return getValue(wrapper.ROOT, currentPath)
},
setValue: (override) => {
setValue(wrapper.ROOT, currentPath, override);
}

@@ -135,3 +177,3 @@ });

}
return out;
return wrapper.ROOT;
}

@@ -138,0 +180,0 @@

@@ -51,3 +51,3 @@ 'use strict';

describe('cats', () => {
it('meows', () => {
it('meow', () => {
var cosmo = new Cat();

@@ -59,3 +59,16 @@ var out = flatten({ foo: { bar: cosmo }});

describe('flatten() maxDepth', () => {
describe('flatten() top-level array', () => {
it('just returns them by default', () => {
var tla = [{ foo: 42 }];
var out = flatten(tla);
expect(out).to.eql([{ foo: 42 }]);
})
it('handles them reasonably when traverseArrays is true', () => {
var tla = [{ foo: 42 }];
var out = flatten(tla, { traverseArrays: true });
expect(out).to.eql({ '0.foo': 42 });
})
})
describe('maxDepth', () => {
var pairs = [

@@ -93,3 +106,3 @@ [

describe('flatten array key indication', () => {
describe('flatten() array key indication', () => {
it('no indication by default', () => {

@@ -129,3 +142,3 @@ var out = flatten({ foo: ['a', 'b', 'c'] }, {

describe('unflatten mixed in non-objects', () => {
describe('unflatten() mixed in non-objects', () => {
it('throws useful error by default (simple case)', () => {

@@ -215,4 +228,4 @@ var error = undefined;

describe('unflatten array keys', () => {
it('can unflatten to array via custom extra handling', () => {
describe('unflatten() arrays', () => {
it('does it properly', () => {
var out = unflatten({

@@ -227,15 +240,18 @@ 'foo': [],

});
})
})
});
describe('unflatten array keys', () => {
it('can unflatten to array via custom extra handling', () => {
it('does it properly when initArrayOnNumericToken=true', () => {
var out = unflatten({
'foo': [],
'foo.0': 'a',
'foo.1': 'b',
'foo.2.x': 'c',
});
'bar.y': '1', // XXX: when this is reversed then well have
// an array
'bar.0': '2',
}, { initArrayOnNumericToken: true });
expect(out).to.eql({
foo: [ 'a', 'b', { x: 'c' }]
foo: [ 'a', 'b', { x: 'c' }],
bar: { 'y': '1', '0': '2' }
});

@@ -242,0 +258,0 @@ })

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc