system-prompts-and-models-o.../unified-ai-platform/node_modules/flat/test/test.js
dopeuni444 b5a22951ae nhj
more
2025-07-31 12:23:33 +04:00

366 lines
7.5 KiB
JavaScript

/* 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.unf