@ms-cloudpack/json-utilities 0.1.9 → 0.1.10

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.
@@ -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
+ /** Clone only these keys. Not propagated to recursive operations. */
13
+ onlyKeys: Record<TKeys, true>;
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,qEAAqE;IACrE,QAAQ,EAAE,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;CAC/B,GACA,IAAI,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC"}
@@ -0,0 +1,22 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
2
+ export function cloneJson(obj, options) {
3
+ const { onlyKeys } = 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 (!onlyKeys || onlyKeys[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,OAA8C;IAChG,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IAEnC,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,QAAQ,IAAI,QAAQ,CAAC,GAAc,CAAC,KAAK,IAAI,EAAE,CAAC;YACnD,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 /** Clone only these keys. Not propagated to recursive operations. */\n onlyKeys: Record<TKeys, true>;\n },\n): Pick<T, TKeys>;\nexport function cloneJson<T extends object>(obj: T, options?: { onlyKeys?: Record<keyof T, true> }): T {\n const { onlyKeys } = 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 (!onlyKeys || onlyKeys[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
@@ -1,3 +1,4 @@
1
1
  export { readJson, readJsonSync, type ReadJsonOptions } from './readJson.js';
2
2
  export { writeJson, writeJsonSync, type WriteJsonOptions } from './writeJson.js';
3
+ export { cloneJson } from './cloneJson.js';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -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
@@ -1,3 +1,4 @@
1
1
  export { readJson, readJsonSync } from './readJson.js';
2
2
  export { writeJson, writeJsonSync } from './writeJson.js';
3
+ export { cloneJson } from './cloneJson.js';
3
4
  //# sourceMappingURL=index.js.map
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.9",
3
+ "version": "0.1.10",
4
4
  "description": "Helpers for reading/writing json files.",
5
5
  "license": "MIT",
6
6
  "type": "module",