added unit testing, and started implementing unit tests...phew

This commit is contained in:
Josh Burman
2019-03-12 22:28:02 -04:00
parent 74aad4a957
commit e8c2539f1b
3489 changed files with 464813 additions and 88 deletions

5
node_modules/flat/.travis.yml generated vendored Normal file
View File

@ -0,0 +1,5 @@
language: node_js
node_js:
- "6"
- "7"
- "8"

12
node_modules/flat/LICENSE generated vendored Normal file
View 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
node_modules/flat/README.md generated vendored Normal file
View File

@ -0,0 +1,187 @@
# flat [![Build Status](https://secure.travis-ci.org/hughsk/flat.png?branch=master)](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
node_modules/flat/cli.js generated vendored Executable file
View 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()
}

110
node_modules/flat/index.js generated vendored Normal file
View File

@ -0,0 +1,110 @@
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) {
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
}

21
node_modules/flat/node_modules/is-buffer/LICENSE generated vendored Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Feross Aboukhadijeh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

53
node_modules/flat/node_modules/is-buffer/README.md generated vendored Normal file
View File

@ -0,0 +1,53 @@
# is-buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
[travis-image]: https://img.shields.io/travis/feross/is-buffer/master.svg
[travis-url]: https://travis-ci.org/feross/is-buffer
[npm-image]: https://img.shields.io/npm/v/is-buffer.svg
[npm-url]: https://npmjs.org/package/is-buffer
[downloads-image]: https://img.shields.io/npm/dm/is-buffer.svg
[downloads-url]: https://npmjs.org/package/is-buffer
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
[standard-url]: https://standardjs.com
#### Determine if an object is a [`Buffer`](http://nodejs.org/api/buffer.html) (including the [browserify Buffer](https://github.com/feross/buffer))
[![saucelabs][saucelabs-image]][saucelabs-url]
[saucelabs-image]: https://saucelabs.com/browser-matrix/is-buffer.svg
[saucelabs-url]: https://saucelabs.com/u/is-buffer
## Why not use `Buffer.isBuffer`?
This module lets you check if an object is a `Buffer` without using `Buffer.isBuffer` (which includes the whole [buffer](https://github.com/feross/buffer) module in [browserify](http://browserify.org/)).
It's future-proof and works in node too!
## install
```bash
npm install is-buffer
```
## usage
```js
var isBuffer = require('is-buffer')
isBuffer(new Buffer(4)) // true
isBuffer(undefined) // false
isBuffer(null) // false
isBuffer('') // false
isBuffer(true) // false
isBuffer(false) // false
isBuffer(0) // false
isBuffer(1) // false
isBuffer(1.0) // false
isBuffer('string') // false
isBuffer({}) // false
isBuffer(function foo () {}) // false
```
## license
MIT. Copyright (C) [Feross Aboukhadijeh](http://feross.org).

11
node_modules/flat/node_modules/is-buffer/index.js generated vendored Normal file
View File

@ -0,0 +1,11 @@
/*!
* Determine if an object is a Buffer
*
* @author Feross Aboukhadijeh <https://feross.org>
* @license MIT
*/
module.exports = function isBuffer (obj) {
return obj != null && obj.constructor != null &&
typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
}

80
node_modules/flat/node_modules/is-buffer/package.json generated vendored Normal file
View File

@ -0,0 +1,80 @@
{
"_from": "is-buffer@~2.0.3",
"_id": "is-buffer@2.0.3",
"_inBundle": false,
"_integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw==",
"_location": "/flat/is-buffer",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "is-buffer@~2.0.3",
"name": "is-buffer",
"escapedName": "is-buffer",
"rawSpec": "~2.0.3",
"saveSpec": null,
"fetchSpec": "~2.0.3"
},
"_requiredBy": [
"/flat"
],
"_resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz",
"_shasum": "4ecf3fcf749cbd1e472689e109ac66261a25e725",
"_spec": "is-buffer@~2.0.3",
"_where": "/Users/josh.burman/Projects/braid/node_modules/flat",
"author": {
"name": "Feross Aboukhadijeh",
"email": "feross@feross.org",
"url": "https://feross.org"
},
"bugs": {
"url": "https://github.com/feross/is-buffer/issues"
},
"bundleDependencies": false,
"dependencies": {},
"deprecated": false,
"description": "Determine if an object is a Buffer",
"devDependencies": {
"airtap": "0.0.7",
"standard": "*",
"tape": "^4.0.0"
},
"engines": {
"node": ">=4"
},
"homepage": "https://github.com/feross/is-buffer#readme",
"keywords": [
"arraybuffer",
"browser",
"browser buffer",
"browserify",
"buffer",
"buffers",
"core buffer",
"dataview",
"float32array",
"float64array",
"int16array",
"int32array",
"type",
"typed array",
"uint32array"
],
"license": "MIT",
"main": "index.js",
"name": "is-buffer",
"repository": {
"type": "git",
"url": "git://github.com/feross/is-buffer.git"
},
"scripts": {
"test": "standard && npm run test-node && npm run test-browser",
"test-browser": "airtap -- test/*.js",
"test-browser-local": "airtap --local -- test/*.js",
"test-node": "tape test/*.js"
},
"testling": {
"files": "test/*.js"
},
"version": "2.0.3"
}

70
node_modules/flat/package.json generated vendored Normal file
View File

@ -0,0 +1,70 @@
{
"_from": "flat@^4.1.0",
"_id": "flat@4.1.0",
"_inBundle": false,
"_integrity": "sha512-Px/TiLIznH7gEDlPXcUD4KnBusa6kR6ayRUVcnEAbreRIuhkqow/mun59BuRXwoYk7ZQOLW1ZM05ilIvK38hFw==",
"_location": "/flat",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "flat@^4.1.0",
"name": "flat",
"escapedName": "flat",
"rawSpec": "^4.1.0",
"saveSpec": null,
"fetchSpec": "^4.1.0"
},
"_requiredBy": [
"/yargs-unparser"
],
"_resolved": "https://registry.npmjs.org/flat/-/flat-4.1.0.tgz",
"_shasum": "090bec8b05e39cba309747f1d588f04dbaf98db2",
"_spec": "flat@^4.1.0",
"_where": "/Users/josh.burman/Projects/braid/node_modules/yargs-unparser",
"author": {
"name": "Hugh Kennedy",
"email": "hughskennedy@gmail.com",
"url": "http://hughskennedy.com"
},
"bin": {
"flat": "cli.js"
},
"bugs": {
"url": "https://github.com/hughsk/flat/issues"
},
"bundleDependencies": false,
"dependencies": {
"is-buffer": "~2.0.3"
},
"deprecated": false,
"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"
},
"homepage": "https://github.com/hughsk/flat",
"keywords": [
"flat",
"json",
"flatten",
"unflatten",
"split",
"object",
"nested"
],
"license": "BSD-3-Clause",
"main": "index.js",
"name": "flat",
"repository": {
"type": "git",
"url": "git://github.com/hughsk/flat.git"
},
"scripts": {
"test": "mocha -u tdd --reporter spec && standard index.js test/index.js"
},
"version": "4.1.0"
}

513
node_modules/flat/test/test.js generated vendored Normal file
View File

@ -0,0 +1,513 @@
/* 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])
}
}
})
})
}
})
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'
}
})
})
})