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
@@ -1,8 +1,14 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
2
- import { merge, set, rmv, get } from './utils/DeepMerge.mjs';
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
+ import { DeepMerge } from './utils/DeepMerge.mjs';
3
3
  import { DiffChecker } from './utils/DiffChecker.mjs';
4
+ import {
5
+ CmpStrInternalError,
6
+ ErrorUtil,
7
+ CmpStrValidationError
8
+ } from './utils/Errors.mjs';
4
9
  import { Filter } from './utils/Filter.mjs';
5
10
  import { Normalizer } from './utils/Normalizer.mjs';
11
+ import { OptionsValidator } from './utils/OptionsValidator.mjs';
6
12
  import { Profiler } from './utils/Profiler.mjs';
7
13
  import { factory } from './utils/Registry.mjs';
8
14
  import { StructuredData } from './utils/StructuredData.mjs';
@@ -79,29 +85,26 @@ class CmpStr {
79
85
  }
80
86
  assert(cond, test) {
81
87
  switch (cond) {
88
+ default:
89
+ throw new CmpStrInternalError(`Cmpstr condition <${cond}> unknown`);
82
90
  case 'metric':
83
- if (!CmpStr.metric.has(test))
84
- throw new Error(
85
- `CmpStr <metric> must be set, call .setMetric(), ` +
86
- `use CmpStr.metric.list() for available metrics`
87
- );
91
+ OptionsValidator.validateMetricName(test);
88
92
  break;
89
93
  case 'phonetic':
90
- if (!CmpStr.phonetic.has(test))
91
- throw new Error(
92
- `CmpStr <phonetic> must be set, call .setPhonetic(), ` +
93
- `use CmpStr.phonetic.list() for available phonetic algorithms`
94
- );
94
+ OptionsValidator.validatePhoneticName(test);
95
95
  break;
96
- default:
97
- throw new Error(`Cmpstr condition <${cond}> unknown`);
98
96
  }
99
97
  }
100
98
  assertMany(...cond) {
101
99
  for (const [c, test] of cond) this.assert(c, test);
102
100
  }
103
101
  resolveOptions(opt) {
104
- return merge({ ...(this.options ?? Object.create(null)) }, opt);
102
+ const merged = DeepMerge.merge(
103
+ { ...(this.options ?? Object.create(null)) },
104
+ opt
105
+ );
106
+ OptionsValidator.validateOptions(merged);
107
+ return merged;
105
108
  }
