flattify 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +332 -120
- package/dist/flatten-CYe7gyYE.js +228 -0
- package/dist/flatten-DWk2mUxV.cjs +287 -0
- package/dist/index.cjs +89 -0
- package/dist/index.d.cts +152 -0
- package/dist/index.d.ts +152 -0
- package/dist/index.js +87 -0
- package/dist/path-types-D9d1uvNh.d.cts +148 -0
- package/dist/path-types-D9d1uvNh.d.ts +148 -0
- package/dist/path.cjs +132 -0
- package/dist/path.d.cts +168 -0
- package/dist/path.d.ts +168 -0
- package/dist/path.js +124 -0
- package/package.json +106 -17
- package/path/package.json +6 -0
- package/.editorconfig +0 -17
- package/.npmignore +0 -3
- package/CHANGELOG.md +0 -6
- package/index.js +0 -7
- package/lib/flattify.js +0 -60
- package/test/flattify.js +0 -180
- package/test/mocks/array.json +0 -44
- package/test/mocks/object.json +0 -21
package/dist/path.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { a as escapeKey$1, c as assign, i as validateNotation, n as validate, o as escapePointer, r as pathFormat, s as isIndex, t as flatten, u as isPlainObject } from "./flatten-CYe7gyYE.js";
|
|
2
|
+
//#region src/path/path.ts
|
|
3
|
+
const MISSING = Symbol("missing");
|
|
4
|
+
const FORBIDDEN = /* @__PURE__ */ new Set([
|
|
5
|
+
"__proto__",
|
|
6
|
+
"constructor",
|
|
7
|
+
"prototype"
|
|
8
|
+
]);
|
|
9
|
+
const hasOwn = (target, key) => Object.prototype.hasOwnProperty.call(target, key);
|
|
10
|
+
const isTraversable = (value) => typeof value === "object" && value !== null || typeof value === "function";
|
|
11
|
+
function formatOf(options) {
|
|
12
|
+
const { delimiter = ".", notation = "dot", escape = true } = options;
|
|
13
|
+
validateNotation(notation);
|
|
14
|
+
if (notation !== "pointer") validate(delimiter, escape);
|
|
15
|
+
return pathFormat(notation, delimiter, escape);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Reads `key` of `target`. Plain objects and arrays expose their own keys only, so `'toString'` or
|
|
19
|
+
* `'constructor'` are missing on data; other objects (class instances, dates...) expose inherited
|
|
20
|
+
* properties too, except `__proto__`, `constructor` and `prototype`.
|
|
21
|
+
*/
|
|
22
|
+
function read(target, key) {
|
|
23
|
+
if (isPlainObject(target) || Array.isArray(target)) return hasOwn(target, key) ? target[key] : MISSING;
|
|
24
|
+
if (FORBIDDEN.has(key) && !hasOwn(target, key)) return MISSING;
|
|
25
|
+
return key in target ? target[key] : MISSING;
|
|
26
|
+
}
|
|
27
|
+
function checkTarget(input, name) {
|
|
28
|
+
if (!isTraversable(input)) throw new TypeError(`${name}() expects an object or an array`);
|
|
29
|
+
}
|
|
30
|
+
function get(obj, path, options = {}) {
|
|
31
|
+
checkTarget(obj, "get");
|
|
32
|
+
let current = obj;
|
|
33
|
+
for (const key of formatOf(options).split(path)) {
|
|
34
|
+
if (!isTraversable(current)) return void 0;
|
|
35
|
+
current = read(current, key);
|
|
36
|
+
if (current === MISSING) return void 0;
|
|
37
|
+
}
|
|
38
|
+
return current;
|
|
39
|
+
}
|
|
40
|
+
function has(obj, path, options = {}) {
|
|
41
|
+
checkTarget(obj, "has");
|
|
42
|
+
let current = obj;
|
|
43
|
+
for (const key of formatOf(options).split(path)) {
|
|
44
|
+
if (!isTraversable(current)) return false;
|
|
45
|
+
current = read(current, key);
|
|
46
|
+
if (current === MISSING) return false;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
function set(obj, path, value, options = {}) {
|
|
51
|
+
checkTarget(obj, "set");
|
|
52
|
+
const { object = false, arrayLimit = 1e3 } = options;
|
|
53
|
+
if (arrayLimit !== Infinity && (!Number.isInteger(arrayLimit) || arrayLimit < 0)) throw new RangeError("`arrayLimit` must be a non-negative integer or Infinity");
|
|
54
|
+
const segments = formatOf(options).split(path);
|
|
55
|
+
if (segments.length === 0) throw new TypeError("set() cannot replace the whole object: the JSON Pointer \"\" has no key");
|
|
56
|
+
if (segments.includes("__proto__")) return obj;
|
|
57
|
+
let target = obj;
|
|
58
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
59
|
+
const child = read(target, segments[i]);
|
|
60
|
+
if (isTraversable(child)) target = child;
|
|
61
|
+
else {
|
|
62
|
+
const next = !object && isIndex(segments[i + 1], arrayLimit) ? [] : {};
|
|
63
|
+
assign(target, segments[i], next);
|
|
64
|
+
target = next;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
assign(target, segments[segments.length - 1], value);
|
|
68
|
+
return obj;
|
|
69
|
+
}
|
|
70
|
+
function del(obj, path, options = {}) {
|
|
71
|
+
checkTarget(obj, "del");
|
|
72
|
+
const segments = formatOf(options).split(path);
|
|
73
|
+
if (segments.length === 0) return false;
|
|
74
|
+
let target = obj;
|
|
75
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
76
|
+
if (!isTraversable(target)) return false;
|
|
77
|
+
target = read(target, segments[i]);
|
|
78
|
+
}
|
|
79
|
+
const last = segments[segments.length - 1];
|
|
80
|
+
if (!isTraversable(target) || !hasOwn(target, last)) return false;
|
|
81
|
+
return delete target[last];
|
|
82
|
+
}
|
|
83
|
+
function paths(obj, options = {}) {
|
|
84
|
+
return Object.keys(flatten(obj, options));
|
|
85
|
+
}
|
|
86
|
+
function parsePath(path, options = {}) {
|
|
87
|
+
return formatOf(options).split(path);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Joins keys into a path, escaping them when needed: the reverse of `parsePath`. In bracket notation,
|
|
91
|
+
* keys that are array indices are written between brackets.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* stringifyPath(['user', 'first.name']); // 'user.first\\.name'
|
|
96
|
+
* stringifyPath(['items', '0', 'id'], { notation: 'bracket' }); // 'items[0].id'
|
|
97
|
+
* stringifyPath(['a/b', 'c'], { notation: 'pointer' }); // '/a~1b/c'
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
function stringifyPath(keys, options = {}) {
|
|
101
|
+
var _path;
|
|
102
|
+
const format = formatOf(options);
|
|
103
|
+
const bracket = options.notation === "bracket";
|
|
104
|
+
let path;
|
|
105
|
+
for (const key of keys) path = format.join(path, String(key), bracket && isIndex(String(key), Infinity));
|
|
106
|
+
return (_path = path) !== null && _path !== void 0 ? _path : "";
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Escapes a single key so it can be put in a path as it is.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* `user.${escapeKey('first.name')}`; // 'user.first\\.name'
|
|
114
|
+
* escapeKey('a/b', { notation: 'pointer' }); // 'a~1b'
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
function escapeKey(key, options = {}) {
|
|
118
|
+
const { delimiter = ".", notation = "dot" } = options;
|
|
119
|
+
formatOf(options);
|
|
120
|
+
if (notation === "pointer") return escapePointer(key);
|
|
121
|
+
return escapeKey$1(key, delimiter, notation === "bracket" ? "[" : "");
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
export { del, escapeKey, get, has, parsePath, paths, set, stringifyPath };
|
package/package.json
CHANGED
|
@@ -1,28 +1,117 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flattify",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Flatten and unflatten nested objects without losing data: dates, nulls, empty objects and dotted keys survive the round trip. Typed paths, safe on untrusted input, zero dependencies.",
|
|
5
|
+
"author": "Fernando Machado Bernardino <fernandombernardino@gmail.com>",
|
|
6
|
+
"homepage": "https://github.com/NandoMB/flattify#readme",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"packageManager": "pnpm@12.5.1",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/NandoMB/flattify/issues"
|
|
8
12
|
},
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
|
11
15
|
"url": "git+https://github.com/NandoMB/flattify.git"
|
|
12
16
|
},
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public",
|
|
19
|
+
"provenance": true
|
|
20
|
+
},
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"main": "./dist/index.cjs",
|
|
23
|
+
"module": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"import": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"require": {
|
|
32
|
+
"types": "./dist/index.d.cts",
|
|
33
|
+
"default": "./dist/index.cjs"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"./path": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/path.d.ts",
|
|
39
|
+
"default": "./dist/path.js"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/path.d.cts",
|
|
43
|
+
"default": "./dist/path.cjs"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist",
|
|
50
|
+
"path"
|
|
17
51
|
],
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
52
|
+
"scripts": {
|
|
53
|
+
"dev": "vitest --project node",
|
|
54
|
+
"test": "vitest run --project node",
|
|
55
|
+
"coverage": "vitest run --project node --coverage",
|
|
56
|
+
"bench": "pnpm run build && vitest bench --run --config bench/vitest.config.mts",
|
|
57
|
+
"claims": "pnpm run build && tsc -p bench && vitest run --config bench/vitest.config.mts",
|
|
58
|
+
"size": "pnpm run build && node bench/size.js",
|
|
59
|
+
"build": "tsdown",
|
|
60
|
+
"lint": "tsc",
|
|
61
|
+
"ci": "pnpm run lint && pnpm run test && pnpm run build && pnpm run lint:package",
|
|
62
|
+
"release": "node scripts/release.js",
|
|
63
|
+
"changeset:version": "changeset version && node scripts/sync-jsr-version.js",
|
|
64
|
+
"lint:package": "publint && attw --pack .",
|
|
65
|
+
"test:browser": "vitest run --project browser",
|
|
66
|
+
"test:bun": "bunx --bun vitest run --project node",
|
|
67
|
+
"test:deno": "deno run -A node_modules/vitest/vitest.mjs run --project node",
|
|
68
|
+
"test:workers": "node test/workers/run.js",
|
|
69
|
+
"test:ts-legacy": "node node_modules/typescript-5.0/bin/tsc -p test/typescript-5.0",
|
|
70
|
+
"lint:jsr": "deno publish --dry-run --allow-dirty",
|
|
71
|
+
"test:ci": "pnpm run ci && pnpm run test:ts-legacy && pnpm run test:workers && pnpm run test:browser && pnpm run test:bun && pnpm run test:deno && pnpm run lint:jsr"
|
|
22
72
|
},
|
|
23
|
-
"homepage": "https://github.com/NandoMB/flattify#readme",
|
|
24
73
|
"devDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
74
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
75
|
+
"@changesets/cli": "^3.0.3",
|
|
76
|
+
"@types/node": "^26.6.2",
|
|
77
|
+
"@vitest/browser-playwright": "^5.0.1",
|
|
78
|
+
"@vitest/coverage-istanbul": "^5.0.1",
|
|
79
|
+
"es-toolkit": "1.52.0",
|
|
80
|
+
"esbuild": "^0.28.2",
|
|
81
|
+
"fast-check": "^4.10.2",
|
|
82
|
+
"flat": "6.0.1",
|
|
83
|
+
"flattie": "1.1.1",
|
|
84
|
+
"miniflare": "^4.20260730.0",
|
|
85
|
+
"nestie": "1.0.3",
|
|
86
|
+
"playwright": "^1.63.0",
|
|
87
|
+
"publint": "^0.3.24",
|
|
88
|
+
"radashi": "12.9.6",
|
|
89
|
+
"tsdown": "^0.23.0",
|
|
90
|
+
"typescript": "^7.0.2",
|
|
91
|
+
"typescript-5.0": "npm:typescript@5.0.4",
|
|
92
|
+
"vitest": "^5.0.1"
|
|
93
|
+
},
|
|
94
|
+
"keywords": [
|
|
95
|
+
"flattify",
|
|
96
|
+
"flatten",
|
|
97
|
+
"unflatten",
|
|
98
|
+
"flat",
|
|
99
|
+
"unflat",
|
|
100
|
+
"nest",
|
|
101
|
+
"unnest",
|
|
102
|
+
"nested",
|
|
103
|
+
"object",
|
|
104
|
+
"keys",
|
|
105
|
+
"paths",
|
|
106
|
+
"dot notation",
|
|
107
|
+
"dot-notation",
|
|
108
|
+
"bracket notation",
|
|
109
|
+
"json pointer",
|
|
110
|
+
"rfc6901",
|
|
111
|
+
"deep",
|
|
112
|
+
"typescript",
|
|
113
|
+
"type inference",
|
|
114
|
+
"prototype pollution",
|
|
115
|
+
"zero dependencies"
|
|
116
|
+
]
|
|
28
117
|
}
|
package/.editorconfig
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
# EditorConfig helps developers define and maintain consistent
|
|
2
|
-
# coding styles between different editors and IDEs
|
|
3
|
-
# editorconfig.org
|
|
4
|
-
|
|
5
|
-
root = true
|
|
6
|
-
|
|
7
|
-
[*]
|
|
8
|
-
|
|
9
|
-
indent_style = space
|
|
10
|
-
indent_size = 2
|
|
11
|
-
end_of_line = lf
|
|
12
|
-
charset = utf-8
|
|
13
|
-
trim_trailing_whitespace = true
|
|
14
|
-
insert_final_newline = true
|
|
15
|
-
|
|
16
|
-
[*.md]
|
|
17
|
-
trim_trailing_whitespace = false
|
package/.npmignore
DELETED
package/CHANGELOG.md
DELETED
package/index.js
DELETED
package/lib/flattify.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var config = {
|
|
4
|
-
isFlattifyArray: false
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
var parseJSON = function (string) {
|
|
8
|
-
try {
|
|
9
|
-
return JSON.parse(string);
|
|
10
|
-
} catch (e) {
|
|
11
|
-
return string;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
var execute = function (json, parent, flattenJson) {
|
|
16
|
-
var flattenJson = flattenJson || {};
|
|
17
|
-
|
|
18
|
-
for (var property in json) {
|
|
19
|
-
if (property !== null) {
|
|
20
|
-
switch (Object.prototype.toString.call(json[property])) {
|
|
21
|
-
case '[object Array]':
|
|
22
|
-
if (config.isFlattifyArray) {
|
|
23
|
-
execute(json[property], parent ? parent + '.' + property : property, flattenJson);
|
|
24
|
-
} else {
|
|
25
|
-
flattenJson[property] = JSON.stringify(json[property]);
|
|
26
|
-
}
|
|
27
|
-
break;
|
|
28
|
-
case '[object Object]':
|
|
29
|
-
execute(json[property], parent ? parent + '.' + property : property, flattenJson);
|
|
30
|
-
break;
|
|
31
|
-
default:
|
|
32
|
-
flattenJson[parent? parent + '.' + property : property] = json[property];
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return flattenJson;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
var isArray = function (json) {
|
|
41
|
-
return Object.prototype.toString.call(json) === '[object Array]';
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
var flattify = function (json, isFlattifyArray) {
|
|
45
|
-
config.isFlattifyArray = !!isFlattifyArray;
|
|
46
|
-
json = parseJSON(json);
|
|
47
|
-
|
|
48
|
-
if (!isArray(json))
|
|
49
|
-
return execute(json);
|
|
50
|
-
|
|
51
|
-
var array = [];
|
|
52
|
-
|
|
53
|
-
json.forEach(function (item) {
|
|
54
|
-
array.push(execute(item));
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
return array;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
module.exports = flattify;
|
package/test/flattify.js
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var flattify = require('../lib/flattify.js');
|
|
4
|
-
var should = require('chai').should();
|
|
5
|
-
var path = require('path');
|
|
6
|
-
var fs = require('fs');
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
describe('When execute flattify,', function () {
|
|
10
|
-
var objectMock, arrayMock, getMock;
|
|
11
|
-
|
|
12
|
-
before(function() {
|
|
13
|
-
getMock = function (name, charset) {
|
|
14
|
-
return fs.readFileSync(path.join(__dirname, 'mocks/' + name), charset);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
objectMock = getMock('object.json', 'UTF-8');
|
|
18
|
-
arrayMock = getMock('array.json', 'UTF-8');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
describe('whith only first parameter:', function () {
|
|
22
|
-
describe('passing a json:', function () {
|
|
23
|
-
it('object', function (done) {
|
|
24
|
-
var result = flattify(objectMock);
|
|
25
|
-
result['id'].should.equal(1);
|
|
26
|
-
result['gender'].should.equal('Male');
|
|
27
|
-
result['first'].should.equal('Joshua');
|
|
28
|
-
result['last'].should.equal('Williams');
|
|
29
|
-
result['emails'].should.equal('[\"jwilliams1@vkontakte.ru\",\"phowell1@ezinearticles.com\",\"aferguson2@pen.io\"]');
|
|
30
|
-
result['addresses'].should.equal('[{\"street\":\"Anzinger Court\",\"number\":73902},{\"street\":\"Anthes Parkway\",\"number\":106}]');
|
|
31
|
-
done();
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('array', function (done) {
|
|
35
|
-
var result = flattify(arrayMock);
|
|
36
|
-
|
|
37
|
-
result[0]['id'].should.equal(1);
|
|
38
|
-
result[0]['gender'].should.equal('Male');
|
|
39
|
-
result[0]['first'].should.equal('Joshua');
|
|
40
|
-
result[0]['last'].should.equal('Williams');
|
|
41
|
-
result[0]['emails'].should.equal('[\"jwilliams1@vkontakte.ru\",\"phowell1@ezinearticles.com\",\"aferguson2@pen.io\"]');
|
|
42
|
-
result[0]['addresses'].should.equal('[{\"street\":\"Anzinger Court\",\"number\":73902},{\"street\":\"Anthes Parkway\",\"number\":106}]');
|
|
43
|
-
|
|
44
|
-
result[1]['id'].should.equal(2);
|
|
45
|
-
result[1]['gender'].should.equal('Male');
|
|
46
|
-
result[1]['first'].should.equal('Joshua');
|
|
47
|
-
result[1]['last'].should.equal('Williams');
|
|
48
|
-
result[1]['emails'].should.equal('[\"jwilliams1@vkontakte.ru\",\"phowell1@ezinearticles.com\",\"aferguson2@pen.io\"]');
|
|
49
|
-
result[1]['addresses'].should.equal('[{\"street\":\"Anzinger Court\",\"number\":73902},{\"street\":\"Anthes Parkway\",\"number\":106}]');
|
|
50
|
-
done();
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe('passing a javascript:', function () {
|
|
55
|
-
it('object', function (done) {
|
|
56
|
-
var result = flattify(JSON.parse(objectMock));
|
|
57
|
-
result['id'].should.equal(1);
|
|
58
|
-
result['gender'].should.equal('Male');
|
|
59
|
-
result['first'].should.equal('Joshua');
|
|
60
|
-
result['last'].should.equal('Williams');
|
|
61
|
-
result['emails'].should.equal('[\"jwilliams1@vkontakte.ru\",\"phowell1@ezinearticles.com\",\"aferguson2@pen.io\"]');
|
|
62
|
-
result['addresses'].should.equal('[{\"street\":\"Anzinger Court\",\"number\":73902},{\"street\":\"Anthes Parkway\",\"number\":106}]');
|
|
63
|
-
done();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('array', function (done) {
|
|
67
|
-
var result = flattify(JSON.parse(arrayMock));
|
|
68
|
-
|
|
69
|
-
result[0]['id'].should.equal(1);
|
|
70
|
-
result[0]['gender'].should.equal('Male');
|
|
71
|
-
result[0]['first'].should.equal('Joshua');
|
|
72
|
-
result[0]['last'].should.equal('Williams');
|
|
73
|
-
result[0]['emails'].should.equal('[\"jwilliams1@vkontakte.ru\",\"phowell1@ezinearticles.com\",\"aferguson2@pen.io\"]');
|
|
74
|
-
result[0]['addresses'].should.equal('[{\"street\":\"Anzinger Court\",\"number\":73902},{\"street\":\"Anthes Parkway\",\"number\":106}]');
|
|
75
|
-
|
|
76
|
-
result[1]['id'].should.equal(2);
|
|
77
|
-
result[1]['gender'].should.equal('Male');
|
|
78
|
-
result[1]['first'].should.equal('Joshua');
|
|
79
|
-
result[1]['last'].should.equal('Williams');
|
|
80
|
-
result[1]['emails'].should.equal('[\"jwilliams1@vkontakte.ru\",\"phowell1@ezinearticles.com\",\"aferguson2@pen.io\"]');
|
|
81
|
-
result[1]['addresses'].should.equal('[{\"street\":\"Anzinger Court\",\"number\":73902},{\"street\":\"Anthes Parkway\",\"number\":106}]');
|
|
82
|
-
done();
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe('with both parameters:', function () {
|
|
88
|
-
describe('passing a json:', function () {
|
|
89
|
-
it('object', function (done) {
|
|
90
|
-
var result = flattify(objectMock, true);
|
|
91
|
-
result['id'].should.equal(1);
|
|
92
|
-
result['gender'].should.equal('Male');
|
|
93
|
-
result['first'].should.equal('Joshua');
|
|
94
|
-
result['last'].should.equal('Williams');
|
|
95
|
-
result['emails.0'].should.equal('jwilliams1@vkontakte.ru');
|
|
96
|
-
result['emails.1'].should.equal('phowell1@ezinearticles.com');
|
|
97
|
-
result['emails.2'].should.equal('aferguson2@pen.io');
|
|
98
|
-
result['addresses.0.street'].should.equal('Anzinger Court');
|
|
99
|
-
result['addresses.0.number'].should.equal(73902);
|
|
100
|
-
result['addresses.1.street'].should.equal('Anthes Parkway');
|
|
101
|
-
result['addresses.1.number'].should.equal(106);
|
|
102
|
-
done();
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('array', function (done) {
|
|
106
|
-
var result = flattify(arrayMock, true);
|
|
107
|
-
result[0]['id'].should.equal(1);
|
|
108
|
-
result[0]['gender'].should.equal('Male');
|
|
109
|
-
result[0]['first'].should.equal('Joshua');
|
|
110
|
-
result[0]['last'].should.equal('Williams');
|
|
111
|
-
result[0]['emails.0'].should.equal('jwilliams1@vkontakte.ru');
|
|
112
|
-
result[0]['emails.1'].should.equal('phowell1@ezinearticles.com');
|
|
113
|
-
result[0]['emails.2'].should.equal('aferguson2@pen.io');
|
|
114
|
-
result[0]['addresses.0.street'].should.equal('Anzinger Court');
|
|
115
|
-
result[0]['addresses.0.number'].should.equal(73902);
|
|
116
|
-
result[0]['addresses.1.street'].should.equal('Anthes Parkway');
|
|
117
|
-
result[0]['addresses.1.number'].should.equal(106);
|
|
118
|
-
|
|
119
|
-
result[1]['id'].should.equal(2);
|
|
120
|
-
result[1]['gender'].should.equal('Male');
|
|
121
|
-
result[1]['first'].should.equal('Joshua');
|
|
122
|
-
result[1]['last'].should.equal('Williams');
|
|
123
|
-
result[1]['emails.0'].should.equal('jwilliams1@vkontakte.ru');
|
|
124
|
-
result[1]['emails.1'].should.equal('phowell1@ezinearticles.com');
|
|
125
|
-
result[1]['emails.2'].should.equal('aferguson2@pen.io');
|
|
126
|
-
result[1]['addresses.0.street'].should.equal('Anzinger Court');
|
|
127
|
-
result[1]['addresses.0.number'].should.equal(73902);
|
|
128
|
-
result[1]['addresses.1.street'].should.equal('Anthes Parkway');
|
|
129
|
-
result[1]['addresses.1.number'].should.equal(106);
|
|
130
|
-
done();
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
describe('passing a javascript:', function () {
|
|
135
|
-
it('object', function (done) {
|
|
136
|
-
var result = flattify(JSON.parse(objectMock), true);
|
|
137
|
-
result['id'].should.equal(1);
|
|
138
|
-
result['gender'].should.equal('Male');
|
|
139
|
-
result['first'].should.equal('Joshua');
|
|
140
|
-
result['last'].should.equal('Williams');
|
|
141
|
-
result['emails.0'].should.equal('jwilliams1@vkontakte.ru');
|
|
142
|
-
result['emails.1'].should.equal('phowell1@ezinearticles.com');
|
|
143
|
-
result['emails.2'].should.equal('aferguson2@pen.io');
|
|
144
|
-
result['addresses.0.street'].should.equal('Anzinger Court');
|
|
145
|
-
result['addresses.0.number'].should.equal(73902);
|
|
146
|
-
result['addresses.1.street'].should.equal('Anthes Parkway');
|
|
147
|
-
result['addresses.1.number'].should.equal(106);
|
|
148
|
-
done();
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
it('array', function (done) {
|
|
152
|
-
var result = flattify(JSON.parse(arrayMock), true);
|
|
153
|
-
result[0]['id'].should.equal(1);
|
|
154
|
-
result[0]['gender'].should.equal('Male');
|
|
155
|
-
result[0]['first'].should.equal('Joshua');
|
|
156
|
-
result[0]['last'].should.equal('Williams');
|
|
157
|
-
result[0]['emails.0'].should.equal('jwilliams1@vkontakte.ru');
|
|
158
|
-
result[0]['emails.1'].should.equal('phowell1@ezinearticles.com');
|
|
159
|
-
result[0]['emails.2'].should.equal('aferguson2@pen.io');
|
|
160
|
-
result[0]['addresses.0.street'].should.equal('Anzinger Court');
|
|
161
|
-
result[0]['addresses.0.number'].should.equal(73902);
|
|
162
|
-
result[0]['addresses.1.street'].should.equal('Anthes Parkway');
|
|
163
|
-
result[0]['addresses.1.number'].should.equal(106);
|
|
164
|
-
|
|
165
|
-
result[1]['id'].should.equal(2);
|
|
166
|
-
result[1]['gender'].should.equal('Male');
|
|
167
|
-
result[1]['first'].should.equal('Joshua');
|
|
168
|
-
result[1]['last'].should.equal('Williams');
|
|
169
|
-
result[1]['emails.0'].should.equal('jwilliams1@vkontakte.ru');
|
|
170
|
-
result[1]['emails.1'].should.equal('phowell1@ezinearticles.com');
|
|
171
|
-
result[1]['emails.2'].should.equal('aferguson2@pen.io');
|
|
172
|
-
result[1]['addresses.0.street'].should.equal('Anzinger Court');
|
|
173
|
-
result[1]['addresses.0.number'].should.equal(73902);
|
|
174
|
-
result[1]['addresses.1.street'].should.equal('Anthes Parkway');
|
|
175
|
-
result[1]['addresses.1.number'].should.equal(106);
|
|
176
|
-
done();
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
});
|
package/test/mocks/array.json
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"id": 1,
|
|
4
|
-
"gender": "Male",
|
|
5
|
-
"first": "Joshua",
|
|
6
|
-
"last": "Williams",
|
|
7
|
-
"emails": [
|
|
8
|
-
"jwilliams1@vkontakte.ru",
|
|
9
|
-
"phowell1@ezinearticles.com",
|
|
10
|
-
"aferguson2@pen.io"
|
|
11
|
-
],
|
|
12
|
-
"addresses": [
|
|
13
|
-
{
|
|
14
|
-
"street": "Anzinger Court",
|
|
15
|
-
"number": 73902
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
"street": "Anthes Parkway",
|
|
19
|
-
"number": 106
|
|
20
|
-
}
|
|
21
|
-
]
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
"id": 2,
|
|
25
|
-
"gender": "Male",
|
|
26
|
-
"first": "Joshua",
|
|
27
|
-
"last": "Williams",
|
|
28
|
-
"emails": [
|
|
29
|
-
"jwilliams1@vkontakte.ru",
|
|
30
|
-
"phowell1@ezinearticles.com",
|
|
31
|
-
"aferguson2@pen.io"
|
|
32
|
-
],
|
|
33
|
-
"addresses": [
|
|
34
|
-
{
|
|
35
|
-
"street": "Anzinger Court",
|
|
36
|
-
"number": 73902
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
"street": "Anthes Parkway",
|
|
40
|
-
"number": 106
|
|
41
|
-
}
|
|
42
|
-
]
|
|
43
|
-
}
|
|
44
|
-
]
|
package/test/mocks/object.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"id": 1,
|
|
3
|
-
"gender": "Male",
|
|
4
|
-
"first": "Joshua",
|
|
5
|
-
"last": "Williams",
|
|
6
|
-
"emails": [
|
|
7
|
-
"jwilliams1@vkontakte.ru",
|
|
8
|
-
"phowell1@ezinearticles.com",
|
|
9
|
-
"aferguson2@pen.io"
|
|
10
|
-
],
|
|
11
|
-
"addresses": [
|
|
12
|
-
{
|
|
13
|
-
"street": "Anzinger Court",
|
|
14
|
-
"number": 73902
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
"street": "Anthes Parkway",
|
|
18
|
-
"number": 106
|
|
19
|
-
}
|
|
20
|
-
]
|
|
21
|
-
}
|