cmpstr 3.2.1 → 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 (155) hide show
  1. package/README.md +24 -18
  2. package/dist/CmpStr.esm.js +1904 -1211
  3. package/dist/CmpStr.esm.min.js +2 -3
  4. package/dist/CmpStr.umd.js +1924 -1236
  5. package/dist/CmpStr.umd.min.js +2 -3
  6. package/dist/cjs/CmpStr.cjs +134 -64
  7. package/dist/cjs/CmpStrAsync.cjs +60 -37
  8. package/dist/cjs/index.cjs +1 -2
  9. package/dist/cjs/metric/Cosine.cjs +1 -2
  10. package/dist/cjs/metric/DamerauLevenshtein.cjs +1 -2
  11. package/dist/cjs/metric/DiceSorensen.cjs +1 -2
  12. package/dist/cjs/metric/Hamming.cjs +5 -4
  13. package/dist/cjs/metric/Jaccard.cjs +1 -2
  14. package/dist/cjs/metric/JaroWinkler.cjs +1 -2
  15. package/dist/cjs/metric/LCS.cjs +1 -2
  16. package/dist/cjs/metric/Levenshtein.cjs +1 -2
  17. package/dist/cjs/metric/Metric.cjs +90 -53
  18. package/dist/cjs/metric/NeedlemanWunsch.cjs +1 -2
  19. package/dist/cjs/metric/QGram.cjs +1 -2
  20. package/dist/cjs/metric/SmithWaterman.cjs +1 -2
  21. package/dist/cjs/phonetic/Caverphone.cjs +1 -2
  22. package/dist/cjs/phonetic/Cologne.cjs +1 -2
  23. package/dist/cjs/phonetic/Metaphone.cjs +1 -2
  24. package/dist/cjs/phonetic/Phonetic.cjs +80 -48
  25. package/dist/cjs/phonetic/Soundex.cjs +1 -2
  26. package/dist/cjs/root.cjs +6 -3
  27. package/dist/cjs/utils/DeepMerge.cjs +109 -99
  28. package/dist/cjs/utils/DiffChecker.cjs +1 -2
  29. package/dist/cjs/utils/Errors.cjs +106 -0
  30. package/dist/cjs/utils/Filter.cjs +97 -37
  31. package/dist/cjs/utils/HashTable.cjs +44 -30
  32. package/dist/cjs/utils/Normalizer.cjs +84 -35
  33. package/dist/cjs/utils/OptionsValidator.cjs +211 -0
  34. package/dist/cjs/utils/Pool.cjs +57 -19
  35. package/dist/cjs/utils/Profiler.cjs +41 -28
  36. package/dist/cjs/utils/Registry.cjs +48 -24
  37. package/dist/cjs/utils/StructuredData.cjs +95 -57
  38. package/dist/cjs/utils/TextAnalyzer.cjs +1 -2
  39. package/dist/esm/CmpStr.mjs +133 -61
  40. package/dist/esm/CmpStrAsync.mjs +56 -33
  41. package/dist/esm/index.mjs +1 -2
  42. package/dist/esm/metric/Cosine.mjs +1 -2
  43. package/dist/esm/metric/DamerauLevenshtein.mjs +1 -2
  44. package/dist/esm/metric/DiceSorensen.mjs +1 -2
  45. package/dist/esm/metric/Hamming.mjs +5 -4
  46. package/dist/esm/metric/Jaccard.mjs +1 -2
  47. package/dist/esm/metric/JaroWinkler.mjs +1 -2
  48. package/dist/esm/metric/LCS.mjs +1 -2
  49. package/dist/esm/metric/Levenshtein.mjs +1 -2
  50. package/dist/esm/metric/Metric.mjs +92 -53
  51. package/dist/esm/metric/NeedlemanWunsch.mjs +1 -2
  52. package/dist/esm/metric/QGram.mjs +1 -2
  53. package/dist/esm/metric/SmithWaterman.mjs +1 -2
  54. package/dist/esm/phonetic/Caverphone.mjs +1 -2
  55. package/dist/esm/phonetic/Cologne.mjs +1 -2
  56. package/dist/esm/phonetic/Metaphone.mjs +1 -2
  57. package/dist/esm/phonetic/Phonetic.mjs +83 -48
  58. package/dist/esm/phonetic/Soundex.mjs +1 -2
  59. package/dist/esm/root.mjs +5 -4
  60. package/dist/esm/utils/DeepMerge.mjs +109 -95
  61. package/dist/esm/utils/DiffChecker.mjs +1 -2
  62. package/dist/esm/utils/Errors.mjs +106 -0
  63. package/dist/esm/utils/Filter.mjs +97 -37
  64. package/dist/esm/utils/HashTable.mjs +44 -30
  65. package/dist/esm/utils/Normalizer.mjs +84 -35
  66. package/dist/esm/utils/OptionsValidator.mjs +210 -0
  67. package/dist/esm/utils/Pool.mjs +53 -19
  68. package/dist/esm/utils/Profiler.mjs +41 -28
  69. package/dist/esm/utils/Registry.mjs +48 -24
  70. package/dist/esm/utils/StructuredData.mjs +95 -57
  71. package/dist/esm/utils/TextAnalyzer.mjs +1 -2
  72. package/dist/types/CmpStr.d.ts +25 -14
  73. package/dist/types/CmpStrAsync.d.ts +4 -0
  74. package/dist/types/index.d.ts +3 -2
  75. package/dist/types/metric/Metric.d.ts +15 -14
  76. package/dist/types/phonetic/Phonetic.d.ts +7 -4
  77. package/dist/types/root.d.ts +4 -2
  78. package/dist/types/utils/DeepMerge.d.ts +80 -58
  79. package/dist/types/utils/Errors.d.ts +154 -0
  80. package/dist/types/utils/Filter.d.ts +8 -1
  81. package/dist/types/utils/HashTable.d.ts +12 -11
  82. package/dist/types/utils/Normalizer.d.ts +5 -1
  83. package/dist/types/utils/OptionsValidator.d.ts +193 -0
  84. package/dist/types/utils/Pool.d.ts +2 -0
  85. package/dist/types/utils/Profiler.d.ts +9 -28
  86. package/dist/types/utils/Registry.d.ts +3 -3
  87. package/dist/types/utils/StructuredData.d.ts +6 -1
  88. package/dist/types/utils/Types.d.ts +39 -1
  89. package/package.json +20 -11
  90. package/dist/CmpStr.esm.js.map +0 -1
  91. package/dist/CmpStr.esm.min.js.map +0 -1
  92. package/dist/CmpStr.umd.js.map +0 -1
  93. package/dist/CmpStr.umd.min.js.map +0 -1
  94. package/dist/cjs/CmpStr.cjs.map +0 -1
  95. package/dist/cjs/CmpStrAsync.cjs.map +0 -1
  96. package/dist/cjs/index.cjs.map +0 -1
  97. package/dist/cjs/metric/Cosine.cjs.map +0 -1
  98. package/dist/cjs/metric/DamerauLevenshtein.cjs.map +0 -1
  99. package/dist/cjs/metric/DiceSorensen.cjs.map +0 -1
  100. package/dist/cjs/metric/Hamming.cjs.map +0 -1
  101. package/dist/cjs/metric/Jaccard.cjs.map +0 -1
  102. package/dist/cjs/metric/JaroWinkler.cjs.map +0 -1
  103. package/dist/cjs/metric/LCS.cjs.map +0 -1
  104. package/dist/cjs/metric/Levenshtein.cjs.map +0 -1
  105. package/dist/cjs/metric/Metric.cjs.map +0 -1
  106. package/dist/cjs/metric/NeedlemanWunsch.cjs.map +0 -1
  107. package/dist/cjs/metric/QGram.cjs.map +0 -1
  108. package/dist/cjs/metric/SmithWaterman.cjs.map +0 -1
  109. package/dist/cjs/phonetic/Caverphone.cjs.map +0 -1
  110. package/dist/cjs/phonetic/Cologne.cjs.map +0 -1
  111. package/dist/cjs/phonetic/Metaphone.cjs.map +0 -1
  112. package/dist/cjs/phonetic/Phonetic.cjs.map +0 -1
  113. package/dist/cjs/phonetic/Soundex.cjs.map +0 -1
  114. package/dist/cjs/root.cjs.map +0 -1
  115. package/dist/cjs/utils/DeepMerge.cjs.map +0 -1
  116. package/dist/cjs/utils/DiffChecker.cjs.map +0 -1
  117. package/dist/cjs/utils/Filter.cjs.map +0 -1
  118. package/dist/cjs/utils/HashTable.cjs.map +0 -1
  119. package/dist/cjs/utils/Normalizer.cjs.map +0 -1
  120. package/dist/cjs/utils/Pool.cjs.map +0 -1
  121. package/dist/cjs/utils/Profiler.cjs.map +0 -1
  122. package/dist/cjs/utils/Registry.cjs.map +0 -1
  123. package/dist/cjs/utils/StructuredData.cjs.map +0 -1
  124. package/dist/cjs/utils/TextAnalyzer.cjs.map +0 -1
  125. package/dist/esm/CmpStr.mjs.map +0 -1
  126. package/dist/esm/CmpStrAsync.mjs.map +0 -1
  127. package/dist/esm/index.mjs.map +0 -1
  128. package/dist/esm/metric/Cosine.mjs.map +0 -1
  129. package/dist/esm/metric/DamerauLevenshtein.mjs.map +0 -1
  130. package/dist/esm/metric/DiceSorensen.mjs.map +0 -1
  131. package/dist/esm/metric/Hamming.mjs.map +0 -1
  132. package/dist/esm/metric/Jaccard.mjs.map +0 -1
  133. package/dist/esm/metric/JaroWinkler.mjs.map +0 -1
  134. package/dist/esm/metric/LCS.mjs.map +0 -1
  135. package/dist/esm/metric/Levenshtein.mjs.map +0 -1
  136. package/dist/esm/metric/Metric.mjs.map +0 -1
  137. package/dist/esm/metric/NeedlemanWunsch.mjs.map +0 -1
  138. package/dist/esm/metric/QGram.mjs.map +0 -1
  139. package/dist/esm/metric/SmithWaterman.mjs.map +0 -1
  140. package/dist/esm/phonetic/Caverphone.mjs.map +0 -1
  141. package/dist/esm/phonetic/Cologne.mjs.map +0 -1
  142. package/dist/esm/phonetic/Metaphone.mjs.map +0 -1
  143. package/dist/esm/phonetic/Phonetic.mjs.map +0 -1
  144. package/dist/esm/phonetic/Soundex.mjs.map +0 -1
  145. package/dist/esm/root.mjs.map +0 -1
  146. package/dist/esm/utils/DeepMerge.mjs.map +0 -1
  147. package/dist/esm/utils/DiffChecker.mjs.map +0 -1
  148. package/dist/esm/utils/Filter.mjs.map +0 -1
  149. package/dist/esm/utils/HashTable.mjs.map +0 -1
  150. package/dist/esm/utils/Normalizer.mjs.map +0 -1
  151. package/dist/esm/utils/Pool.mjs.map +0 -1
  152. package/dist/esm/utils/Profiler.mjs.map +0 -1
  153. package/dist/esm/utils/Registry.mjs.map +0 -1
  154. package/dist/esm/utils/StructuredData.mjs.map +0 -1
  155. package/dist/esm/utils/TextAnalyzer.mjs.map +0 -1
