cmpstr 3.2.0 → 3.2.2

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 (171) hide show
  1. package/README.md +26 -18
  2. package/dist/CmpStr.esm.js +490 -220
  3. package/dist/CmpStr.esm.min.js +2 -3
  4. package/dist/CmpStr.umd.js +489 -220
  5. package/dist/CmpStr.umd.min.js +2 -3
  6. package/dist/cjs/CmpStr.cjs +58 -36
  7. package/dist/cjs/CmpStrAsync.cjs +30 -24
  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 +57 -38
  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 +55 -35
  25. package/dist/cjs/phonetic/Soundex.cjs +1 -2
  26. package/dist/cjs/root.cjs +3 -2
  27. package/dist/cjs/utils/DeepMerge.cjs +10 -5
  28. package/dist/cjs/utils/DiffChecker.cjs +1 -2
  29. package/dist/cjs/utils/Errors.cjs +103 -0
  30. package/dist/cjs/utils/Filter.cjs +56 -27
  31. package/dist/cjs/utils/HashTable.cjs +1 -2
  32. package/dist/cjs/utils/Normalizer.cjs +54 -34
  33. package/dist/cjs/utils/Pool.cjs +42 -18
  34. package/dist/cjs/utils/Profiler.cjs +1 -2
  35. package/dist/cjs/utils/Registry.cjs +46 -22
  36. package/dist/cjs/utils/StructuredData.cjs +13 -5
  37. package/dist/cjs/utils/TextAnalyzer.cjs +1 -2
  38. package/dist/esm/CmpStr.mjs +56 -32
  39. package/dist/esm/CmpStrAsync.mjs +26 -20
  40. package/dist/esm/index.mjs +1 -2
  41. package/dist/esm/metric/Cosine.mjs +1 -2
  42. package/dist/esm/metric/DamerauLevenshtein.mjs +1 -2
  43. package/dist/esm/metric/DiceSorensen.mjs +1 -2
  44. package/dist/esm/metric/Hamming.mjs +5 -4
  45. package/dist/esm/metric/Jaccard.mjs +1 -2
  46. package/dist/esm/metric/JaroWinkler.mjs +1 -2
  47. package/dist/esm/metric/LCS.mjs +1 -2
  48. package/dist/esm/metric/Levenshtein.mjs +1 -2
  49. package/dist/esm/metric/Metric.mjs +59 -38
  50. package/dist/esm/metric/NeedlemanWunsch.mjs +1 -2
  51. package/dist/esm/metric/QGram.mjs +1 -2
  52. package/dist/esm/metric/SmithWaterman.mjs +1 -2
  53. package/dist/esm/phonetic/Caverphone.mjs +1 -2
  54. package/dist/esm/phonetic/Cologne.mjs +1 -2
  55. package/dist/esm/phonetic/Metaphone.mjs +1 -2
  56. package/dist/esm/phonetic/Phonetic.mjs +55 -35
  57. package/dist/esm/phonetic/Soundex.mjs +1 -2
  58. package/dist/esm/root.mjs +3 -2
  59. package/dist/esm/utils/DeepMerge.mjs +10 -5
  60. package/dist/esm/utils/DiffChecker.mjs +1 -2
  61. package/dist/esm/utils/Errors.mjs +103 -0
  62. package/dist/esm/utils/Filter.mjs +56 -27
  63. package/dist/esm/utils/HashTable.mjs +1 -2
  64. package/dist/esm/utils/Normalizer.mjs +54 -34
  65. package/dist/esm/utils/Pool.mjs +38 -18
  66. package/dist/esm/utils/Profiler.mjs +1 -2
  67. package/dist/esm/utils/Registry.mjs +46 -22
  68. package/dist/esm/utils/StructuredData.mjs +13 -5
  69. package/dist/esm/utils/TextAnalyzer.mjs +1 -2
  70. package/dist/types/CmpStr.d.ts +12 -6
  71. package/dist/types/CmpStrAsync.d.ts +6 -4
  72. package/dist/types/index.d.ts +3 -2
  73. package/dist/types/metric/Cosine.d.ts +2 -1
  74. package/dist/types/metric/DamerauLevenshtein.d.ts +2 -1
  75. package/dist/types/metric/DiceSorensen.d.ts +2 -1
  76. package/dist/types/metric/Hamming.d.ts +2 -1
  77. package/dist/types/metric/Jaccard.d.ts +2 -1
  78. package/dist/types/metric/JaroWinkler.d.ts +2 -1
  79. package/dist/types/metric/LCS.d.ts +2 -1
  80. package/dist/types/metric/Levenshtein.d.ts +2 -1
  81. package/dist/types/metric/Metric.d.ts +7 -5
  82. package/dist/types/metric/NeedlemanWunsch.d.ts +2 -1
  83. package/dist/types/metric/QGram.d.ts +2 -1
  84. package/dist/types/metric/SmithWaterman.d.ts +2 -1
  85. package/dist/types/metric/index.d.ts +1 -0
  86. package/dist/types/phonetic/Caverphone.d.ts +2 -1
  87. package/dist/types/phonetic/Cologne.d.ts +2 -1
  88. package/dist/types/phonetic/Metaphone.d.ts +2 -1
  89. package/dist/types/phonetic/Phonetic.d.ts +4 -1
  90. package/dist/types/phonetic/Soundex.d.ts +2 -1
  91. package/dist/types/phonetic/index.d.ts +1 -0
  92. package/dist/types/root.d.ts +2 -1
  93. package/dist/types/utils/DeepMerge.d.ts +3 -2
  94. package/dist/types/utils/DiffChecker.d.ts +2 -1
  95. package/dist/types/utils/Errors.d.ts +137 -0
  96. package/dist/types/utils/Filter.d.ts +33 -22
  97. package/dist/types/utils/HashTable.d.ts +2 -1
  98. package/dist/types/utils/Normalizer.d.ts +5 -1
  99. package/dist/types/utils/Pool.d.ts +4 -1
  100. package/dist/types/utils/Profiler.d.ts +3 -2
  101. package/dist/types/utils/Registry.d.ts +5 -4
  102. package/dist/types/utils/StructuredData.d.ts +5 -2
  103. package/dist/types/utils/TextAnalyzer.d.ts +2 -1
  104. package/dist/types/utils/Types.d.ts +34 -2
  105. package/package.json +10 -7
  106. package/dist/CmpStr.esm.js.map +0 -1
  107. package/dist/CmpStr.esm.min.js.map +0 -1
  108. package/dist/CmpStr.umd.js.map +0 -1
  109. package/dist/CmpStr.umd.min.js.map +0 -1
  110. package/dist/cjs/CmpStr.cjs.map +0 -1
  111. package/dist/cjs/CmpStrAsync.cjs.map +0 -1
  112. package/dist/cjs/index.cjs.map +0 -1
  113. package/dist/cjs/metric/Cosine.cjs.map +0 -1
  114. package/dist/cjs/metric/DamerauLevenshtein.cjs.map +0 -1
  115. package/dist/cjs/metric/DiceSorensen.cjs.map +0 -1
  116. package/dist/cjs/metric/Hamming.cjs.map +0 -1
  117. package/dist/cjs/metric/Jaccard.cjs.map +0 -1
  118. package/dist/cjs/metric/JaroWinkler.cjs.map +0 -1
  119. package/dist/cjs/metric/LCS.cjs.map +0 -1
  120. package/dist/cjs/metric/Levenshtein.cjs.map +0 -1
  121. package/dist/cjs/metric/Metric.cjs.map +0 -1
  122. package/dist/cjs/metric/NeedlemanWunsch.cjs.map +0 -1
  123. package/dist/cjs/metric/QGram.cjs.map +0 -1
  124. package/dist/cjs/metric/SmithWaterman.cjs.map +0 -1
  125. package/dist/cjs/phonetic/Caverphone.cjs.map +0 -1
  126. package/dist/cjs/phonetic/Cologne.cjs.map +0 -1
  127. package/dist/cjs/phonetic/Metaphone.cjs.map +0 -1
  128. package/dist/cjs/phonetic/Phonetic.cjs.map +0 -1
  129. package/dist/cjs/phonetic/Soundex.cjs.map +0 -1
  130. package/dist/cjs/root.cjs.map +0 -1
  131. package/dist/cjs/utils/DeepMerge.cjs.map +0 -1
  132. package/dist/cjs/utils/DiffChecker.cjs.map +0 -1
  133. package/dist/cjs/utils/Filter.cjs.map +0 -1
  134. package/dist/cjs/utils/HashTable.cjs.map +0 -1
  135. package/dist/cjs/utils/Normalizer.cjs.map +0 -1
  136. package/dist/cjs/utils/Pool.cjs.map +0 -1
  137. package/dist/cjs/utils/Profiler.cjs.map +0 -1
  138. package/dist/cjs/utils/Registry.cjs.map +0 -1
  139. package/dist/cjs/utils/StructuredData.cjs.map +0 -1
  140. package/dist/cjs/utils/TextAnalyzer.cjs.map +0 -1
  141. package/dist/esm/CmpStr.mjs.map +0 -1
  142. package/dist/esm/CmpStrAsync.mjs.map +0 -1
  143. package/dist/esm/index.mjs.map +0 -1
  144. package/dist/esm/metric/Cosine.mjs.map +0 -1
  145. package/dist/esm/metric/DamerauLevenshtein.mjs.map +0 -1
  146. package/dist/esm/metric/DiceSorensen.mjs.map +0 -1
  147. package/dist/esm/metric/Hamming.mjs.map +0 -1
  148. package/dist/esm/metric/Jaccard.mjs.map +0 -1
  149. package/dist/esm/metric/JaroWinkler.mjs.map +0 -1
  150. package/dist/esm/metric/LCS.mjs.map +0 -1
  151. package/dist/esm/metric/Levenshtein.mjs.map +0 -1
  152. package/dist/esm/metric/Metric.mjs.map +0 -1
  153. package/dist/esm/metric/NeedlemanWunsch.mjs.map +0 -1
  154. package/dist/esm/metric/QGram.mjs.map +0 -1
  155. package/dist/esm/metric/SmithWaterman.mjs.map +0 -1
  156. package/dist/esm/phonetic/Caverphone.mjs.map +0 -1
  157. package/dist/esm/phonetic/Cologne.mjs.map +0 -1
  158. package/dist/esm/phonetic/Metaphone.mjs.map +0 -1
  159. package/dist/esm/phonetic/Phonetic.mjs.map +0 -1
  160. package/dist/esm/phonetic/Soundex.mjs.map +0 -1
  161. package/dist/esm/root.mjs.map +0 -1
  162. package/dist/esm/utils/DeepMerge.mjs.map +0 -1
  163. package/dist/esm/utils/DiffChecker.mjs.map +0 -1
  164. package/dist/esm/utils/Filter.mjs.map +0 -1
  165. package/dist/esm/utils/HashTable.mjs.map +0 -1
  166. package/dist/esm/utils/Normalizer.mjs.map +0 -1
  167. package/dist/esm/utils/Pool.mjs.map +0 -1
  168. package/dist/esm/utils/Profiler.mjs.map +0 -1
  169. package/dist/esm/utils/Registry.mjs.map +0 -1
  170. package/dist/esm/utils/StructuredData.mjs.map +0 -1
  171. package/dist/esm/utils/TextAnalyzer.mjs.map +0 -1
