@maiyunnet/kebab 9.29.5 → 9.29.6

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/abox/text.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { IJsonObject } from '#kebab/lib/text.js';
1
2
  export declare function redact(value: string, secrets: readonly string[], replacement: string, encoded: boolean): string;
2
3
  export declare function parseJson<T>(str: string): T | false;
3
4
  export declare function stringifyJson(obj: unknown, space?: string | number): string;
5
+ export declare function normalizeJsonObject(value: unknown): IJsonObject | false;
package/abox/text.js CHANGED
@@ -55,3 +55,54 @@ export function stringifyJson(obj, space) {
55
55
  return typeof value === 'bigint' ? nativeJson.rawJSON(value.toString()) : value;
56
56
  }, space);
57
57
  }
58
+ function copyJson(value, ancestors) {
59
+ if (value === null || typeof value === 'string' || typeof value === 'boolean' ||
60
+ (typeof value === 'number' && Number.isFinite(value)))
61
+ return value;
62
+ if (typeof value !== 'object' || ancestors.has(value))
63
+ throw new TypeError();
64
+ const array = Array.isArray(value);
65
+ if (!array) {
66
+ const proto = Object.getPrototypeOf(value);
67
+ if (proto !== null && proto !== Object.prototype)
68
+ throw new TypeError();
69
+ }
70
+ ancestors.add(value);
71
+ const keys = Reflect.ownKeys(value);
72
+ let result;
73
+ if (array) {
74
+ if (keys.length !== value.length + 1)
75
+ throw new TypeError();
76
+ const items = [];
77
+ for (let i = 0; i < value.length; ++i)
78
+ items.push(copyProperty(value, i, ancestors));
79
+ result = items;
80
+ }
81
+ else {
82
+ const object = Object.create(null);
83
+ for (const key of keys.sort()) {
84
+ if (typeof key !== 'string')
85
+ throw new TypeError();
86
+ object[key] = copyProperty(value, key, ancestors);
87
+ }
88
+ result = object;
89
+ }
90
+ ancestors.delete(value);
91
+ return result;
92
+ }
93
+ function copyProperty(object, key, ancestors) {
94
+ const prop = Object.getOwnPropertyDescriptor(object, key);
95
+ if (!prop?.enumerable || !Object.hasOwn(prop, 'value'))
96
+ throw new TypeError();
97
+ return copyJson(prop.value, ancestors);
98
+ }
99
+ export function normalizeJsonObject(value) {
100
+ try {
101
+ if (value === null || typeof value !== 'object' || Array.isArray(value))
102
+ return false;
103
+ return copyJson(value, new Set());
104
+ }
105
+ catch {
106
+ return false;
107
+ }
108
+ }