@@ -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 {Error} - Throws an error if the key is not a valid identifier
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
+ }
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Error Utilities
3
+ * src/utils/Errors.ts
4
+ *
5
+ * This module provides a small hierarchy of error classes and helper methods
6
+ * that standardize error creation and formatting across the CmpStr project.
7
+ * It improves on vanilla JavaScript errors by adding codes, structured metadata,
8
+ * and consistent `toString()` / `toJSON()` output while keeping the original
9
+ * message text intact.
10
+ *
11
+ * The error classes are designed to be lightweight and fast, while still
12
+ * providing useful context for debugging (including optional `cause` chaining).
13
+ *
14
+ * @module Utils
15
+ * @name Errors
16
+ * @author Paul Köhler (komed3)
17
+ * @license MIT
18
+ */
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
+ }
33
+ /**
34
+ * Base error class for CmpStr.
35
+ *
36
+ * It provides a standard `code` field and a consistent `toString()` / `toJSON()`
37
+ * output without changing the original error message expectations.
38
+ */
39
+ export declare class CmpStrError extends Error {
40
+ /** A short, machine-readable error code */
41
+ readonly code: string;
42
+ /** Optional structured metadata for the error */
43
+ readonly meta?: CmpStrErrorMeta;
44
+ /** Timestamp when the error was created (ISO 8601) */
45
+ readonly when: string;
46
+ /**
47
+ * Constructor for CmpStrError.
48
+ *
49
+ * Will construct an error with a code, message, optional metadata, and optional cause.
50
+ *
51
+ * @param {string} code - A short, machine-readable error code
52
+ * @param {string} message - The error message (human-readable)
53
+ * @param {CmpStrErrorMeta} [meta] - Optional structured metadata for the error
54
+ * @param {unknown} [cause] - Optional cause (native JS Error chaining)
55
+ */
56
+ constructor(code: string, message: string, meta?: CmpStrErrorMeta, cause?: unknown);
57
+ /**
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
61
+ */
62
+ format(stack?: boolean): string;
63
+ /**
64
+ * Pretty string representation of the error.
65
+ */
66
+ toString(): string;
67
+ /**
68
+ * Serialize the error into a plain object for JSON output.
69
+ *
70
+ * @param {boolean} [stack=false] - Whether to include the stack trace in the JSON
71
+ */
72
+ toJSON(stack?: boolean): CmpStrErrorJSON;
73
+ }
74
+ /**
75
+ * Error thrown when user input (options, arguments) is invalid.
76
+ */
77
+ export declare class CmpStrValidationError extends CmpStrError {
78
+ constructor(message: string, meta?: CmpStrErrorMeta, cause?: unknown);
79
+ }
80
+ /**
81
+ * Error thrown when a requested resource is missing or not found.
82
+ */
83
+ export declare class CmpStrNotFoundError extends CmpStrError {
84
+ constructor(message: string, meta?: CmpStrErrorMeta, cause?: unknown);
85
+ }
86
+ /**
87
+ * Error thrown for incorrect usage or invalid state (assertions).
88
+ */
89
+ export declare class CmpStrUsageError extends CmpStrError {
90
+ constructor(message: string, meta?: CmpStrErrorMeta, cause?: unknown);
91
+ }
92
+ /**
93
+ * Error thrown for internal failures that should not happen under normal usage.
94
+ */
95
+ export declare class CmpStrInternalError extends CmpStrError {
96
+ constructor(message: string, meta?: CmpStrErrorMeta, cause?: unknown);
97
+ }
98
+ /**
99
+ * Helper utilities for throwing and formatting errors.
100
+ *
101
+ * Provides methods for asserting conditions, wrapping unknown errors, and formatting
102
+ * errors into readable strings. This centralizes error handling logic and ensures
103
+ * consistent error messages across the codebase.
104
+ */
105
+ export declare class ErrorUtil {
106
+ /**
107
+ * Throw a `CmpStrUsageError` if a condition is not met.
108
+ *
109
+ * @param {boolean} condition - The condition to assert
110
+ * @param {string} message - The error message to throw if the condition is false
111
+ * @param {CmpStrErrorMeta} [meta] - Optional structured metadata for the error
112
+ * @throws {CmpStrUsageError} - If the condition is false
113
+ */
114
+ static assert(condition: boolean, message: string, meta?: CmpStrErrorMeta): asserts condition;
115
+ /**
116
+ * Wrap an unknown error into a `CmpStrInternalError`.
117
+ *
118
+ * @param {unknown} err - The error to wrap
119
+ * @param {string} message - The error message to use for the wrapped error
120
+ * @param {CmpStrErrorMeta} [meta] - Optional structured metadata for the error
121
+ * @throws {CmpStrInternalError} - Always throws a new `CmpStrInternalError` wrapping the original error
122
+ */
123
+ static rethrow(err: unknown, message: string, meta?: CmpStrErrorMeta): never;
124
+ /**
125
+ * Format any error into a readable string.
126
+ *
127
+ * @param {unknown} err - The error to format
128
+ * @returns {string} - A formatted string representation of the error
129
+ */
130
+ static format(err: unknown): string;
131
+ /**
132
+ * Execute a synchronous operation and wrap any exception as a `CmpStrInternalError`.
133
+ *
134
+ * This is used to avoid repeating try/catch blocks and to add consistent context
135
+ * to unexpected failures while preserving the original error as `cause`.
136
+ *
137
+ * @param {() => T} fn - The function to execute
138
+ * @param {string} message - The error message to use if an exception is thrown
139
+ * @param {CmpStrErrorMeta} [meta] - Optional structured metadata for the error
140
+ * @return {T} The result of the function if it executes successfully
141
+ * @throws {CmpStrInternalError} - If the function throws an error, it will be wrapped and re-thrown as a `CmpStrInternalError`
142
+ */
143
+ static wrap<T>(fn: () => T, message: string, meta?: CmpStrErrorMeta): T;
144
+ /**
145
+ * Execute an asynchronous operation and wrap any exception as a `CmpStrInternalError`.
146
+ *
147
+ * @param {() => Promise< T >} fn - The asynchronous function to execute
148
+ * @param {string} message - The error message to use if an exception is thrown
149
+ * @param {CmpStrErrorMeta} [meta] - Optional structured metadata for the error
150
+ * @return {Promise< T >} A promise that resolves to the result of the function if it executes successfully
151
+ * @throws {CmpStrInternalError} - If the function throws an error, it will be wrapped and re-thrown as a `CmpStrInternalError`
152
+ */
153
+ static wrapAsync<T>(fn: () => Promise<T>, message: string, meta?: CmpStrErrorMeta): Promise<T>;
154
+ }
@@ -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,7 +34,9 @@ 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
39
+ * @throws {CmpStrInternalError} - Throws an error if the pipeline compilation fails
36
40
  */
