Update to NPM version
This commit is contained in:
5
ProjectSourceCode/node_modules/flat/.travis.yml
generated
vendored
Normal file
5
ProjectSourceCode/node_modules/flat/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "6"
|
||||
- "7"
|
||||
- "8"
|
||||
12
ProjectSourceCode/node_modules/flat/LICENSE
generated
vendored
Normal file
12
ProjectSourceCode/node_modules/flat/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
Copyright (c) 2014, Hugh Kennedy
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
187
ProjectSourceCode/node_modules/flat/README.md
generated
vendored
Normal file
187
ProjectSourceCode/node_modules/flat/README.md
generated
vendored
Normal file
@@ -0,0 +1,187 @@
|
||||
# flat [](http://travis-ci.org/hughsk/flat)
|
||||
|
||||
Take a nested Javascript object and flatten it, or unflatten an object with
|
||||
delimited keys.
|
||||
|
||||
## Installation
|
||||
|
||||
``` bash
|
||||
$ npm install flat
|
||||
```
|
||||
|
||||
## Methods
|
||||
|
||||
### flatten(original, options)
|
||||
|
||||
Flattens the object - it'll return an object one level deep, regardless of how
|
||||
nested the original object was:
|
||||
|
||||
``` javascript
|
||||
var flatten = require('flat')
|
||||
|
||||
flatten({
|
||||
key1: {
|
||||
keyA: 'valueI'
|
||||
},
|
||||
key2: {
|
||||
keyB: 'valueII'
|
||||
},
|
||||
key3: { a: { b: { c: 2 } } }
|
||||
})
|
||||
|
||||
// {
|
||||
// 'key1.keyA': 'valueI',
|
||||
// 'key2.keyB': 'valueII',
|
||||
// 'key3.a.b.c': 2
|
||||
// }
|
||||
```
|
||||
|
||||
### unflatten(original, options)
|
||||
|
||||
Flattening is reversible too, you can call `flatten.unflatten()` on an object:
|
||||
|
||||
``` javascript
|
||||
var unflatten = require('flat').unflatten
|
||||
|
||||
unflatten({
|
||||
'three.levels.deep': 42,
|
||||
'three.levels': {
|
||||
nested: true
|
||||
}
|
||||
})
|
||||
|
||||
// {
|
||||
// three: {
|
||||
// levels: {
|
||||
// deep: 42,
|
||||
// nested: true
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
### delimiter
|
||||
|
||||
Use a custom delimiter for (un)flattening your objects, instead of `.`.
|
||||
|
||||
### safe
|
||||
|
||||
When enabled, both `flat` and `unflatten` will preserve arrays and their
|
||||
contents. This is disabled by default.
|
||||
|
||||
``` javascript
|
||||
var flatten = require('flat')
|
||||
|
||||
flatten({
|
||||
this: [
|
||||
{ contains: 'arrays' },
|
||||
{ preserving: {
|
||||
them: 'for you'
|
||||
}}
|
||||
]
|
||||
}, {
|
||||
safe: true
|
||||
})
|
||||
|
||||
// {
|
||||
// 'this': [
|
||||
// { contains: 'arrays' },
|
||||
// { preserving: {
|
||||
// them: 'for you'
|
||||
// }}
|
||||
// ]
|
||||
// }
|
||||
```
|
||||
|
||||
### object
|
||||
|
||||
When enabled, arrays will not be created automatically when calling unflatten, like so:
|
||||
|
||||
``` javascript
|
||||
unflatten({
|
||||
'hello.you.0': 'ipsum',
|
||||
'hello.you.1': 'lorem',
|
||||
'hello.other.world': 'foo'
|
||||
}, { object: true })
|
||||
|
||||
// hello: {
|
||||
// you: {
|
||||
// 0: 'ipsum',
|
||||
// 1: 'lorem',
|
||||
// },
|
||||
// other: { world: 'foo' }
|
||||
// }
|
||||
```
|
||||
|
||||
### overwrite
|
||||
|
||||
When enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value:
|
||||
|
||||
```javascript
|
||||
unflatten({
|
||||
'TRAVIS': 'true',
|
||||
'TRAVIS_DIR': '/home/travis/build/kvz/environmental'
|
||||
}, { overwrite: true })
|
||||
|
||||
// TRAVIS: {
|
||||
// DIR: '/home/travis/build/kvz/environmental'
|
||||
// }
|
||||
```
|
||||
|
||||
Without `overwrite` set to `true`, the `TRAVIS` key would already have been set to a string, thus could not accept the nested `DIR` element.
|
||||
|
||||
This only makes sense on ordered arrays, and since we're overwriting data, should be used with care.
|
||||
|
||||
|
||||
### maxDepth
|
||||
|
||||
Maximum number of nested objects to flatten.
|
||||
|
||||
``` javascript
|
||||
var flatten = require('flat')
|
||||
|
||||
flatten({
|
||||
key1: {
|
||||
keyA: 'valueI'
|
||||
},
|
||||
key2: {
|
||||
keyB: 'valueII'
|
||||
},
|
||||
key3: { a: { b: { c: 2 } } }
|
||||
}, { maxDepth: 2 })
|
||||
|
||||
// {
|
||||
// 'key1.keyA': 'valueI',
|
||||
// 'key2.keyB': 'valueII',
|
||||
// 'key3.a': { b: { c: 2 } }
|
||||
// }
|
||||
```
|
||||
|
||||
## Command Line Usage
|
||||
|
||||
`flat` is also available as a command line tool. You can run it with
|
||||
[`npx`](https://ghub.io/npx):
|
||||
|
||||
```sh
|
||||
npx flat foo.json
|
||||
```
|
||||
|
||||
Or install the `flat` command globally:
|
||||
|
||||
```sh
|
||||
npm i -g flat && flat foo.json
|
||||
```
|
||||
|
||||
Accepts a filename as an argument:
|
||||
|
||||
```sh
|
||||
flat foo.json
|
||||
```
|
||||
|
||||
Also accepts JSON on stdin:
|
||||
|
||||
```sh
|
||||
cat foo.json | flat
|
||||
```
|
||||
39
ProjectSourceCode/node_modules/flat/cli.js
generated
vendored
Executable file
39
ProjectSourceCode/node_modules/flat/cli.js
generated
vendored
Executable file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const flat = require('.')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const readline = require('readline')
|
||||
|
||||
if (process.stdin.isTTY) {
|
||||
// Read from file
|
||||
const file = path.resolve(process.cwd(), process.argv.slice(2)[0])
|
||||
if (!file) usage()
|
||||
if (!fs.existsSync(file)) usage()
|
||||
out(require(file))
|
||||
} else {
|
||||
// Read from newline-delimited STDIN
|
||||
const lines = []
|
||||
readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
terminal: false
|
||||
})
|
||||
.on('line', line => lines.push(line))
|
||||
.on('close', () => out(JSON.parse(lines.join('\n'))))
|
||||
}
|
||||
|
||||
function out (data) {
|
||||
process.stdout.write(JSON.stringify(flat(data), null, 2))
|
||||
}
|
||||
|
||||
function usage () {
|
||||
console.log(`
|
||||
Usage:
|
||||
|
||||
flat foo.json
|
||||
cat foo.json | flat
|
||||
`)
|
||||
|
||||
process.exit()
|
||||
}
|
||||
113
ProjectSourceCode/node_modules/flat/index.js
generated
vendored
Normal file
113
ProjectSourceCode/node_modules/flat/index.js
generated
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
var isBuffer = require('is-buffer')
|
||||
|
||||
module.exports = flatten
|
||||
flatten.flatten = flatten
|
||||
flatten.unflatten = unflatten
|
||||
|
||||
function flatten (target, opts) {
|
||||
opts = opts || {}
|
||||
|
||||
var delimiter = opts.delimiter || '.'
|
||||
var maxDepth = opts.maxDepth
|
||||
var output = {}
|
||||
|
||||
function step (object, prev, currentDepth) {
|
||||
currentDepth = currentDepth || 1
|
||||
Object.keys(object).forEach(function (key) {
|
||||
var value = object[key]
|
||||
var isarray = opts.safe && Array.isArray(value)
|
||||
var type = Object.prototype.toString.call(value)
|
||||
var isbuffer = isBuffer(value)
|
||||
var isobject = (
|
||||
type === '[object Object]' ||
|
||||
type === '[object Array]'
|
||||
)
|
||||
|
||||
var newKey = prev
|
||||
? prev + delimiter + key
|
||||
: key
|
||||
|
||||
if (!isarray && !isbuffer && isobject && Object.keys(value).length &&
|
||||
(!opts.maxDepth || currentDepth < maxDepth)) {
|
||||
return step(value, newKey, currentDepth + 1)
|
||||
}
|
||||
|
||||
output[newKey] = value
|
||||
})
|
||||
}
|
||||
|
||||
step(target)
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
function unflatten (target, opts) {
|
||||
opts = opts || {}
|
||||
|
||||
var delimiter = opts.delimiter || '.'
|
||||
var overwrite = opts.overwrite || false
|
||||
var result = {}
|
||||
|
||||
var isbuffer = isBuffer(target)
|
||||
if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
|
||||
return target
|
||||
}
|
||||
|
||||
// safely ensure that the key is
|
||||
// an integer.
|
||||
function getkey (key) {
|
||||
var parsedKey = Number(key)
|
||||
|
||||
return (
|
||||
isNaN(parsedKey) ||
|
||||
key.indexOf('.') !== -1 ||
|
||||
opts.object
|
||||
) ? key
|
||||
: parsedKey
|
||||
}
|
||||
|
||||
var sortedKeys = Object.keys(target).sort(function (keyA, keyB) {
|
||||
return keyA.length - keyB.length
|
||||
})
|
||||
|
||||
sortedKeys.forEach(function (key) {
|
||||
var split = key.split(delimiter)
|
||||
var key1 = getkey(split.shift())
|
||||
var key2 = getkey(split[0])
|
||||
var recipient = result
|
||||
|
||||
while (key2 !== undefined) {
|
||||
if (key1 === '__proto__') {
|
||||
return
|
||||
}
|
||||
var type = Object.prototype.toString.call(recipient[key1])
|
||||
var isobject = (
|
||||
type === '[object Object]' ||
|
||||
type === '[object Array]'
|
||||
)
|
||||
|
||||
// do not write over falsey, non-undefined values if overwrite is false
|
||||
if (!overwrite && !isobject && typeof recipient[key1] !== 'undefined') {
|
||||
return
|
||||
}
|
||||
|
||||
if ((overwrite && !isobject) || (!overwrite && recipient[key1] == null)) {
|
||||
recipient[key1] = (
|
||||
typeof key2 === 'number' &&
|
||||
!opts.object ? [] : {}
|
||||
)
|
||||
}
|
||||
|
||||
recipient = recipient[key1]
|
||||
if (split.length > 0) {
|
||||
key1 = getkey(split.shift())
|
||||
key2 = getkey(split[0])
|
||||
}
|
||||
}
|
||||
|
||||
// unflatten again for 'messy objects'
|
||||
recipient[key1] = unflatten(target[key], opts)
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
39
ProjectSourceCode/node_modules/flat/package.json
generated
vendored
Normal file
39
ProjectSourceCode/node_modules/flat/package.json
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"name": "flat",
|
||||
"version": "4.1.1",
|
||||
"main": "index.js",
|
||||
"bin": "cli.js",
|
||||
"scripts": {
|
||||
"test": "mocha -u tdd --reporter spec && standard index.js test/index.js"
|
||||
},
|
||||
"license": "BSD-3-Clause",
|
||||
"description": "Take a nested Javascript object and flatten it, or unflatten an object with delimited keys",
|
||||
"devDependencies": {
|
||||
"mocha": "~5.2.0",
|
||||
"standard": "^11.0.1"
|
||||
},
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"dependencies": {
|
||||
"is-buffer": "~2.0.3"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/hughsk/flat.git"
|
||||
},
|
||||
"keywords": [
|
||||
"flat",
|
||||
"json",
|
||||
"flatten",
|
||||
"unflatten",
|
||||
"split",
|
||||
"object",
|
||||
"nested"
|
||||
],
|
||||
"author": "Hugh Kennedy <hughskennedy@gmail.com> (http://hughskennedy.com)",
|
||||
"bugs": {
|
||||
"url": "https://github.com/hughsk/flat/issues"
|
||||
},
|
||||
"homepage": "https://github.com/hughsk/flat"
|
||||
}
|
||||
527
ProjectSourceCode/node_modules/flat/test/test.js
generated
vendored
Normal file
527
ProjectSourceCode/node_modules/flat/test/test.js
generated
vendored
Normal file
@@ -0,0 +1,527 @@
|
||||
/* globals suite test */
|
||||
|
||||
var assert = require('assert')
|
||||
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
|
||||
}
|
||||
|
||||
suite('Flatten Primitives', function () {
|
||||
Object.keys(primitives).forEach(function (key) {
|
||||
var value = primitives[key]
|
||||
|
||||
test(key, function () {
|
||||
assert.deepEqual(flatten({
|
||||
hello: {
|
||||
world: value
|
||||
}
|
||||
}), {
|
||||
'hello.world': value
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
suite('Unflatten Primitives', function () {
|
||||
Object.keys(primitives).forEach(function (key) {
|
||||
var value = primitives[key]
|
||||
|
||||
test(key, function () {
|
||||
assert.deepEqual(unflatten({
|
||||
'hello.world': value
|
||||
}), {
|
||||
hello: {
|
||||
world: value
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
suite('Flatten', function () {
|
||||
test('Nested once', function () {
|
||||
assert.deepEqual(flatten({
|
||||
hello: {
|
||||
world: 'good morning'
|
||||
}
|
||||
}), {
|
||||
'hello.world': 'good morning'
|
||||
})
|
||||
})
|
||||
|
||||
test('Nested twice', function () {
|
||||
assert.deepEqual(flatten({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
}
|
||||
}), {
|
||||
'hello.world.again': 'good morning'
|
||||
})
|
||||
})
|
||||
|
||||
test('Multiple Keys', function () {
|
||||
assert.deepEqual(flatten({
|
||||
hello: {
|
||||
lorem: {
|
||||
ipsum: 'again',
|
||||
dolor: 'sit'
|
||||
}
|
||||
},
|
||||
world: {
|
||||
lorem: {
|
||||
ipsum: 'again',
|
||||
dolor: 'sit'
|
||||
}
|
||||
}
|
||||
}), {
|
||||
'hello.lorem.ipsum': 'again',
|
||||
'hello.lorem.dolor': 'sit',
|
||||
'world.lorem.ipsum': 'again',
|
||||
'world.lorem.dolor': 'sit'
|
||||
})
|
||||
})
|
||||
|
||||
test('Custom Delimiter', function () {
|
||||
assert.deepEqual(flatten({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
delimiter: ':'
|
||||
}), {
|
||||
'hello:world:again': 'good morning'
|
||||
})
|
||||
})
|
||||
|
||||
test('Empty Objects', function () {
|
||||
assert.deepEqual(flatten({
|
||||
hello: {
|
||||
empty: {
|
||||
nested: { }
|
||||
}
|
||||
}
|
||||
}), {
|
||||
'hello.empty.nested': { }
|
||||
})
|
||||
})
|
||||
|
||||
if (typeof Buffer !== 'undefined') {
|
||||
test('Buffer', function () {
|
||||
assert.deepEqual(flatten({
|
||||
hello: {
|
||||
empty: {
|
||||
nested: Buffer.from('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])
|
||||
}
|
||||
}
|
||||
}), {
|
||||
'hello.empty.nested': new Uint8Array([1, 2, 3, 4])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
test('Custom Depth', function () {
|
||||
assert.deepEqual(flatten({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
},
|
||||
lorem: {
|
||||
ipsum: {
|
||||
dolor: 'good evening'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
maxDepth: 2
|
||||
}), {
|
||||
'hello.world': {
|
||||
again: 'good morning'
|
||||
},
|
||||
'lorem.ipsum': {
|
||||
dolor: 'good evening'
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
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 () {
|
||||
assert.deepEqual({
|
||||
hello: {
|
||||
world: 'good morning'
|
||||
}
|
||||
}, unflatten({
|
||||
'hello.world': 'good morning'
|
||||
}))
|
||||
})
|
||||
|
||||
test('Nested twice', function () {
|
||||
assert.deepEqual({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
}
|
||||
}, unflatten({
|
||||
'hello.world.again': 'good morning'
|
||||
}))
|
||||
})
|
||||
|
||||
test('Multiple Keys', function () {
|
||||
assert.deepEqual({
|
||||
hello: {
|
||||
lorem: {
|
||||
ipsum: 'again',
|
||||
dolor: 'sit'
|
||||
}
|
||||
},
|
||||
world: {
|
||||
greet: 'hello',
|
||||
lorem: {
|
||||
ipsum: 'again',
|
||||
dolor: 'sit'
|
||||
}
|
||||
}
|
||||
}, unflatten({
|
||||
'hello.lorem.ipsum': 'again',
|
||||
'hello.lorem.dolor': 'sit',
|
||||
'world.lorem.ipsum': 'again',
|
||||
'world.lorem.dolor': 'sit',
|
||||
'world': {greet: 'hello'}
|
||||
}))
|
||||
})
|
||||
|
||||
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({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
}
|
||||
}, unflatten({
|
||||
'hello world again': 'good morning'
|
||||
}, {
|
||||
delimiter: ' '
|
||||
}))
|
||||
})
|
||||
|
||||
test('Overwrite', function () {
|
||||
assert.deepEqual({
|
||||
travis: {
|
||||
build: {
|
||||
dir: '/home/travis/build/kvz/environmental'
|
||||
}
|
||||
}
|
||||
}, unflatten({
|
||||
travis: 'true',
|
||||
travis_build_dir: '/home/travis/build/kvz/environmental'
|
||||
}, {
|
||||
delimiter: '_',
|
||||
overwrite: true
|
||||
}))
|
||||
})
|
||||
|
||||
test('Messy', function () {
|
||||
assert.deepEqual({
|
||||
hello: { world: 'again' },
|
||||
lorem: { ipsum: 'another' },
|
||||
good: {
|
||||
morning: {
|
||||
hash: {
|
||||
key: { nested: {
|
||||
deep: { and: { even: {
|
||||
deeper: { still: 'hello' }
|
||||
} } }
|
||||
} }
|
||||
},
|
||||
again: { testing: { 'this': 'out' } }
|
||||
}
|
||||
}
|
||||
}, unflatten({
|
||||
'hello.world': 'again',
|
||||
'lorem.ipsum': 'another',
|
||||
'good.morning': {
|
||||
'hash.key': {
|
||||
'nested.deep': {
|
||||
'and.even.deeper.still': 'hello'
|
||||
}
|
||||
}
|
||||
},
|
||||
'good.morning.again': {
|
||||
'testing.this': 'out'
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
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' } })
|
||||
assert.deepEqual(flat.unflatten({ a: 0, 'a.b': 'c' }, {overwrite: true}), { a: { b: 'c' } })
|
||||
assert.deepEqual(flat.unflatten({ a: 1, 'a.b': 'c' }, {overwrite: true}), { a: { b: 'c' } })
|
||||
assert.deepEqual(flat.unflatten({ a: '', 'a.b': 'c' }, {overwrite: true}), { a: { b: 'c' } })
|
||||
})
|
||||
|
||||
test('overwrite value should not affect undefined keys', function () {
|
||||
assert.deepEqual(flat.unflatten({ a: undefined, 'a.b': 'c' }, {overwrite: true}), { a: { b: 'c' } })
|
||||
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 () {
|
||||
assert.deepEqual(flat.unflatten({ a: null, 'a.b': 'c' }), { a: null })
|
||||
assert.deepEqual(flat.unflatten({ a: 0, 'a.b': 'c' }), { a: 0 })
|
||||
assert.deepEqual(flat.unflatten({ a: 1, 'a.b': 'c' }), { a: 1 })
|
||||
assert.deepEqual(flat.unflatten({ a: '', 'a.b': 'c' }), { a: '' })
|
||||
})
|
||||
})
|
||||
|
||||
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: {
|
||||
ipsum: 'whoop'
|
||||
}
|
||||
}, {
|
||||
safe: true
|
||||
}), {
|
||||
hello: [
|
||||
{ world: { again: 'foo' } },
|
||||
{ lorem: 'ipsum' }
|
||||
],
|
||||
'lorem.ipsum': 'whoop',
|
||||
'another.nested': [{ array: { too: 'deep' } }]
|
||||
})
|
||||
})
|
||||
|
||||
test('Should not protect arrays when false', function () {
|
||||
assert.deepEqual(flatten({
|
||||
hello: [
|
||||
{ world: { again: 'foo' } },
|
||||
{ lorem: 'ipsum' }
|
||||
]
|
||||
}, {
|
||||
safe: false
|
||||
}), {
|
||||
'hello.0.world.again': 'foo',
|
||||
'hello.1.lorem': 'ipsum'
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
suite('.object', function () {
|
||||
test('Should create object instead of array when true', function () {
|
||||
var unflattened = unflatten({
|
||||
'hello.you.0': 'ipsum',
|
||||
'hello.you.1': 'lorem',
|
||||
'hello.other.world': 'foo'
|
||||
}, {
|
||||
object: true
|
||||
})
|
||||
assert.deepEqual({
|
||||
hello: {
|
||||
you: {
|
||||
0: 'ipsum',
|
||||
1: 'lorem'
|
||||
},
|
||||
other: { world: 'foo' }
|
||||
}
|
||||
}, unflattened)
|
||||
assert(!Array.isArray(unflattened.hello.you))
|
||||
})
|
||||
|
||||
test('Should create object instead of array when nested', function () {
|
||||
var unflattened = unflatten({
|
||||
'hello': {
|
||||
'you.0': 'ipsum',
|
||||
'you.1': 'lorem',
|
||||
'other.world': 'foo'
|
||||
}
|
||||
}, {
|
||||
object: true
|
||||
})
|
||||
assert.deepEqual({
|
||||
hello: {
|
||||
you: {
|
||||
0: 'ipsum',
|
||||
1: 'lorem'
|
||||
},
|
||||
other: { world: 'foo' }
|
||||
}
|
||||
}, 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
|
||||
})
|
||||
|
||||
assert.deepEqual({
|
||||
hello: {
|
||||
'0200': 'world',
|
||||
'0500': 'darkness my old friend'
|
||||
}
|
||||
}, unflattened)
|
||||
})
|
||||
|
||||
test('Should not create object when false', function () {
|
||||
var unflattened = unflatten({
|
||||
'hello.you.0': 'ipsum',
|
||||
'hello.you.1': 'lorem',
|
||||
'hello.other.world': 'foo'
|
||||
}, {
|
||||
object: false
|
||||
})
|
||||
assert.deepEqual({
|
||||
hello: {
|
||||
you: ['ipsum', 'lorem'],
|
||||
other: { world: 'foo' }
|
||||
}
|
||||
}, unflattened)
|
||||
assert(Array.isArray(unflattened.hello.you))
|
||||
})
|
||||
})
|
||||
|
||||
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])
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
test('should not pollute prototype', function () {
|
||||
unflatten({
|
||||
'__proto__.polluted': true
|
||||
});
|
||||
unflatten({
|
||||
'prefix.__proto__.polluted': true
|
||||
});
|
||||
unflatten({
|
||||
'prefix.0.__proto__.polluted': true
|
||||
});
|
||||
|
||||
assert.notStrictEqual({}.polluted, true);
|
||||
})
|
||||
})
|
||||
|
||||
suite('Arrays', function () {
|
||||
test('Should be able to flatten arrays properly', function () {
|
||||
assert.deepEqual({
|
||||
'a.0': 'foo',
|
||||
'a.1': 'bar'
|
||||
}, flatten({
|
||||
a: ['foo', 'bar']
|
||||
}))
|
||||
})
|
||||
|
||||
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'
|
||||
}))
|
||||
})
|
||||
|
||||
test('Array typed objects should be restored by unflatten', function () {
|
||||
assert.equal(
|
||||
Object.prototype.toString.call(['foo', 'bar'])
|
||||
, Object.prototype.toString.call(unflatten({
|
||||
'a.0': 'foo',
|
||||
'a.1': 'bar'
|
||||
}).a)
|
||||
)
|
||||
})
|
||||
|
||||
test('Do not include keys with numbers inside them', function () {
|
||||
assert.deepEqual(unflatten({
|
||||
'1key.2_key': 'ok'
|
||||
}), {
|
||||
'1key': {
|
||||
'2_key': 'ok'
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user