106
109
  normalize(input, flags) {
107
110
  return Normalizer.normalize(input, flags ?? this.options.flags ?? '');
@@ -117,7 +120,7 @@ class CmpStr {
117
120
  return input;
118
121
  }
119
122
  postProcess(result, opt) {
120
- if (opt?.removeZero && Array.isArray(result))
123
+ if (Array.isArray(result) && opt?.removeZero)
121
124
  result = result.filter((r) => r.res > 0);
122
125
  return result;
123
126
  }
@@ -135,64 +138,114 @@ class CmpStr {
135
138
  compute(a, b, opt, mode, raw, skip) {
136
139
  const resolved = this.resolveOptions(opt);
137
140
  this.assert('metric', resolved.metric);
138
- const A = skip ? a : this.prepare(a, resolved);
139
- const B = skip ? b : this.prepare(b, resolved);
140
- if (
141
- resolved.safeEmpty &&
142
- ((Array.isArray(A) && A.length === 0) ||
143
- (Array.isArray(B) && B.length === 0) ||
144
- A === '' ||
145
- B === '')
146
- ) {
147
- return [];
148
- }
149
- const metric = factory['metric'](resolved.metric, A, B, resolved.opt);
150
- if (resolved.output !== 'prep') metric.setOriginal(a, b);
151
- metric.run(mode);
152
- const result = this.postProcess(metric.getResults(), resolved);
153
- return this.output(result, raw ?? resolved.raw);
141
+ return ErrorUtil.wrap(
142
+ () => {
143
+ const A = skip ? a : this.prepare(a, resolved);
144
+ const B = skip ? b : this.prepare(b, resolved);
145
+ if (
146
+ resolved.safeEmpty &&
147
+ ((Array.isArray(A) && A.length === 0) ||
148
+ (Array.isArray(B) && B.length === 0) ||
149
+ A === '' ||
150
+ B === '')
151
+ ) {
152
+ return [];
153
+ }
154
+ const metric = factory['metric'](resolved.metric, A, B, resolved.opt);
155
+ if (resolved.output !== 'prep') metric.setOriginal(a, b);
156
+ metric.run(mode);
157
+ const result = this.postProcess(metric.getResults(), resolved);
158
+ return this.output(result, raw ?? resolved.raw);
159
+ },
160
+ `Failed to compute metric <${resolved.metric}> for the given inputs`,
161
+ { a, b, options: opt }
162
+ );
154
163
  }
155
164
  output(result, raw) {
156
- return (raw ?? this.options.raw)
157
- ? result
158
- : Array.isArray(result)
159
- ? result.map((r) => ({ source: r.a, target: r.b, match: r.res }))
160
- : { source: result.a, target: result.b, match: result.res };
165
+ return ErrorUtil.wrap(
166
+ () =>
167
+ (raw ?? this.options.raw)
168
+ ? result
169
+ : Array.isArray(result)
170
+ ? result.map((r) => ({ source: r.a, target: r.b, match: r.res }))
171
+ : { source: result.a, target: result.b, match: result.res },
172
+ `Failed to resolve output format for the metric result`,
173
+ { result, raw }
174
+ );
175
+ }
176
+ clone() {
177
+ const inst = Object.assign(
178
+ Object.create(Object.getPrototypeOf(this)),
179
+ this
180
+ );
181
+ inst.options = DeepMerge.merge(Object.create(null), this.options);
182
+ return inst;
161
183
  }
162
- clone = () => Object.assign(Object.create(Object.getPrototypeOf(this)), this);
163
184
  reset() {
164
- for (const k in this.options) delete this.options[k];
185
+ this.options = Object.create(null);
165
186
  return this;
166
187
  }
167
188
  setOptions(opt) {
189
+ OptionsValidator.validateOptions(opt);
168
190
  this.options = opt;
169
191
  return this;
170
192
  }
171
193
  mergeOptions(opt) {
172
- merge(this.options, opt);
194
+ DeepMerge.merge(this.options, opt);
195
+ OptionsValidator.validateOptions(this.options);
173
196
  return this;
174
197
  }
175
198
  setSerializedOptions(opt) {
176
- this.options = JSON.parse(opt);
177
- return this;
199
+ try {
200
+ const parsed = JSON.parse(opt);
201
+ OptionsValidator.validateOptions(parsed);
202
+ this.options = parsed;
203
+ return this;
204
+ } catch (err) {
205
+ if (err instanceof SyntaxError)
206
+ throw new CmpStrValidationError(
207
+ `Failed to parse serialized options, invalid JSON string`,
208
+ { opt, error: err instanceof Error ? err.message : String(err) }
209
+ );
210
+ throw err;
211
+ }
178
212
  }
179
213
  setOption(path, value) {
180
- set(this.options, path, value);
214
+ DeepMerge.set(this.options, path, value);
215
+ OptionsValidator.validateOptions(this.options);
181
216
  return this;
182
217
  }
183
218
  rmvOption(path) {
184
- rmv(this.options, path);
219
+ DeepMerge.rmv(this.options, path);
185
220
  return this;
186
221
  }
187
- setRaw = (enable) => this.setOption('raw', enable);
188
- setMetric = (name) => this.setOption('metric', name);
189
- setFlags = (flags) => this.setOption('flags', flags);
190
- rmvFlags = () => this.rmvOption('flags');
191
- setProcessors = (opt) => this.setOption('processors', opt);
192
- rmvProcessors = () => this.rmvOption('processors');
193
- getOptions = () => this.options;
194
- getSerializedOptions = () => JSON.stringify(this.options);
195
- getOption = (path) => get(this.options, path);
222
+ setRaw(enable) {
223
+ return this.setOption('raw', enable);
224
+ }
225
+ setMetric(name) {
226
+ return this.setOption('metric', name);
227
+ }
228
+ setFlags(flags) {
229
+ return this.setOption('flags', flags);
230
+ }
231
+ rmvFlags() {
232
+ return this.rmvOption('flags');
233
+ }
234
+ setProcessors(opt) {
235
+ return this.setOption('processors', opt);
236
+ }
237
+ rmvProcessors() {
238
+ return this.rmvOption('processors');
239
+ }
240
+ getOptions() {
241
+ return this.options;
242
+ }
243
+ getSerializedOptions() {
244
+ return JSON.stringify(this.options);
245
+ }
246
+ getOption(path) {
247
+ return DeepMerge.get(this.options, path);
248
+ }
196
249
  test(a, b, opt) {
197
250
  return this.compute(a, b, opt, 'single');
198
251
  }
@@ -231,15 +284,35 @@ class CmpStr {
231
284
  const resolved = this.resolveOptions({ flags, processors });
232
285
  const test = this.prepare(needle, resolved);
233
286
  const hstk = this.prepare(haystack, resolved);
234
- return haystack.filter((_, i) => hstk[i].includes(test));
287
+ const out = [];
288
+ for (let i = 0, len = hstk.length; i < len; i++) {
289
+ if (hstk[i].includes(test)) out.push(haystack[i]);
290
+ }
291
+ return out;
235
292
  }
236
293
  matrix(input, opt) {
237
- input = this.prepare(input, this.resolveOptions(opt));
238
- return input.map((a) =>
239
- this.compute(a, input, undefined, 'batch', true, true).map(
240
- (b) => b.res ?? 0
241
- )
242
- );
294
+ const resolved = this.resolveOptions(opt);
295
+ const arr = this.prepare(input, resolved);
296
+ const n = arr.length;
297
+ const out = Array.from({ length: n }, () => new Array(n).fill(0));
298
+ for (let i = 0; i < n; i++)
299
+ for (let j = i; j < n; j++) {
300
+ if (i === j) {
301
+ out[i][j] = 1;
302
+ } else {
303
+ const score = this.compute(
304
+ arr[i],
305
+ arr[j],
306
+ resolved,
307
+ 'single',
308
+ true,
309
+ true
310
+ ).res;
311
+ out[i][j] = score;
312
+ out[j][i] = score;
313
+ }
314
+ }
315
+ return out;
243
316
  }
244
317
  phoneticIndex(input, algo, opt) {
245
318
  const { algo: a, opt: o } = this.options.processors?.phonetic ?? {};
@@ -284,4 +357,3 @@ class CmpStr {
284
357
  }
285
358
 
286
359
  export { CmpStr };
287
- //# sourceMappingURL=CmpStr.mjs.map
@@ -1,4 +1,5 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
+ import { ErrorUtil } from './utils/Errors.mjs';
2
3
  import { Filter } from './utils/Filter.mjs';
3
4
  import { Normalizer } from './utils/Normalizer.mjs';
4
5
  import { factory } from './utils/Registry.mjs';
@@ -40,22 +41,28 @@ class CmpStrAsync extends CmpStr {
40
41
  async computeAsync(a, b, opt, mode, raw, skip) {
41
42
  const resolved = this.resolveOptions(opt);
42
43
  this.assert('metric', resolved.metric);
43
- const A = skip ? a : await this.prepareAsync(a, resolved);
44
- const B = skip ? b : await this.prepareAsync(b, resolved);
45
- if (
46
- resolved.safeEmpty &&
47
- ((Array.isArray(A) && A.length === 0) ||
48
- (Array.isArray(B) && B.length === 0) ||
49
- A === '' ||
50
- B === '')
51
- ) {
52
- return [];
53
- }
54
- const metric = factory['metric'](resolved.metric, A, B, resolved.opt);
55
- if (resolved.output !== 'prep') metric.setOriginal(a, b);
56
- await metric.runAsync(mode);
57
- const result = this.postProcess(metric.getResults(), resolved);
58
- return this.output(result, raw ?? resolved.raw);
44
+ return ErrorUtil.wrapAsync(
45
+ async () => {
46
+ const A = skip ? a : await this.prepareAsync(a, resolved);
47
+ const B = skip ? b : await this.prepareAsync(b, resolved);
48
+ if (
49
+ resolved.safeEmpty &&
50
+ ((Array.isArray(A) && A.length === 0) ||
51
+ (Array.isArray(B) && B.length === 0) ||
52
+ A === '' ||
53
+ B === '')
54
+ ) {
55
+ return [];
56
+ }
57
+ const metric = factory['metric'](resolved.metric, A, B, resolved.opt);
58
+ if (resolved.output !== 'prep') metric.setOriginal(a, b);
59
+ await metric.runAsync(mode);
60
+ const result = this.postProcess(metric.getResults(), resolved);
61
+ return this.output(result, raw ?? resolved.raw);
62
+ },
63
+ `Failed to compute metric <${opt?.metric ?? this.options.metric}> for the given inputs`,
64
+ { a, b, opt }
65
+ );
59
66
  }
60
67
  async testAsync(a, b, opt) {
61
68
  return this.computeAsync(a, b, opt, 'single');
@@ -93,23 +100,40 @@ class CmpStrAsync extends CmpStr {
93
100
  const resolved = this.resolveOptions({ flags, processors });
94
101
  const test = await this.prepareAsync(needle, resolved);
95
102
  const hstk = await this.prepareAsync(haystack, resolved);
96
- return haystack.filter((_, i) => hstk[i].includes(test));
103
+ const out = [];
104
+ for (let i = 0; i < hstk.length; i++) {
105
+ if (hstk[i].includes(test)) out.push(haystack[i]);
106
+ }
107
+ return out;
97
108
  }
98
109
  async matrixAsync(input, opt) {
99
- input = await this.prepareAsync(input, this.resolveOptions(opt));
100
- return Promise.all(
101
- input.map(
102
- async (a) =>
103
- await this.computeAsync(
104
- a,
105
- input,
106
- undefined,
107
- 'batch',
108
- true,
109
- true
110
- ).then((r) => r.map((b) => b.res ?? 0))
111
- )
112
- );
110
+ const resolved = this.resolveOptions(opt);
111
+ const arr = await this.prepareAsync(input, resolved);
112
+ const n = arr.length;
113
+ const out = Array.from({ length: n }, () => new Array(n).fill(0));
114
+ for (let i = 0; i < n; i++) {
115
+ await Promise.all(
116
+ Array.from({ length: n - i }, (_, k) => i + k).map(async (j) => {
117
+ if (i === j) {
118
+ out[i][j] = 1;
119
+ } else {
120
+ const score = (
121
+ await this.computeAsync(
122
+ arr[i],
123
+ arr[j],
124
+ resolved,
125
+ 'single',
126
+ true,
127
+ true
128
+ )
129
+ ).res;
130
+ out[i][j] = score;
131
+ out[j][i] = score;
132
+ }
133
+ })
134
+ );
135
+ }
136
+ return out;
113
137
  }
114
138
  async phoneticIndexAsync(input, algo, opt) {
115
139
  const { algo: a, opt: o } = this.options.processors?.phonetic ?? {};
@@ -155,4 +179,3 @@ class CmpStrAsync extends CmpStr {
155
179
  }
156
180
 
157
181
  export { CmpStrAsync };
158
- //# sourceMappingURL=CmpStrAsync.mjs.map
@@ -1,7 +1,6 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
2
  export { CmpStr } from './CmpStr.mjs';
3
3
  export { CmpStrAsync } from './CmpStrAsync.mjs';
4
4
  export { DiffChecker } from './utils/DiffChecker.mjs';
5
5
  export { Normalizer } from './utils/Normalizer.mjs';
6
6
  export { TextAnalyzer } from './utils/TextAnalyzer.mjs';
7
- //# sourceMappingURL=index.mjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
2
  import { Pool } from '../utils/Pool.mjs';
3
3
  import { MetricRegistry, Metric } from './Metric.mjs';
4
4
 
@@ -41,4 +41,3 @@ class CosineSimilarity extends Metric {
41
41
  MetricRegistry.add('cosine', CosineSimilarity);
42
42
 
43
43
  export { CosineSimilarity };
44
- //# sourceMappingURL=Cosine.mjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
2
  import { Pool } from '../utils/Pool.mjs';
3
3
  import { MetricRegistry, Metric } from './Metric.mjs';
4
4
 
@@ -45,4 +45,3 @@ class DamerauLevenshteinDistance extends Metric {
45
45
  MetricRegistry.add('damerau', DamerauLevenshteinDistance);
46
46
 
47
47
  export { DamerauLevenshteinDistance };
48
- //# sourceMappingURL=DamerauLevenshtein.mjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
2
  import { Pool } from '../utils/Pool.mjs';
3
3
  import { MetricRegistry, Metric } from './Metric.mjs';
4
4
 
@@ -34,4 +34,3 @@ class DiceSorensenCoefficient extends Metric {
34
34
  MetricRegistry.add('dice', DiceSorensenCoefficient);
35
35
 
36
36
  export { DiceSorensenCoefficient };
37
- //# sourceMappingURL=DiceSorensen.mjs.map
@@ -1,5 +1,6 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
2
  import { MetricRegistry, Metric } from './Metric.mjs';
3
+ import { CmpStrUsageError } from '../utils/Errors.mjs';
3
4
 
4
5
  class HammingDistance extends Metric {
5
6
  constructor(a, b, opt = {}) {
@@ -12,9 +13,10 @@ class HammingDistance extends Metric {
12
13
  if (n < maxLen) b = b.padEnd(maxLen, this.options.pad);
13
14
  m = n = maxLen;
14
15
  } else
15
- throw new Error(
16
+ throw new CmpStrUsageError(
16
17
  `Strings must be of equal length for Hamming Distance, a=${m} and b=${n} given, ` +
17
- `use option.pad for automatic adjustment`
18
+ `use option.pad for automatic adjustment`,
19
+ { a: m, b: n }
18
20
  );
19
21
  }
20
22
  let dist = 0;
@@ -25,4 +27,3 @@ class HammingDistance extends Metric {
25
27
  MetricRegistry.add('hamming', HammingDistance);
26
28
 
27
29
  export { HammingDistance };
28
- //# sourceMappingURL=Hamming.mjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
2
  import { Pool } from '../utils/Pool.mjs';
3
3
  import { MetricRegistry, Metric } from './Metric.mjs';
4
4
 
@@ -27,4 +27,3 @@ class JaccardIndex extends Metric {
27
27
  MetricRegistry.add('jaccard', JaccardIndex);
28
28
 
29
29
  export { JaccardIndex };
30
- //# sourceMappingURL=Jaccard.mjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
2
  import { Pool } from '../utils/Pool.mjs';
3
3
  import { MetricRegistry, Metric } from './Metric.mjs';
4
4
 
@@ -59,4 +59,3 @@ class JaroWinklerDistance extends Metric {
59
59
  MetricRegistry.add('jaroWinkler', JaroWinklerDistance);
60
60
 
61
61
  export { JaroWinklerDistance };
62
- //# sourceMappingURL=JaroWinkler.mjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
2
  import { Pool } from '../utils/Pool.mjs';
3
3
  import { MetricRegistry, Metric } from './Metric.mjs';
4
4
 
@@ -34,4 +34,3 @@ class LCSMetric extends Metric {
34
34
  MetricRegistry.add('lcs', LCSMetric);
35
35
 
36
36
  export { LCSMetric };
37
- //# sourceMappingURL=LCS.mjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.1 build-3439ccb-260130 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.3.0 build-3699f85-260318 by Paul Köhler @komed3 / MIT License
2
2
  import { Pool } from '../utils/Pool.mjs';
3
3
  import { MetricRegistry, Metric } from './Metric.mjs';
4
4
 
@@ -34,4 +34,3 @@ class LevenshteinDistance extends Metric {
34
34
  MetricRegistry.add('levenshtein', LevenshteinDistance);
35
35
 
36
36
  export { LevenshteinDistance };
37
- //# sourceMappingURL=Levenshtein.mjs.map