37
41
  private static getPipeline;
38
42
  /**
@@ -52,10 +56,11 @@ export declare class Filter {
52
56
  * @param {FilterOptions} [opt] - Additional options for the filter
53
57
  * @returns {boolean} - Returns true if the filter was added,
54
58
  * false if it was not added due to override restrictions
59
+ * @throws {CmpStrInternalError} - Throws an error if there is an issue adding the filter
55
60
  */
56
61
  static add(hook: FilterHooks, id: string, fn: FilterFn, opt?: FilterOptions): boolean;
57
62
  /**
58
- * Removes a filter by its hook and id.
63
+ * Removes a filter from the specified hook by its id.
59
64
  *
60
65
  * @param {FilterHooks} hook - The name of the hook
61
66
  * @param {string} id - The id of the filter
@@ -92,6 +97,7 @@ export declare class Filter {
92
97
  * @param {FilterHooks} hook - The name of the hook
93
98
  * @param {string | string[]} input - The input string(s) to be filtered
94
99
  * @returns {string | string[]} - The filtered string(s)
100
+ * @throws {CmpStrInternalError} - Throws an error if there is an issue applying the filters
95
101
  */
96
102
  static apply(hook: FilterHooks, input: string | string[]): string | string[];
97
103
  /**
@@ -101,6 +107,7 @@ export declare class Filter {
101
107
  * @param {FilterHooks} hook - The name of the hook
102
108
  * @param {string | string[]} input - The input string(s) to be filtered
103
109
  * @returns {Promise< string | string[] >} - The filtered string(s)
110
+ * @throws {CmpStrInternalError} - Throws an error if there is an issue applying the filters
104
111
  */
