Updated the files.
This commit is contained in:
parent
1553e6b971
commit
753967d4f5
23418 changed files with 3784666 additions and 0 deletions
8
my-app/node_modules/flat/.travis.yml
generated
vendored
Executable file
8
my-app/node_modules/flat/.travis.yml
generated
vendored
Executable file
|
@ -0,0 +1,8 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- "6"
|
||||
- "7"
|
||||
- "8"
|
||||
- "10"
|
||||
- "12"
|
||||
- "14"
|
12
my-app/node_modules/flat/LICENSE
generated
vendored
Executable file
12
my-app/node_modules/flat/LICENSE
generated
vendored
Executable 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.
|
236
my-app/node_modules/flat/README.md
generated
vendored
Executable file
236
my-app/node_modules/flat/README.md
generated
vendored
Executable file
|
@ -0,0 +1,236 @@
|
|||
# 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 } }
|
||||
// }
|
||||
```
|
||||
|
||||
### transformKey
|
||||
|
||||
Transform each part of a flat key before and after flattening.
|
||||
|
||||
```javascript
|
||||
var flatten = require('flat')
|
||||
var unflatten = require('flat').unflatten
|
||||
|
||||
flatten({
|
||||
key1: {
|
||||
keyA: 'valueI'
|
||||
},
|
||||
key2: {
|
||||
keyB: 'valueII'
|
||||
},
|
||||
key3: { a: { b: { c: 2 } } }
|
||||
}, {
|
||||
transformKey: function(key){
|
||||
return '__' + key + '__';
|
||||
}
|
||||
})
|
||||
|
||||
// {
|
||||
// '__key1__.__keyA__': 'valueI',
|
||||
// '__key2__.__keyB__': 'valueII',
|
||||
// '__key3__.__a__.__b__.__c__': 2
|
||||
// }
|
||||
|
||||
unflatten({
|
||||
'__key1__.__keyA__': 'valueI',
|
||||
'__key2__.__keyB__': 'valueII',
|
||||
'__key3__.__a__.__b__.__c__': 2
|
||||
}, {
|
||||
transformKey: function(key){
|
||||
return key.substring(2, key.length - 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
|
||||
```
|
42
my-app/node_modules/flat/cli.js
generated
vendored
Executable file
42
my-app/node_modules/flat/cli.js
generated
vendored
Executable file
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const readline = require('readline')
|
||||
|
||||
const flat = require('./index')
|
||||
|
||||
const filepath = process.argv.slice(2)[0]
|
||||
if (filepath) {
|
||||
// Read from file
|
||||
const file = path.resolve(process.cwd(), filepath)
|
||||
fs.accessSync(file, fs.constants.R_OK) // allow to throw if not readable
|
||||
out(require(file))
|
||||
} else if (process.stdin.isTTY) {
|
||||
usage(0)
|
||||
} 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 (code) {
|
||||
console.log(`
|
||||
Usage:
|
||||
|
||||
flat foo.json
|
||||
cat foo.json | flat
|
||||
`)
|
||||
|
||||
process.exit(code || 0)
|
||||
}
|
158
my-app/node_modules/flat/index.js
generated
vendored
Executable file
158
my-app/node_modules/flat/index.js
generated
vendored
Executable file
|
@ -0,0 +1,158 @@
|
|||
module.exports = flatten
|
||||
flatten.flatten = flatten
|
||||
flatten.unflatten = unflatten
|
||||
|
||||
function isBuffer (obj) {
|
||||
return obj &&
|
||||
obj.constructor &&
|
||||
(typeof obj.constructor.isBuffer === 'function') &&
|
||||
obj.constructor.isBuffer(obj)
|
||||
}
|
||||
|
||||
function keyIdentity (key) {
|
||||
return key
|
||||
}
|
||||
|
||||
function flatten (target, opts) {
|
||||
opts = opts || {}
|
||||
|
||||
const delimiter = opts.delimiter || '.'
|
||||
const maxDepth = opts.maxDepth
|
||||
const transformKey = opts.transformKey || keyIdentity
|
||||
const output = {}
|
||||
|
||||
function step (object, prev, currentDepth) {
|
||||
currentDepth = currentDepth || 1
|
||||
Object.keys(object).forEach(function (key) {
|
||||
const value = object[key]
|
||||
const isarray = opts.safe && Array.isArray(value)
|
||||
const type = Object.prototype.toString.call(value)
|
||||
const isbuffer = isBuffer(value)
|
||||
const isobject = (
|
||||
type === '[object Object]' ||
|
||||
type === '[object Array]'
|
||||
)
|
||||
|
||||
const newKey = prev
|
||||
? prev + delimiter + transformKey(key)
|
||||
: transformKey(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 || {}
|
||||
|
||||
const delimiter = opts.delimiter || '.'
|
||||
const overwrite = opts.overwrite || false
|
||||
const transformKey = opts.transformKey || keyIdentity
|
||||
const result = {}
|
||||
|
||||
const 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) {
|
||||
const parsedKey = Number(key)
|
||||
|
||||
return (
|
||||
isNaN(parsedKey) ||
|
||||
key.indexOf('.') !== -1 ||
|
||||
opts.object
|
||||
) ? key
|
||||
: parsedKey
|
||||
}
|
||||
|
||||
function addKeys (keyPrefix, recipient, target) {
|
||||
return Object.keys(target).reduce(function (result, key) {
|
||||
result[keyPrefix + delimiter + key] = target[key]
|
||||
|
||||
return result
|
||||
}, recipient)
|
||||
}
|
||||
|
||||
function isEmpty (val) {
|
||||
const type = Object.prototype.toString.call(val)
|
||||
const isArray = type === '[object Array]'
|
||||
const isObject = type === '[object Object]'
|
||||
|
||||
if (!val) {
|
||||
return true
|
||||
} else if (isArray) {
|
||||
return !val.length
|
||||
} else if (isObject) {
|
||||
return !Object.keys(val).length
|
||||
}
|
||||
}
|
||||
|
||||
target = Object.keys(target).reduce(function (result, key) {
|
||||
const type = Object.prototype.toString.call(target[key])
|
||||
const isObject = (type === '[object Object]' || type === '[object Array]')
|
||||
if (!isObject || isEmpty(target[key])) {
|
||||
result[key] = target[key]
|
||||
return result
|
||||
} else {
|
||||
return addKeys(
|
||||
key,
|
||||
result,
|
||||
flatten(target[key], opts)
|
||||
)
|
||||
}
|
||||
}, {})
|
||||
|
||||
Object.keys(target).forEach(function (key) {
|
||||
const split = key.split(delimiter).map(transformKey)
|
||||
let key1 = getkey(split.shift())
|
||||
let key2 = getkey(split[0])
|
||||
let recipient = result
|
||||
|
||||
while (key2 !== undefined) {
|
||||
if (key1 === '__proto__') {
|
||||
return
|
||||
}
|
||||
|
||||
const type = Object.prototype.toString.call(recipient[key1])
|
||||
const 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
|
||||
}
|
37
my-app/node_modules/flat/package.json
generated
vendored
Executable file
37
my-app/node_modules/flat/package.json
generated
vendored
Executable file
|
@ -0,0 +1,37 @@
|
|||
{
|
||||
"name": "flat",
|
||||
"version": "5.0.2",
|
||||
"main": "index.js",
|
||||
"bin": "cli.js",
|
||||
"scripts": {
|
||||
"test": "mocha -u tdd --reporter spec && standard cli.js 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": "~8.1.1",
|
||||
"standard": "^14.3.4"
|
||||
},
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"dependencies": {},
|
||||
"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"
|
||||
}
|
643
my-app/node_modules/flat/test/test.js
generated
vendored
Executable file
643
my-app/node_modules/flat/test/test.js
generated
vendored
Executable file
|
@ -0,0 +1,643 @@
|
|||
/* globals suite test */
|
||||
|
||||
const assert = require('assert')
|
||||
const path = require('path')
|
||||
const { exec } = require('child_process')
|
||||
const pkg = require('../package.json')
|
||||
const flat = require('../index')
|
||||
|
||||
const flatten = flat.flatten
|
||||
const unflatten = flat.unflatten
|
||||
|
||||
const 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) {
|
||||
const value = primitives[key]
|
||||
|
||||
test(key, function () {
|
||||
assert.deepStrictEqual(flatten({
|
||||
hello: {
|
||||
world: value
|
||||
}
|
||||
}), {
|
||||
'hello.world': value
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
suite('Unflatten Primitives', function () {
|
||||
Object.keys(primitives).forEach(function (key) {
|
||||
const value = primitives[key]
|
||||
|
||||
test(key, function () {
|
||||
assert.deepStrictEqual(unflatten({
|
||||
'hello.world': value
|
||||
}), {
|
||||
hello: {
|
||||
world: value
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
suite('Flatten', function () {
|
||||
test('Nested once', function () {
|
||||
assert.deepStrictEqual(flatten({
|
||||
hello: {
|
||||
world: 'good morning'
|
||||
}
|
||||
}), {
|
||||
'hello.world': 'good morning'
|
||||
})
|
||||
})
|
||||
|
||||
test('Nested twice', function () {
|
||||
assert.deepStrictEqual(flatten({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
}
|
||||
}), {
|
||||
'hello.world.again': 'good morning'
|
||||
})
|
||||
})
|
||||
|
||||
test('Multiple Keys', function () {
|
||||
assert.deepStrictEqual(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.deepStrictEqual(flatten({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
delimiter: ':'
|
||||
}), {
|
||||
'hello:world:again': 'good morning'
|
||||
})
|
||||
})
|
||||
|
||||
test('Empty Objects', function () {
|
||||
assert.deepStrictEqual(flatten({
|
||||
hello: {
|
||||
empty: {
|
||||
nested: {}
|
||||
}
|
||||
}
|
||||
}), {
|
||||
'hello.empty.nested': {}
|
||||
})
|
||||
})
|
||||
|
||||
if (typeof Buffer !== 'undefined') {
|
||||
test('Buffer', function () {
|
||||
assert.deepStrictEqual(flatten({
|
||||
hello: {
|
||||
empty: {
|
||||
nested: Buffer.from('test')
|
||||
}
|
||||
}
|
||||
}), {
|
||||
'hello.empty.nested': Buffer.from('test')
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof Uint8Array !== 'undefined') {
|
||||
test('typed arrays', function () {
|
||||
assert.deepStrictEqual(flatten({
|
||||
hello: {
|
||||
empty: {
|
||||
nested: new Uint8Array([1, 2, 3, 4])
|
||||
}
|
||||
}
|
||||
}), {
|
||||
'hello.empty.nested': new Uint8Array([1, 2, 3, 4])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
test('Custom Depth', function () {
|
||||
assert.deepStrictEqual(flatten({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
},
|
||||
lorem: {
|
||||
ipsum: {
|
||||
dolor: 'good evening'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
maxDepth: 2
|
||||
}), {
|
||||
'hello.world': {
|
||||
again: 'good morning'
|
||||
},
|
||||
'lorem.ipsum': {
|
||||
dolor: 'good evening'
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('Transformed Keys', function () {
|
||||
assert.deepStrictEqual(flatten({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
},
|
||||
lorem: {
|
||||
ipsum: {
|
||||
dolor: 'good evening'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
transformKey: function (key) {
|
||||
return '__' + key + '__'
|
||||
}
|
||||
}), {
|
||||
'__hello__.__world__.__again__': 'good morning',
|
||||
'__lorem__.__ipsum__.__dolor__': 'good evening'
|
||||
})
|
||||
})
|
||||
|
||||
test('Should keep number in the left when object', function () {
|
||||
assert.deepStrictEqual(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.deepStrictEqual({
|
||||
hello: {
|
||||
world: 'good morning'
|
||||
}
|
||||
}, unflatten({
|
||||
'hello.world': 'good morning'
|
||||
}))
|
||||
})
|
||||
|
||||
test('Nested twice', function () {
|
||||
assert.deepStrictEqual({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
}
|
||||
}, unflatten({
|
||||
'hello.world.again': 'good morning'
|
||||
}))
|
||||
})
|
||||
|
||||
test('Multiple Keys', function () {
|
||||
assert.deepStrictEqual({
|
||||
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 () {
|
||||
const x = {}
|
||||
x['foo.bar'] = { t: 123 }
|
||||
x.foo = { p: 333 }
|
||||
assert.deepStrictEqual(unflatten(x), {
|
||||
foo: {
|
||||
bar: {
|
||||
t: 123
|
||||
},
|
||||
p: 333
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('Custom Delimiter', function () {
|
||||
assert.deepStrictEqual({
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
}
|
||||
}, unflatten({
|
||||
'hello world again': 'good morning'
|
||||
}, {
|
||||
delimiter: ' '
|
||||
}))
|
||||
})
|
||||
|
||||
test('Overwrite', function () {
|
||||
assert.deepStrictEqual({
|
||||
travis: {
|
||||
build: {
|
||||
dir: '/home/travis/build/kvz/environmental'
|
||||
}
|
||||
}
|
||||
}, unflatten({
|
||||
travis: 'true',
|
||||
travis_build_dir: '/home/travis/build/kvz/environmental'
|
||||
}, {
|
||||
delimiter: '_',
|
||||
overwrite: true
|
||||
}))
|
||||
})
|
||||
|
||||
test('Transformed Keys', function () {
|
||||
assert.deepStrictEqual(unflatten({
|
||||
'__hello__.__world__.__again__': 'good morning',
|
||||
'__lorem__.__ipsum__.__dolor__': 'good evening'
|
||||
}, {
|
||||
transformKey: function (key) {
|
||||
return key.substring(2, key.length - 2)
|
||||
}
|
||||
}), {
|
||||
hello: {
|
||||
world: {
|
||||
again: 'good morning'
|
||||
}
|
||||
},
|
||||
lorem: {
|
||||
ipsum: {
|
||||
dolor: 'good evening'
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('Messy', function () {
|
||||
assert.deepStrictEqual({
|
||||
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.deepStrictEqual(flat.unflatten({ a: null, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
|
||||
assert.deepStrictEqual(flat.unflatten({ a: 0, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
|
||||
assert.deepStrictEqual(flat.unflatten({ a: 1, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
|
||||
assert.deepStrictEqual(flat.unflatten({ a: '', 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
|
||||
})
|
||||
|
||||
test('overwrite value should not affect undefined keys', function () {
|
||||
assert.deepStrictEqual(flat.unflatten({ a: undefined, 'a.b': 'c' }, { overwrite: true }), { a: { b: 'c' } })
|
||||
assert.deepStrictEqual(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.deepStrictEqual(flat.unflatten({ a: null, 'a.b': 'c' }), { a: null })
|
||||
assert.deepStrictEqual(flat.unflatten({ a: 0, 'a.b': 'c' }), { a: 0 })
|
||||
assert.deepStrictEqual(flat.unflatten({ a: 1, 'a.b': 'c' }), { a: 1 })
|
||||
assert.deepStrictEqual(flat.unflatten({ a: '', 'a.b': 'c' }), { a: '' })
|
||||
})
|
||||
})
|
||||
|
||||
suite('.safe', function () {
|
||||
test('Should protect arrays when true', function () {
|
||||
assert.deepStrictEqual(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.deepStrictEqual(flatten({
|
||||
hello: [
|
||||
{ world: { again: 'foo' } },
|
||||
{ lorem: 'ipsum' }
|
||||
]
|
||||
}, {
|
||||
safe: false
|
||||
}), {
|
||||
'hello.0.world.again': 'foo',
|
||||
'hello.1.lorem': 'ipsum'
|
||||
})
|
||||
})
|
||||
|
||||
test('Empty objects should not be removed', function () {
|
||||
assert.deepStrictEqual(unflatten({
|
||||
foo: [],
|
||||
bar: {}
|
||||
}), { foo: [], bar: {} })
|
||||
})
|
||||
})
|
||||
|
||||
suite('.object', function () {
|
||||
test('Should create object instead of array when true', function () {
|
||||
const unflattened = unflatten({
|
||||
'hello.you.0': 'ipsum',
|
||||
'hello.you.1': 'lorem',
|
||||
'hello.other.world': 'foo'
|
||||
}, {
|
||||
object: true
|
||||
})
|
||||
assert.deepStrictEqual({
|
||||
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 () {
|
||||
const unflattened = unflatten({
|
||||
hello: {
|
||||
'you.0': 'ipsum',
|
||||
'you.1': 'lorem',
|
||||
'other.world': 'foo'
|
||||
}
|
||||
}, {
|
||||
object: true
|
||||
})
|
||||
assert.deepStrictEqual({
|
||||
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 () {
|
||||
const unflattened = unflatten({
|
||||
'hello.0200': 'world',
|
||||
'hello.0500': 'darkness my old friend'
|
||||
}, {
|
||||
object: true
|
||||
})
|
||||
|
||||
assert.deepStrictEqual({
|
||||
hello: {
|
||||
'0200': 'world',
|
||||
'0500': 'darkness my old friend'
|
||||
}
|
||||
}, unflattened)
|
||||
})
|
||||
|
||||
test('Should not create object when false', function () {
|
||||
const unflattened = unflatten({
|
||||
'hello.you.0': 'ipsum',
|
||||
'hello.you.1': 'lorem',
|
||||
'hello.other.world': 'foo'
|
||||
}, {
|
||||
object: false
|
||||
})
|
||||
assert.deepStrictEqual({
|
||||
hello: {
|
||||
you: ['ipsum', 'lorem'],
|
||||
other: { world: 'foo' }
|
||||
}
|
||||
}, unflattened)
|
||||
assert(Array.isArray(unflattened.hello.you))
|
||||
})
|
||||
})
|
||||
|
||||
if (typeof Buffer !== 'undefined') {
|
||||
test('Buffer', function () {
|
||||
assert.deepStrictEqual(unflatten({
|
||||
'hello.empty.nested': Buffer.from('test')
|
||||
}), {
|
||||
hello: {
|
||||
empty: {
|
||||
nested: Buffer.from('test')
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof Uint8Array !== 'undefined') {
|
||||
test('typed arrays', function () {
|
||||
assert.deepStrictEqual(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.deepStrictEqual({
|
||||
'a.0': 'foo',
|
||||
'a.1': 'bar'
|
||||
}, flatten({
|
||||
a: ['foo', 'bar']
|
||||
}))
|
||||
})
|
||||
|
||||
test('Should be able to revert and reverse array serialization via unflatten', function () {
|
||||
assert.deepStrictEqual({
|
||||
a: ['foo', 'bar']
|
||||
}, unflatten({
|
||||
'a.0': 'foo',
|
||||
'a.1': 'bar'
|
||||
}))
|
||||
})
|
||||
|
||||
test('Array typed objects should be restored by unflatten', function () {
|
||||
assert.strictEqual(
|
||||
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.deepStrictEqual(unflatten({
|
||||
'1key.2_key': 'ok'
|
||||
}), {
|
||||
'1key': {
|
||||
'2_key': 'ok'
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
suite('Order of Keys', function () {
|
||||
test('Order of keys should not be changed after round trip flatten/unflatten', function () {
|
||||
const obj = {
|
||||
b: 1,
|
||||
abc: {
|
||||
c: [{
|
||||
d: 1,
|
||||
bca: 1,
|
||||
a: 1
|
||||
}]
|
||||
},
|
||||
a: 1
|
||||
}
|
||||
const result = unflatten(
|
||||
flatten(obj)
|
||||
)
|
||||
|
||||
assert.deepStrictEqual(Object.keys(obj), Object.keys(result))
|
||||
assert.deepStrictEqual(Object.keys(obj.abc), Object.keys(result.abc))
|
||||
assert.deepStrictEqual(Object.keys(obj.abc.c[0]), Object.keys(result.abc.c[0]))
|
||||
})
|
||||
})
|
||||
|
||||
suite('CLI', function () {
|
||||
test('can take filename', function (done) {
|
||||
const cli = path.resolve(__dirname, '..', pkg.bin)
|
||||
const pkgJSON = path.resolve(__dirname, '..', 'package.json')
|
||||
exec(`${cli} ${pkgJSON}`, (err, stdout, stderr) => {
|
||||
assert.ifError(err)
|
||||
assert.strictEqual(stdout.trim(), JSON.stringify(flatten(pkg), null, 2))
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
test('exits with usage if no file', function (done) {
|
||||
const cli = path.resolve(__dirname, '..', pkg.bin)
|
||||
const pkgJSON = path.resolve(__dirname, '..', 'package.json')
|
||||
exec(`${cli} ${pkgJSON}`, (err, stdout, stderr) => {
|
||||
assert.ifError(err)
|
||||
assert.strictEqual(stdout.trim(), JSON.stringify(flatten(pkg), null, 2))
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
test('can take piped file', function (done) {
|
||||
const cli = path.resolve(__dirname, '..', pkg.bin)
|
||||
const pkgJSON = path.resolve(__dirname, '..', 'package.json')
|
||||
exec(`cat ${pkgJSON} | ${cli}`, (err, stdout, stderr) => {
|
||||
assert.ifError(err)
|
||||
assert.strictEqual(stdout.trim(), JSON.stringify(flatten(pkg), null, 2))
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue