Socket
Socket
Sign inDemoInstall

flat

Package Overview
Dependencies
Maintainers
2
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flat - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

28

index.js
var isBuffer = require('is-buffer')
var flat = module.exports = flatten
module.exports = flatten
flatten.flatten = flatten
flatten.unflatten = unflatten
function flatten(target, opts) {
function flatten (target, opts) {
opts = opts || {}

@@ -14,5 +14,5 @@

function step(object, prev, currentDepth) {
currentDepth = currentDepth ? currentDepth : 1
Object.keys(object).forEach(function(key) {
function step (object, prev, currentDepth) {
currentDepth = currentDepth || 1
Object.keys(object).forEach(function (key) {
var value = object[key]

@@ -23,4 +23,4 @@ var isarray = opts.safe && Array.isArray(value)

var isobject = (
type === "[object Object]" ||
type === "[object Array]"
type === '[object Object]' ||
type === '[object Array]'
)

@@ -46,3 +46,3 @@

function unflatten(target, opts) {
function unflatten (target, opts) {
opts = opts || {}

@@ -61,3 +61,3 @@

// an integer.
function getkey(key) {
function getkey (key) {
var parsedKey = Number(key)

@@ -73,3 +73,7 @@

Object.keys(target).forEach(function(key) {
var sortedKeys = Object.keys(target).sort(function (keyA, keyB) {
return keyA.length - keyB.length
})
sortedKeys.forEach(function (key) {
var split = key.split(delimiter)

@@ -83,4 +87,4 @@ var key1 = getkey(split.shift())

var isobject = (
type === "[object Object]" ||
type === "[object Array]"
type === '[object Object]' ||
type === '[object Array]'
)

@@ -87,0 +91,0 @@

{
"name": "flat",
"version": "3.0.0",
"version": "4.0.0",
"main": "index.js",
"scripts": {
"test": "mocha -u tdd --reporter spec"
"test": "mocha -u tdd --reporter spec && standard index.js test/index.js"
},

@@ -11,3 +11,4 @@ "license": "BSD-3-Clause",

"devDependencies": {
"mocha": "~3.4.2"
"mocha": "~3.4.2",
"standard": "^10.0.2"
},

@@ -14,0 +15,0 @@ "directories": {

@@ -0,20 +1,22 @@

/* globals suite test */
var assert = require('assert')
, flat = require('../index')
, flatten = flat.flatten
, unflatten = flat.unflatten
var flat = require('../index')
var flatten = flat.flatten
var unflatten = flat.unflatten
var primitives = {
String: 'good morning'
, Number: 1234.99
, Boolean: true
, Date: new Date
, null: null
, undefined: undefined
String: 'good morning',
Number: 1234.99,
Boolean: true,
Date: new Date(),
null: null,
undefined: undefined
}
suite('Flatten Primitives', function() {
Object.keys(primitives).forEach(function(key) {
suite('Flatten Primitives', function () {
Object.keys(primitives).forEach(function (key) {
var value = primitives[key]
test(key, function() {
test(key, function () {
assert.deepEqual(flatten({

@@ -31,7 +33,7 @@ hello: {

suite('Unflatten Primitives', function() {
Object.keys(primitives).forEach(function(key) {
suite('Unflatten Primitives', function () {
Object.keys(primitives).forEach(function (key) {
var value = primitives[key]
test(key, function() {
test(key, function () {
assert.deepEqual(unflatten({

@@ -48,4 +50,4 @@ 'hello.world': value

suite('Flatten', function() {
test('Nested once', function() {
suite('Flatten', function () {
test('Nested once', function () {
assert.deepEqual(flatten({

@@ -60,3 +62,3 @@ hello: {

test('Nested twice', function() {
test('Nested twice', function () {
assert.deepEqual(flatten({

@@ -73,3 +75,3 @@ hello: {

test('Multiple Keys', function() {
test('Multiple Keys', function () {
assert.deepEqual(flatten({

@@ -96,3 +98,3 @@ hello: {

test('Custom Delimiter', function() {
test('Custom Delimiter', function () {
assert.deepEqual(flatten({

@@ -111,3 +113,3 @@ hello: {

test('Empty Objects', function() {
test('Empty Objects', function () {
assert.deepEqual(flatten({

@@ -124,27 +126,31 @@ hello: {

if (typeof Buffer !== 'undefined') test('Buffer', function() {
assert.deepEqual(flatten({
hello: {
empty: {
nested: new Buffer('test')
if (typeof Buffer !== 'undefined') {
test('Buffer', function () {
assert.deepEqual(flatten({
hello: {
empty: {
nested: Buffer.from('test')
}
}
}
}), {
'hello.empty.nested': new Buffer('test')
}), {
'hello.empty.nested': Buffer.from('test')
})
})
})
}
if (typeof Uint8Array !== 'undefined') test('typed arrays', function() {
assert.deepEqual(flatten({
hello: {
empty: {
nested: new Uint8Array([1,2,3,4])
if (typeof Uint8Array !== 'undefined') {
test('typed arrays', function () {
assert.deepEqual(flatten({
hello: {
empty: {
nested: new Uint8Array([1, 2, 3, 4])
}
}
}
}), {
'hello.empty.nested': new Uint8Array([1,2,3,4])
}), {
'hello.empty.nested': new Uint8Array([1, 2, 3, 4])
})
})
})
}
test('Custom Depth', function() {
test('Custom Depth', function () {
assert.deepEqual(flatten({

@@ -173,17 +179,17 @@ hello: {

test('Should keep number in the left when object', function() {
assert.deepEqual(flatten({
hello: {
'0200': 'world',
'0500': 'darkness my old friend'
}
}), {
'hello.0200': 'world',
'hello.0500': 'darkness my old friend'
})
test('Should keep number in the left when object', function () {
assert.deepEqual(flatten({
hello: {
'0200': 'world',
'0500': 'darkness my old friend'
}
}), {
'hello.0200': 'world',
'hello.0500': 'darkness my old friend'
})
})
})
suite('Unflatten', function() {
test('Nested once', function() {
suite('Unflatten', function () {
test('Nested once', function () {
assert.deepEqual({

@@ -198,3 +204,3 @@ hello: {

test('Nested twice', function() {
test('Nested twice', function () {
assert.deepEqual({

@@ -211,3 +217,3 @@ hello: {

test('Multiple Keys', function() {
test('Multiple Keys', function () {
assert.deepEqual({

@@ -221,2 +227,3 @@ hello: {

world: {
greet: 'hello',
lorem: {

@@ -231,7 +238,22 @@ ipsum: 'again',

'world.lorem.ipsum': 'again',
'world.lorem.dolor': 'sit'
'world.lorem.dolor': 'sit',
'world': {greet: 'hello'}
}))
})
test('Custom Delimiter', function() {
test('nested objects do not clobber each other when a.b inserted before a', function () {
var x = {}
x['foo.bar'] = {t: 123}
x['foo'] = {p: 333}
assert.deepEqual(unflatten(x), {
foo: {
bar: {
t: 123
},
p: 333
}
})
})
test('Custom Delimiter', function () {
assert.deepEqual({

@@ -250,19 +272,19 @@ hello: {

test('Overwrite', function() {
test('Overwrite', function () {
assert.deepEqual({
travis: {
build: {
dir: "/home/travis/build/kvz/environmental"
dir: '/home/travis/build/kvz/environmental'
}
}
}, unflatten({
travis: "true",
travis_build_dir: "/home/travis/build/kvz/environmental"
travis: 'true',
travis_build_dir: '/home/travis/build/kvz/environmental'
}, {
delimiter: '_',
overwrite: true,
overwrite: true
}))
})
test('Messy', function() {
test('Messy', function () {
assert.deepEqual({

@@ -299,4 +321,4 @@ hello: { world: 'again' },

suite('Overwrite + non-object values in key positions', function() {
test('non-object keys + overwrite should be overwritten', function() {
suite('Overwrite + non-object values in key positions', function () {
test('non-object keys + overwrite should be overwritten', function () {
assert.deepEqual(flat.unflatten({ a: null, 'a.b': 'c' }, {overwrite: true}), { a: { b: 'c' } })

@@ -308,3 +330,3 @@ assert.deepEqual(flat.unflatten({ a: 0, 'a.b': 'c' }, {overwrite: true}), { a: { b: 'c' } })

test('overwrite value should not affect undefined keys', function() {
test('overwrite value should not affect undefined keys', function () {
assert.deepEqual(flat.unflatten({ a: undefined, 'a.b': 'c' }, {overwrite: true}), { a: { b: 'c' } })

@@ -314,3 +336,3 @@ assert.deepEqual(flat.unflatten({ a: undefined, 'a.b': 'c' }, {overwrite: false}), { a: { b: 'c' } })

test('if no overwrite, should ignore nested values under non-object key', function() {
test('if no overwrite, should ignore nested values under non-object key', function () {
assert.deepEqual(flat.unflatten({ a: null, 'a.b': 'c' }), { a: null })

@@ -323,13 +345,13 @@ assert.deepEqual(flat.unflatten({ a: 0, 'a.b': 'c' }), { a: 0 })

suite('.safe', function() {
test('Should protect arrays when true', function() {
suite('.safe', function () {
test('Should protect arrays when true', function () {
assert.deepEqual(flatten({
hello: [
{ world: { again: 'foo' } }
, { lorem: 'ipsum' }
]
, another: {
nested: [{ array: { too: 'deep' }}]
}
, lorem: {
{ world: { again: 'foo' } },
{ lorem: 'ipsum' }
],
another: {
nested: [{ array: { too: 'deep' } }]
},
lorem: {
ipsum: 'whoop'

@@ -341,15 +363,15 @@ }

hello: [
{ world: { again: 'foo' } }
, { lorem: 'ipsum' }
]
, 'lorem.ipsum': 'whoop'
, 'another.nested': [{ array: { too: 'deep' }}]
{ world: { again: 'foo' } },
{ lorem: 'ipsum' }
],
'lorem.ipsum': 'whoop',
'another.nested': [{ array: { too: 'deep' } }]
})
})
test('Should not protect arrays when false', function() {
test('Should not protect arrays when false', function () {
assert.deepEqual(flatten({
hello: [
{ world: { again: 'foo' } }
, { lorem: 'ipsum' }
{ world: { again: 'foo' } },
{ lorem: 'ipsum' }
]

@@ -359,4 +381,4 @@ }, {

}), {
'hello.0.world.again': 'foo'
, 'hello.1.lorem': 'ipsum'
'hello.0.world.again': 'foo',
'hello.1.lorem': 'ipsum'
})

@@ -366,4 +388,4 @@ })

suite('.object', function() {
test('Should create object instead of array when true', function() {
suite('.object', function () {
test('Should create object instead of array when true', function () {
var unflattened = unflatten({

@@ -375,3 +397,3 @@ 'hello.you.0': 'ipsum',

object: true
});
})
assert.deepEqual({

@@ -381,11 +403,11 @@ hello: {

0: 'ipsum',
1: 'lorem',
1: 'lorem'
},
other: { world: 'foo' }
}
}, unflattened);
assert(!Array.isArray(unflattened.hello.you));
}, unflattened)
assert(!Array.isArray(unflattened.hello.you))
})
test('Should create object instead of array when nested', function() {
test('Should create object instead of array when nested', function () {
var unflattened = unflatten({

@@ -399,3 +421,3 @@ 'hello': {

object: true
});
})
assert.deepEqual({

@@ -405,27 +427,27 @@ hello: {

0: 'ipsum',
1: 'lorem',
1: 'lorem'
},
other: { world: 'foo' }
}
}, unflattened);
assert(!Array.isArray(unflattened.hello.you));
}, unflattened)
assert(!Array.isArray(unflattened.hello.you))
})
test('Should keep the zero in the left when object is true', function() {
var unflattened = unflatten({
'hello.0200': 'world',
'hello.0500': 'darkness my old friend'
}, {
object: true
});
test('Should keep the zero in the left when object is true', function () {
var unflattened = unflatten({
'hello.0200': 'world',
'hello.0500': 'darkness my old friend'
}, {
object: true
})
assert.deepEqual({
hello: {
'0200': 'world',
'0500': 'darkness my old friend'
}
}, unflattened);
assert.deepEqual({
hello: {
'0200': 'world',
'0500': 'darkness my old friend'
}
}, unflattened)
})
test('Should not create object when false', function() {
test('Should not create object when false', function () {
var unflattened = unflatten({

@@ -437,3 +459,3 @@ 'hello.you.0': 'ipsum',

object: false
});
})
assert.deepEqual({

@@ -444,38 +466,41 @@ hello: {

}
}, unflattened);
assert(Array.isArray(unflattened.hello.you));
}, unflattened)
assert(Array.isArray(unflattened.hello.you))
})
})
if (typeof Buffer !== 'undefined') test('Buffer', function() {
assert.deepEqual(unflatten({
'hello.empty.nested': new Buffer('test')
}), {
hello: {
empty: {
nested: new Buffer('test')
if (typeof Buffer !== 'undefined') {
test('Buffer', function () {
assert.deepEqual(unflatten({
'hello.empty.nested': Buffer.from('test')
}), {
hello: {
empty: {
nested: Buffer.from('test')
}
}
}
})
})
})
}
if (typeof Uint8Array !== 'undefined') test('typed arrays', function() {
assert.deepEqual(unflatten({
'hello.empty.nested': new Uint8Array([1,2,3,4])
}), {
hello: {
empty: {
nested: new Uint8Array([1,2,3,4])
if (typeof Uint8Array !== 'undefined') {
test('typed arrays', function () {
assert.deepEqual(unflatten({
'hello.empty.nested': new Uint8Array([1, 2, 3, 4])
}), {
hello: {
empty: {
nested: new Uint8Array([1, 2, 3, 4])
}
}
}
})
})
})
}
})
suite('Arrays', function() {
test('Should be able to flatten arrays properly', function() {
suite('Arrays', function () {
test('Should be able to flatten arrays properly', function () {
assert.deepEqual({
'a.0': 'foo'
, 'a.1': 'bar'
'a.0': 'foo',
'a.1': 'bar'
}, flatten({

@@ -486,8 +511,8 @@ a: ['foo', 'bar']

test('Should be able to revert and reverse array serialization via unflatten', function() {
test('Should be able to revert and reverse array serialization via unflatten', function () {
assert.deepEqual({
a: ['foo', 'bar']
}, unflatten({
'a.0': 'foo'
, 'a.1': 'bar'
'a.0': 'foo',
'a.1': 'bar'
}))

@@ -500,4 +525,4 @@ })

, Object.prototype.toString.call(unflatten({
'a.0': 'foo'
, 'a.1': 'bar'
'a.0': 'foo',
'a.1': 'bar'
}).a)

@@ -507,3 +532,3 @@ )

test('Do not include keys with numbers inside them', function() {
test('Do not include keys with numbers inside them', function () {
assert.deepEqual(unflatten({

@@ -510,0 +535,0 @@ '1key.2_key': 'ok'

Sorry, the diff of this file is not supported yet

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