cmpstr 3.2.2 → 3.3.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 (86) hide show
  1. package/dist/CmpStr.esm.js +2149 -1721
  2. package/dist/CmpStr.esm.min.js +2 -2
  3. package/dist/CmpStr.umd.js +2028 -1604
  4. package/dist/CmpStr.umd.min.js +2 -2
  5. package/dist/cjs/CmpStr.cjs +100 -51
  6. package/dist/cjs/CmpStrAsync.cjs +35 -18
  7. package/dist/cjs/index.cjs +1 -1
  8. package/dist/cjs/metric/Cosine.cjs +1 -1
  9. package/dist/cjs/metric/DamerauLevenshtein.cjs +1 -1
  10. package/dist/cjs/metric/DiceSorensen.cjs +1 -1
  11. package/dist/cjs/metric/Hamming.cjs +1 -1
  12. package/dist/cjs/metric/Jaccard.cjs +1 -1
  13. package/dist/cjs/metric/JaroWinkler.cjs +1 -1
  14. package/dist/cjs/metric/LCS.cjs +1 -1
  15. package/dist/cjs/metric/Levenshtein.cjs +1 -1
  16. package/dist/cjs/metric/Metric.cjs +40 -22
  17. package/dist/cjs/metric/NeedlemanWunsch.cjs +1 -1
  18. package/dist/cjs/metric/QGram.cjs +1 -1
  19. package/dist/cjs/metric/SmithWaterman.cjs +1 -1
  20. package/dist/cjs/phonetic/Caverphone.cjs +1 -1
  21. package/dist/cjs/phonetic/Cologne.cjs +1 -1
  22. package/dist/cjs/phonetic/Metaphone.cjs +1 -1
  23. package/dist/cjs/phonetic/Phonetic.cjs +27 -15
  24. package/dist/cjs/phonetic/Soundex.cjs +1 -1
  25. package/dist/cjs/root.cjs +4 -2
  26. package/dist/cjs/utils/DeepMerge.cjs +102 -97
  27. package/dist/cjs/utils/DiffChecker.cjs +1 -1
  28. package/dist/cjs/utils/Errors.cjs +22 -19
  29. package/dist/cjs/utils/Filter.cjs +59 -24
  30. package/dist/cjs/utils/HashTable.cjs +44 -29
  31. package/dist/cjs/utils/Normalizer.cjs +57 -28
  32. package/dist/cjs/utils/OptionsValidator.cjs +211 -0
  33. package/dist/cjs/utils/Pool.cjs +27 -13
  34. package/dist/cjs/utils/Profiler.cjs +41 -27
  35. package/dist/cjs/utils/Registry.cjs +5 -5
  36. package/dist/cjs/utils/StructuredData.cjs +83 -53
  37. package/dist/cjs/utils/TextAnalyzer.cjs +1 -1
  38. package/dist/esm/CmpStr.mjs +101 -52
  39. package/dist/esm/CmpStrAsync.mjs +35 -18
  40. package/dist/esm/index.mjs +1 -1
  41. package/dist/esm/metric/Cosine.mjs +1 -1
  42. package/dist/esm/metric/DamerauLevenshtein.mjs +1 -1
  43. package/dist/esm/metric/DiceSorensen.mjs +1 -1
  44. package/dist/esm/metric/Hamming.mjs +1 -1
  45. package/dist/esm/metric/Jaccard.mjs +1 -1
  46. package/dist/esm/metric/JaroWinkler.mjs +1 -1
  47. package/dist/esm/metric/LCS.mjs +1 -1
  48. package/dist/esm/metric/Levenshtein.mjs +1 -1
  49. package/dist/esm/metric/Metric.mjs +40 -22
  50. package/dist/esm/metric/NeedlemanWunsch.mjs +1 -1
  51. package/dist/esm/metric/QGram.mjs +1 -1
  52. package/dist/esm/metric/SmithWaterman.mjs +1 -1
  53. package/dist/esm/phonetic/Caverphone.mjs +1 -1
  54. package/dist/esm/phonetic/Cologne.mjs +1 -1
  55. package/dist/esm/phonetic/Metaphone.mjs +1 -1
  56. package/dist/esm/phonetic/Phonetic.mjs +30 -15
  57. package/dist/esm/phonetic/Soundex.mjs +1 -1
  58. package/dist/esm/root.mjs +3 -3
  59. package/dist/esm/utils/DeepMerge.mjs +103 -94
  60. package/dist/esm/utils/DiffChecker.mjs +1 -1
  61. package/dist/esm/utils/Errors.mjs +22 -19
  62. package/dist/esm/utils/Filter.mjs +59 -24
  63. package/dist/esm/utils/HashTable.mjs +44 -29
  64. package/dist/esm/utils/Normalizer.mjs +57 -28
  65. package/dist/esm/utils/OptionsValidator.mjs +210 -0
  66. package/dist/esm/utils/Pool.mjs +27 -13
  67. package/dist/esm/utils/Profiler.mjs +41 -27
  68. package/dist/esm/utils/Registry.mjs +5 -5
  69. package/dist/esm/utils/StructuredData.mjs +83 -53
  70. package/dist/esm/utils/TextAnalyzer.mjs +1 -1
  71. package/dist/types/CmpStr.d.ts +22 -15
  72. package/dist/types/CmpStrAsync.d.ts +3 -0
  73. package/dist/types/index.d.ts +3 -3
  74. package/dist/types/metric/Metric.d.ts +9 -9
  75. package/dist/types/phonetic/Phonetic.d.ts +4 -3
  76. package/dist/types/root.d.ts +3 -2
  77. package/dist/types/utils/DeepMerge.d.ts +80 -58
  78. package/dist/types/utils/Errors.d.ts +25 -8
  79. package/dist/types/utils/Filter.d.ts +4 -1
  80. package/dist/types/utils/HashTable.d.ts +12 -11
  81. package/dist/types/utils/Normalizer.d.ts +2 -1
  82. package/dist/types/utils/OptionsValidator.d.ts +193 -0
  83. package/dist/types/utils/Profiler.d.ts +9 -28
  84. package/dist/types/utils/StructuredData.d.ts +3 -0
  85. package/dist/types/utils/Types.d.ts +13 -1
  86. package/package.json +14 -5