105
112
  static applyAsync(hook: FilterHooks, input: string | string[]): Promise<string | string[]>;
106
113
  /**
@@ -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
  }
@@ -57,6 +57,7 @@ export declare class Normalizer {
57
57
  *
58
58
  * @param {NormalizeFlags} flags - A string of characters representing the normalization steps
59
59
  * @returns {NormalizerFn} - A function that normalizes a string based on the provided flags
60
+ * @throws {CmpStrInternalError} - Throws an error if the pipeline creation fails
60
61
  */
61
62
  private static getPipeline;
62
63
  /**
@@ -65,9 +66,11 @@ export declare class Normalizer {
65
66
  *
66
67
  * @param {string | string[]} input - The string or array of strings to normalize
67
68
  * @param {NormalizeFlags} flags - A string of characters representing the normalization steps
69
+ * @param {NormalizeFlags} [normalizedFlags] - Optional pre-canonicalized flags to avoid redundant canonicalization
68
70
  * @returns {string | string[]} - The normalized string(s)
71
+ * @throws {CmpStrInternalError} - Throws an error if the normalization process fails
69
72
  */
70
- static normalize(input: string | string[], flags: NormalizeFlags): string | string[];
73
+ static normalize(input: string | string[], flags: NormalizeFlags, normalizedFlags?: NormalizeFlags): string | string[];
71
74
  /**
72
75
  * Asynchronously normalizes the input string or array of strings based on the
73
76
  * provided flags. This method is useful for handling large inputs or when
@@ -76,6 +79,7 @@ export declare class Normalizer {
76
79
  * @param {string | string[]} input - The string or array of strings to normalize
77
80
  * @param {NormalizeFlags} flags - A string of characters representing the normalization steps
78
81
  * @returns {Promise< string | string[] >} - A promise that resolves to the normalized string(s)
82
+ * @throws {CmpStrInternalError} - Throws an error if the normalization process fails
79
83
  */
