cmpstr 3.2.1 → 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.
- package/README.md +24 -18
- package/dist/CmpStr.esm.js +485 -220
- package/dist/CmpStr.esm.min.js +2 -3
- package/dist/CmpStr.umd.js +484 -220
- package/dist/CmpStr.umd.min.js +2 -3
- package/dist/cjs/CmpStr.cjs +57 -36
- package/dist/cjs/CmpStrAsync.cjs +30 -24
- package/dist/cjs/index.cjs +1 -2
- package/dist/cjs/metric/Cosine.cjs +1 -2
- package/dist/cjs/metric/DamerauLevenshtein.cjs +1 -2
- package/dist/cjs/metric/DiceSorensen.cjs +1 -2
- package/dist/cjs/metric/Hamming.cjs +5 -4
- package/dist/cjs/metric/Jaccard.cjs +1 -2
- package/dist/cjs/metric/JaroWinkler.cjs +1 -2
- package/dist/cjs/metric/LCS.cjs +1 -2
- package/dist/cjs/metric/Levenshtein.cjs +1 -2
- package/dist/cjs/metric/Metric.cjs +57 -38
- package/dist/cjs/metric/NeedlemanWunsch.cjs +1 -2
- package/dist/cjs/metric/QGram.cjs +1 -2
- package/dist/cjs/metric/SmithWaterman.cjs +1 -2
- package/dist/cjs/phonetic/Caverphone.cjs +1 -2
- package/dist/cjs/phonetic/Cologne.cjs +1 -2
- package/dist/cjs/phonetic/Metaphone.cjs +1 -2
- package/dist/cjs/phonetic/Phonetic.cjs +55 -35
- package/dist/cjs/phonetic/Soundex.cjs +1 -2
- package/dist/cjs/root.cjs +3 -2
- package/dist/cjs/utils/DeepMerge.cjs +10 -5
- package/dist/cjs/utils/DiffChecker.cjs +1 -2
- package/dist/cjs/utils/Errors.cjs +103 -0
- package/dist/cjs/utils/Filter.cjs +52 -27
- package/dist/cjs/utils/HashTable.cjs +1 -2
- package/dist/cjs/utils/Normalizer.cjs +54 -34
- package/dist/cjs/utils/Pool.cjs +42 -18
- package/dist/cjs/utils/Profiler.cjs +1 -2
- package/dist/cjs/utils/Registry.cjs +46 -22
- package/dist/cjs/utils/StructuredData.cjs +13 -5
- package/dist/cjs/utils/TextAnalyzer.cjs +1 -2
- package/dist/esm/CmpStr.mjs +55 -32
- package/dist/esm/CmpStrAsync.mjs +26 -20
- package/dist/esm/index.mjs +1 -2
- package/dist/esm/metric/Cosine.mjs +1 -2
- package/dist/esm/metric/DamerauLevenshtein.mjs +1 -2
- package/dist/esm/metric/DiceSorensen.mjs +1 -2
- package/dist/esm/metric/Hamming.mjs +5 -4
- package/dist/esm/metric/Jaccard.mjs +1 -2
- package/dist/esm/metric/JaroWinkler.mjs +1 -2
- package/dist/esm/metric/LCS.mjs +1 -2
- package/dist/esm/metric/Levenshtein.mjs +1 -2
- package/dist/esm/metric/Metric.mjs +59 -38
- package/dist/esm/metric/NeedlemanWunsch.mjs +1 -2
- package/dist/esm/metric/QGram.mjs +1 -2
- package/dist/esm/metric/SmithWaterman.mjs +1 -2
- package/dist/esm/phonetic/Caverphone.mjs +1 -2
- package/dist/esm/phonetic/Cologne.mjs +1 -2
- package/dist/esm/phonetic/Metaphone.mjs +1 -2
- package/dist/esm/phonetic/Phonetic.mjs +55 -35
- package/dist/esm/phonetic/Soundex.mjs +1 -2
- package/dist/esm/root.mjs +3 -2
- package/dist/esm/utils/DeepMerge.mjs +10 -5
- package/dist/esm/utils/DiffChecker.mjs +1 -2
- package/dist/esm/utils/Errors.mjs +103 -0
- package/dist/esm/utils/Filter.mjs +52 -27
- package/dist/esm/utils/HashTable.mjs +1 -2
- package/dist/esm/utils/Normalizer.mjs +54 -34
- package/dist/esm/utils/Pool.mjs +38 -18
- package/dist/esm/utils/Profiler.mjs +1 -2
- package/dist/esm/utils/Registry.mjs +46 -22
- package/dist/esm/utils/StructuredData.mjs +13 -5
- package/dist/esm/utils/TextAnalyzer.mjs +1 -2
- package/dist/types/CmpStr.d.ts +5 -1
- package/dist/types/CmpStrAsync.d.ts +1 -0
- package/dist/types/index.d.ts +3 -2
- package/dist/types/metric/Metric.d.ts +6 -5
- package/dist/types/phonetic/Phonetic.d.ts +3 -1
- package/dist/types/root.d.ts +2 -1
- package/dist/types/utils/DeepMerge.d.ts +1 -1
- package/dist/types/utils/Errors.d.ts +137 -0
- package/dist/types/utils/Filter.d.ts +4 -0
- package/dist/types/utils/Normalizer.d.ts +3 -0
- package/dist/types/utils/Pool.d.ts +2 -0
- package/dist/types/utils/Registry.d.ts +3 -3
- package/dist/types/utils/StructuredData.d.ts +3 -1
- package/dist/types/utils/Types.d.ts +26 -0
- package/package.json +7 -7
- package/dist/CmpStr.esm.js.map +0 -1
- package/dist/CmpStr.esm.min.js.map +0 -1
- package/dist/CmpStr.umd.js.map +0 -1
- package/dist/CmpStr.umd.min.js.map +0 -1
- package/dist/cjs/CmpStr.cjs.map +0 -1
- package/dist/cjs/CmpStrAsync.cjs.map +0 -1
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/metric/Cosine.cjs.map +0 -1
- package/dist/cjs/metric/DamerauLevenshtein.cjs.map +0 -1
- package/dist/cjs/metric/DiceSorensen.cjs.map +0 -1
- package/dist/cjs/metric/Hamming.cjs.map +0 -1
- package/dist/cjs/metric/Jaccard.cjs.map +0 -1
- package/dist/cjs/metric/JaroWinkler.cjs.map +0 -1
- package/dist/cjs/metric/LCS.cjs.map +0 -1
- package/dist/cjs/metric/Levenshtein.cjs.map +0 -1
- package/dist/cjs/metric/Metric.cjs.map +0 -1
- package/dist/cjs/metric/NeedlemanWunsch.cjs.map +0 -1
- package/dist/cjs/metric/QGram.cjs.map +0 -1
- package/dist/cjs/metric/SmithWaterman.cjs.map +0 -1
- package/dist/cjs/phonetic/Caverphone.cjs.map +0 -1
- package/dist/cjs/phonetic/Cologne.cjs.map +0 -1
- package/dist/cjs/phonetic/Metaphone.cjs.map +0 -1
- package/dist/cjs/phonetic/Phonetic.cjs.map +0 -1
- package/dist/cjs/phonetic/Soundex.cjs.map +0 -1
- package/dist/cjs/root.cjs.map +0 -1
- package/dist/cjs/utils/DeepMerge.cjs.map +0 -1
- package/dist/cjs/utils/DiffChecker.cjs.map +0 -1
- package/dist/cjs/utils/Filter.cjs.map +0 -1
- package/dist/cjs/utils/HashTable.cjs.map +0 -1
- package/dist/cjs/utils/Normalizer.cjs.map +0 -1
- package/dist/cjs/utils/Pool.cjs.map +0 -1
- package/dist/cjs/utils/Profiler.cjs.map +0 -1
- package/dist/cjs/utils/Registry.cjs.map +0 -1
- package/dist/cjs/utils/StructuredData.cjs.map +0 -1
- package/dist/cjs/utils/TextAnalyzer.cjs.map +0 -1
- package/dist/esm/CmpStr.mjs.map +0 -1
- package/dist/esm/CmpStrAsync.mjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/metric/Cosine.mjs.map +0 -1
- package/dist/esm/metric/DamerauLevenshtein.mjs.map +0 -1
- package/dist/esm/metric/DiceSorensen.mjs.map +0 -1
- package/dist/esm/metric/Hamming.mjs.map +0 -1
- package/dist/esm/metric/Jaccard.mjs.map +0 -1
- package/dist/esm/metric/JaroWinkler.mjs.map +0 -1
- package/dist/esm/metric/LCS.mjs.map +0 -1
- package/dist/esm/metric/Levenshtein.mjs.map +0 -1
- package/dist/esm/metric/Metric.mjs.map +0 -1
- package/dist/esm/metric/NeedlemanWunsch.mjs.map +0 -1
- package/dist/esm/metric/QGram.mjs.map +0 -1
- package/dist/esm/metric/SmithWaterman.mjs.map +0 -1
- package/dist/esm/phonetic/Caverphone.mjs.map +0 -1
- package/dist/esm/phonetic/Cologne.mjs.map +0 -1
- package/dist/esm/phonetic/Metaphone.mjs.map +0 -1
- package/dist/esm/phonetic/Phonetic.mjs.map +0 -1
- package/dist/esm/phonetic/Soundex.mjs.map +0 -1
- package/dist/esm/root.mjs.map +0 -1
- package/dist/esm/utils/DeepMerge.mjs.map +0 -1
- package/dist/esm/utils/DiffChecker.mjs.map +0 -1
- package/dist/esm/utils/Filter.mjs.map +0 -1
- package/dist/esm/utils/HashTable.mjs.map +0 -1
- package/dist/esm/utils/Normalizer.mjs.map +0 -1
- package/dist/esm/utils/Pool.mjs.map +0 -1
- package/dist/esm/utils/Profiler.mjs.map +0 -1
- package/dist/esm/utils/Registry.mjs.map +0 -1
- package/dist/esm/utils/StructuredData.mjs.map +0 -1
- package/dist/esm/utils/TextAnalyzer.mjs.map +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
// CmpStr v3.2.
|
|
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
|
-
|
|
28
|
-
|
|
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
|
|
45
|
+
throw new Errors.CmpStrInternalError(
|
|
46
|
+
`Method compute() must be overridden in a subclass`
|
|
47
|
+
);
|
|
42
48
|
}
|
|
43
49
|
runSingle(i, j) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
|
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
|
|
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
|
|
174
|
+
throw new Errors.CmpStrInternalError(
|
|
175
|
+
`Unsupported async mode <${mode}>`
|
|
176
|
+
);
|
|
159
177
|
}
|
|
160
178
|
}
|
|
161
179
|
getMetricName = () => this.metric;
|
|
162
180
|
getResults() {
|
|
163
|
-
|
|
164
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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)
|
|
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
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
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
|
|
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.
|
|
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.
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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,14 +67,26 @@ class Filter {
|
|
|
53
67
|
return out;
|
|
54
68
|
}
|
|
55
69
|
static apply(hook, input) {
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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) {
|
|
66
92
|
Filter.pipeline.clear();
|
|
@@ -73,4 +99,3 @@ class Filter {
|
|
|
73
99
|
}
|
|
74
100
|
|
|
75
101
|
exports.Filter = Filter;
|
|
76
|
-
//# sourceMappingURL=Filter.cjs.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// CmpStr v3.2.
|
|
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
|