@@ -98,8 +98,8 @@ export declare class CmpStr<R = MetricRaw> {
98
98
  static readonly clearCache: {
99
99
  normalizer: typeof Normalizer.clear;
100
100
  filter: typeof Filter.clearPipeline;
101
- metric: () => void;
102
- phonetic: () => void;
101
+ metric: typeof Metric.clear;
102
+ phonetic: typeof Phonetic.clear;
103
103
  };
104
104
  /**
105
105
  * Returns a TextAnalyzer instance for the given input string.
@@ -153,7 +153,7 @@ export declare class CmpStr<R = MetricRaw> {
153
153
  *
154
154
  * @param {string} cond - The condition to met
155
155
  * @param {any} [test] - Value to test for
156
- * @throws {CmpStrNotFoundError} - If the specified metric or phonetic algorithm is not found
156
+ * @throws {CmpStrValidationError} - If the specified metric or phonetic algorithm is not found
157
157
  * @throws {CmpStrInternalError} - If an unknown condition is specified
158
158
  */
159
159
  protected assert(cond: string, test?: any): void;
@@ -165,10 +165,11 @@ export declare class CmpStr<R = MetricRaw> {
165
165
  protected assertMany(...cond: [string, any?][]): void;
166
166
  /**
167
167
  * Resolves the options for the CmpStr instance, merging the provided options with
168
- * the existing options.
168
+ * the existing options. Validates them and throws if the options are invalid.
169
169
  *
170
170
  * @param {CmpStrOptions} [opt] - Optional options to merge
171
171
  * @returns {CmpStrOptions} - The resolved options
172
+ * @throws {CmpStrValidationError} - If the merged options are invalid
172
173
  */
173
174
  protected resolveOptions(opt?: CmpStrOptions): CmpStrOptions;
174
175
  /**
@@ -234,6 +235,7 @@ export declare class CmpStr<R = MetricRaw> {
234
235
  * @param {boolean} [raw=false] - Whether to return raw results
235
236
  * @param {boolean} [skip=false] - Whether to skip normalization and filtering
236
237
  * @returns {T} - The computed metric result
238
+ * @throws {CmpStrValidationError} - If the options are invalid
237
239
  * @throws {CmpStrInternalError} - If the computation fails due to internal errors
238
240
  */
239
241
  protected compute<T extends MetricResult<R> | CmpStrResult | CmpStrResult[]>(a: MetricInput, b: MetricInput, opt?: CmpStrOptions, mode?: MetricMode, raw?: boolean, skip?: boolean): T;
@@ -260,7 +262,7 @@ export declare class CmpStr<R = MetricRaw> {
260
262
  *
261
263
  * @returns {CmpStr< R >} - The cloned instance
262
264
  */
263
- clone: () => CmpStr<R>;
265
+ clone(): CmpStr<R>;
264
266
  /**
265
267
  * Resets the instance, clearing all data and options.
266
268
  *
@@ -272,6 +274,7 @@ export declare class CmpStr<R = MetricRaw> {
272
274
  *
273
275
  * @param {CmpStrOptions} opt - The options
274
276
  * @returns {this}
277
+ * @throws {CmpStrValidationError} - If the provided options are invalid
275
278
  */
276
279
  setOptions(opt: CmpStrOptions): this;
277
280
  /**
@@ -279,6 +282,7 @@ export declare class CmpStr<R = MetricRaw> {
279
282
  *
280
283
  * @param {CmpStrOptions} opt - The options to merge
281
284
  * @returns {this}
285
+ * @throws {CmpStrValidationError} - If the merged options are invalid
282
286
  */
283
287
  mergeOptions(opt: CmpStrOptions): this;
284
288
  /**
@@ -286,7 +290,7 @@ export declare class CmpStr<R = MetricRaw> {
286
290
  *
287
291
  * @param {string} opt - The serialized options
288
292
  * @returns {this}
289
- * @throws {CmpStrValidationError} - If the provided string is not valid JSON
293
+ * @throws {CmpStrValidationError} - If the provided string is not valid JSON or the options are invalid
290
294
  */
291
295
  setSerializedOptions(opt: string): this;
292
296
  /**
@@ -295,6 +299,7 @@ export declare class CmpStr<R = MetricRaw> {
295
299
  * @param {string} path - The path to the option
296
300
  * @param {any} value - The value to set
297
301
  * @returns {this}
302
+ * @throws {CmpStrValidationError} - If the updated options are invalid
298
303
  */
299
304
  setOption(path: string, value: any): this;
300
305
  /**
@@ -310,59 +315,59 @@ export declare class CmpStr<R = MetricRaw> {
310
315
  * @param {boolean} enable - Whether to enable or disable raw output
311
316
  * @returns {this}
312
317
  */
313
- setRaw: (enable: boolean) => this;
318
+ setRaw(enable: boolean): this;
314
319
  /**
315
320
  * Sets the similatity metric to use (e.g., 'levenshtein', 'dice').
316
321
  *
317
322
  * @param {string} name - The metric name
318
323
  * @returns {this}
319
324
  */
320
- setMetric: (name: string) => this;
325
+ setMetric(name: string): this;
321
326
  /**
322
327
  * Sets the normalization flags (e.g., 'itw', 'nfc').
323
328
  *
324
329
  * @param {NormalizeFlags} flags - The normalization flags
325
330
  * @returns {this}
326
331
  */
327
- setFlags: (flags: NormalizeFlags) => this;
332
+ setFlags(flags: NormalizeFlags): this;
328
333
  /**
329
334
  * Removes the normalization flags entirely.
330
335
  *
331
336
  * @return {this}
332
337
  */
333
- rmvFlags: () => this;
338
+ rmvFlags(): this;
334
339
  /**
335
340
  * Sets the pre-processors to use for preparing the input.
336
341
  *
337
342
  * @param {CmpStrProcessors} opt - The processors to set
338
343
  * @returns {this}
339
344
  */
340
- setProcessors: (opt: CmpStrProcessors) => this;
345
+ setProcessors(opt: CmpStrProcessors): this;
341
346
  /**
342
347
  * Removes the processors entirely.
343
348
  *
344
349
  * @returns {this}
345
350
  */
346
- rmvProcessors: () => this;
351
+ rmvProcessors(): this;
347
352
  /**
348
353
  * Returns the current options object.
349
354
  *
350
355
  * @returns {CmpStrOptions} - The options
351
356
  */
352
- getOptions: () => CmpStrOptions;
357
+ getOptions(): CmpStrOptions;
353
358
  /**
354
359
  * Returns the options as a JSON string.
355
360
  *
356
361
  * @returns {string} - The serialized options
357
362
  */
358
- getSerializedOptions: () => string;
363
+ getSerializedOptions(): string;
359
364
  /**
360
365
  * Returns a specific option value by path.
361
366
  *
362
367
  * @param {string} path - The path to the option
363
368
  * @returns {any} - The option value
364
369
  */
365
- getOption: (path: string) => any;
370
+ getOption(path: string): any;
366
371
  /**
367
372
  * ================================================================================-
368
373
  * Public core methods for string comparison
@@ -472,6 +477,8 @@ export declare class CmpStr<R = MetricRaw> {
472
477
  /**
473
478
  * Computes a similarity matrix for the given input array.
474
479
  *
480
+ * Only works for symmetric metrics.
481
+ *
475
482
  * @param {string[]} input - The input array
476
483
  * @param {CmpStrOptions} [opt] - Optional options
477
484
  * @returns {number[][]} - The similarity matrix
@@ -106,6 +106,7 @@ export declare class CmpStrAsync<R = MetricRaw> extends CmpStr<R> {
106
106
  * @param {boolean} [raw=false] - Whether to return raw results
107
107
  * @param {boolean} [skip=false] - Whether to skip normalization and filtering
108
108
  * @returns {Promise< T >} - The computed metric result
109
+ * @throws {CmpStrValidationError} - If the options are invalid
109
110
  * @throws {CmpStrInternalError} - If the computation fails due to internal errors
110
111
  */
111
112
  protected computeAsync<T extends MetricResult<R> | CmpStrResult | CmpStrResult[]>(a: MetricInput, b: MetricInput, opt?: CmpStrOptions, mode?: MetricMode, raw?: boolean, skip?: boolean): Promise<T>;
@@ -218,6 +219,8 @@ export declare class CmpStrAsync<R = MetricRaw> extends CmpStr<R> {
218
219
  /**
219
220
  * Asynchronously computes a similarity matrix for the given input array.
220
221
  *
222
+ * Only works for symmetric metrics.
223
+ *
221
224
  * @param {string[]} input - The input array
222
225
  * @param {CmpStrOptions} [opt] - Optional options
223
226
  * @returns {Promise< number[][] >} - The similarity matrix
@@ -7,7 +7,7 @@
7
7
  * filtering, and text analysis. It is designed for both high-level application development
8
8
  * and research, offering a unified API for single, batch, and pairwise operations.
9
9
  *
10
- * Version: 3.2.2
10
+ * Version: 3.3.0
11
11
  * Author: Paul Köhler (komed3)
12
12
  * License: MIT
13
13
  *
@@ -41,12 +41,12 @@
41
41
  * For asynchronous workloads, use `CmpStrAsync`, which provides the same API with
42
42
  * Promise-based, non-blocking methods for large-scale or I/O-bound operations.
43
43
  *
44
- * @version 3.2.2
44
+ * @version 3.3.0
45
45
  * @author Paul Köhler (komed3)
46
46
  * @license MIT
47
47
  */
48
48
  export * from './utils/Types';
49
- export type { CmpStrError, CmpStrValidationError, CmpStrNotFoundError, CmpStrUsageError, CmpStrInternalError } from './utils/Errors';
49
+ export type { CmpStrError, CmpStrValidationError, CmpStrNotFoundError, CmpStrUsageError, CmpStrInternalError, ErrorCode } from './utils/Errors';
50
50
  export { CmpStr } from './CmpStr';
51
51
  export { CmpStrAsync } from './CmpStrAsync';
52
52
  export { DiffChecker } from './utils/DiffChecker';
@@ -56,7 +56,7 @@ export declare abstract class Metric<R = MetricRaw> {
56
56
  /**
57
57
  * Static method to clear the cache of metric computations.
58
58
  */
59
- static clear: () => void;
59
+ static clear(): void;
60
60
  /**
61
61
  * Swaps two strings and their lengths if the first is longer than the second.
62
62
  *
@@ -66,14 +66,14 @@ export declare abstract class Metric<R = MetricRaw> {
66
66
  * @param {number} n - Length of the second string
67
67
  * @returns {[ string, string, number, number ]} - Swapped strings and lengths
68
68
  */
69
- protected static swap: (a: string, b: string, m: number, n: number) => [string, string, number, number];
69
+ protected static swap(a: string, b: string, m: number, n: number): [string, string, number, number];
70
70
  /**
71
71
  * Clamps the similarity result between 0 and 1.
72
72
  *
73
73
  * @param {number} res - The input similarity to clamp
74
74
  * @returns {number} - The clamped similarity (0 to 1)
75
75
  */
76
- protected static clamp: (res: number) => number;
76
+ protected static clamp(res: number): number;
77
77
  /**
78
78
  * Constructor for the Metric class.
79
79
  * Initializes the metric with two inputs (strings or arrays of strings) and options.
@@ -167,7 +167,7 @@ export declare abstract class Metric<R = MetricRaw> {
167
167
  *
168
168
  * @returns {boolean} - True if either input is an array with more than one element
169
169
  */
170
- isBatch: () => boolean;
170
+ isBatch(): boolean;
171
171
  /**
172
172
  * Check if the inputs are in single mode.
173
173
  *
@@ -176,7 +176,7 @@ export declare abstract class Metric<R = MetricRaw> {
176
176
  *
177
177
  * @returns {boolean} - True if both inputs are single strings
178
178
  */
179
- isSingle: () => boolean;
179
+ isSingle(): boolean;
180
180
  /**
181
181
  * Check if the inputs are in pairwise mode.
182
182
  *
@@ -196,7 +196,7 @@ export declare abstract class Metric<R = MetricRaw> {
196
196
  *
197
197
  * @returns {boolean} - True if the metric is symmetric
198
198
  */
199
- isSymmetrical: () => boolean;
199
+ isSymmetrical(): boolean;
200
200
  /**
201
201
  * Determine which mode to run the metric in.
202
202
  *
@@ -206,7 +206,7 @@ export declare abstract class Metric<R = MetricRaw> {
206
206
  * @param {MetricMode} [mode] - The mode to run the metric in (optional)
207
207
  * @returns {MetricMode} - The determined mode
208
208
  */
209
- whichMode: (mode?: MetricMode) => MetricMode;
209
+ whichMode(mode?: MetricMode): MetricMode;
210
210
  /**
211
211
  * Clear the cached results of the metric.
212
212
  *
@@ -214,7 +214,7 @@ export declare abstract class Metric<R = MetricRaw> {
214
214
  * any previously computed results. It can be useful for re-running the metric
215
215
  * with new inputs or options.
216
216
  */
217
- clear: () => void;
217
+ clear(): void;
218
218
  /**
219
219
  * Run the metric computation based on the specified mode.
220
220
  *
@@ -237,7 +237,7 @@ export declare abstract class Metric<R = MetricRaw> {
237
237
  *
238
238
  * @returns {string} - The name of the metric
239
239
  */
240
- getMetricName: () => string;
240
+ getMetricName(): string;
241
241
  /**
242
242
  * Get the result of the metric computation.
243
243
  *
@@ -49,10 +49,11 @@ export declare abstract class Phonetic {
49
49
  protected readonly options: PhoneticOptions;
50
50
  protected readonly optKey: string;
51
51
  protected readonly map: PhoneticMap;
52
+ protected readonly ignoreSet: Set<string>;
52
53
  /**
53
54
  * Static method to clear the cache of indexed words.
54
55
  */
55
- static clear: () => void;
56
+ static clear(): void;
56
57
  /**
57
58
  * Constructor for the Phonetic class.
58
59
  *
@@ -124,7 +125,7 @@ export declare abstract class Phonetic {
124
125
  * @param {string} word - The input word to be converted
125
126
  * @returns {string[]} - An array of characters from the input word
126
127
  */
127
- protected word2Chars: (word: string) => string[];
128
+ protected word2Chars(word: string): string[];
128
129
  /**
129
130
  * Determines whether to exit early based on the current phonetic code length.
130
131
  *
@@ -168,7 +169,7 @@ export declare abstract class Phonetic {
168
169
  *
169
170
  * @returns {string} - The name of the algorithm
170
171
  */
171
- getAlgoName: () => string;
172
+ getAlgoName(): string;
172
173
  /**
173
174
  * Generates a phonetic index for the given input string.
174
175
  *
@@ -9,7 +9,7 @@
9
9
  * Please visit CmpStr's documentation for more information:
10
10
  * https://github.com/komed3/cmpstr/wiki/Extending-CmpStr
11
11
  *
12
- * @version 3.2.2
12
+ * @version 3.3.0
13
13
  * @author Paul Köhler (komed3)
14
14
  * @license MIT
15
15
  */
@@ -31,10 +31,11 @@ export { Metric, MetricCls, MetricRegistry } from './metric';
31
31
  * - PhoneticRegistry: Phonetic registry service for managing phonetic algorithm implementations.
32
32
  */
33
33
  export { Phonetic, PhoneticCls, PhoneticMappingRegistry, PhoneticRegistry } from './phonetic';
34
- export * as DeepMerge from './utils/DeepMerge';
34
+ export { DeepMerge } from './utils/DeepMerge';
35
35
  export * as CmpStrError from './utils/Errors';
36
36
  export { Filter } from './utils/Filter';
37
37
  export { Hasher, HashTable } from './utils/HashTable';
38
+ export { OptionsValidator } from './utils/OptionsValidator';
38
39
  export { Pool } from './utils/Pool';
39
40
  export { Profiler } from './utils/Profiler';
40
41
  export { StructuredData } from './utils/StructuredData';
@@ -2,71 +2,93 @@
2
2
  * Deep Merge Utility
3
3
  * src/utils/DeepMerge.ts
4
4
  *
5
- * This module provides utility functions for deep merging objects, getting values by path,
6
- * and setting values by path in a deeply nested object structure.
5
+ * This module provides the utility class `DeepMerge` for deep merging objects and
6
+ * manipulating nested properties using path strings. It allows you to get, set,
7
+ * check, and delete values at deeply nested paths within an object, as well as
8
+ * merge two objects together while preserving their structure.
7
9
  *
8
10
  * It supports dot and bracket notation (e.g. `a.b[0].c`) as well as escaped keys.
9
11
  *
10
- * Included functions:
11
- * - `get`: Retrieve a deeply nested value by path
12
- * - `set`: Assign a value to a nested path
13
- * - `merge`: Deeply merge two objects
14
- * - `has`: Check whether a path exists
15
- * - `rmv`: Delete a value at a path
16
- *
17
12
  * @module Utils
18
13
  * @name DeepMerge
19
14
  * @author Paul Köhler
20
15
  * @license MIT
21
16
  */
22
17
  /**
23
- * Deeply get a value from an object by a path string.
24
- *
25
- * @template T - The type of the object to get the value from
26
- * @template R - The return type of the value
27
- * @param {T} t - The object to get the value from
28
- * @param {string} path - The path string, e.g. `a.b.c`
29
- * @param {any} fb - The default value to return if the path does not exist
30
- * @returns {R | undefined} - The value at the specified path, otherwise the default value
31
- */
32
- export declare function get<T extends Record<string, any>, R = any>(t: T, path: string, fb?: R): R | undefined;
33
- /**
34
- * Check if a path exists in an object.
35
- *
36
- * @template T - The type of the object to get the value from
37
- * @param {T} t - The object to check
38
- * @param {string} path - The path string, e.g. `a.b.c`
39
- * @returns {boolean} - True if the path exists, otherwise false
40
- */
41
- export declare function has<T extends Record<string, any>>(t: T, path: string): boolean;
42
- /**
43
- * Deeply set a value in an object by a path string.
44
- *
45
- * @template T - The type of the object to get the value from
46
- * @param {T} t - The object to set the value in
47
- * @param {string} path - The path string, e.g. `a.b.c`
48
- * @param {any} value - The value to set at the specified path
49
- * @returns {T} - The modified object with the value set at the specified path
50
- * @throws {CmpStrUsageError} - If the path is invalid or if a non-object value is encountered along the path
51
- */
52
- export declare function set<T extends Record<string, any>>(t: T, path: string, value: any): T;
53
- /**
54
- * Deeply merge two objects, where the second object overrides the first.
55
- *
56
- * @template T - The type of the object to get the value from
57
- * @param {T} t - The target object to merge into
58
- * @param {T} o - The source object to merge from
59
- * @param {boolean} [mergeUndefined=false] - Whether to merge undefined values
60
- * @returns {T} - The merged object
61
- */
62
- export declare function merge<T extends Record<string, any>>(t?: T | undefined, o?: T | undefined, mergeUndefined?: boolean): T;
63
- /**
64
- * Delete a value at a specified path in an object.
65
- *
66
- * @template T - The type of the object to get the value from
67
- * @param {T} t - The object to delete the value from
68
- * @param {string} path - The path string, e.g. `a.b.c`
69
- * @param {boolean} [preserveEmpty=false] - Whether to preserve empty objects/arrays
70
- * @returns {T} - The modified object with the value deleted at the specified path
18
+ * DeepMerge class provides static methods for deep merging objects and
19
+ * manipulating nested properties using path strings.
71
20
  */
72
- export declare function rmv<T extends Record<string, any>>(t: T, path: string, preserveEmpty?: boolean): T;
21
+ export declare class DeepMerge {
22
+ /** Regular expression to match bracket notation in paths */
23
+ private static readonly BRACKET_PATTERN;
24
+ /** Path cache for efficient parsing */
25
+ private static readonly PATH_CACHE;
26
+ /**
27
+ * Walk through an object using an array of keys and return
28
+ * whether the path exists and its value.
29
+ *
30
+ * @param {any} obj - The object to walk through
31
+ * @param {( string | number )[]} keys - An array of keys representing the path to walk
32
+ * @returns {{ exists: false } | { exists: true, value: any }} -
33
+ * An object indicating whether the path exists and its value if it does
34
+ */
35
+ private static walk;
36
+ /**
37
+ * Parse a path string into an array of keys.
38
+ *
39
+ * @param {string} p - The path string, e.g. `a.b.c` or `a[0].b`
40
+ * @returns {( string | number )[]} - An array of keys, e.g. `['a', 'b', 'c']` or `['a', 0, 'b']`
41
+ */
42
+ static parse(p: string): (string | number)[];
43
+ /**
44
+ * Check if a path exists in an object.
45
+ *
46
+ * @template T - The type of the object to get the value from
47
+ * @param {T} t - The object to check
48
+ * @param {string} path - The path string, e.g. `a.b.c`
49
+ * @returns {boolean} - True if the path exists, otherwise false
50
+ */
51
+ static has<T extends Record<string, any>>(t: T, path: string): boolean;
52
+ /**
53
+ * Deeply get a value from an object by a path string.
54
+ *
55
+ * @template T - The type of the object to get the value from
56
+ * @template R - The return type of the value
57
+ * @param {T} t - The object to get the value from
58
+ * @param {string} path - The path string, e.g. `a.b.c`
59
+ * @param {any} fb - The default value to return if the path does not exist
60
+ * @returns {R | undefined} - The value at the specified path, otherwise the default value
61
+ */
62
+ static get<T extends Record<string, any>, R = any>(t: T, path: string, fb?: R): R | undefined;
63
+ /**
64
+ * Deeply set a value in an object by a path string.
65
+ *
66
+ * @template T - The type of the object to get the value from
67
+ * @param {T} t - The object to set the value in
68
+ * @param {string} path - The path string, e.g. `a.b.c`
69
+ * @param {any} value - The value to set at the specified path
70
+ * @returns {T} - The modified object with the value set at the specified path
71
+ * @throws {CmpStrUsageError} - If the path is invalid or if a non-object value is encountered along the path
72
+ */
73
+ static set<T extends Record<string, any>>(t: T, path: string, value: any): T;
74
+ /**
75
+ * Delete a value at a specified path in an object.
76
+ *
77
+ * @template T - The type of the object to get the value from
78
+ * @param {T} t - The object to delete the value from
79
+ * @param {string} path - The path string, e.g. `a.b.c`
80
+ * @param {boolean} [preserveEmpty=false] - Whether to preserve empty objects/arrays
81
+ * @returns {T} - The modified object with the value deleted at the specified path
82
+ */
83
+ static rmv<T extends Record<string, any>>(t: T, path: string, preserveEmpty?: boolean): T;
84
+ /**
85
+ * Deeply merge two objects, where the second object overrides the first.
86
+ *
87
+ * @template T - The type of the object to get the value from
88
+ * @param {T} t - The target object to merge into
89
+ * @param {T} o - The source object to merge from
90
+ * @param {boolean} [mergeUndefined=false] - Whether to merge undefined values
91
+ * @returns {T} - The merged object
92
+ */
93
+ static merge<T extends Record<string, any>>(t?: T | undefined, o?: T | undefined, mergeUndefined?: boolean): T;
94
+ }
@@ -17,6 +17,19 @@
17
17
  * @license MIT
18
18
  */
19
19
  import type { CmpStrErrorJSON, CmpStrErrorMeta } from './Types';
20
+ /**
21
+ * Standardized error codes for CmpStr errors.
22
+ *
23
+ * These codes are used in the `code` field of `CmpStrError` instances to allow
24
+ * for easy programmatic handling of different error types without relying on
25
+ * string matching of messages or class names.
26
+ */
27
+ export declare const enum ErrorCode {
28
+ VALIDATION = "E_VALIDATION",
29
+ NOT_FOUND = "E_NOT_FOUND",
30
+ USAGE = "E_USAGE",
31
+ INTERNAL = "E_INTERNAL"
32
+ }
20
33
  /**
21
34
  * Base error class for CmpStr.
22
35
  *
@@ -28,8 +41,6 @@ export declare class CmpStrError extends Error {
28
41
  readonly code: string;
29
42
  /** Optional structured metadata for the error */
30
43
  readonly meta?: CmpStrErrorMeta;
31
- /** Optional cause (native JS Error chaining) */
32
- readonly cause?: unknown;
33
44
  /** Timestamp when the error was created (ISO 8601) */
34
45
  readonly when: string;
35
46
  /**
@@ -44,15 +55,21 @@ export declare class CmpStrError extends Error {
44
55
  */
45
56
  constructor(code: string, message: string, meta?: CmpStrErrorMeta, cause?: unknown);
46
57
  /**
47
- * Serialize the error into a plain object for JSON output.
58
+ * Format the error into a readable string, including code, message, and optional metadata.
59
+ *
60
+ * @param {boolean} [stack=false] - Whether to include the stack trace in the output
48
61
  */
49
- toJSON(): CmpStrErrorJSON;
62
+ format(stack?: boolean): string;
50
63
  /**
51
64
  * Pretty string representation of the error.
65
+ */
66
+ toString(): string;
67
+ /**
68
+ * Serialize the error into a plain object for JSON output.
52
69
  *
53
- * @param {boolean} [stack=false] - Whether to include the stack trace in the output
70
+ * @param {boolean} [stack=false] - Whether to include the stack trace in the JSON
54
71
  */
55
- toString(stack?: boolean): string;
72
+ toJSON(stack?: boolean): CmpStrErrorJSON;
56
73
  }
57
74
  /**
58
75
  * Error thrown when user input (options, arguments) is invalid.
@@ -103,7 +120,7 @@ export declare class ErrorUtil {
103
120
  * @param {CmpStrErrorMeta} [meta] - Optional structured metadata for the error
104
121
  * @throws {CmpStrInternalError} - Always throws a new `CmpStrInternalError` wrapping the original error
105
122
  */
106
- static create(err: unknown, message: string, meta?: CmpStrErrorMeta): never;
123
+ static rethrow(err: unknown, message: string, meta?: CmpStrErrorMeta): never;
107
124
  /**
108
125
  * Format any error into a readable string.
109
126
  *
@@ -130,7 +147,7 @@ export declare class ErrorUtil {
130
147
  * @param {() => Promise< T >} fn - The asynchronous function to execute
131
148
  * @param {string} message - The error message to use if an exception is thrown
132
149
  * @param {CmpStrErrorMeta} [meta] - Optional structured metadata for the error
133
- * @return {Promise<T>} A promise that resolves to the result of the function if it executes successfully
150
+ * @return {Promise< T >} A promise that resolves to the result of the function if it executes successfully
134
151
  * @throws {CmpStrInternalError} - If the function throws an error, it will be wrapped and re-thrown as a `CmpStrInternalError`
135
152
  */
136
153
  static wrapAsync<T>(fn: () => Promise<T>, message: string, meta?: CmpStrErrorMeta): Promise<T>;
@@ -17,6 +17,8 @@ import type { FilterFn, FilterHooks, FilterOptions } from './Types';
17
17
  * The Filter class provides a way to manage and apply filters to strings based on hooks.
18
18
  */
19
19
  export declare class Filter {
20
+ /** Filter function that returns the input string unchanged */
21
+ private static readonly IDENTITY;
20
22
  /**
21
23
  * A static map to hold all filters.
22
24
  * The key is the hook name, and the value is an Map of FilterEntry objects.
@@ -32,6 +34,7 @@ export declare class Filter {
32
34
  * If the function is not cached, it compiles it from the active filters.
33
35
  *
34
36
  * @param {FilterHooks} hook - The name of the hook
37
+ * @param {boolean} [force=false] - If true, forces recompilation of the pipeline even if it is cached
35
38
  * @returns {FilterFn} - The compiled filter function for the hook
36
39
  * @throws {CmpStrInternalError} - Throws an error if the pipeline compilation fails
37
40
  */
@@ -57,7 +60,7 @@ export declare class Filter {
57
60
  */
58
61
  static add(hook: FilterHooks, id: string, fn: FilterFn, opt?: FilterOptions): boolean;
59
62
  /**
60
- * Removes a filter by its hook and id.
63
+ * Removes a filter from the specified hook by its id.
61
64
  *
62
65
  * @param {FilterHooks} hook - The name of the hook
63
66
  * @param {string} id - The id of the filter
@@ -48,11 +48,10 @@ export declare class Hasher {
48
48
  * @template T - The type of value to be stored in the hash table (e.g. MetricCompute, string, …)
49
49
  */
50
50
  export declare class HashTable<K extends string, T> {
51
- private readonly LRU;
51
+ private readonly FIFO;
52
+ private readonly maxSize;
52
53
  /** The max. length of a string to hash, which is set to 2048 characters */
53
54
  private static readonly MAX_LEN;
54
- /** The max. size of the hash table, which is set to 10,000 */
55
- private static readonly TABLE_SIZE;
56
55
  /**
57
56
  * The internal map to store entries.
58
57
  * The key is a string generated from the label and any number of hashed strings.
@@ -62,9 +61,10 @@ export declare class HashTable<K extends string, T> {
62
61
  /**
63
62
  * Creates an instance of HashTable.
64
63
  *
65
- * @param {boolean} [LRU=true] - Whether to use Least Recently Used (LRU) eviction policy
64
+ * @param {boolean} [FIFO=true] - Whether to use FIFO eviction (true) when the table is full
65
+ * @param {number} [maxSize=10000] - The maximum number of entries in the hash table
66
66
  */
67
- constructor(LRU?: boolean);
67
+ constructor(FIFO?: boolean, maxSize?: number);
68
68
  /**
69
69
  * Generates a unique hash key for any number of string arguments.
70
70
  * Return false if any string exceeds the maximum length.
@@ -82,17 +82,18 @@ export declare class HashTable<K extends string, T> {
82
82
  * @param {string} key - The key to check
83
83
  * @returns {boolean} - True if the key exists, false otherwise
84
84
  */
85
- has: (key: string) => boolean;
85
+ has(key: string): boolean;
86
86
  /**
87
87
  * Retrieves the entry from the hash table by its key.
88
88
  *
89
89
  * @param {string} key - The key to look up
90
90
  * @returns {T | undefined} - The entry if found, undefined otherwise
91
91
  */
92
- get: (key: string) => T | undefined;
92
+ get(key: string): T | undefined;
93
93
  /**
94
94
  * Adds an entry to the hash table.
95
- * If the table is full, it evicts the least recently used entry (if LRU is enabled).
95
+ * If the table is full, it evicts the least recently used entry
96
+ * (if FIFO is enabled) or returns false.
96
97
  *
97
98
  * @param {string} key - The hashed key for the entry
98
99
  * @param {T} entry - The entry itself to add
@@ -106,16 +107,16 @@ export declare class HashTable<K extends string, T> {
106
107
  * @param {string} key - The key of the entry to delete
107
108
  * @returns {boolean} - True if the entry was deleted, false if the key was not found
108
109
  */
109
- delete: (key: string) => boolean;
110
+ delete(key: string): boolean;
110
111
  /**
111
112
  * Clears the hash table.
112
113
  * This method removes all entries from the hash table.
113
114
  */
114
- clear: () => void;
115
+ clear(): void;
115
116
  /**
116
117
  * Returns the current size of the hash table.
117
118
  *
118
119
  * @returns {number} - The number of entries in the hash table
119
120
  */
120
- size: () => number;
121
+ size(): number;
121
122
  }
@@ -66,10 +66,11 @@ export declare class Normalizer {
66
66
  *
67
67
  * @param {string | string[]} input - The string or array of strings to normalize
68
68
  * @param {NormalizeFlags} flags - A string of characters representing the normalization steps
69
+ * @param {NormalizeFlags} [normalizedFlags] - Optional pre-canonicalized flags to avoid redundant canonicalization
69
70
  * @returns {string | string[]} - The normalized string(s)
70
71
  * @throws {CmpStrInternalError} - Throws an error if the normalization process fails
71
72
  */
72
- static normalize(input: string | string[], flags: NormalizeFlags): string | string[];
73
+ static normalize(input: string | string[], flags: NormalizeFlags, normalizedFlags?: NormalizeFlags): string | string[];
73
74
  /**
74
75
  * Asynchronously normalizes the input string or array of strings based on the
75
76
  * provided flags. This method is useful for handling large inputs or when