cmpstr 3.0.4 → 3.1.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.
- package/README.md +2 -1
- package/dist/CmpStr.esm.js +345 -90
- package/dist/CmpStr.esm.js.map +1 -1
- package/dist/CmpStr.esm.min.js +3 -3
- package/dist/CmpStr.esm.min.js.map +1 -1
- package/dist/CmpStr.umd.js +342 -89
- package/dist/CmpStr.umd.js.map +1 -1
- package/dist/CmpStr.umd.min.js +3 -3
- package/dist/CmpStr.umd.min.js.map +1 -1
- package/dist/cjs/CmpStr.cjs +41 -1
- package/dist/cjs/CmpStr.cjs.map +1 -1
- package/dist/cjs/CmpStrAsync.cjs +38 -1
- package/dist/cjs/CmpStrAsync.cjs.map +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/metric/Cosine.cjs +1 -1
- package/dist/cjs/metric/DamerauLevenshtein.cjs +1 -1
- package/dist/cjs/metric/DiceSorensen.cjs +1 -1
- package/dist/cjs/metric/Hamming.cjs +1 -1
- package/dist/cjs/metric/Jaccard.cjs +1 -1
- package/dist/cjs/metric/JaroWinkler.cjs +1 -1
- package/dist/cjs/metric/LCS.cjs +1 -1
- package/dist/cjs/metric/Levenshtein.cjs +1 -1
- package/dist/cjs/metric/Metric.cjs +1 -1
- package/dist/cjs/metric/NeedlemanWunsch.cjs +1 -1
- package/dist/cjs/metric/SmithWaterman.cjs +1 -1
- package/dist/cjs/metric/qGram.cjs +1 -1
- package/dist/cjs/phonetic/Caverphone.cjs +1 -1
- package/dist/cjs/phonetic/Cologne.cjs +1 -1
- package/dist/cjs/phonetic/Metaphone.cjs +1 -1
- package/dist/cjs/phonetic/Phonetic.cjs +1 -1
- package/dist/cjs/phonetic/Soundex.cjs +1 -1
- package/dist/cjs/root.cjs +13 -1
- package/dist/cjs/root.cjs.map +1 -1
- package/dist/cjs/utils/DeepMerge.cjs +1 -1
- package/dist/cjs/utils/DiffChecker.cjs +1 -1
- package/dist/cjs/utils/Filter.cjs +1 -1
- package/dist/cjs/utils/HashTable.cjs +1 -1
- package/dist/cjs/utils/Normalizer.cjs +1 -1
- package/dist/cjs/utils/Pool.cjs +10 -1
- package/dist/cjs/utils/Pool.cjs.map +1 -1
- package/dist/cjs/utils/Profiler.cjs +1 -1
- package/dist/cjs/utils/Registry.cjs +1 -1
- package/dist/cjs/utils/StructuredData.cjs +145 -0
- package/dist/cjs/utils/StructuredData.cjs.map +1 -0
- package/dist/cjs/utils/TextAnalyzer.cjs +1 -1
- package/dist/esm/CmpStr.mjs +41 -1
- package/dist/esm/CmpStr.mjs.map +1 -1
- package/dist/esm/CmpStrAsync.mjs +38 -1
- package/dist/esm/CmpStrAsync.mjs.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/metric/Cosine.mjs +1 -1
- package/dist/esm/metric/DamerauLevenshtein.mjs +1 -1
- package/dist/esm/metric/DiceSorensen.mjs +1 -1
- package/dist/esm/metric/Hamming.mjs +1 -1
- package/dist/esm/metric/Jaccard.mjs +1 -1
- package/dist/esm/metric/JaroWinkler.mjs +1 -1
- package/dist/esm/metric/LCS.mjs +1 -1
- package/dist/esm/metric/Levenshtein.mjs +1 -1
- package/dist/esm/metric/Metric.mjs +1 -1
- package/dist/esm/metric/NeedlemanWunsch.mjs +1 -1
- package/dist/esm/metric/SmithWaterman.mjs +1 -1
- package/dist/esm/metric/qGram.mjs +1 -1
- package/dist/esm/phonetic/Caverphone.mjs +1 -1
- package/dist/esm/phonetic/Cologne.mjs +1 -1
- package/dist/esm/phonetic/Metaphone.mjs +1 -1
- package/dist/esm/phonetic/Phonetic.mjs +1 -1
- package/dist/esm/phonetic/Soundex.mjs +1 -1
- package/dist/esm/root.mjs +7 -1
- package/dist/esm/root.mjs.map +1 -1
- package/dist/esm/utils/DeepMerge.mjs +1 -1
- package/dist/esm/utils/DiffChecker.mjs +1 -1
- package/dist/esm/utils/Filter.mjs +1 -1
- package/dist/esm/utils/HashTable.mjs +1 -1
- package/dist/esm/utils/Normalizer.mjs +1 -1
- package/dist/esm/utils/Pool.mjs +10 -1
- package/dist/esm/utils/Pool.mjs.map +1 -1
- package/dist/esm/utils/Profiler.mjs +1 -1
- package/dist/esm/utils/Registry.mjs +1 -1
- package/dist/esm/utils/StructuredData.mjs +143 -0
- package/dist/esm/utils/StructuredData.mjs.map +1 -0
- package/dist/esm/utils/TextAnalyzer.mjs +1 -1
- package/dist/types/CmpStr.d.ts +90 -8
- package/dist/types/CmpStrAsync.d.ts +82 -8
- package/dist/types/index.d.ts +3 -2
- package/dist/types/root.d.ts +3 -2
- package/dist/types/utils/Pool.d.ts +2 -2
- package/dist/types/utils/StructuredData.d.ts +162 -0
- package/dist/types/utils/Types.d.ts +35 -1
- package/package.json +52 -17
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://www.typescriptlang.org)
|
|
6
6
|
[](https://npmjs.com/package/cmpstr)
|
|
7
7
|
[](https://bundlephobia.com/package/cmpstr)
|
|
8
|
-
[](https://
|
|
8
|
+
[](https://npm-stat.com/charts.html?package=cmpstr)
|
|
9
9
|
[](https://github.com/komed3/cmpstr/actions/workflows/build.yml)
|
|
10
10
|
[](https://github.com/komed3/cmpstr/wiki/Installation-&-Setup#import-in-your-project)
|
|
11
11
|
[](https://github.com/komed3/cmpstr/wiki/Installation-&-Setup#browser)
|
|
@@ -24,6 +24,7 @@ Originally launched in 2023 with a minimal feature set, the library was redesign
|
|
|
24
24
|
- Normalization and filtering pipeline for consistent input processing
|
|
25
25
|
- Single, batch and pairwise comparisons with structured, type-safe results
|
|
26
26
|
- Phonetic-aware search and comparison
|
|
27
|
+
- Structured data comparison by extracting object properties
|
|
27
28
|
- Utilities for text structure and readability analysis (e.g., syllables, word statistics)
|
|
28
29
|
- Diffing tools with CLI-friendly formatting
|
|
29
30
|
- TypeScript-native with full type declarations and extensibility
|
package/dist/CmpStr.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* CmpStr v3.0
|
|
2
|
+
* CmpStr v3.1.0 build-76aadb9-260117
|
|
3
3
|
* This is a lightweight, fast and well performing library for calculating string similarity.
|
|
4
|
-
* (c) 2023-
|
|
4
|
+
* (c) 2023-2026 Paul Köhler @komed3 / MIT License
|
|
5
5
|
* Visit https://github.com/komed3/cmpstr and https://npmjs.org/package/cmpstr
|
|
6
6
|
*/
|
|
7
7
|
const parse = (p) =>
|
|
@@ -12,6 +12,12 @@ const parse = (p) =>
|
|
|
12
12
|
function get(t, path, fallback) {
|
|
13
13
|
return parse(path).reduce((o, k) => o?.[k] ?? fallback, t);
|
|
14
14
|
}
|
|
15
|
+
function has(t, path) {
|
|
16
|
+
return (
|
|
17
|
+
parse(path).reduce((o, k) => (o && k in o ? o[k] : undefined), t) !==
|
|
18
|
+
undefined
|
|
19
|
+
);
|
|
20
|
+
}
|
|
15
21
|
function set(t, path, value) {
|
|
16
22
|
if (path === '') return value;
|
|
17
23
|
const [k, ...r] = parse(path);
|
|
@@ -66,6 +72,15 @@ function rmv(t, path, preserveEmpty = false) {
|
|
|
66
72
|
return t;
|
|
67
73
|
}
|
|
68
74
|
|
|
75
|
+
var DeepMerge = /*#__PURE__*/ Object.freeze({
|
|
76
|
+
__proto__: null,
|
|
77
|
+
get: get,
|
|
78
|
+
has: has,
|
|
79
|
+
merge: merge,
|
|
80
|
+
rmv: rmv,
|
|
81
|
+
set: set
|
|
82
|
+
});
|
|
83
|
+
|
|
69
84
|
class Profiler {
|
|
70
85
|
static ENV;
|
|
71
86
|
static instance;
|
|
@@ -830,6 +845,240 @@ class Filter {
|
|
|
830
845
|
}
|
|
831
846
|
}
|
|
832
847
|
|
|
848
|
+
class RingPool {
|
|
849
|
+
maxSize;
|
|
850
|
+
buffers = [];
|
|
851
|
+
pointer = 0;
|
|
852
|
+
constructor(maxSize) {
|
|
853
|
+
this.maxSize = maxSize;
|
|
854
|
+
}
|
|
855
|
+
acquire(minSize, allowOversize) {
|
|
856
|
+
const len = this.buffers.length;
|
|
857
|
+
for (let i = 0; i < len; i++) {
|
|
858
|
+
const idx = (this.pointer + i) % len;
|
|
859
|
+
const item = this.buffers[idx];
|
|
860
|
+
if (item.size >= minSize) {
|
|
861
|
+
this.pointer = (idx + 1) % len;
|
|
862
|
+
return allowOversize || item.size === minSize ? item : null;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
return null;
|
|
866
|
+
}
|
|
867
|
+
release(item) {
|
|
868
|
+
if (this.buffers.length < this.maxSize) {
|
|
869
|
+
this.buffers.push(item);
|
|
870
|
+
} else {
|
|
871
|
+
this.buffers[this.pointer] = item;
|
|
872
|
+
this.pointer = (this.pointer + 1) % this.maxSize;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
clear() {
|
|
876
|
+
this.buffers = [];
|
|
877
|
+
this.pointer = 0;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
class Pool {
|
|
881
|
+
static CONFIG = {
|
|
882
|
+
uint16: {
|
|
883
|
+
type: 'uint16',
|
|
884
|
+
maxSize: 32,
|
|
885
|
+
maxItemSize: 2048,
|
|
886
|
+
allowOversize: true
|
|
887
|
+
},
|
|
888
|
+
'number[]': {
|
|
889
|
+
type: 'number[]',
|
|
890
|
+
maxSize: 16,
|
|
891
|
+
maxItemSize: 1024,
|
|
892
|
+
allowOversize: false
|
|
893
|
+
},
|
|
894
|
+
'string[]': {
|
|
895
|
+
type: 'string[]',
|
|
896
|
+
maxSize: 4,
|
|
897
|
+
maxItemSize: 1024,
|
|
898
|
+
allowOversize: false
|
|
899
|
+
},
|
|
900
|
+
set: { type: 'set', maxSize: 8, maxItemSize: 0, allowOversize: false },
|
|
901
|
+
map: { type: 'map', maxSize: 8, maxItemSize: 0, allowOversize: false }
|
|
902
|
+
};
|
|
903
|
+
static POOLS = {
|
|
904
|
+
uint16: new RingPool(32),
|
|
905
|
+
'number[]': new RingPool(16),
|
|
906
|
+
'string[]': new RingPool(4),
|
|
907
|
+
set: new RingPool(8),
|
|
908
|
+
map: new RingPool(8)
|
|
909
|
+
};
|
|
910
|
+
static allocate(type, size) {
|
|
911
|
+
switch (type) {
|
|
912
|
+
case 'uint16':
|
|
913
|
+
return new Uint16Array(size);
|
|
914
|
+
case 'number[]':
|
|
915
|
+
return new Array(size).fill(0);
|
|
916
|
+
case 'string[]':
|
|
917
|
+
return new Array(size);
|
|
918
|
+
case 'set':
|
|
919
|
+
return new Set();
|
|
920
|
+
case 'map':
|
|
921
|
+
return new Map();
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
static acquire(type, size) {
|
|
925
|
+
const CONFIG = this.CONFIG[type];
|
|
926
|
+
if (size > CONFIG.maxItemSize) return this.allocate(type, size);
|
|
927
|
+
const item = this.POOLS[type].acquire(size, CONFIG.allowOversize);
|
|
928
|
+
if (item) {
|
|
929
|
+
return type === 'uint16' ? item.buffer.subarray(0, size) : item.buffer;
|
|
930
|
+
}
|
|
931
|
+
return this.allocate(type, size);
|
|
932
|
+
}
|
|
933
|
+
static acquireMany(type, sizes) {
|
|
934
|
+
return sizes.map((size) => this.acquire(type, size));
|
|
935
|
+
}
|
|
936
|
+
static release(type, buffer, size) {
|
|
937
|
+
const CONFIG = this.CONFIG[type];
|
|
938
|
+
if (size <= CONFIG.maxItemSize) {
|
|
939
|
+
this.POOLS[type].release({ buffer, size });
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
class StructuredData {
|
|
945
|
+
static create(data, key) {
|
|
946
|
+
return new StructuredData(data, key);
|
|
947
|
+
}
|
|
948
|
+
data;
|
|
949
|
+
key;
|
|
950
|
+
constructor(data, key) {
|
|
951
|
+
this.data = data;
|
|
952
|
+
this.key = key;
|
|
953
|
+
}
|
|
954
|
+
extractFrom(arr, key) {
|
|
955
|
+
const result = Pool.acquire('string[]', arr.length);
|
|
956
|
+
for (let i = 0; i < arr.length; i++) {
|
|
957
|
+
const val = arr[i][key];
|
|
958
|
+
result[i] = typeof val === 'string' ? val : String(val ?? '');
|
|
959
|
+
}
|
|
960
|
+
return result;
|
|
961
|
+
}
|
|
962
|
+
extract() {
|
|
963
|
+
return this.extractFrom(this.data, this.key);
|
|
964
|
+
}
|
|
965
|
+
isMetricResult(v) {
|
|
966
|
+
return (
|
|
967
|
+
typeof v === 'object' && v !== null && 'a' in v && 'b' in v && 'res' in v
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
isCmpStrResult(v) {
|
|
971
|
+
return (
|
|
972
|
+
typeof v === 'object' &&
|
|
973
|
+
v !== null &&
|
|
974
|
+
'source' in v &&
|
|
975
|
+
'target' in v &&
|
|
976
|
+
'match' in v
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
normalizeResults(results) {
|
|
980
|
+
if (!Array.isArray(results) || results.length === 0) return [];
|
|
981
|
+
const first = results[0];
|
|
982
|
+
if (this.isMetricResult(first)) return results;
|
|
983
|
+
if (this.isCmpStrResult(first))
|
|
984
|
+
return results.map((r) => ({
|
|
985
|
+
metric: 'unknown',
|
|
986
|
+
a: r.source,
|
|
987
|
+
b: r.target,
|
|
988
|
+
res: r.match,
|
|
989
|
+
raw: r.raw
|
|
990
|
+
}));
|
|
991
|
+
return [];
|
|
992
|
+
}
|
|
993
|
+
rebuild(results, sourceData, extractedStrings, removeZero, objectsOnly) {
|
|
994
|
+
const output = new Array(results.length);
|
|
995
|
+
let out = 0;
|
|
996
|
+
for (let i = 0; i < results.length; i++) {
|
|
997
|
+
const result = results[i];
|
|
998
|
+
if (removeZero && result.res === 0) continue;
|
|
999
|
+
let dataIndex =
|
|
1000
|
+
result.b && extractedStrings.length
|
|
1001
|
+
? extractedStrings.indexOf(result.b)
|
|
1002
|
+
: i;
|
|
1003
|
+
if (dataIndex < 0 || dataIndex >= sourceData.length) dataIndex = i;
|
|
1004
|
+
const sourceObj = sourceData[dataIndex];
|
|
1005
|
+
if (objectsOnly) output[out++] = sourceObj;
|
|
1006
|
+
else
|
|
1007
|
+
output[out++] = {
|
|
1008
|
+
obj: sourceObj,
|
|
1009
|
+
key: this.key,
|
|
1010
|
+
result: {
|
|
1011
|
+
source: result.a,
|
|
1012
|
+
target: extractedStrings[dataIndex] || result.b,
|
|
1013
|
+
match: result.res
|
|
1014
|
+
},
|
|
1015
|
+
...(result.raw ? { raw: result.raw } : null)
|
|
1016
|
+
};
|
|
1017
|
+
}
|
|
1018
|
+
output.length = out;
|
|
1019
|
+
return output;
|
|
1020
|
+
}
|
|
1021
|
+
sort(results, sort) {
|
|
1022
|
+
if (!sort || results.length <= 1) return results;
|
|
1023
|
+
const asc = sort === 'asc';
|
|
1024
|
+
return results.sort((a, b) => (asc ? a.res - b.res : b.res - a.res));
|
|
1025
|
+
}
|
|
1026
|
+
performLookup(fn, extractedStrings, opt) {
|
|
1027
|
+
return this.rebuild(
|
|
1028
|
+
this.sort(this.normalizeResults(fn()), opt?.sort),
|
|
1029
|
+
this.data,
|
|
1030
|
+
extractedStrings,
|
|
1031
|
+
opt?.removeZero,
|
|
1032
|
+
opt?.objectsOnly
|
|
1033
|
+
);
|
|
1034
|
+
}
|
|
1035
|
+
async performLookupAsync(fn, extractedStrings, opt) {
|
|
1036
|
+
return this.rebuild(
|
|
1037
|
+
this.sort(this.normalizeResults(await fn()), opt?.sort),
|
|
1038
|
+
this.data,
|
|
1039
|
+
extractedStrings,
|
|
1040
|
+
opt?.removeZero,
|
|
1041
|
+
opt?.objectsOnly
|
|
1042
|
+
);
|
|
1043
|
+
}
|
|
1044
|
+
lookup(fn, query, opt) {
|
|
1045
|
+
const b = this.extract();
|
|
1046
|
+
try {
|
|
1047
|
+
return this.performLookup(() => fn(query, b, opt), b, opt);
|
|
1048
|
+
} finally {
|
|
1049
|
+
Pool.release('string[]', b, b.length);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
lookupPairs(fn, other, otherKey, opt) {
|
|
1053
|
+
const a = this.extract();
|
|
1054
|
+
const b = this.extractFrom(other, otherKey);
|
|
1055
|
+
try {
|
|
1056
|
+
return this.performLookup(() => fn(a, b, opt), a, opt);
|
|
1057
|
+
} finally {
|
|
1058
|
+
Pool.release('string[]', a, a.length);
|
|
1059
|
+
Pool.release('string[]', b, b.length);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
async lookupAsync(fn, query, opt) {
|
|
1063
|
+
const b = this.extract();
|
|
1064
|
+
try {
|
|
1065
|
+
return await this.performLookupAsync(() => fn(query, b, opt), b, opt);
|
|
1066
|
+
} finally {
|
|
1067
|
+
Pool.release('string[]', b, b.length);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
async lookupPairsAsync(fn, other, otherKey, opt) {
|
|
1071
|
+
const a = this.extract();
|
|
1072
|
+
const b = this.extractFrom(other, otherKey);
|
|
1073
|
+
try {
|
|
1074
|
+
return await this.performLookupAsync(() => fn(a, b, opt), a, opt);
|
|
1075
|
+
} finally {
|
|
1076
|
+
Pool.release('string[]', a, a.length);
|
|
1077
|
+
Pool.release('string[]', b, b.length);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
|
|
833
1082
|
const registry = Object.create(null);
|
|
834
1083
|
const factory = Object.create(null);
|
|
835
1084
|
function Registry(reg, ctor) {
|
|
@@ -1056,93 +1305,6 @@ class Metric {
|
|
|
1056
1305
|
}
|
|
1057
1306
|
const MetricRegistry = Registry('metric', Metric);
|
|
1058
1307
|
|
|
1059
|
-
class RingPool {
|
|
1060
|
-
maxSize;
|
|
1061
|
-
buffers = [];
|
|
1062
|
-
pointer = 0;
|
|
1063
|
-
constructor(maxSize) {
|
|
1064
|
-
this.maxSize = maxSize;
|
|
1065
|
-
}
|
|
1066
|
-
acquire(minSize, allowOversize) {
|
|
1067
|
-
const len = this.buffers.length;
|
|
1068
|
-
for (let i = 0; i < len; i++) {
|
|
1069
|
-
const idx = (this.pointer + i) % len;
|
|
1070
|
-
const item = this.buffers[idx];
|
|
1071
|
-
if (item.size >= minSize) {
|
|
1072
|
-
this.pointer = (idx + 1) % len;
|
|
1073
|
-
return allowOversize || item.size === minSize ? item : null;
|
|
1074
|
-
}
|
|
1075
|
-
}
|
|
1076
|
-
return null;
|
|
1077
|
-
}
|
|
1078
|
-
release(item) {
|
|
1079
|
-
if (this.buffers.length < this.maxSize) {
|
|
1080
|
-
this.buffers.push(item);
|
|
1081
|
-
} else {
|
|
1082
|
-
this.buffers[this.pointer] = item;
|
|
1083
|
-
this.pointer = (this.pointer + 1) % this.maxSize;
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
clear() {
|
|
1087
|
-
this.buffers = [];
|
|
1088
|
-
this.pointer = 0;
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
class Pool {
|
|
1092
|
-
static CONFIG = {
|
|
1093
|
-
uint16: {
|
|
1094
|
-
type: 'uint16',
|
|
1095
|
-
maxSize: 32,
|
|
1096
|
-
maxItemSize: 2048,
|
|
1097
|
-
allowOversize: true
|
|
1098
|
-
},
|
|
1099
|
-
'number[]': {
|
|
1100
|
-
type: 'number[]',
|
|
1101
|
-
maxSize: 16,
|
|
1102
|
-
maxItemSize: 1024,
|
|
1103
|
-
allowOversize: false
|
|
1104
|
-
},
|
|
1105
|
-
set: { type: 'set', maxSize: 8, maxItemSize: 0, allowOversize: false },
|
|
1106
|
-
map: { type: 'map', maxSize: 8, maxItemSize: 0, allowOversize: false }
|
|
1107
|
-
};
|
|
1108
|
-
static POOLS = {
|
|
1109
|
-
uint16: new RingPool(32),
|
|
1110
|
-
'number[]': new RingPool(16),
|
|
1111
|
-
set: new RingPool(8),
|
|
1112
|
-
map: new RingPool(8)
|
|
1113
|
-
};
|
|
1114
|
-
static allocate(type, size) {
|
|
1115
|
-
switch (type) {
|
|
1116
|
-
case 'uint16':
|
|
1117
|
-
return new Uint16Array(size);
|
|
1118
|
-
case 'number[]':
|
|
1119
|
-
return new Array(size).fill(0);
|
|
1120
|
-
case 'set':
|
|
1121
|
-
return new Set();
|
|
1122
|
-
case 'map':
|
|
1123
|
-
return new Map();
|
|
1124
|
-
}
|
|
1125
|
-
}
|
|
1126
|
-
static acquire(type, size) {
|
|
1127
|
-
const CONFIG = this.CONFIG[type];
|
|
1128
|
-
if (size > CONFIG.maxItemSize) return this.allocate(type, size);
|
|
1129
|
-
const item = this.POOLS[type].acquire(size, CONFIG.allowOversize);
|
|
1130
|
-
if (item) {
|
|
1131
|
-
return type === 'uint16' ? item.buffer.subarray(0, size) : item.buffer;
|
|
1132
|
-
}
|
|
1133
|
-
return this.allocate(type, size);
|
|
1134
|
-
}
|
|
1135
|
-
static acquireMany(type, sizes) {
|
|
1136
|
-
return sizes.map((size) => this.acquire(type, size));
|
|
1137
|
-
}
|
|
1138
|
-
static release(type, buffer, size) {
|
|
1139
|
-
const CONFIG = this.CONFIG[type];
|
|
1140
|
-
if (size <= CONFIG.maxItemSize) {
|
|
1141
|
-
this.POOLS[type].release({ buffer, size });
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
|
|
1146
1308
|
class CosineSimilarity extends Metric {
|
|
1147
1309
|
constructor(a, b, opt = {}) {
|
|
1148
1310
|
super('cosine', a, b, opt, true);
|
|
@@ -2127,6 +2289,9 @@ class CmpStr {
|
|
|
2127
2289
|
? input.map((s) => phonetic.getIndex(s).join(delimiter))
|
|
2128
2290
|
: phonetic.getIndex(input).join(delimiter);
|
|
2129
2291
|
}
|
|
2292
|
+
structured(data, key) {
|
|
2293
|
+
return StructuredData.create(data, key);
|
|
2294
|
+
}
|
|
2130
2295
|
compute(a, b, opt, mode, raw, skip) {
|
|
2131
2296
|
const resolved = this.resolveOptions(opt);
|
|
2132
2297
|
this.assert('metric', resolved.metric);
|
|
@@ -2260,6 +2425,42 @@ class CmpStr {
|
|
|
2260
2425
|
const { algo: a, opt: o } = this.options.processors?.phonetic ?? {};
|
|
2261
2426
|
return this.index(input, { algo: algo ?? a, opt: opt ?? o });
|
|
2262
2427
|
}
|
|
2428
|
+
structuredLookup(query, data, key, opt) {
|
|
2429
|
+
return this.structured(data, key).lookup(
|
|
2430
|
+
(q, items, options) => this.batchTest(q, items, options),
|
|
2431
|
+
query,
|
|
2432
|
+
opt
|
|
2433
|
+
);
|
|
2434
|
+
}
|
|
2435
|
+
structuredMatch(query, data, key, threshold, opt) {
|
|
2436
|
+
return this.structured(data, key).lookup(
|
|
2437
|
+
(q, items, options) => this.match(q, items, threshold, options),
|
|
2438
|
+
query,
|
|
2439
|
+
{ ...opt, sort: 'desc' }
|
|
2440
|
+
);
|
|
2441
|
+
}
|
|
2442
|
+
structuredClosest(query, data, key, n = 1, opt) {
|
|
2443
|
+
return this.structured(data, key).lookup(
|
|
2444
|
+
(q, items, options) => this.closest(q, items, n, options),
|
|
2445
|
+
query,
|
|
2446
|
+
{ ...opt, sort: 'desc' }
|
|
2447
|
+
);
|
|
2448
|
+
}
|
|
2449
|
+
structuredFurthest(query, data, key, n = 1, opt) {
|
|
2450
|
+
return this.structured(data, key).lookup(
|
|
2451
|
+
(q, items, options) => this.furthest(q, items, n, options),
|
|
2452
|
+
query,
|
|
2453
|
+
{ ...opt, sort: 'asc' }
|
|
2454
|
+
);
|
|
2455
|
+
}
|
|
2456
|
+
structuredPairs(data, key, other, otherKey, opt) {
|
|
2457
|
+
return this.structured(data, key).lookupPairs(
|
|
2458
|
+
(items, otherItems, options) => this.pairs(items, otherItems, options),
|
|
2459
|
+
other,
|
|
2460
|
+
otherKey,
|
|
2461
|
+
opt
|
|
2462
|
+
);
|
|
2463
|
+
}
|
|
2263
2464
|
}
|
|
2264
2465
|
|
|
2265
2466
|
class CmpStrAsync extends CmpStr {
|
|
@@ -2373,7 +2574,61 @@ class CmpStrAsync extends CmpStr {
|
|
|
2373
2574
|
const { algo: a, opt: o } = this.options.processors?.phonetic ?? {};
|
|
2374
2575
|
return this.indexAsync(input, { algo: algo ?? a, opt: opt ?? o });
|
|
2375
2576
|
}
|
|
2577
|
+
async structuredLookupAsync(query, data, key, opt) {
|
|
2578
|
+
return await this.structured(data, key).lookupAsync(
|
|
2579
|
+
(q, items, options) => this.batchTestAsync(q, items, options),
|
|
2580
|
+
query,
|
|
2581
|
+
opt
|
|
2582
|
+
);
|
|
2583
|
+
}
|
|
2584
|
+
async structuredMatchAsync(query, data, key, threshold, opt) {
|
|
2585
|
+
return await this.structured(data, key).lookupAsync(
|
|
2586
|
+
(q, items, options) => this.matchAsync(q, items, threshold, options),
|
|
2587
|
+
query,
|
|
2588
|
+
{ ...opt, sort: 'desc' }
|
|
2589
|
+
);
|
|
2590
|
+
}
|
|
2591
|
+
async structuredClosestAsync(query, data, key, n = 1, opt) {
|
|
2592
|
+
return await this.structured(data, key).lookupAsync(
|
|
2593
|
+
(q, items, options) => this.closestAsync(q, items, n, options),
|
|
2594
|
+
query,
|
|
2595
|
+
{ ...opt, sort: 'desc' }
|
|
2596
|
+
);
|
|
2597
|
+
}
|
|
2598
|
+
async structuredFurthestAsync(query, data, key, n = 1, opt) {
|
|
2599
|
+
return await this.structured(data, key).lookupAsync(
|
|
2600
|
+
(q, items, options) => this.furthestAsync(q, items, n, options),
|
|
2601
|
+
query,
|
|
2602
|
+
{ ...opt, sort: 'asc' }
|
|
2603
|
+
);
|
|
2604
|
+
}
|
|
2605
|
+
async structuredPairsAsync(data, key, other, otherKey, opt) {
|
|
2606
|
+
return await this.structured(data, key).lookupPairsAsync(
|
|
2607
|
+
(items, otherItems, options) =>
|
|
2608
|
+
this.pairsAsync(items, otherItems, options),
|
|
2609
|
+
other,
|
|
2610
|
+
otherKey,
|
|
2611
|
+
opt
|
|
2612
|
+
);
|
|
2613
|
+
}
|
|
2376
2614
|
}
|
|
2377
2615
|
|
|
2378
|
-
export {
|
|
2616
|
+
export {
|
|
2617
|
+
CmpStr,
|
|
2618
|
+
CmpStrAsync,
|
|
2619
|
+
DeepMerge,
|
|
2620
|
+
DiffChecker,
|
|
2621
|
+
Filter,
|
|
2622
|
+
HashTable,
|
|
2623
|
+
Metric,
|
|
2624
|
+
MetricRegistry,
|
|
2625
|
+
Normalizer,
|
|
2626
|
+
Phonetic,
|
|
2627
|
+
PhoneticMappingRegistry,
|
|
2628
|
+
PhoneticRegistry,
|
|
2629
|
+
Pool,
|
|
2630
|
+
Profiler,
|
|
2631
|
+
StructuredData,
|
|
2632
|
+
TextAnalyzer
|
|
2633
|
+
};
|
|
2379
2634
|
//# sourceMappingURL=CmpStr.esm.js.map
|