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
@@ -32,6 +32,7 @@ export declare class Profiler {
32
32
  private memFn;
33
33
  /** Store for profiler entries */
34
34
  private store;
35
+ private last?;
35
36
  /** Total time and memory consumption */
36
37
  private totalTime;
37
38
  private totalMem;
@@ -56,41 +57,21 @@ export declare class Profiler {
56
57
  */
57
58
  private constructor();
58
59
  /**
59
- * Gets the current time based on the environment.
60
+ * Stores a profiler entry in the store and updates the total time and memory consumption.
60
61
  *
61
- * Uses process.hrtime.bigint() for Node.js, performance.now() for browsers,
62
- * and Date.now() as a fallback.
63
- *
64
- * @returns {number} - Current time in milliseconds
65
- */
66
- private now;
67
- /**
68
- * Gets the current memory usage based on the environment.
69
- *
70
- * Uses process.memoryUsage().heapUsed for Node.js, performance.memory.usedJSHeapSize
71
- * for browsers, and returns 0 as a fallback.
72
- *
73
- * @returns {number} - Current memory usage in bytes
74
- */
75
- private mem;
76
- /**
77
- * Profiles a synchronous function by measuring its execution time and memory usage.
78
- *
79
- * @param {() => T} fn - Function to be executed and profiled
80
- * @param {Record< string, any >} meta - Metadata to be associated with the profiling entry
81
- * @returns {T} - The result of the executed function
62
+ * @param {ProfilerEntry< T >} entry - The profiler entry to be stored
82
63
  */
83
- private profile;
64
+ private storeRes;
84
65
  /**
85
66
  * Enables the profiler.
86
67
  * Sets the active state to true, allowing profiling to occur.
87
68
  */
88
- enable: () => void;
69
+ enable(): void;
89
70
  /**
90
71
  * Disables the profiler.
91
72
  * Sets the active state to false, preventing further profiling.
92
73
  */
93
- disable: () => void;
74
+ disable(): void;
94
75
  /**
95
76
  * Resets the profiler by clearing the store, total time and memory consumption.
96
77
  * This method is useful for starting a new profiling session.
@@ -119,19 +100,19 @@ export declare class Profiler {
119
100
  *
120
101
  * @returns {ProfilerEntry< any >[]} - An array of profiler entries
121
102
  */
122
- getAll: () => ProfilerEntry<any>[];
103
+ getAll(): ProfilerEntry<any>[];
123
104
  /**
124
105
  * Retrieves the last profiler entry stored in the profiler.
125
106
  *
126
107
  * @returns {ProfilerEntry< any > | undefined} - The last profiler entry or undefined if no entries exist
127
108
  */
128
- getLast: () => ProfilerEntry<any> | undefined;
109
+ getLast(): ProfilerEntry<any> | undefined;
129
110
  /**
130
111
  * Retrieves the total time and memory consumption recorded by the profiler.
131
112
  *
132
113
  * @returns {{ time: number, mem: number }} - An object containing total time and memory usage
133
114
  */
134
- getTotal: () => {
115
+ getTotal(): {
135
116
  time: number;
136
117
  mem: number;
137
118
  };
@@ -35,7 +35,7 @@ export declare const factory: Record<string, (cls: string, ...args: any[]) => In
35
35
  * @param {string} reg - The name of the registry
36
36
  * @param {RegistryConstructor< T >} ctor - The base constructor that all registered classes must extend
37
37
  * @returns {RegistryService< T >} - An object with methods to register, remove, check, get, and list classes
38
- * @throws {Error} If the registry already exists (overwriting is forbidden)
38
+ * @throws {Error} - If the registry already exists (overwriting is forbidden)
39
39
  */
40
40
  export declare function Registry<T>(reg: string, ctor: RegistryConstructor<T>): RegistryService<T>;
41
41
  /**
@@ -45,7 +45,7 @@ export declare function Registry<T>(reg: string, ctor: RegistryConstructor<T>):
45
45
  * @param {string} reg - The name of the registry
46
46
  * @param {T | string} cls - The class itself or name of the class to resolve
47
47
  * @returns {T | undefined} - The class constructor if found, otherwise undefined
48
- * @throws {ReferenceError} If the registry does not exist
48
+ * @throws {CmpStrNotFoundError} - If the registry or class does not exist
49
49
  */
50
50
  export declare function resolveCls<T extends RegistryConstructor<any>>(reg: string, cls: T | string): T;
51
51
  /**
@@ -56,6 +56,6 @@ export declare function resolveCls<T extends RegistryConstructor<any>>(reg: stri
56
56
  * @param {T | string} cls - The class itself or name of the class to instantiate
57
57
  * @param {...any} args - Arguments to pass to the class constructor
58
58
  * @returns {T} - An instance of the class
59
- * @throws {Error} If the class cannot be instantiated
59
+ * @throws {CmpStrInternalError} - If instantiation fails due to an internal error
60
60
  */
61
61
  export declare function createFromRegistry<T extends RegistryConstructor<any>>(reg: string, cls: T | string, ...args: any[]): InstanceType<T>;
@@ -29,6 +29,9 @@ import type { CmpFnResult, CmpStrOptions, MetricRaw, StructuredDataBatchResult,
29
29
  export declare class StructuredData<T = any, R = MetricRaw> {
30
30
  private readonly data;
31
31
  private readonly key;
32
+ /** Sorting functions for ascending and descending order based on the 'res' property. */
33
+ private static readonly SORT_ASC;
34
+ private static readonly SORT_DESC;
32
35
  /**
33
36
  * Creates a new StructuredData instance for processing structured data.
34
37
  *
@@ -80,7 +83,7 @@ export declare class StructuredData<T = any, R = MetricRaw> {
80
83
  *
81
84
  * @param {any} results - The raw metric results
82
85
  * @returns {IndexedResult< R >[]} - Normalized results with indices
83
- * @throws {TypeError} - If results format is unsupported
86
+ * @throws {CmpStrValidationError} - If the results format is unsupported
84
87
  */
85
88
  private normalizeResults;
86
89
  /**
@@ -121,6 +124,7 @@ export declare class StructuredData<T = any, R = MetricRaw> {
121
124
  * @param {string[]} extractedStrings - The extracted strings for index mapping
122
125
  * @param {StructuredDataOptions} [opt] - Additional options
123
126
  * @returns {StructuredDataBatchResult< T, R > | T[]} - The lookup results
127
+ * @throws {CmpStrUsageError} - If the lookup process fails
124
128
  */
125
129
  private performLookup;
126
130
  /**
@@ -130,6 +134,7 @@ export declare class StructuredData<T = any, R = MetricRaw> {
130
134
  * @param {string[]} extractedStrings - The extracted strings for index mapping
131
135
  * @param {StructuredDataOptions} [opt] - Additional options
132
136
  * @returns {Promise< StructuredDataBatchResult< T, R > | T[] >} - The async lookup results
137
+ * @throws {CmpStrUsageError} - If the async lookup process fails
133
138
  */
134
139
  private performLookupAsync;
135
140
  /**
@@ -14,6 +14,32 @@
14
14
  * @author Paul Köhler (komed3)
15
15
  * @license MIT
16
16
  */
17
+ /**
18
+ * ================================================================================
19
+ * ERROR HANDLING
20
+ * ================================================================================
21
+ *
22
+ * Types for standardized error classes and structured error metadata.
23
+ */
24
+ /**
25
+ * Metadata attached to a CmpStr error.
26
+ */
27
+ export type CmpStrErrorMeta = Record<string, any> | undefined;
28
+ /**
29
+ * Structured format for CmpStr errors when serialized to JSON.
30
+ */
31
+ export interface CmpStrErrorJSON {
32
+ name: string;
33
+ code: string;
34
+ message: string;
35
+ meta?: CmpStrErrorMeta;
36
+ when: string;
37
+ cause?: {
38
+ name: string;
39
+ message: string;
40
+ stack?: string;
41
+ } | unknown;
42
+ }
17
43
  /**
18
44
  * ================================================================================
19
45
  * PROFILER & POOL UTILITIES
@@ -53,7 +79,7 @@ export interface ProfilerService<T> {
53
79
  /**
54
80
  * PoolType enumerates the supported buffer types for the Pool utility.
55
81
  */
56
- export type PoolType = 'int32' | 'number[]' | 'string[]' | 'set' | 'map';
82
+ export type PoolType = 'int32' | 'arr[]' | 'number[]' | 'string[]' | 'set' | 'map';
57
83
  /**
58
84
  * PoolConfig defines the configuration for a buffer pool.
59
85
  */
@@ -72,6 +98,18 @@ export interface PoolBuffer<T> {
72
98
  buffer: T;
73
99
  size: number;
74
100
  }
101
+ /**
102
+ * ================================================================================
103
+ * VALIDATION
104
+ * ================================================================================
105
+ *
106
+ * Types for option validation.
107
+ */
108
+ /**
109
+ * ValidatorFn defines the signature for a validation function.
110
+ * It takes an unknown value and either returns void if valid or throws an error if invalid.
111
+ */
112
+ export type ValidatorFn = (v: unknown) => void;
75
113
  /**
76
114
  * ================================================================================
77
115
  * NORMALIZATION & FILTERING
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "cmpstr",
3
3
  "description": "CmpStr is a lightweight, fast and well performing package for calculating string similarity",
4
4
  "license": "MIT",
5
- "version": "3.2.1",
5
+ "version": "3.3.0",
6
6
  "author": {
7
7
  "name": "komed3 (Paul Köhler)",
8
8
  "email": "webmaster@komed3.de",
@@ -74,19 +74,28 @@
74
74
  "node": ">=18.0.0"
75
75
  },
76
76
  "types": "./dist/types/index.d.ts",
77
- "browser": "./dist/CmpStr.umd.min.js",
78
77
  "main": "./dist/cjs/index.cjs",
79
- "module": "./dist/esm/index.mjs",
80
78
  "exports": {
81
79
  ".": {
80
+ "browser": "./dist/CmpStr.umd.min.js",
82
81
  "types": "./dist/types/index.d.ts",
82
+ "import": "./dist/esm/index.mjs",
83
83
  "require": "./dist/cjs/index.cjs",
84
- "import": "./dist/esm/index.mjs"
84
+ "default": "./dist/esm/index.mjs"
85
85
  },
86
86
  "./root": {
87
87
  "types": "./dist/types/root.d.ts",
88
88
  "require": "./dist/cjs/root.cjs",
89
- "import": "./dist/esm/root.mjs"
89
+ "import": "./dist/esm/root.mjs",
90
+ "default": "./dist/esm/root.mjs"
91
+ },
92
+ "./bundle": {
93
+ "import": "./dist/CmpStr.esm.js",
94
+ "default": "./dist/CmpStr.esm.js"
95
+ },
96
+ "./bundle/min": {
97
+ "import": "./dist/CmpStr.esm.min.js",
98
+ "default": "./dist/CmpStr.esm.min.js"
90
99
  }
91
100
  },
92
101
  "scripts": {
@@ -97,19 +106,19 @@
97
106
  "docs": "typedoc --logLevel Verbose --options typedoc.json"
98
107
  },
99
108
  "devDependencies": {
100
- "@rollup/plugin-commonjs": "^29.0.0",
109
+ "@rollup/plugin-commonjs": "^29.0.2",
101
110
  "@rollup/plugin-node-resolve": "^16.0.3",
102
- "@rollup/plugin-terser": "^0.4.4",
111
+ "@rollup/plugin-terser": "^1.0.0",
103
112
  "@rollup/plugin-typescript": "^12.3.0",
104
- "@types/node": "^25.0.10",
113
+ "@types/node": "^25.4.0",
105
114
  "package-verify": "^1.0.3",
106
115
  "prettier": "^3.8.1",
107
- "rollup": "^4.57.0",
116
+ "rollup": "^4.59.0",
108
117
  "rollup-plugin-cleanup": "^3.2.1",
109
118
  "rollup-plugin-prettier": "^4.1.2",
110
119
  "tslib": "^2.8.1",
111
- "typedoc": "^0.28.16",
112
- "typedoc-github-theme": "^0.3.1",
120
+ "typedoc": "^0.28.17",
121
+ "typedoc-github-theme": "^0.4.0",
113
122
  "typescript": "^5.9.3",
114
123
  "vitest": "^4.0.18"
115
124
  }