80
84
  static normalizeAsync(input: string | string[], flags: NormalizeFlags): Promise<string | string[]>;
81
85
  /**
@@ -0,0 +1,193 @@
1
+ /**
2
+ * CmpStr Options Validator
3
+ * src/utils/OptionsValidator.ts
4
+ *
5
+ * This module provides the OptionsValidator class, which contains static methods for validating
6
+ * the options passed to the CmpStr function. It checks for correct types, allowed values, and
7
+ * the existence of specified metrics and phonetic algorithms in their respective registries.
8
+ *
9
+ * If any validation fails, a CmpStrValidationError is thrown with a descriptive message and
10
+ * relevant details about the invalid option.
11
+ *
12
+ * @module Utils
13
+ * @name OptionsValidator
14
+ * @author Paul Köhler (komed3)
15
+ * @license MIT
16
+ */
17
+ import type { CmpStrOptions, CmpStrProcessors, MetricOptions, PhoneticOptions, StructuredDataOptions } from './Types';
18
+ /**
19
+ * Utility for validating CmpStr options.
20
+ *
21
+ * This class provides static methods to validate various aspects of the
22
+ * options object passed to CmpStr.
23
+ */
24
+ export declare class OptionsValidator {
25
+ /** Allowed normalization flags */
26
+ private static readonly ALLOWED_FLAGS;
27
+ /** Allowed output modes */
28
+ private static readonly ALLOWED_OUTPUT;
29
+ /** Allowed comparison modes */
30
+ private static readonly ALLOWED_MODES;
31
+ /** Allowed sort modes */
32
+ private static readonly ALLOWED_SORT;
33
+ /** Processor dispatch table */
34
+ private static readonly PROCESSORS;
35
+ /** Metric options validation dispatch table */
36
+ private static readonly METRIC_OPT_MAP;
37
+ /** Phonetic algorithm options validation dispatch table */
38
+ private static readonly PHONETIC_OPT_MAP;
39
+ /** CmpStr options validation dispatch table */
40
+ private static readonly CMPSTR_OPT_MAP;
41
+ /**
42
+ * Internal helper to convert a Set to a string for error messages.
43
+ *
44
+ * @param {Set< string >} set - The set to convert
45
+ * @returns {string} - A string representation of the set
46
+ */
47
+ private static set2string;
48
+ /**
49
+ * Internal helper to validate primitive types.
50
+ *
51
+ * @param {unknown} value - The value to validate.
52
+ * @param {string} name - The name of the option (for error messages).
53
+ * @param {'boolean' | 'number' | 'string'} type - The expected type of the value.
54
+ * @throws {CmpStrValidationError} If the value is not of the expected type or is NaN (for numbers).
55
+ */
56
+ private static validateType;
57
+ /**
58
+ * Internal helper to validate enum-like values.
59
+ *
60
+ * @param {unknown} value - The value to validate.
61
+ * @param {string} name - The name of the option (for error messages).
62
+ * @param {Set< string >} set - The set of allowed values.
63
+ * @throws {CmpStrValidationError} If the value is not a string or is not in the allowed set.
64
+ */
65
+ private static validateEnum;
66
+ /**
67
+ * Internal helper to validate objects against a dispatch table of validation functions.
68
+ *
69
+ * @param {unknown} opt - The object to validate.
70
+ * @param {Object} map - A dispatch table mapping keys to validation functions.
71
+ * @throws {CmpStrValidationError} If any property in the object fails validation.
72
+ */
73
+ private static validateMap;
74
+ /**
75
+ * Internal helper to validate registry-based options (metrics and phonetic algorithms).
76
+ *
77
+ * @param {unknown} value - The value to validate.
78
+ * @param {string} name - The name of the option (for error messages).
79
+ * @param {string} label - The label to use in error messages (e.g. "Metric" or "Phonetic algorithm").
80
+ * @param {( v: string ) => boolean} has - A function that checks if the registry contains a given name.
81
+ * @param {() => string[]} list - A function that returns a list of registered names for error messages.
82
+ * @throws {CmpStrValidationError} If the value is not a non-empty string or is not registered.
83
+ */
84
+ private static validateRegistryName;
85
+ /**
86
+ * Validate boolean-like values.
87
+ *
88
+ * @param {unknown} value - The value to validate
89
+ * @param {string} name - The name of the option (for error messages)
90
+ * @throws {CmpStrValidationError} - If the value is not a boolean
91
+ */
92
+ static validateBoolean(value: unknown, name: string): void;
93
+ /**
94
+ * Validate number-like values.
95
+ *
96
+ * @param {unknown} value - The value to validate
97
+ * @param {string} name - The name of the option (for error messages)
98
+ * @throws {CmpStrValidationError} - If the value is not a number or is NaN
99
+ */
100
+ static validateNumber(value: unknown, name: string): void;
101
+ /**
102
+ * Validate string-like values.
103
+ *
104
+ * @param {unknown} value - The value to validate
105
+ * @param {string} name - The name of the option (for error messages)
106
+ * @throws {CmpStrValidationError} - If the value is not a string
107
+ */
108
+ static validateString(value: unknown, name: string): void;
109
+ /**
110
+ * Validate normalization flags.
111
+ *
112
+ * @param {unknown} value - The flags to validate
113
+ * @throws {CmpStrValidationError} - If the flags are not a string or contain invalid characters
114
+ */
115
+ static validateFlags(value: unknown): void;
116
+ /**
117
+ * Validate CmpStr output mode.
118
+ *
119
+ * @param {unknown} value - The output mode to validate
120
+ * @throws {CmpStrValidationError} - If the output mode is not a string or not allowed
121
+ */
122
+ static validateOutput(value: unknown): void;
123
+ /**
124
+ * Validate CmpStr comparison mode.
125
+ *
126
+ * @param {unknown} value - The comparison mode to validate
127
+ * @throws {CmpStrValidationError} - If the comparison mode is not a string or not allowed
128
+ */
129
+ static validateMode(value: unknown): void;
130
+ /**
131
+ * Validate CmpStr structured data sort mode.
132
+ *
133
+ * @param {unknown} value - The sort mode to validate
134
+ * @param {string} name - The name of the option (for error messages)
135
+ * @throws {CmpStrValidationError} - If the sort mode is neither 'asc', 'desc', nor a boolean
136
+ */
137
+ static validateSort(value: unknown, name: string): void;
138
+ /**
139
+ * Validate metric name against the MetricRegistry.
140
+ *
141
+ * @param {unknown} value - The metric name to validate
142
+ * @throws {CmpStrValidationError} - If the metric is not a string or not registered
143
+ */
144
+ static validateMetricName(value: unknown): void;
145
+ /**
146
+ * Validate phonetic algorithm name against the PhoneticRegistry.
147
+ *
148
+ * @param {unknown} value - The phonetic algorithm name to validate
149
+ * @throws {CmpStrValidationError} - If the phonetic algorithm is not a string or not registered
150
+ */
151
+ static validatePhoneticName(value: unknown): void;
152
+ /**
153
+ * Validate metric options.
154
+ *
155
+ * @param {MetricOptions} opt - The metric options to validate
156
+ * @throws {CmpStrValidationError} - If any metric option is invalid
157
+ */
158
+ static validateMetricOptions(opt?: MetricOptions): void;
159
+ /**
160
+ * Validate phonetic options.
161
+ *
162
+ * @param {PhoneticOptions} opt - The phonetic options to validate
163
+ * @throws {CmpStrValidationError} - If any phonetic option is invalid
164
+ */
165
+ static validatePhoneticOptions(opt?: PhoneticOptions): void;
166
+ /**
167
+ * Validate processor options.
168
+ *
169
+ * This method iterates over the keys in the provided processor options and dispatches
170
+ * validation to the corresponding function in the PROCESSORS table.
171
+ *
172
+ * If an invalid processor type is found, a CmpStrValidationError is thrown indicating
173
+ * the invalid type and the expected processor types.
174
+ *
175
+ * @param {unknown} opt - The processor options to validate
176
+ * @throws {CmpStrValidationError} - If any processor type is invalid
177
+ */
178
+ static validateProcessors(opt?: CmpStrProcessors): void;
179
+ /**
180
+ * Validate the provided CmpStr options object.
181
+ *
182
+ * This method performs a comprehensive validation of the options object passed to CmpStr.
183
+ * It checks for the presence and validity of all supported options, including primitive
184
+ * types, enum-like values, registry-based names, and nested processor options.
185
+ *
186
+ * If any validation check fails, a CmpStrValidationError is thrown with a descriptive
187
+ * message and relevant details about the invalid option.
188
+ *
189
+ * @param {CmpStrOptions | StructuredDataOptions} [opt] - The options object to validate
190
+ * @throws {CmpStrValidationError} - If any validation check fails
191
+ */
192
+ static validateOptions(opt?: CmpStrOptions | StructuredDataOptions): void;
193
+ }
@@ -44,6 +44,7 @@ export declare class Pool {
44
44
  * @param {PoolType} type - The type of buffer to acquire (e.g., 'int32', 'number[]', 'map')
45
45
  * @param {number} size - The size of the buffer to acquire
46
46
  * @return {T} - The acquired buffer of the specified type
47
+ * @throws {CmpStrUsageError} - Throws an error if the pool type is unsupported
47
48
  */
48
49
  static acquire<T = any>(type: PoolType, size: number): T;
49
50
  /**
@@ -61,6 +62,7 @@ export declare class Pool {
61
62
  * @param {PoolType} type - The type of buffer to release
62
63
  * @param {T} buffer - The buffer to release
63
64
  * @param {number} size - The size of the buffer
65
+ * @throws {CmpStrUsageError} - Throws an error if the pool type is unsupported
64
66
  */
65
67
  static release<T = any>(type: PoolType, buffer: T, size: number): void;
66
68
  }