@@ -1,6 +1,7 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
+ var Errors = require('../utils/Errors.cjs');
4
5
  var HashTable = require('../utils/HashTable.cjs');
5
6
  var Profiler = require('../utils/Profiler.cjs');
6
7
  var Registry = require('../utils/Registry.cjs');
@@ -24,8 +25,11 @@ class Metric {
24
25
  this.metric = metric;
25
26
  this.a = Array.isArray(a) ? a : [a];
26
27
  this.b = Array.isArray(b) ? b : [b];
27
- if (this.a.length === 0 || this.b.length === 0)
28
- throw new Error(`Inputs <a> and <b> must not be empty`);
28
+ Errors.ErrorUtil.assert(
29
+ this.a.length > 0 && this.b.length > 0,
30
+ `Inputs <a> and <b> must not be empty`,
31
+ { a: this.a, b: this.b }
32
+ );
29
33
  this.options = opt;
30
34
  this.optKey = HashTable.Hasher.fastFNV1a(
31
35
  JSON.stringify(opt, Object.keys(opt).sort())
@@ -38,37 +42,46 @@ class Metric {
38
42
  return undefined;
39
43
  }
40
44
  compute(a, b, m, n, maxLen) {
41
- throw new Error(`Method compute() must be overridden in a subclass`);
45
+ throw new Errors.CmpStrInternalError(
46
+ `Method compute() must be overridden in a subclass`
47
+ );
42
48
  }
43
49
  runSingle(i, j) {
44
- let a = String(this.a[i]),
45
- A = a;
46
- let b = String(this.b[j]),
47
- B = b;
48
- let m = A.length,
49
- n = B.length;
50
- let result = this.preCompute(A, B, m, n);
51
- if (!result) {
52
- result = profiler.run(() => {
53
- if (this.symmetric) [A, B, m, n] = Metric.swap(A, B, m, n);
54
- const key =
55
- Metric.cache.key(this.metric, [A, B], this.symmetric) + this.optKey;
56
- return (
57
- Metric.cache.get(key || '') ??
58
- (() => {
59
- const res = this.compute(A, B, m, n, Math.max(m, n));
60
- if (key) Metric.cache.set(key, res);
61
- return res;
62
- })()
63
- );
64
- });
65
- }
66
- return {
67
- metric: this.metric,
68
- a: this.origA[i] ?? a,
69
- b: this.origB[j] ?? b,
70
- ...result
71
- };
50
+ return Errors.ErrorUtil.wrap(
51
+ () => {
52
+ let a = String(this.a[i]),
53
+ A = a;
54
+ let b = String(this.b[j]),
55
+ B = b;
56
+ let m = A.length,
57
+ n = B.length;
58
+ let result = this.preCompute(A, B, m, n);
59
+ if (!result) {
60
+ result = profiler.run(() => {
61
+ if (this.symmetric) [A, B, m, n] = Metric.swap(A, B, m, n);
62
+ const key =
63
+ Metric.cache.key(this.metric, [A, B], this.symmetric) +
64
+ this.optKey;
65
+ return (
66
+ Metric.cache.get(key || '') ??
67
+ (() => {
68
+ const res = this.compute(A, B, m, n, Math.max(m, n));
69
+ if (key) Metric.cache.set(key, res);
70
+ return res;
71
+ })()
72
+ );
73
+ });
74
+ }
75
+ return {
76
+ metric: this.metric,
77
+ a: this.origA[i] ?? a,
78
+ b: this.origB[j] ?? b,
79
+ ...result
80
+ };
81
+ },
82
+ `Failed to compute metric for inputs at indices a[${i}] and b[${j}]`,
83
+ { i, j }
84
+ );
72
85
  }
73
86
  async runSingleAsync(i, j) {
74
87
  return Promise.resolve(this.runSingle(i, j));
@@ -110,7 +123,10 @@ class Metric {
110
123
  ? true
111
124
  : !safe &&
112
125
  (() => {
113
- throw new Error(`Mode <pairwise> requires arrays of equal length`);
126
+ throw new Errors.CmpStrUsageError(
127
+ `Mode <pairwise> requires arrays of equal length`,
128
+ { a: this.a, b: this.b }
129
+ );
114
130
  })();
115
131
  }
116
132
  isSymmetrical = () => this.symmetric;
@@ -134,7 +150,7 @@ class Metric {
134
150
  if (this.isPairwise()) this.runPairwise();
135
151
  break;
136
152
  default:
137
- throw new Error(`Unsupported mode <${mode}>`);
153
+ throw new Errors.CmpStrInternalError(`Unsupported mode <${mode}>`);
138
154
  }
139
155
  }
140
156
  async runAsync(mode, clear = true) {
@@ -155,13 +171,17 @@ class Metric {
155
171
  if (this.isPairwise()) await this.runPairwiseAsync();
156
172
  break;
157
173
  default:
158
- throw new Error(`Unsupported async mode <${mode}>`);
174
+ throw new Errors.CmpStrInternalError(
175
+ `Unsupported async mode <${mode}>`
176
+ );
159
177
  }
160
178
  }
161
179
  getMetricName = () => this.metric;
162
180
  getResults() {
163
- if (this.results === undefined)
164
- throw new Error(`run() must be called before getResult()`);
181
+ Errors.ErrorUtil.assert(
182
+ this.results !== undefined,
183
+ `run() must be called before getResults()`
184
+ );
165
185
  return this.results;
166
186
  }
167
187
  }
@@ -169,4 +189,3 @@ const MetricRegistry = Registry.Registry('metric', Metric);
169
189
 
170
190
  exports.Metric = Metric;
171
191
  exports.MetricRegistry = MetricRegistry;
172
- //# sourceMappingURL=Metric.cjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  var Pool = require('../utils/Pool.cjs');
@@ -43,4 +43,3 @@ class NeedlemanWunschDistance extends Metric.Metric {
43
43
  Metric.MetricRegistry.add('needlemanWunsch', NeedlemanWunschDistance);
44
44
 
45
45
  exports.NeedlemanWunschDistance = NeedlemanWunschDistance;
46
- //# sourceMappingURL=NeedlemanWunsch.cjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  var Pool = require('../utils/Pool.cjs');
@@ -37,4 +37,3 @@ class QGramSimilarity extends Metric.Metric {
37
37
  Metric.MetricRegistry.add('qGram', QGramSimilarity);
38
38
 
39
39
  exports.QGramSimilarity = QGramSimilarity;
40
- //# sourceMappingURL=QGram.cjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  var Pool = require('../utils/Pool.cjs');
@@ -44,4 +44,3 @@ class SmithWatermanDistance extends Metric.Metric {
44
44
  Metric.MetricRegistry.add('smithWaterman', SmithWatermanDistance);
45
45
 
46
46
  exports.SmithWatermanDistance = SmithWatermanDistance;
47
- //# sourceMappingURL=SmithWaterman.cjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  var Phonetic = require('./Phonetic.cjs');
@@ -116,4 +116,3 @@ Phonetic.PhoneticMappingRegistry.add('caverphone', 'en2', {
116
116
  });
117
117
 
118
118
  exports.Caverphone = Caverphone;
119
- //# sourceMappingURL=Caverphone.cjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  var Phonetic = require('./Phonetic.cjs');
@@ -67,4 +67,3 @@ Phonetic.PhoneticMappingRegistry.add('cologne', 'default', {
67
67
  });
68
68
 
69
69
  exports.Cologne = Cologne;
70
- //# sourceMappingURL=Cologne.cjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  var Phonetic = require('./Phonetic.cjs');
@@ -97,4 +97,3 @@ Phonetic.PhoneticMappingRegistry.add('metaphone', 'en90', {
97
97
  });
98
98
 
99
99
  exports.Metaphone = Metaphone;
100
- //# sourceMappingURL=Metaphone.cjs.map
@@ -1,7 +1,8 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  var DeepMerge = require('../utils/DeepMerge.cjs');
5
+ var Errors = require('../utils/Errors.cjs');
5
6
  var HashTable = require('../utils/HashTable.cjs');
6
7
  var Profiler = require('../utils/Profiler.cjs');
7
8
  var Registry = require('../utils/Registry.cjs');
@@ -18,10 +19,17 @@ class Phonetic {
18
19
  constructor(algo, opt = {}) {
19
20
  const defaults = this.constructor.default ?? {};
20
21
  const mapId = opt.map ?? defaults.map;
21
- if (!mapId) throw new Error(`No mapping specified for phonetic algorithm`);
22
+ if (!mapId)
23
+ throw new Errors.CmpStrNotFoundError(
24
+ `No mapping specified for phonetic algorithm`,
25
+ { algo }
26
+ );
22
27
  const map = PhoneticMappingRegistry.get(algo, mapId);
23
28
  if (map === undefined)
24
- throw new Error(`Requested mapping <${mapId}> is not declared`);
29
+ throw new Errors.CmpStrNotFoundError(
30
+ `Requested mapping <${mapId}> is not declared`,
31
+ { algo, mapId }
32
+ );
25
33
  this.options = DeepMerge.merge(
26
34
  DeepMerge.merge(defaults, map.options ?? {}),
27
35
  opt
@@ -114,35 +122,47 @@ class Phonetic {
114
122
  return code;
115
123
  }
116
124
  loop(words) {
117
- const index = [];
118
- for (const word of words) {
119
- const key = Phonetic.cache.key(this.algo, [word]) + this.optKey;
120
- const code =
121
- Phonetic.cache.get(key || '') ??
122
- (() => {
123
- const res = this.encode(word);
124
- if (key) Phonetic.cache.set(key, res);
125
- return res;
126
- })();
127
- if (code && code.length) index.push(this.equalLen(code));
128
- }
129
- return index;
125
+ return Errors.ErrorUtil.wrap(
126
+ () => {
127
+ const index = [];
128
+ for (const word of words) {
129
+ const key = Phonetic.cache.key(this.algo, [word]) + this.optKey;
130
+ const code =
131
+ Phonetic.cache.get(key || '') ??
132
+ (() => {
133
+ const res = this.encode(word);
134
+ if (key) Phonetic.cache.set(key, res);
135
+ return res;
136
+ })();
137
+ if (code && code.length) index.push(this.equalLen(code));
138
+ }
139
+ return index;
140
+ },
141
+ `Failed to generate phonetic index`,
142
+ { algo: this.algo, words }
143
+ );
130
144
  }
131
145
  async loopAsync(words) {
132
- const index = [];
133
- for (const word of words) {
134
- const key = Phonetic.cache.key(this.algo, [word]) + this.optKey;
135
- const code = await Promise.resolve(
136
- Phonetic.cache.get(key || '') ??
137
- (() => {
138
- const res = this.encode(word);
139
- if (key) Phonetic.cache.set(key, res);
140
- return res;
141
- })()
142
- );
143
- if (code && code.length) index.push(this.equalLen(code));
144
- }
145
- return index;
146
+ return Errors.ErrorUtil.wrapAsync(
147
+ async () => {
148
+ const index = [];
149
+ for (const word of words) {
150
+ const key = Phonetic.cache.key(this.algo, [word]) + this.optKey;
151
+ const code = await Promise.resolve(
152
+ Phonetic.cache.get(key || '') ??
153
+ (() => {
154
+ const res = this.encode(word);
155
+ if (key) Phonetic.cache.set(key, res);
156
+ return res;
157
+ })()
158
+ );
159
+ if (code && code.length) index.push(this.equalLen(code));
160
+ }
161
+ return index;
162
+ },
163
+ `Failed to generate phonetic index asynchronously`,
164
+ { algo: this.algo, words }
165
+ );
146
166
  }
147
167
  getAlgoName = () => this.algo;
148
168
  getIndex(input) {
@@ -167,10 +187,11 @@ const PhoneticMappingRegistry = (() => {
167
187
  return Object.freeze({
168
188
  add(algo, id, map, update = false) {
169
189
  const mappings = maps(algo);
170
- if (!update && id in mappings)
171
- throw new Error(
172
- `Entry <${id}> already exists / use <update=true> to overwrite`
173
- );
190
+ Errors.ErrorUtil.assert(
191
+ !(!id || id in mappings) || update,
192
+ `Entry <${id}> already exists / use <update=true> to overwrite`,
193
+ { algo, id }
194
+ );
174
195
  mappings[id] = map;
175
196
  },
176
197
  remove(algo, id) {
@@ -191,4 +212,3 @@ const PhoneticMappingRegistry = (() => {
191
212
  exports.Phonetic = Phonetic;
192
213
  exports.PhoneticMappingRegistry = PhoneticMappingRegistry;
193
214
  exports.PhoneticRegistry = PhoneticRegistry;
194
- //# sourceMappingURL=Phonetic.cjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  var Phonetic = require('./Phonetic.cjs');
@@ -86,4 +86,3 @@ Phonetic.PhoneticMappingRegistry.add('soundex', 'de', {
86
86
  });
87
87
 
88
88
  exports.Soundex = Soundex;
89
- //# sourceMappingURL=Soundex.cjs.map
package/dist/cjs/root.cjs CHANGED
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  var CmpStr = require('./CmpStr.cjs');
@@ -24,6 +24,7 @@ require('./phonetic/Metaphone.cjs');
24
24
  require('./phonetic/Soundex.cjs');
25
25
  var Phonetic = require('./phonetic/Phonetic.cjs');
26
26
  var DeepMerge = require('./utils/DeepMerge.cjs');
27
+ var Errors = require('./utils/Errors.cjs');
27
28
  var Filter = require('./utils/Filter.cjs');
28
29
  var HashTable = require('./utils/HashTable.cjs');
29
30
  var Pool = require('./utils/Pool.cjs');
@@ -41,10 +42,10 @@ exports.Phonetic = Phonetic.Phonetic;
41
42
  exports.PhoneticMappingRegistry = Phonetic.PhoneticMappingRegistry;
42
43
  exports.PhoneticRegistry = Phonetic.PhoneticRegistry;
43
44
  exports.DeepMerge = DeepMerge;
45
+ exports.CmpStrError = Errors;
44
46
  exports.Filter = Filter.Filter;
45
47
  exports.HashTable = HashTable.HashTable;
46
48
  exports.Hasher = HashTable.Hasher;
47
49
  exports.Pool = Pool.Pool;
48
50
  exports.Profiler = Profiler.Profiler;
49
51
  exports.StructuredData = StructuredData.StructuredData;
50
- //# sourceMappingURL=root.cjs.map
@@ -1,6 +1,8 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
+ var Errors = require('./Errors.cjs');
5
+
4
6
  const BRACKET_PATTERN = /\[(\d+)]/g;
5
7
  const PATH_CACHE = new Map();
6
8
  function parse(p) {
@@ -36,15 +38,19 @@ function set(t, path, value) {
36
38
  if (path === '') return value;
37
39
  const keys = parse(path);
38
40
  if (t !== undefined && (typeof t !== 'object' || t === null))
39
- throw Error(`Cannot set property <${keys[0]}> of <${JSON.stringify(t)}>`);
41
+ throw new Errors.CmpStrUsageError(
42
+ `Cannot set property <${keys[0]}> of <${JSON.stringify(t)}>`,
43
+ { path: keys[0], target: t }
44
+ );
40
45
  const root = t ?? (typeof keys[0] === 'number' ? [] : Object.create(null));
41
46
  let cur = root;
42
47
  for (let i = 0; i < keys.length - 1; i++) {
43
48
  const k = keys[i];
44
49
  let n = cur[k];
45
50
  if (n != null && typeof n !== 'object')
46
- throw Error(
47
- `Cannot set property <${keys[i + 1]}> of <${JSON.stringify(n)}>`
51
+ throw new Errors.CmpStrUsageError(
52
+ `Cannot set property <${keys[i + 1]}> of <${JSON.stringify(n)}>`,
53
+ { path: keys.slice(0, i + 2), value: n }
48
54
  );
49
55
  if (n == null)
50
56
  n = cur[k] = typeof keys[i + 1] === 'number' ? [] : Object.create(null);
@@ -105,4 +111,3 @@ exports.has = has;
105
111
  exports.merge = merge;
106
112
  exports.rmv = rmv;
107
113
  exports.set = set;
108
- //# sourceMappingURL=DeepMerge.cjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  class DiffChecker {
@@ -297,4 +297,3 @@ class DiffChecker {
297
297
  }
298
298
 
299
299
  exports.DiffChecker = DiffChecker;
300
- //# sourceMappingURL=DiffChecker.cjs.map
@@ -0,0 +1,103 @@
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
+ 'use strict';
3
+
4
+ class CmpStrError extends Error {
5
+ code;
6
+ meta;
7
+ cause;
8
+ when = new Date().toISOString();
9
+ constructor(code, message, meta, cause) {
10
+ super(message);
11
+ this.name = this.constructor.name;
12
+ this.code = code;
13
+ this.meta = meta;
14
+ this.cause = cause;
15
+ if (typeof Error.captureStackTrace === 'function') {
16
+ Error.captureStackTrace(this, this.constructor);
17
+ }
18
+ }
19
+ toJSON() {
20
+ return {
21
+ name: this.name,
22
+ code: this.code,
23
+ message: this.message,
24
+ meta: this.meta,
25
+ when: this.when,
26
+ cause:
27
+ this.cause instanceof Error
28
+ ? {
29
+ name: this.cause.name,
30
+ message: this.cause.message,
31
+ stack: this.cause.stack
32
+ }
33
+ : this.cause
34
+ };
35
+ }
36
+ toString(stack = false) {
37
+ const parts = [`${this.name} [${this.code}]`, this.message];
38
+ if (this.meta && Object.keys(this.meta).length) {
39
+ try {
40
+ parts.push(JSON.stringify(this.meta));
41
+ } catch {}
42
+ }
43
+ return (
44
+ parts.join(' - ') +
45
+ (stack && this.stack ? `\nStack Trace:\n${this.stack}` : '')
46
+ );
47
+ }
48
+ }
49
+ class CmpStrValidationError extends CmpStrError {
50
+ constructor(message, meta, cause) {
51
+ super('E_VALIDATION', message, meta, cause);
52
+ }
53
+ }
54
+ class CmpStrNotFoundError extends CmpStrError {
55
+ constructor(message, meta, cause) {
56
+ super('E_NOT_FOUND', message, meta, cause);
57
+ }
58
+ }
59
+ class CmpStrUsageError extends CmpStrError {
60
+ constructor(message, meta, cause) {
61
+ super('E_USAGE', message, meta, cause);
62
+ }
63
+ }
64
+ class CmpStrInternalError extends CmpStrError {
65
+ constructor(message, meta, cause) {
66
+ super('E_INTERNAL', message, meta, cause);
67
+ }
68
+ }
69
+ class ErrorUtil {
70
+ static assert(condition, message, meta) {
71
+ if (!condition) throw new CmpStrUsageError(message, meta);
72
+ }
73
+ static create(err, message, meta) {
74
+ if (err instanceof CmpStrError) throw err;
75
+ throw new CmpStrInternalError(message, meta, err);
76
+ }
77
+ static format(err) {
78
+ if (err instanceof CmpStrError) return err.toString();
79
+ if (err instanceof Error) return `${err.name}: ${err.message}`;
80
+ return String(err);
81
+ }
82
+ static wrap(fn, message, meta) {
83
+ try {
84
+ return fn();
85
+ } catch (err) {
86
+ throw new CmpStrInternalError(message, meta, err);
87
+ }
88
+ }
89
+ static async wrapAsync(fn, message, meta) {
90
+ try {
91
+ return await fn();
92
+ } catch (err) {
93
+ throw new CmpStrInternalError(message, meta, err);
94
+ }
95
+ }
96
+ }
97
+
98
+ exports.CmpStrError = CmpStrError;
99
+ exports.CmpStrInternalError = CmpStrInternalError;
100
+ exports.CmpStrNotFoundError = CmpStrNotFoundError;
101
+ exports.CmpStrUsageError = CmpStrUsageError;
102
+ exports.CmpStrValidationError = CmpStrValidationError;
103
+ exports.ErrorUtil = ErrorUtil;
@@ -1,34 +1,48 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
+ var Errors = require('./Errors.cjs');
5
+
4
6
  class Filter {
5
7
  static filters = new Map();
6
8
  static pipeline = new Map();
7
9
  static getPipeline(hook) {
8
- const cached = Filter.pipeline.get(hook);
9
- if (cached) return cached;
10
- const filter = Filter.filters.get(hook);
11
- if (!filter) return (s) => s;
12
- const pipeline = Array.from(filter.values())
13
- .filter((f) => f.active)
14
- .sort((a, b) => a.priority - b.priority)
15
- .map((f) => f.fn);
16
- const fn = (input) => pipeline.reduce((v, f) => f(v), input);
17
- Filter.pipeline.set(hook, fn);
18
- return fn;
10
+ return Errors.ErrorUtil.wrap(
11
+ () => {
12
+ const cached = Filter.pipeline.get(hook);
13
+ if (cached) return cached;
14
+ const filter = Filter.filters.get(hook);
15
+ if (!filter) return (s) => s;
16
+ const pipeline = Array.from(filter.values())
17
+ .filter((f) => f.active)
18
+ .sort((a, b) => a.priority - b.priority)
19
+ .map((f) => f.fn);
20
+ const fn = (input) => pipeline.reduce((v, f) => f(v), input);
21
+ Filter.pipeline.set(hook, fn);
22
+ return fn;
23
+ },
24
+ `Error compiling filter pipeline for hook <${hook}>`,
25
+ { hook }
26
+ );
19
27
  }
20
28
  static has(hook, id) {
21
29
  return !!Filter.filters.get(hook)?.has(id);
22
30
  }
23
31
  static add(hook, id, fn, opt = {}) {
24
- const { priority = 10, active = true, overrideable = true } = opt;
25
- const filter = Filter.filters.get(hook) ?? new Map();
26
- const index = filter.get(id);
27
- if (index && !index.overrideable) return false;
28
- filter.set(id, { id, fn, priority, active, overrideable });
29
- Filter.filters.set(hook, filter);
30
- Filter.pipeline.delete(hook);
31
- return true;
32
+ return Errors.ErrorUtil.wrap(
33
+ () => {
34
+ const { priority = 10, active = true, overrideable = true } = opt;
35
+ const filter = Filter.filters.get(hook) ?? new Map();
36
+ const index = filter.get(id);
37
+ if (index && !index.overrideable) return false;
38
+ filter.set(id, { id, fn, priority, active, overrideable });
39
+ Filter.filters.set(hook, filter);
40
+ Filter.pipeline.delete(hook);
41
+ return true;
42
+ },
43
+ `Error adding filter <${id}> to hook <${hook}>`,
44
+ { hook, id, opt }
45
+ );
32
46
  }
33
47
  static remove(hook, id) {
34
48
  Filter.pipeline.delete(hook);
@@ -53,20 +67,35 @@ class Filter {
53
67
  return out;
54
68
  }
55
69
  static apply(hook, input) {
56
- const fn = Filter.getPipeline(hook);
57
- return Array.isArray(input) ? input.map(fn) : fn(input);
70
+ return Errors.ErrorUtil.wrap(
71
+ () => {
72
+ const fn = Filter.getPipeline(hook);
73
+ return Array.isArray(input) ? input.map(fn) : fn(input);
74
+ },
75
+ `Error applying filters for hook <${hook}>`,
76
+ { hook, input }
77
+ );
58
78
  }
59
79
  static async applyAsync(hook, input) {
60
- const fn = Filter.getPipeline(hook);
61
- return Array.isArray(input)
62
- ? Promise.all(input.map(fn))
63
- : Promise.resolve(fn(input));
80
+ return Errors.ErrorUtil.wrapAsync(
81
+ async () => {
82
+ const fn = Filter.getPipeline(hook);
83
+ return Array.isArray(input)
84
+ ? Promise.all(input.map(fn))
85
+ : Promise.resolve(fn(input));
86
+ },
87
+ `Error applying filters for hook <${hook}>`,
88
+ { hook, input }
89
+ );
64
90
  }
65
91
  static clear(hook) {
92
+ Filter.pipeline.clear();
66
93
  if (hook) Filter.filters.delete(hook);
67
94
  else Filter.filters.clear();
68
95
  }
96
+ static clearPipeline() {
97
+ Filter.pipeline.clear();
98
+ }
69
99
  }
70
100
 
71
101
  exports.Filter = Filter;
72
- //# sourceMappingURL=Filter.cjs.map
@@ -1,4 +1,4 @@
1
- // CmpStr v3.2.0 build-6929b12-260122 by Paul Köhler @komed3 / MIT License
1
+ // CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
2
2
  'use strict';
3
3
 
4
4
  class Hasher {
@@ -65,4 +65,3 @@ class HashTable {
65
65
 
66
66
  exports.HashTable = HashTable;
67
67
  exports.Hasher = Hasher;
68
- //# sourceMappingURL=HashTable.cjs.map