@ms-cloudpack/json-utilities 0.1.9 → 0.1.11
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/lib/cloneJson.d.ts +15 -0
- package/lib/cloneJson.d.ts.map +1 -0
- package/lib/cloneJson.js +22 -0
- package/lib/cloneJson.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clone a JSON-like object, fast. Does NOT handle non-JSON-serializable values
|
|
3
|
+
* (such as dates or functions), circular references, etc.
|
|
4
|
+
*
|
|
5
|
+
* This is decently faster than `structuredClone` or `JSON.parse(JSON.stringify())` on a
|
|
6
|
+
* very large package.json, and a bit faster for a simple object.
|
|
7
|
+
* https://jsperf.app/rugosa/5
|
|
8
|
+
*/
|
|
9
|
+
export declare function cloneJson<T extends unknown[]>(obj: T): T;
|
|
10
|
+
export declare function cloneJson<T extends object>(obj: T): T;
|
|
11
|
+
export declare function cloneJson<T extends object, TKeys extends keyof T>(obj: T, options: {
|
|
12
|
+
/** Keys with true values are cloned, keys with false values are not cloned. Not propagated to recursive operations. */
|
|
13
|
+
keys: Record<TKeys, boolean>;
|
|
14
|
+
}): Pick<T, TKeys>;
|
|
15
|
+
//# sourceMappingURL=cloneJson.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cloneJson.d.ts","sourceRoot":"","sources":["../src/cloneJson.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,OAAO,EAAE,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;AAC1D,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;AACvD,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,SAAS,MAAM,CAAC,EAC/D,GAAG,EAAE,CAAC,EACN,OAAO,EAAE;IACP,uHAAuH;IACvH,IAAI,EAAE,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;CAC9B,GACA,IAAI,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC"}
|
package/lib/cloneJson.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
2
|
+
export function cloneJson(obj, options) {
|
|
3
|
+
const { keys } = options || {};
|
|
4
|
+
if (Array.isArray(obj)) {
|
|
5
|
+
const clone = [];
|
|
6
|
+
for (let i = 0; i < obj.length; i++) {
|
|
7
|
+
const val = obj[i];
|
|
8
|
+
// Skip the recursive call if not an object.
|
|
9
|
+
clone[i] = val && typeof val === 'object' ? cloneJson(val) : val;
|
|
10
|
+
}
|
|
11
|
+
return clone;
|
|
12
|
+
}
|
|
13
|
+
const clone = {};
|
|
14
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
15
|
+
if (!keys || keys[key] === true) {
|
|
16
|
+
// eslint-disable-next-line
|
|
17
|
+
clone[key] = val && typeof val === 'object' ? cloneJson(val) : val;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return clone;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=cloneJson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cloneJson.js","sourceRoot":"","sources":["../src/cloneJson.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAmB5D,MAAM,UAAU,SAAS,CAAmB,GAAM,EAAE,OAA6C;IAC/F,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IAE/B,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,KAAK,GAAG,EAAgB,CAAC;QAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACpC,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;YACnB,4CAA4C;YAC5C,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;QACnE,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,KAAK,GAAG,EAAgB,CAAC;IAC/B,KAAK,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QAC7C,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,GAAc,CAAC,KAAK,IAAI,EAAE,CAAC;YAC3C,2BAA2B;YAC1B,KAAa,CAAC,GAAG,CAAC,GAAG,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;QAC9E,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-unsafe-assignment */\n\n/**\n * Clone a JSON-like object, fast. Does NOT handle non-JSON-serializable values\n * (such as dates or functions), circular references, etc.\n *\n * This is decently faster than `structuredClone` or `JSON.parse(JSON.stringify())` on a\n * very large package.json, and a bit faster for a simple object.\n * https://jsperf.app/rugosa/5\n */\nexport function cloneJson<T extends unknown[]>(obj: T): T;\nexport function cloneJson<T extends object>(obj: T): T;\nexport function cloneJson<T extends object, TKeys extends keyof T>(\n obj: T,\n options: {\n /** Keys with true values are cloned, keys with false values are not cloned. Not propagated to recursive operations. */\n keys: Record<TKeys, boolean>;\n },\n): Pick<T, TKeys>;\nexport function cloneJson<T extends object>(obj: T, options?: { keys?: Record<keyof T, boolean> }): T {\n const { keys } = options || {};\n\n if (Array.isArray(obj)) {\n const clone = [] as typeof obj;\n for (let i = 0; i < obj.length; i++) {\n const val = obj[i];\n // Skip the recursive call if not an object.\n clone[i] = val && typeof val === 'object' ? cloneJson(val) : val;\n }\n return clone;\n }\n\n const clone = {} as typeof obj;\n for (const [key, val] of Object.entries(obj)) {\n if (!keys || keys[key as keyof T] === true) {\n // eslint-disable-next-line\n (clone as any)[key] = val && typeof val === 'object' ? cloneJson(val) : val;\n }\n }\n return clone;\n}\n"]}
|
package/lib/index.d.ts
CHANGED
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC"}
|
package/lib/index.js
CHANGED
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAwB,MAAM,eAAe,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,aAAa,EAAyB,MAAM,gBAAgB,CAAC","sourcesContent":["export { readJson, readJsonSync, type ReadJsonOptions } from './readJson.js';\nexport { writeJson, writeJsonSync, type WriteJsonOptions } from './writeJson.js';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAwB,MAAM,eAAe,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,aAAa,EAAyB,MAAM,gBAAgB,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["export { readJson, readJsonSync, type ReadJsonOptions } from './readJson.js';\nexport { writeJson, writeJsonSync, type WriteJsonOptions } from './writeJson.js';\nexport { cloneJson } from './cloneJson.js';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/json-utilities",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "Helpers for reading/writing json files.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
|
|
22
22
|
"@ms-cloudpack/scripts": "^0.0.1",
|
|
23
|
+
"@ms-cloudpack/test-utilities": "^0.5.0",
|
|
23
24
|
"@types/jju": "^1.4.4"
|
|
24
25
|
},
|
|
25
26
|
"scripts": {
|