@did-btcr2/common 2.2.2 → 3.1.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.
Files changed (94) hide show
  1. package/dist/cjs/canonicalization.js +68 -56
  2. package/dist/cjs/canonicalization.js.map +1 -1
  3. package/dist/cjs/constants.js +17 -12
  4. package/dist/cjs/constants.js.map +1 -1
  5. package/dist/cjs/errors.js +20 -3
  6. package/dist/cjs/errors.js.map +1 -1
  7. package/dist/cjs/index.js +5 -3
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/cjs/json-patch.js +98 -0
  10. package/dist/cjs/json-patch.js.map +1 -0
  11. package/dist/cjs/logger.js +46 -12
  12. package/dist/cjs/logger.js.map +1 -1
  13. package/dist/cjs/types.js.map +1 -1
  14. package/dist/cjs/utils/date.js +123 -0
  15. package/dist/cjs/utils/date.js.map +1 -0
  16. package/dist/cjs/utils/json.js +280 -0
  17. package/dist/cjs/utils/json.js.map +1 -0
  18. package/dist/cjs/utils/set.js +23 -0
  19. package/dist/cjs/utils/set.js.map +1 -0
  20. package/dist/cjs/utils/string.js +55 -0
  21. package/dist/cjs/utils/string.js.map +1 -0
  22. package/dist/esm/canonicalization.js +68 -56
  23. package/dist/esm/canonicalization.js.map +1 -1
  24. package/dist/esm/constants.js +17 -12
  25. package/dist/esm/constants.js.map +1 -1
  26. package/dist/esm/errors.js +20 -3
  27. package/dist/esm/errors.js.map +1 -1
  28. package/dist/esm/index.js +5 -3
  29. package/dist/esm/index.js.map +1 -1
  30. package/dist/esm/json-patch.js +98 -0
  31. package/dist/esm/json-patch.js.map +1 -0
  32. package/dist/esm/logger.js +46 -12
  33. package/dist/esm/logger.js.map +1 -1
  34. package/dist/esm/types.js.map +1 -1
  35. package/dist/esm/utils/date.js +123 -0
  36. package/dist/esm/utils/date.js.map +1 -0
  37. package/dist/esm/utils/json.js +280 -0
  38. package/dist/esm/utils/json.js.map +1 -0
  39. package/dist/esm/utils/set.js +23 -0
  40. package/dist/esm/utils/set.js.map +1 -0
  41. package/dist/esm/utils/string.js +55 -0
  42. package/dist/esm/utils/string.js.map +1 -0
  43. package/dist/types/canonicalization.d.ts +40 -31
  44. package/dist/types/canonicalization.d.ts.map +1 -1
  45. package/dist/types/constants.d.ts +6 -9
  46. package/dist/types/constants.d.ts.map +1 -1
  47. package/dist/types/errors.d.ts +14 -3
  48. package/dist/types/errors.d.ts.map +1 -1
  49. package/dist/types/index.d.ts +5 -3
  50. package/dist/types/index.d.ts.map +1 -1
  51. package/dist/types/interfaces.d.ts +2 -273
  52. package/dist/types/interfaces.d.ts.map +1 -1
  53. package/dist/types/json-patch.d.ts +47 -0
  54. package/dist/types/json-patch.d.ts.map +1 -0
  55. package/dist/types/logger.d.ts +31 -8
  56. package/dist/types/logger.d.ts.map +1 -1
  57. package/dist/types/types.d.ts +12 -4
  58. package/dist/types/types.d.ts.map +1 -1
  59. package/dist/types/utils/date.d.ts +39 -0
  60. package/dist/types/utils/date.d.ts.map +1 -0
  61. package/dist/types/utils/json.d.ts +89 -0
  62. package/dist/types/utils/json.d.ts.map +1 -0
  63. package/dist/types/utils/set.d.ts +14 -0
  64. package/dist/types/utils/set.d.ts.map +1 -0
  65. package/dist/types/utils/string.d.ts +39 -0
  66. package/dist/types/utils/string.d.ts.map +1 -0
  67. package/package.json +3 -4
  68. package/src/canonicalization.ts +81 -64
  69. package/src/constants.ts +19 -13
  70. package/src/errors.ts +25 -3
  71. package/src/index.ts +5 -5
  72. package/src/interfaces.ts +2 -302
  73. package/src/json-patch.ts +103 -0
  74. package/src/logger.ts +59 -27
  75. package/src/types.ts +12 -6
  76. package/src/utils/date.ts +130 -0
  77. package/src/utils/json.ts +315 -0
  78. package/src/utils/set.ts +23 -0
  79. package/src/utils/string.ts +59 -0
  80. package/dist/cjs/exts.js +0 -189
  81. package/dist/cjs/exts.js.map +0 -1
  82. package/dist/cjs/patch.js +0 -163
  83. package/dist/cjs/patch.js.map +0 -1
  84. package/dist/esm/exts.js +0 -189
  85. package/dist/esm/exts.js.map +0 -1
  86. package/dist/esm/patch.js +0 -163
  87. package/dist/esm/patch.js.map +0 -1
  88. package/dist/types/exts.d.ts +0 -90
  89. package/dist/types/exts.d.ts.map +0 -1
  90. package/dist/types/patch.d.ts +0 -63
  91. package/dist/types/patch.d.ts.map +0 -1
  92. package/src/exts.ts +0 -310
  93. package/src/patch.ts +0 -181
  94. package/src/rdf-canonize.d.ts +0 -6
