123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- /* 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'
- }
- })
- })
- })
|