package/src/logger.ts CHANGED
@@ -51,18 +51,35 @@ const LEVEL_METHODS: Record<Level, keyof Console> = {
51
51
  * - File/line tracing
52
52
  * - Timestamps
53
53
  * - Colorized output
54
+ * @class Logger
55
+ * @type {Logger}
54
56
  */
55
57
  export class Logger {
56
58
  private levels: Level[];
57
59
  private namespace?: string;
60
+ private useColors: boolean;
61
+ private static shared: Logger;
58
62
 
59
- constructor(namespace?: string) {
60
- this.levels = LOG_LEVELS[NODE_ENV] || [];
61
- this.namespace = namespace ?? 'did-btcr2-js';
63
+ /**
64
+ * Creates a new Logger instance.
65
+ * @param {string} namespace - Optional namespace for log messages.
66
+ * @param {Object} options - Configuration options.
67
+ * @param {Level[]} options.levels - Log levels to enable.
68
+ * @param {boolean} options.useColors - Whether to use colored output.
69
+ */
70
+ constructor(namespace?: string, options: { levels?: Level[]; useColors?: boolean } = {}) {
71
+ this.levels = options.levels || LOG_LEVELS[NODE_ENV] || [];
72
+ this.namespace = namespace || 'did-btcr2-js';
73
+ const envForce = process.env.LOG_COLORS;
74
+ this.useColors = options.useColors || (envForce ? envForce !== '0' && envForce.toLowerCase() !== 'false' : Boolean(process.stdout.isTTY));
62
75
  }
63
76
 
64
77
  /**
65
78
  * Logs a message with the specified level.
79
+ * @param {Level} level - The log level.
80
+ * @param {unknown} message - The message to log.
81
+ * @param {...unknown[]} args - Additional arguments to log.
82
+ * @returns {void}
66
83
  */
67
84
  private _log(level: Level, message?: unknown, ...args: unknown[]): void {
68
85
  if (!this.levels.includes(level)) return;
@@ -72,70 +89,85 @@ export class Logger {
72
89
 
73
90
  const timestamp = new Date().toISOString();
74
91
  const namespace = this.namespace ? `[${this.namespace}]` : '';
92
+ const render = this.useColors ? color : (v: string) => v;
93
+ const renderGray = this.useColors ? chalk.gray : (v: string) => v;
75
94
 
76
95
  (console[method] as (...args: any[]) => void)(
77
- `${chalk.gray(timestamp)} ${namespace} ${color(level)}: ${chalk.white(message)}`,
96
+ `${renderGray(timestamp)} ${namespace} ${render(level)}: ${message}`,
78
97
  ...args
79
98
  );
80
99
  }
81
100
 
82
- // 🔹 Instance-based logging methods
83
- public debug(message?: unknown, ...args: unknown[]) {
101
+ debug(message?: unknown, ...args: unknown[]): Logger {
84
102
  this._log('debug', message, ...args); return this;
85
103
  }
86
104
 
87
- public error(message?: unknown, ...args: unknown[]) {
105
+ error(message?: unknown, ...args: unknown[]): Logger {
88
106
  this._log('error', message, ...args); return this;
89
107
  }
90
108
 
91
- public info(message?: unknown, ...args: unknown[]) {
109
+ info(message?: unknown, ...args: unknown[]): Logger {
92
110
  this._log('info', message, ...args); return this;
93
111
  }
94
112
 
95
- public warn(message?: unknown, ...args: unknown[]) {
113
+ warn(message?: unknown, ...args: unknown[]): Logger {
96
114
  this._log('warn', message, ...args); return this;
97
115
  }
98
116
 
99
- public security(message?: unknown, ...args: unknown[]) {
117
+ security(message?: unknown, ...args: unknown[]): Logger {
100
118
  this._log('security', message, ...args); return this;
101
119
  }
102
120
 
103
- public log(message?: unknown, ...args: unknown[]) {
121
+ log(message?: unknown, ...args: unknown[]): Logger {
104
122
  this._log('log', message, ...args); return this;
105
123
  }
106
124
 
107
- public newline() {
125
+ newline(): Logger {
108
126
  console.log(); return this;
109
127
  }
110
128
 
111
129
  /**
112
130
  * Static methods for convenience (auto-instantiate).
131
+ * These use a shared singleton instance.
132
+ * @param {unknown} message - The message to log.
133
+ * @param {...unknown[]} args - Additional arguments to log.
134
+ * @returns {void}
113
135
  */
114
- public static debug(message?: unknown, ...args: unknown[]) {
115
- new Logger().debug(message, ...args);
136
+ static debug(message?: unknown, ...args: unknown[]): void {
137
+ Logger.instance().debug(message, ...args);
116
138
  }
117
139
 
118
- public static error(message?: unknown, ...args: unknown[]) {
119
- new Logger().error(message, ...args);
140
+ static error(message?: unknown, ...args: unknown[]): void {
141
+ Logger.instance().error(message, ...args);
120
142
  }
121
143
 
122
- public static info(message?: unknown, ...args: unknown[]) {
123
- new Logger().info(message, ...args);
144
+ static info(message?: unknown, ...args: unknown[]): void {
145
+ Logger.instance().info(message, ...args);
124
146
  }
125
147
 
126
- public static warn(message?: unknown, ...args: unknown[]) {
127
- new Logger().warn(message, ...args);
148
+ static warn(message?: unknown, ...args: unknown[]): void {
149
+ Logger.instance().warn(message, ...args);
128
150
  }
129
151
 
130
- public static security(message?: unknown, ...args: unknown[]) {
131
- new Logger().security(message, ...args);
152
+ static security(message?: unknown, ...args: unknown[]): void {
153
+ Logger.instance().security(message, ...args);
132
154
  }
133
155
 
134
- public static log(message?: unknown, ...args: unknown[]) {
135
- new Logger().log(message, ...args);
156
+ static log(message?: unknown, ...args: unknown[]): void {
157
+ Logger.instance().log(message, ...args);
136
158
  }
137
159
 
138
- public static newline() {
139
- new Logger().newline();
160
+ static newline() {
161
+ Logger.instance().newline();
140
162
  }
141
- }
163
+
164
+ private static instance(levels?: Level[], useColors?: boolean): Logger {
165
+ if (!Logger.shared) {
166
+ Logger.shared = new Logger(undefined, { levels, useColors });
167
+ } else {
168
+ if (levels) Logger.shared.levels = levels;
169
+ if (useColors !== undefined) Logger.shared.useColors = useColors;
170
+ }
171
+ return Logger.shared;
172
+ }
173
+ }
package/src/types.ts CHANGED
@@ -3,6 +3,7 @@ import { HDKey } from '@scure/bip32';
3
3
  /* Crypto Types */
4
4
  export type Bytes = Uint8Array;
5
5
  export type Hex = Bytes | string;
6
+ export type HexString = string;
6
7
  export type SignatureHex = Hex;
7
8
  export type HashHex = Hex;
8
9
 
@@ -72,14 +73,18 @@ export type BeaconUri = string;
72
73
  export type DidPlaceholder = 'did:btcr2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
73
74
  export type CanonicalizedProofConfig = string;
74
75
  export type CryptosuiteName = 'bip340-jcs-2025' | 'bip340-rdfc-2025';
75
- export type ContextObject = Record<string | number | symbol, any>;
76
+ export type JsonPrimitive = string | number | boolean | null;
77
+ export type JsonArray = JsonValue[];
78
+ export type JsonValue = JsonPrimitive | JsonArray | JsonObject;
79
+ export type JsonObject = { [key: string]: JsonValue };
80
+ export type JSONObject = JsonObject; // JSON object: prototyped or unprototyped
81
+ export type Prototyped = JSONObject;
82
+ export type Unprototyped = JSONObject;
83
+ export type ContextObject = Record<string, JsonValue>;
76
84
  export type Context = string | string[] | ContextObject | ContextObject[]
77
85
 
78
86
  /* General Types */
79
- export type Maybe<T> = T | any;
80
- export type JSONObject = Record<string | number | symbol, any>; // JSON object: prototyped or unprototyped
81
- export type Prototyped = JSONObject;
82
- export type Unprototyped = JSONObject;
87
+ export type Maybe<T> = T | unknown;
83
88
  export type TwoDigits = `${number}${number}`;
84
89
  export type ThreeDigits = `${number}${number}${number}`;
85
90
  export type Year = `${1 | 2}${ThreeDigits}`;
@@ -93,4 +98,5 @@ export type TzOffset = `${Hours}:${Minutes}`;
93
98
  export type DateTimestamp = `${UtcTimestamp}Z` | `${UtcTimestamp}-${TzOffset}`;
94
99
  export type CanonicalizableObject = Record<string, any>;
95
100
  export type CanonicalizationAlgorithm = 'jcs' | 'rdfc';
96
- export type UnixTimestamp = number;
101
+ export type CanonicalizationEncoding = 'hex' | 'base58';
102
+ export type UnixTimestamp = number;
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Utility class for date-related operations.
3
+ * @name DateUtils
4
+ * @class DateUtils
5
+ */
6
+ export class DateUtils {
7
+ /**
8
+ * Render an ISO 8601 UTC timestamp without fractional seconds.
9
+ * @param {Date} [date=new Date()] - The date to format.
10
+ * @returns {string} The formatted date string.
11
+ */
12
+ static toISOStringNonFractional(date: Date = new Date()): string {
13
+ const time = date.getTime();
14
+ if (Number.isNaN(time)) {
15
+ throw new Error(`Invalid date: ${date}`);
16
+ }
17
+ return date.toISOString().replace(/\.\d{3}Z$/, 'Z');
18
+ }
19
+
20
+ /**
21
+ * Unix timestamp in seconds (integer).
22
+ * @param {Date} [date=new Date()] - The date to convert.
23
+ * @returns {number} The Unix timestamp in seconds.
24
+ */
25
+ static toUnixSeconds(date: Date = new Date()): number {
26
+ const time = date.getTime();
27
+ if (Number.isNaN(time)) {
28
+ throw new Error(`Invalid date: ${date}`);
29
+ }
30
+ return Math.floor(date.getTime() / 1000);
31
+ }
32
+
33
+ /**
34
+ * Validate if a string is a valid UTC date string.
35
+ * @param {string} dateString - The date string to validate.
36
+ * @returns {boolean} True if valid, otherwise false.
37
+ * @throws {Error} If the date string is invalid.
38
+ */
39
+ static dateStringToTimestamp(dateString: string): Date {
40
+ const date = new Date(dateString);
41
+ if (Number.isNaN(date.getTime())) {
42
+ return new Date(0);
43
+ }
44
+ return date;
45
+ }
46
+
47
+ /**
48
+ * Convert a blocktime (Unix timestamp in seconds) to a Date object.
49
+ * @param {number} blocktime - The blocktime in seconds.
50
+ * @returns {Date} The corresponding Date object.
51
+ */
52
+ static blocktimeToTimestamp(blocktime: number): Date {
53
+ return new Date(blocktime * 1000);
54
+ }
55
+
56
+ /**
57
+ * Validates an XMLSCHEMA11-2 dateTime string.
58
+ * Format: [-]YYYY-MM-DDThh:mm:ss[.fractional][Z|(+|-)hh:mm]
59
+ *
60
+ * @see https://www.w3.org/TR/xmlschema11-2/#dateTime
61
+ */
62
+ static isValidXsdDateTime(value?: string): boolean {
63
+ // Empty or undefined value is not a valid dateTime
64
+ if(!value) return false;
65
+
66
+ // Regex for XML Schema dateTime:
67
+ // - Optional leading minus for BCE years
68
+ // - Year: 4+ digits
69
+ // - Month: 01-12
70
+ // - Day: 01-31 (further validated below)
71
+ // - T separator
72
+ // - Hour: 00-23 (24:00:00 is valid end-of-day per spec)
73
+ // - Minute: 00-59
74
+ // - Second: 00-59 (with optional fractional part)
75
+ // - Timezone: Z or (+|-)hh:mm
76
+ const xsdDateTimeRegex =
77
+ /^-?(\d{4,})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d+)?(Z|[+-]\d{2}:\d{2})?$/;
78
+
79
+ const match = value.match(xsdDateTimeRegex);
80
+ if (!match) return false;
81
+
82
+ const [, y, m, d, H, M, S, , tz] = match;
83
+
84
+ const year = parseInt(y, 10);
85
+ const month = parseInt(m, 10);
86
+ const day = parseInt(d, 10);
87
+ const hour = parseInt(H, 10);
88
+ const minute = parseInt(M, 10);
89
+ const second = parseInt(S, 10);
90
+
91
+ // Year 0000 is not valid in XML Schema (no year zero)
92
+ if (year === 0) return false;
93
+
94
+ // Month: 1-12
95
+ if (month < 1 || month > 12) return false;
96
+
97
+ // Day: validate against month (and leap year for February)
98
+ const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
99
+ const isLeapYear = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
100
+ const maxDay = month === 2 && isLeapYear ? 29 : daysInMonth[month - 1];
101
+ if (day < 1 || day > maxDay) return false;
102
+
103
+ // Hour: 00-23, or 24:00:00 exactly (end-of-day)
104
+ if (hour === 24) {
105
+ if (minute !== 0 || second !== 0) return false;
106
+ } else if (hour > 23) {
107
+ return false;
108
+ }
109
+
110
+ // Minute: 00-59
111
+ if (minute > 59) return false;
112
+
113
+ // Second: 00-59 (leap second 60 is debatable; XML Schema doesn't explicitly allow it)
114
+ if (second > 59) return false;
115
+
116
+ // Validate timezone offset if present
117
+ if (tz && tz !== 'Z') {
118
+ const tzMatch = tz.match(/^[+-](\d{2}):(\d{2})$/);
119
+ if (tzMatch) {
120
+ const tzHour = parseInt(tzMatch[1], 10);
121
+ const tzMin = parseInt(tzMatch[2], 10);
122
+ if (tzHour > 14 || (tzHour === 14 && tzMin !== 0) || tzMin > 59) {
123
+ return false;
124
+ }
125
+ }
126
+ }
127
+
128
+ return true;
129
+ }
130
+ }
@@ -0,0 +1,315 @@
1
+ import { JSONObject, Prototyped, Unprototyped } from '../types.js';
2
+
3
+ /**
4
+ * Options for cloning JSON values.
5
+ */
6
+ type CloneOptions = {
7
+ stripPrototypes?: boolean;
8
+ transform?: (value: any) => any;
9
+ };
10
+
11
+ /**
12
+ * Utilities for working with JSON data.
13
+ * @name JSONUtils
14
+ * @class JSONUtils
15
+ */
16
+ export class JSONUtils {
17
+ /**
18
+ * Check if a value is a JSON object (not an array, not null, and has Object prototype).
19
+ * @param {unknown} value - The value to check.
20
+ * @returns {boolean} True if the value is a JSON object.
21
+ */
22
+ static isObject(value: unknown): value is JSONObject {
23
+ if (value === null || typeof value !== 'object') return false;
24
+ if (Array.isArray(value)) return false;
25
+ const proto = Object.getPrototypeOf(value);
26
+ return proto === Object.prototype || proto === null;
27
+ }
28
+
29
+ /**
30
+ * Check if a value is a parsable JSON string.
31
+ * @param {unknown} value - The value to check.
32
+ * @returns {boolean} True if the value is a parsable JSON string.
33
+ */
34
+ static isParsable(value: unknown): value is string {
35
+ if (typeof value !== 'string') return false;
36
+ try {
37
+ JSON.parse(value);
38
+ return true;
39
+ } catch {
40
+ return false;
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Check if a value is an unprototyped object (i.e., Object.create(null)).
46
+ * @param {unknown} value - The value to check.
47
+ * @returns {boolean} True if the value is an unprototyped object.
48
+ */
49
+ static isUnprototyped(value: unknown): value is Unprototyped {
50
+ if (value === null || typeof value !== 'object') return false;
51
+ return Object.getPrototypeOf(value) === null;
52
+ }
53
+
54
+ /**
55
+ * Normalize a JSON value by stripping prototypes from all objects within it.
56
+ * @param {T} value - The JSON value to normalize.
57
+ * @returns {Prototyped} The normalized JSON value.
58
+ */
59
+ static normalize<T extends JSONObject | Array<any>>(value: T): Prototyped {
60
+ return this.cloneInternal(value, { stripPrototypes: true });
61
+ }
62
+
63
+ /**
64
+ * Shallow copy of a JSON object.
65
+ * @param {T extends JSONObject} value - The JSON object to copy.
66
+ * @returns {T} The copied JSON object.
67
+ */
68
+ static copy<T extends JSONObject>(value: T): T {
69
+ return { ...value };
70
+ }
71
+
72
+ /**
73
+ * Deep clone a JSON value.
74
+ * @param {T} value - The JSON value to clone.
75
+ * @returns {T} The cloned JSON value.
76
+ */
77
+ static clone<T>(value: T): T {
78
+ if (typeof structuredClone === 'function') {
79
+ return structuredClone(value);
80
+ }
81
+ return this.cloneInternal(value);
82
+ }
83
+
84
+ /**
85
+ * Deep clone a JSON value, replacing strings that match a pattern.
86
+ * @param {T} value - The JSON value to clone.
87
+ * @param {RegExp} pattern - The regex pattern to match strings.
88
+ * @param {string} replacement - The replacement string.
89
+ * @returns {T} The cloned JSON value with replacements.
90
+ */
91
+ static cloneReplace<T>(value: T, pattern: RegExp, replacement: string): T {
92
+ return this.cloneInternal(value, {
93
+ transform : (candidate) => typeof candidate === 'string'
94
+ ? candidate.replace(pattern, replacement)
95
+ : candidate
96
+ });
97
+ }
98
+
99
+ /**
100
+ * Deep equality check between two values.
101
+ * @param {unknown} a - The first value to compare.
102
+ * @param {unknown} b - The second value to compare.
103
+ * @param {WeakMap<object, object>} seen - A WeakMap to track seen object pairs for circular reference detection.
104
+ * @returns {boolean} True if the values are deeply equal.
105
+ */
106
+ static deepEqual(
107
+ a: unknown,
108
+ b: unknown,
109
+ seen: WeakMap<object, object> = new WeakMap<object, object>(),
110
+ depth: number = 0
111
+ ): boolean {
112
+ if (depth > 1024) {
113
+ throw new Error('Maximum comparison depth exceeded');
114
+ }
115
+ if (Object.is(a, b)) return true;
116
+
117
+ if (typeof a !== typeof b) return false;
118
+ if (a === null || b === null) return false;
119
+ if (typeof a !== 'object') return false;
120
+
121
+ if (seen.get(a as object) === b) return true;
122
+ seen.set(a as object, b as object);
123
+
124
+ if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
125
+ const viewA = new Uint8Array((a as ArrayBufferView).buffer, (a as ArrayBufferView).byteOffset, (a as ArrayBufferView).byteLength);
126
+ const viewB = new Uint8Array((b as ArrayBufferView).buffer, (b as ArrayBufferView).byteOffset, (b as ArrayBufferView).byteLength);
127
+ if (viewA.byteLength !== viewB.byteLength) return false;
128
+ for (let i = 0; i < viewA.byteLength; i++) {
129
+ if (viewA[i] !== viewB[i]) return false;
130
+ }
131
+ return true;
132
+ }
133
+
134
+ if (a instanceof Date && b instanceof Date) {
135
+ return a.getTime() === b.getTime();
136
+ }
137
+
138
+ if (a instanceof Set && b instanceof Set) {
139
+ if (a.size !== b.size) return false;
140
+ for (const itemA of a) {
141
+ let matched = false;
142
+ for (const itemB of b) {
143
+ if (this.deepEqual(itemA, itemB, seen, depth + 1)) {
144
+ matched = true;
145
+ break;
146
+ }
147
+ }
148
+ if (!matched) return false;
149
+ }
150
+ return true;
151
+ }
152
+
153
+ if (a instanceof Map && b instanceof Map) {
154
+ if (a.size !== b.size) return false;
155
+ for (const [keyA, valueA] of a) {
156
+ let matched = false;
157
+ if (b.has(keyA)) {
158
+ matched = this.deepEqual(valueA, b.get(keyA), seen, depth + 1);
159
+ } else {
160
+ for (const [keyB, valueB] of b) {
161
+ if (
162
+ this.deepEqual(keyA, keyB, seen, depth + 1)
163
+ && this.deepEqual(valueA, valueB, seen, depth + 1)
164
+ ) {
165
+ matched = true;
166
+ break;
167
+ }
168
+ }
169
+ }
170
+ if (!matched) return false;
171
+ }
172
+ return true;
173
+ }
174
+
175
+ if (Array.isArray(a) && Array.isArray(b)) {
176
+ if (a.length !== b.length) return false;
177
+ for (let i = 0; i < a.length; i++) {
178
+ if (!this.deepEqual(a[i], b[i], seen, depth + 1)) return false;
179
+ }
180
+ return true;
181
+ }
182
+
183
+ const keysA = Object.keys(a as object);
184
+ const keysB = Object.keys(b as object);
185
+ if (keysA.length !== keysB.length) return false;
186
+
187
+ for (const key of keysA) {
188
+ if (!Object.prototype.hasOwnProperty.call(b, key)) return false;
189
+ if (!this.deepEqual((a as any)[key], (b as any)[key], seen, depth + 1)) return false;
190
+ }
191
+
192
+ return true;
193
+ }
194
+
195
+ /**
196
+ * Delete specified keys from a JSON value.
197
+ * @param {T} value - The JSON value to process.
198
+ * @param {Array<string | number | symbol>} keys - The keys to delete.
199
+ * @returns {T} The JSON value with specified keys deleted.
200
+ */
201
+ static deleteKeys<T>(value: T, keys: Array<string | number | symbol>): T {
202
+ const keySet = new Set(keys.map(key => typeof key === 'number' ? key.toString() : key));
203
+
204
+ const walk = (candidate: any): any => {
205
+ if (Array.isArray(candidate)) {
206
+ return candidate.map(item => walk(item));
207
+ }
208
+
209
+ if (candidate && typeof candidate === 'object') {
210
+ const result: any = Array.isArray(candidate) ? [] : {};
211
+ for (const key of Object.keys(candidate)) {
212
+ if (keySet.has(key)) continue;
213
+ result[key] = walk(candidate[key]);
214
+ }
215
+ return result;
216
+ }
217
+
218
+ return candidate;
219
+ };
220
+
221
+ return walk(value);
222
+ }
223
+
224
+ /**
225
+ * Sanitize a JSON value by removing undefined values from objects and arrays.
226
+ * @param {T} value - The JSON value to sanitize.
227
+ * @returns {T} The sanitized JSON value.
228
+ */
229
+ static sanitize<T>(value: T): T {
230
+ const walk = (candidate: any): any => {
231
+ if (Array.isArray(candidate)) {
232
+ return candidate.map(item => walk(item));
233
+ }
234
+
235
+ if (candidate && typeof candidate === 'object') {
236
+ const result: any = {};
237
+ for (const [key, val] of Object.entries(candidate)) {
238
+ const sanitized = walk(val);
239
+ if (sanitized !== undefined) {
240
+ result[key] = sanitized;
241
+ }
242
+ }
243
+ return result;
244
+ }
245
+
246
+ return candidate;
247
+ };
248
+
249
+ return walk(value);
250
+ }
251
+
252
+ /**
253
+ * Internal function to clone JSON values with options.
254
+ * @param {T} value - The value to clone.
255
+ * @param {CloneOptions} options - The cloning options.
256
+ * @param {WeakMap<object, any>} seen - A WeakMap to track seen objects for circular reference detection.
257
+ * @returns {any} The cloned value.
258
+ */
259
+ static cloneInternal<T>(
260
+ value: T,
261
+ options: CloneOptions = {},
262
+ seen: WeakMap<object, any> = new WeakMap<object, any>(),
263
+ depth: number = 0
264
+ ): any {
265
+ if (depth > 1024) {
266
+ throw new Error('Maximum clone depth exceeded');
267
+ }
268
+ const transformed = options.transform ? options.transform(value) : value;
269
+ if (transformed !== value) return transformed;
270
+
271
+ if (typeof value !== 'object' || value === null) {
272
+ return transformed;
273
+ }
274
+
275
+ if (seen.has(value as object)) {
276
+ throw new Error('Cannot clone circular structure');
277
+ }
278
+
279
+ // Handle arrays and typed arrays
280
+ if (Array.isArray(value)) {
281
+ const clone: any[] = [];
282
+ seen.set(value as object, clone);
283
+ for (const item of value) {
284
+ clone.push(this.cloneInternal(item, options, seen, depth + 1));
285
+ }
286
+ return clone;
287
+ }
288
+
289
+ // Handle ArrayBuffer views (typed arrays, DataView)
290
+ if (ArrayBuffer.isView(value)) {
291
+ if (value instanceof DataView) {
292
+ return new DataView(
293
+ value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength)
294
+ );
295
+ }
296
+
297
+ if (typeof (value as any).slice === 'function') {
298
+ return (value as any).slice();
299
+ }
300
+ }
301
+
302
+ if (value instanceof Date) {
303
+ return new Date(value.getTime());
304
+ }
305
+
306
+ const result: any = options.stripPrototypes ? {} : Object.create(Object.getPrototypeOf(value));
307
+ seen.set(value as object, result);
308
+
309
+ for (const key of Object.keys(value as object)) {
310
+ result[key] = this.cloneInternal((value as any)[key], options, seen, depth + 1);
311
+ }
312
+
313
+ return result;
314
+ }
315
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Utility class for set operations.
3
+ * @name SetUtils
4
+ * @class SetUtils
5
+ */
6
+ export class SetUtils {
7
+ /**
8
+ * Compute the set difference without mutating the inputs.
9
+ * @param {Set<T>} left - The left set.
10
+ * @param {Set<T>} right - The right set.
11
+ * @returns {Set<T>} A new set containing elements in `left` that are not in `right`.
12
+ */
13
+ static difference<T>(left: Set<T>, right: Set<T>): Set<T> {
14
+ const result = new Set<T>();
15
+ for (const value of left) {
16
+ if (!right.has(value)) {
17
+ result.add(value);
18
+ }
19
+ }
20
+ return result;
21
+ }
22
+
23
+ }