@n8n/utils 1.17.0 → 1.18.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/dist/assert.cjs +7 -5
- package/dist/assert.cjs.map +1 -1
- package/dist/assert.d.cts +3 -1
- package/dist/assert.d.ts +3 -1
- package/dist/assert.js +8 -7
- package/dist/assert.js.map +1 -1
- package/dist/event-bus.cjs +39 -35
- package/dist/event-bus.cjs.map +1 -1
- package/dist/event-bus.d.cts +9 -9
- package/dist/event-bus.d.ts +9 -9
- package/dist/event-bus.js +40 -37
- package/dist/event-bus.js.map +1 -1
- package/dist/event-queue.cjs +36 -25
- package/dist/event-queue.cjs.map +1 -1
- package/dist/event-queue.d.cts +4 -2
- package/dist/event-queue.d.ts +4 -2
- package/dist/event-queue.js +37 -27
- package/dist/event-queue.js.map +1 -1
- package/dist/number/smartDecimal.cjs +7 -10
- package/dist/number/smartDecimal.cjs.map +1 -1
- package/dist/number/smartDecimal.d.cts +3 -1
- package/dist/number/smartDecimal.d.ts +3 -1
- package/dist/number/smartDecimal.js +8 -12
- package/dist/number/smartDecimal.js.map +1 -1
- package/dist/retry.cjs +35 -26
- package/dist/retry.cjs.map +1 -1
- package/dist/retry.d.cts +3 -1
- package/dist/retry.d.ts +3 -1
- package/dist/retry.js +36 -28
- package/dist/retry.js.map +1 -1
- package/dist/search/reRankSearchResults.cjs +15 -17
- package/dist/search/reRankSearchResults.cjs.map +1 -1
- package/dist/search/reRankSearchResults.d.cts +8 -6
- package/dist/search/reRankSearchResults.d.ts +8 -6
- package/dist/search/reRankSearchResults.js +16 -19
- package/dist/search/reRankSearchResults.js.map +1 -1
- package/dist/search/sublimeSearch.cjs +161 -186
- package/dist/search/sublimeSearch.cjs.map +1 -1
- package/dist/search/sublimeSearch.d.cts +9 -7
- package/dist/search/sublimeSearch.d.ts +9 -7
- package/dist/search/sublimeSearch.js +160 -187
- package/dist/search/sublimeSearch.js.map +1 -1
- package/dist/sort/sortByProperty.cjs +9 -8
- package/dist/sort/sortByProperty.cjs.map +1 -1
- package/dist/sort/sortByProperty.d.cts +3 -1
- package/dist/sort/sortByProperty.d.ts +3 -1
- package/dist/sort/sortByProperty.js +10 -10
- package/dist/sort/sortByProperty.js.map +1 -1
- package/dist/string/truncate.cjs +27 -27
- package/dist/string/truncate.cjs.map +1 -1
- package/dist/string/truncate.d.cts +3 -1
- package/dist/string/truncate.d.ts +3 -1
- package/dist/string/truncate.js +26 -28
- package/dist/string/truncate.js.map +1 -1
- package/package.json +8 -8
|
@@ -1,198 +1,173 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
|
|
2
|
+
//#region src/search/sublimeSearch.ts
|
|
3
|
+
const SEQUENTIAL_BONUS = 60;
|
|
4
|
+
const SEPARATOR_BONUS = 38;
|
|
5
|
+
const CAMEL_BONUS = 30;
|
|
6
|
+
const FIRST_LETTER_BONUS = 15;
|
|
7
|
+
const LEADING_LETTER_PENALTY = -20;
|
|
8
|
+
const MAX_LEADING_LETTER_PENALTY = -200;
|
|
9
|
+
const UNMATCHED_LETTER_PENALTY = -5;
|
|
10
|
+
const DEFAULT_KEYS = [{
|
|
11
|
+
key: "properties.displayName",
|
|
12
|
+
weight: 1.3
|
|
13
|
+
}, {
|
|
14
|
+
key: "properties.codex.alias",
|
|
15
|
+
weight: 1
|
|
16
|
+
}];
|
|
17
|
+
/**
|
|
18
|
+
* Returns true if each character in pattern is found sequentially within target
|
|
19
|
+
* @param {*} pattern string
|
|
20
|
+
* @param {*} target string
|
|
21
|
+
*/
|
|
13
22
|
function fuzzyMatchSimple(pattern, target) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
++strIdx;
|
|
23
|
-
}
|
|
24
|
-
return pattern.length !== 0 && target.length !== 0 && patternIdx === pattern.length;
|
|
23
|
+
let patternIdx = 0;
|
|
24
|
+
let strIdx = 0;
|
|
25
|
+
while (patternIdx < pattern.length && strIdx < target.length) {
|
|
26
|
+
if (pattern.charAt(patternIdx).toLowerCase() === target.charAt(strIdx).toLowerCase()) patternIdx++;
|
|
27
|
+
++strIdx;
|
|
28
|
+
}
|
|
29
|
+
return pattern.length !== 0 && target.length !== 0 && patternIdx === pattern.length;
|
|
25
30
|
}
|
|
26
31
|
function fuzzyMatchRecursive(pattern, target, patternCurIndex, targetCurrIndex, targetMatches, matches, maxMatches, nextMatch, recursionCount, recursionLimit) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
outScore = bestRecursiveScore;
|
|
108
|
-
return { matched: true, outScore };
|
|
109
|
-
} else if (matched) {
|
|
110
|
-
return { matched: true, outScore };
|
|
111
|
-
} else {
|
|
112
|
-
return { matched: false, outScore };
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return { matched: false, outScore };
|
|
32
|
+
let outScore = 0;
|
|
33
|
+
if (++recursionCount >= recursionLimit) return {
|
|
34
|
+
matched: false,
|
|
35
|
+
outScore
|
|
36
|
+
};
|
|
37
|
+
if (patternCurIndex === pattern.length || targetCurrIndex === target.length) return {
|
|
38
|
+
matched: false,
|
|
39
|
+
outScore
|
|
40
|
+
};
|
|
41
|
+
let recursiveMatch = false;
|
|
42
|
+
let bestRecursiveMatches = [];
|
|
43
|
+
let bestRecursiveScore = 0;
|
|
44
|
+
let firstMatch = true;
|
|
45
|
+
while (patternCurIndex < pattern.length && targetCurrIndex < target.length) {
|
|
46
|
+
if (pattern[patternCurIndex].toLowerCase() === target[targetCurrIndex].toLowerCase()) {
|
|
47
|
+
if (nextMatch >= maxMatches) return {
|
|
48
|
+
matched: false,
|
|
49
|
+
outScore
|
|
50
|
+
};
|
|
51
|
+
if (firstMatch && targetMatches) {
|
|
52
|
+
matches = [...targetMatches];
|
|
53
|
+
firstMatch = false;
|
|
54
|
+
}
|
|
55
|
+
const recursiveMatches = [];
|
|
56
|
+
const recursiveResult = fuzzyMatchRecursive(pattern, target, patternCurIndex, targetCurrIndex + 1, matches, recursiveMatches, maxMatches, nextMatch, recursionCount, recursionLimit);
|
|
57
|
+
const recursiveScore = recursiveResult.outScore;
|
|
58
|
+
if (recursiveResult.matched) {
|
|
59
|
+
if (!recursiveMatch || recursiveScore > bestRecursiveScore) {
|
|
60
|
+
bestRecursiveMatches = [...recursiveMatches];
|
|
61
|
+
bestRecursiveScore = recursiveScore;
|
|
62
|
+
}
|
|
63
|
+
recursiveMatch = true;
|
|
64
|
+
}
|
|
65
|
+
matches[nextMatch++] = targetCurrIndex;
|
|
66
|
+
++patternCurIndex;
|
|
67
|
+
}
|
|
68
|
+
++targetCurrIndex;
|
|
69
|
+
}
|
|
70
|
+
const matched = patternCurIndex === pattern.length;
|
|
71
|
+
if (matched) {
|
|
72
|
+
outScore = 100;
|
|
73
|
+
if (!target.toLowerCase().startsWith("n8n")) {
|
|
74
|
+
let penalty = LEADING_LETTER_PENALTY * matches[0];
|
|
75
|
+
penalty = penalty < MAX_LEADING_LETTER_PENALTY ? MAX_LEADING_LETTER_PENALTY : penalty;
|
|
76
|
+
outScore += penalty;
|
|
77
|
+
}
|
|
78
|
+
const unmatched = target.length - nextMatch;
|
|
79
|
+
outScore += UNMATCHED_LETTER_PENALTY * unmatched;
|
|
80
|
+
for (let i = 0; i < nextMatch; i++) {
|
|
81
|
+
const currIdx = matches[i];
|
|
82
|
+
if (i > 0) {
|
|
83
|
+
if (currIdx === matches[i - 1] + 1) outScore += SEQUENTIAL_BONUS;
|
|
84
|
+
}
|
|
85
|
+
if (currIdx > 0) {
|
|
86
|
+
const neighbor = target[currIdx - 1];
|
|
87
|
+
const curr = target[currIdx];
|
|
88
|
+
if (neighbor !== neighbor.toUpperCase() && curr !== curr.toLowerCase()) outScore += CAMEL_BONUS;
|
|
89
|
+
if (neighbor === "_" || neighbor === " ") outScore += SEPARATOR_BONUS;
|
|
90
|
+
} else outScore += FIRST_LETTER_BONUS;
|
|
91
|
+
}
|
|
92
|
+
if (recursiveMatch && (!matched || bestRecursiveScore > outScore)) {
|
|
93
|
+
matches = [...bestRecursiveMatches];
|
|
94
|
+
outScore = bestRecursiveScore;
|
|
95
|
+
return {
|
|
96
|
+
matched: true,
|
|
97
|
+
outScore
|
|
98
|
+
};
|
|
99
|
+
} else if (matched) return {
|
|
100
|
+
matched: true,
|
|
101
|
+
outScore
|
|
102
|
+
};
|
|
103
|
+
else return {
|
|
104
|
+
matched: false,
|
|
105
|
+
outScore
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
matched: false,
|
|
110
|
+
outScore
|
|
111
|
+
};
|
|
116
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Does a fuzzy search to find pattern inside a string.
|
|
115
|
+
* @param {*} pattern string pattern to search for
|
|
116
|
+
* @param {*} target string string which is being searched
|
|
117
|
+
* @returns [boolean, number] a boolean which tells if pattern was
|
|
118
|
+
* found or not and a search score
|
|
119
|
+
*/
|
|
117
120
|
function fuzzyMatch(pattern, target) {
|
|
118
|
-
|
|
119
|
-
const recursionLimit = 5;
|
|
120
|
-
const matches = [];
|
|
121
|
-
const maxMatches = 256;
|
|
122
|
-
return fuzzyMatchRecursive(
|
|
123
|
-
pattern,
|
|
124
|
-
target,
|
|
125
|
-
0,
|
|
126
|
-
0,
|
|
127
|
-
null,
|
|
128
|
-
matches,
|
|
129
|
-
maxMatches,
|
|
130
|
-
0,
|
|
131
|
-
recursionCount,
|
|
132
|
-
recursionLimit
|
|
133
|
-
);
|
|
121
|
+
return fuzzyMatchRecursive(pattern, target, 0, 0, null, [], 256, 0, 0, 5);
|
|
134
122
|
}
|
|
135
123
|
function getValue(obj, prop) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
return result;
|
|
124
|
+
if (obj.hasOwnProperty(prop)) return obj[prop];
|
|
125
|
+
const segments = prop.split(".");
|
|
126
|
+
let result = obj;
|
|
127
|
+
let i = 0;
|
|
128
|
+
while (result && i < segments.length) {
|
|
129
|
+
const key = segments[i];
|
|
130
|
+
result = result[key];
|
|
131
|
+
i++;
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
148
134
|
}
|
|
149
135
|
function sublimeSearch(filter, data, keys = DEFAULT_KEYS) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
accu.push({
|
|
183
|
-
score: itemMatch.outScore,
|
|
184
|
-
item
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
return accu;
|
|
188
|
-
}, []);
|
|
189
|
-
results.sort((a, b) => {
|
|
190
|
-
return b.score - a.score;
|
|
191
|
-
});
|
|
192
|
-
return results;
|
|
136
|
+
const results = data.reduce((accu, item) => {
|
|
137
|
+
let values = [];
|
|
138
|
+
keys.forEach(({ key, weight }) => {
|
|
139
|
+
const value = getValue(item, key);
|
|
140
|
+
if (Array.isArray(value)) values = values.concat(value.map((v) => ({
|
|
141
|
+
value: v,
|
|
142
|
+
weight
|
|
143
|
+
})));
|
|
144
|
+
else if (typeof value === "string") values.push({
|
|
145
|
+
value,
|
|
146
|
+
weight
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
const itemMatch = values.reduce((result, { value, weight }) => {
|
|
150
|
+
if (!fuzzyMatchSimple(filter, value)) return result;
|
|
151
|
+
const match = fuzzyMatch(filter, value);
|
|
152
|
+
match.outScore *= weight;
|
|
153
|
+
const { matched, outScore } = match;
|
|
154
|
+
if (!result && matched) return match;
|
|
155
|
+
if (matched && result && outScore > result.outScore) return match;
|
|
156
|
+
return result;
|
|
157
|
+
}, null);
|
|
158
|
+
if (itemMatch) accu.push({
|
|
159
|
+
score: itemMatch.outScore,
|
|
160
|
+
item
|
|
161
|
+
});
|
|
162
|
+
return accu;
|
|
163
|
+
}, []);
|
|
164
|
+
results.sort((a, b) => {
|
|
165
|
+
return b.score - a.score;
|
|
166
|
+
});
|
|
167
|
+
return results;
|
|
193
168
|
}
|
|
194
169
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
exports.
|
|
170
|
+
//#endregion
|
|
171
|
+
exports.DEFAULT_KEYS = DEFAULT_KEYS;
|
|
172
|
+
exports.sublimeSearch = sublimeSearch;
|
|
198
173
|
//# sourceMappingURL=sublimeSearch.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/n8n/n8n/packages/@n8n/utils/dist/search/sublimeSearch.cjs","../../src/search/sublimeSearch.ts"],"names":[],"mappings":"AAAA;ACKA,IAAM,iBAAA,EAAmB,EAAA;AACzB,IAAM,gBAAA,EAAkB,EAAA;AACxB,IAAM,YAAA,EAAc,EAAA;AACpB,IAAM,mBAAA,EAAqB,EAAA;AAE3B,IAAM,uBAAA,EAAyB,CAAA,EAAA;AAC/B,IAAM,2BAAA,EAA6B,CAAA,GAAA;AACnC,IAAM,yBAAA,EAA2B,CAAA,CAAA;AAE1B,IAAM,aAAA,EAAe;AAAA,EAC3B,EAAE,GAAA,EAAK,wBAAA,EAA0B,MAAA,EAAQ,IAAI,CAAA;AAAA,EAC7C,EAAE,GAAA,EAAK,wBAAA,EAA0B,MAAA,EAAQ,EAAE;AAC5C,CAAA;AAOA,SAAS,gBAAA,CAAiB,OAAA,EAAiB,MAAA,EAAyB;AACnE,EAAA,IAAI,WAAA,EAAa,CAAA;AACjB,EAAA,IAAI,OAAA,EAAS,CAAA;AAEb,EAAA,MAAA,CAAO,WAAA,EAAa,OAAA,CAAQ,OAAA,GAAU,OAAA,EAAS,MAAA,CAAO,MAAA,EAAQ;AAC7D,IAAA,MAAM,YAAA,EAAc,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G;AACrC,UAAA,OAAO,MAAA;AAAA,QACR;AAEA,QAAA,MAAM,MAAA,EAAQ,UAAA,CAAW,MAAA,EAAQ,KAAK,CAAA;AACtC,QAAA,KAAA,CAAM,SAAA,GAAY,MAAA;AAElB,QAAA,MAAM,EAAE,OAAA,EAAS,SAAS,EAAA,EAAI,KAAA;AAC9B,QAAA,GAAA,CAAI,CAAC,OAAA,GAAU,OAAA,EAAS;AACvB,UAAA,OAAO,KAAA;AAAA,QACR;AACA,QAAA,GAAA,CAAI,QAAA,GAAW,OAAA,GAAU,SAAA,EAAW,MAAA,CAAO,QAAA,EAAU;AACpD,UAAA,OAAO,KAAA;AAAA,QACR;AACA,QAAA,OAAO,MAAA;AAAA,MACR,CAAA;AAAA,MACA;AAAA,IACD,CAAA;AAEA,IAAA,GAAA,CAAI,SAAA,EAAW;AACd,MAAA,IAAA,CAAK,IAAA,CAAK;AAAA,QACT,KAAA,EAAO,SAAA,CAAU,QAAA;AAAA,QACjB;AAAA,MACD,CAAC,CAAA;AAAA,IACF;AAEA,IAAA,OAAO,IAAA;AAAA,EACR,CAAA,EAAG,CAAC,CAAC,CAAA;AAEL,EAAA,OAAA,CAAQ,IAAA,CAAK,CAAC,CAAA,EAAG,CAAA,EAAA,GAAM;AACtB,IAAA,OAAO,CAAA,CAAE,MAAA,EAAQ,CAAA,CAAE,KAAA;AAAA,EACpB,CAAC,CAAA;AAED,EAAA,OAAO,OAAA;AACR;ADxFA;AACE;AACA;AACF,2EAAC","file":"/home/runner/work/n8n/n8n/packages/@n8n/utils/dist/search/sublimeSearch.cjs","sourcesContent":[null,"/*\n * Constants and utility functions used for searching for node types in node creator component\n * based on https://github.com/forrestthewoods/lib_fts/blob/master/code/fts_fuzzy_match.js\n */\n\nconst SEQUENTIAL_BONUS = 60; // bonus for adjacent matches\nconst SEPARATOR_BONUS = 38; // bonus if match occurs after a separator\nconst CAMEL_BONUS = 30; // bonus if match is uppercase and prev is lower\nconst FIRST_LETTER_BONUS = 15; // bonus if the first letter is matched\n\nconst LEADING_LETTER_PENALTY = -20; // penalty applied for every letter in str before the first match\nconst MAX_LEADING_LETTER_PENALTY = -200; // maximum penalty for leading letters\nconst UNMATCHED_LETTER_PENALTY = -5;\n\nexport const DEFAULT_KEYS = [\n\t{ key: 'properties.displayName', weight: 1.3 },\n\t{ key: 'properties.codex.alias', weight: 1 },\n];\n\n/**\n * Returns true if each character in pattern is found sequentially within target\n * @param {*} pattern string\n * @param {*} target string\n */\nfunction fuzzyMatchSimple(pattern: string, target: string): boolean {\n\tlet patternIdx = 0;\n\tlet strIdx = 0;\n\n\twhile (patternIdx < pattern.length && strIdx < target.length) {\n\t\tconst patternChar = pattern.charAt(patternIdx).toLowerCase();\n\t\tconst targetChar = target.charAt(strIdx).toLowerCase();\n\t\tif (patternChar === targetChar) {\n\t\t\tpatternIdx++;\n\t\t}\n\t\t++strIdx;\n\t}\n\n\treturn pattern.length !== 0 && target.length !== 0 && patternIdx === pattern.length;\n}\n\nfunction fuzzyMatchRecursive(\n\tpattern: string,\n\ttarget: string,\n\tpatternCurIndex: number,\n\ttargetCurrIndex: number,\n\ttargetMatches: null | number[],\n\tmatches: number[],\n\tmaxMatches: number,\n\tnextMatch: number,\n\trecursionCount: number,\n\trecursionLimit: number,\n): { matched: boolean; outScore: number } {\n\tlet outScore = 0;\n\n\t// Return if recursion limit is reached.\n\tif (++recursionCount >= recursionLimit) {\n\t\treturn { matched: false, outScore };\n\t}\n\n\t// Return if we reached ends of strings.\n\tif (patternCurIndex === pattern.length || targetCurrIndex === target.length) {\n\t\treturn { matched: false, outScore };\n\t}\n\n\t// Recursion params\n\tlet recursiveMatch = false;\n\tlet bestRecursiveMatches: number[] = [];\n\tlet bestRecursiveScore = 0;\n\n\t// Loop through pattern and str looking for a match.\n\tlet firstMatch = true;\n\twhile (patternCurIndex < pattern.length && targetCurrIndex < target.length) {\n\t\t// Match found.\n\t\tif (pattern[patternCurIndex].toLowerCase() === target[targetCurrIndex].toLowerCase()) {\n\t\t\tif (nextMatch >= maxMatches) {\n\t\t\t\treturn { matched: false, outScore };\n\t\t\t}\n\n\t\t\tif (firstMatch && targetMatches) {\n\t\t\t\tmatches = [...targetMatches];\n\t\t\t\tfirstMatch = false;\n\t\t\t}\n\n\t\t\tconst recursiveMatches: number[] = [];\n\t\t\tconst recursiveResult = fuzzyMatchRecursive(\n\t\t\t\tpattern,\n\t\t\t\ttarget,\n\t\t\t\tpatternCurIndex,\n\t\t\t\ttargetCurrIndex + 1,\n\t\t\t\tmatches,\n\t\t\t\trecursiveMatches,\n\t\t\t\tmaxMatches,\n\t\t\t\tnextMatch,\n\t\t\t\trecursionCount,\n\t\t\t\trecursionLimit,\n\t\t\t);\n\n\t\t\tconst recursiveScore = recursiveResult.outScore;\n\t\t\tif (recursiveResult.matched) {\n\t\t\t\t// Pick best recursive score.\n\t\t\t\tif (!recursiveMatch || recursiveScore > bestRecursiveScore) {\n\t\t\t\t\tbestRecursiveMatches = [...recursiveMatches];\n\t\t\t\t\tbestRecursiveScore = recursiveScore;\n\t\t\t\t}\n\t\t\t\trecursiveMatch = true;\n\t\t\t}\n\n\t\t\tmatches[nextMatch++] = targetCurrIndex;\n\t\t\t++patternCurIndex;\n\t\t}\n\t\t++targetCurrIndex;\n\t}\n\n\tconst matched = patternCurIndex === pattern.length;\n\n\tif (matched) {\n\t\toutScore = 100;\n\n\t\t// Apply leading letter penalty (if not n8n-prefixed)\n\t\tif (!target.toLowerCase().startsWith('n8n')) {\n\t\t\tlet penalty = LEADING_LETTER_PENALTY * matches[0];\n\t\t\tpenalty = penalty < MAX_LEADING_LETTER_PENALTY ? MAX_LEADING_LETTER_PENALTY : penalty;\n\t\t\toutScore += penalty;\n\t\t}\n\n\t\t//Apply unmatched penalty\n\t\tconst unmatched = target.length - nextMatch;\n\t\toutScore += UNMATCHED_LETTER_PENALTY * unmatched;\n\n\t\t// Apply ordering bonuses\n\t\tfor (let i = 0; i < nextMatch; i++) {\n\t\t\tconst currIdx = matches[i];\n\n\t\t\tif (i > 0) {\n\t\t\t\tconst prevIdx = matches[i - 1];\n\t\t\t\tif (currIdx === prevIdx + 1) {\n\t\t\t\t\toutScore += SEQUENTIAL_BONUS;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Check for bonuses based on neighbor character value.\n\t\t\tif (currIdx > 0) {\n\t\t\t\t// Camel case\n\t\t\t\tconst neighbor = target[currIdx - 1];\n\t\t\t\tconst curr = target[currIdx];\n\t\t\t\tif (neighbor !== neighbor.toUpperCase() && curr !== curr.toLowerCase()) {\n\t\t\t\t\toutScore += CAMEL_BONUS;\n\t\t\t\t}\n\t\t\t\tconst isNeighbourSeparator = neighbor === '_' || neighbor === ' ';\n\t\t\t\tif (isNeighbourSeparator) {\n\t\t\t\t\toutScore += SEPARATOR_BONUS;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// First letter\n\t\t\t\toutScore += FIRST_LETTER_BONUS;\n\t\t\t}\n\t\t}\n\n\t\t// Return best result\n\t\tif (recursiveMatch && (!matched || bestRecursiveScore > outScore)) {\n\t\t\t// Recursive score is better than \"this\"\n\t\t\tmatches = [...bestRecursiveMatches];\n\t\t\toutScore = bestRecursiveScore;\n\t\t\treturn { matched: true, outScore };\n\t\t} else if (matched) {\n\t\t\t// \"this\" score is better than recursive\n\t\t\treturn { matched: true, outScore };\n\t\t} else {\n\t\t\treturn { matched: false, outScore };\n\t\t}\n\t}\n\treturn { matched: false, outScore };\n}\n\n/**\n * Does a fuzzy search to find pattern inside a string.\n * @param {*} pattern string pattern to search for\n * @param {*} target string string which is being searched\n * @returns [boolean, number] a boolean which tells if pattern was\n * found or not and a search score\n */\nfunction fuzzyMatch(pattern: string, target: string): { matched: boolean; outScore: number } {\n\tconst recursionCount = 0;\n\tconst recursionLimit = 5;\n\tconst matches: number[] = [];\n\tconst maxMatches = 256;\n\n\treturn fuzzyMatchRecursive(\n\t\tpattern,\n\t\ttarget,\n\t\t0 /* patternCurIndex */,\n\t\t0 /* strCurrIndex */,\n\t\tnull /* srcMatces */,\n\t\tmatches,\n\t\tmaxMatches,\n\t\t0 /* nextMatch */,\n\t\trecursionCount,\n\t\trecursionLimit,\n\t);\n}\n\n// prop = 'key'\n// prop = 'key1.key2'\n// prop = ['key1', 'key2']\nfunction getValue<T extends object>(obj: T, prop: string): unknown {\n\tif (obj.hasOwnProperty(prop)) {\n\t\treturn obj[prop as keyof T];\n\t}\n\n\tconst segments = prop.split('.');\n\n\tlet result = obj;\n\tlet i = 0;\n\twhile (result && i < segments.length) {\n\t\tconst key = segments[i] as keyof T;\n\t\tresult = result[key] as T;\n\t\ti++;\n\t}\n\treturn result;\n}\n\nexport function sublimeSearch<T extends object>(\n\tfilter: string,\n\tdata: readonly T[],\n\tkeys: Array<{ key: string; weight: number }> = DEFAULT_KEYS,\n): Array<{ score: number; item: T }> {\n\tconst results = data.reduce((accu: Array<{ score: number; item: T }>, item: T) => {\n\t\tlet values: Array<{ value: string; weight: number }> = [];\n\t\tkeys.forEach(({ key, weight }) => {\n\t\t\tconst value = getValue(item, key);\n\t\t\tif (Array.isArray(value)) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tvalues = values.concat(value.map((v) => ({ value: v, weight })));\n\t\t\t} else if (typeof value === 'string') {\n\t\t\t\tvalues.push({\n\t\t\t\t\tvalue,\n\t\t\t\t\tweight,\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\t// for each item, check every key and get maximum score\n\t\tconst itemMatch = values.reduce(\n\t\t\t(\n\t\t\t\tresult: null | { matched: boolean; outScore: number },\n\t\t\t\t{ value, weight }: { value: string; weight: number },\n\t\t\t) => {\n\t\t\t\tif (!fuzzyMatchSimple(filter, value)) {\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\n\t\t\t\tconst match = fuzzyMatch(filter, value);\n\t\t\t\tmatch.outScore *= weight;\n\n\t\t\t\tconst { matched, outScore } = match;\n\t\t\t\tif (!result && matched) {\n\t\t\t\t\treturn match;\n\t\t\t\t}\n\t\t\t\tif (matched && result && outScore > result.outScore) {\n\t\t\t\t\treturn match;\n\t\t\t\t}\n\t\t\t\treturn result;\n\t\t\t},\n\t\t\tnull,\n\t\t);\n\n\t\tif (itemMatch) {\n\t\t\taccu.push({\n\t\t\t\tscore: itemMatch.outScore,\n\t\t\t\titem,\n\t\t\t});\n\t\t}\n\n\t\treturn accu;\n\t}, []);\n\n\tresults.sort((a, b) => {\n\t\treturn b.score - a.score;\n\t});\n\n\treturn results;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"sublimeSearch.cjs","names":["bestRecursiveMatches: number[]","recursiveMatches: number[]","values: Array<{ value: string; weight: number }>"],"sources":["../../src/search/sublimeSearch.ts"],"sourcesContent":["/*\n * Constants and utility functions used for searching for node types in node creator component\n * based on https://github.com/forrestthewoods/lib_fts/blob/master/code/fts_fuzzy_match.js\n */\n\nconst SEQUENTIAL_BONUS = 60; // bonus for adjacent matches\nconst SEPARATOR_BONUS = 38; // bonus if match occurs after a separator\nconst CAMEL_BONUS = 30; // bonus if match is uppercase and prev is lower\nconst FIRST_LETTER_BONUS = 15; // bonus if the first letter is matched\n\nconst LEADING_LETTER_PENALTY = -20; // penalty applied for every letter in str before the first match\nconst MAX_LEADING_LETTER_PENALTY = -200; // maximum penalty for leading letters\nconst UNMATCHED_LETTER_PENALTY = -5;\n\nexport const DEFAULT_KEYS = [\n\t{ key: 'properties.displayName', weight: 1.3 },\n\t{ key: 'properties.codex.alias', weight: 1 },\n];\n\n/**\n * Returns true if each character in pattern is found sequentially within target\n * @param {*} pattern string\n * @param {*} target string\n */\nfunction fuzzyMatchSimple(pattern: string, target: string): boolean {\n\tlet patternIdx = 0;\n\tlet strIdx = 0;\n\n\twhile (patternIdx < pattern.length && strIdx < target.length) {\n\t\tconst patternChar = pattern.charAt(patternIdx).toLowerCase();\n\t\tconst targetChar = target.charAt(strIdx).toLowerCase();\n\t\tif (patternChar === targetChar) {\n\t\t\tpatternIdx++;\n\t\t}\n\t\t++strIdx;\n\t}\n\n\treturn pattern.length !== 0 && target.length !== 0 && patternIdx === pattern.length;\n}\n\nfunction fuzzyMatchRecursive(\n\tpattern: string,\n\ttarget: string,\n\tpatternCurIndex: number,\n\ttargetCurrIndex: number,\n\ttargetMatches: null | number[],\n\tmatches: number[],\n\tmaxMatches: number,\n\tnextMatch: number,\n\trecursionCount: number,\n\trecursionLimit: number,\n): { matched: boolean; outScore: number } {\n\tlet outScore = 0;\n\n\t// Return if recursion limit is reached.\n\tif (++recursionCount >= recursionLimit) {\n\t\treturn { matched: false, outScore };\n\t}\n\n\t// Return if we reached ends of strings.\n\tif (patternCurIndex === pattern.length || targetCurrIndex === target.length) {\n\t\treturn { matched: false, outScore };\n\t}\n\n\t// Recursion params\n\tlet recursiveMatch = false;\n\tlet bestRecursiveMatches: number[] = [];\n\tlet bestRecursiveScore = 0;\n\n\t// Loop through pattern and str looking for a match.\n\tlet firstMatch = true;\n\twhile (patternCurIndex < pattern.length && targetCurrIndex < target.length) {\n\t\t// Match found.\n\t\tif (pattern[patternCurIndex].toLowerCase() === target[targetCurrIndex].toLowerCase()) {\n\t\t\tif (nextMatch >= maxMatches) {\n\t\t\t\treturn { matched: false, outScore };\n\t\t\t}\n\n\t\t\tif (firstMatch && targetMatches) {\n\t\t\t\tmatches = [...targetMatches];\n\t\t\t\tfirstMatch = false;\n\t\t\t}\n\n\t\t\tconst recursiveMatches: number[] = [];\n\t\t\tconst recursiveResult = fuzzyMatchRecursive(\n\t\t\t\tpattern,\n\t\t\t\ttarget,\n\t\t\t\tpatternCurIndex,\n\t\t\t\ttargetCurrIndex + 1,\n\t\t\t\tmatches,\n\t\t\t\trecursiveMatches,\n\t\t\t\tmaxMatches,\n\t\t\t\tnextMatch,\n\t\t\t\trecursionCount,\n\t\t\t\trecursionLimit,\n\t\t\t);\n\n\t\t\tconst recursiveScore = recursiveResult.outScore;\n\t\t\tif (recursiveResult.matched) {\n\t\t\t\t// Pick best recursive score.\n\t\t\t\tif (!recursiveMatch || recursiveScore > bestRecursiveScore) {\n\t\t\t\t\tbestRecursiveMatches = [...recursiveMatches];\n\t\t\t\t\tbestRecursiveScore = recursiveScore;\n\t\t\t\t}\n\t\t\t\trecursiveMatch = true;\n\t\t\t}\n\n\t\t\tmatches[nextMatch++] = targetCurrIndex;\n\t\t\t++patternCurIndex;\n\t\t}\n\t\t++targetCurrIndex;\n\t}\n\n\tconst matched = patternCurIndex === pattern.length;\n\n\tif (matched) {\n\t\toutScore = 100;\n\n\t\t// Apply leading letter penalty (if not n8n-prefixed)\n\t\tif (!target.toLowerCase().startsWith('n8n')) {\n\t\t\tlet penalty = LEADING_LETTER_PENALTY * matches[0];\n\t\t\tpenalty = penalty < MAX_LEADING_LETTER_PENALTY ? MAX_LEADING_LETTER_PENALTY : penalty;\n\t\t\toutScore += penalty;\n\t\t}\n\n\t\t//Apply unmatched penalty\n\t\tconst unmatched = target.length - nextMatch;\n\t\toutScore += UNMATCHED_LETTER_PENALTY * unmatched;\n\n\t\t// Apply ordering bonuses\n\t\tfor (let i = 0; i < nextMatch; i++) {\n\t\t\tconst currIdx = matches[i];\n\n\t\t\tif (i > 0) {\n\t\t\t\tconst prevIdx = matches[i - 1];\n\t\t\t\tif (currIdx === prevIdx + 1) {\n\t\t\t\t\toutScore += SEQUENTIAL_BONUS;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Check for bonuses based on neighbor character value.\n\t\t\tif (currIdx > 0) {\n\t\t\t\t// Camel case\n\t\t\t\tconst neighbor = target[currIdx - 1];\n\t\t\t\tconst curr = target[currIdx];\n\t\t\t\tif (neighbor !== neighbor.toUpperCase() && curr !== curr.toLowerCase()) {\n\t\t\t\t\toutScore += CAMEL_BONUS;\n\t\t\t\t}\n\t\t\t\tconst isNeighbourSeparator = neighbor === '_' || neighbor === ' ';\n\t\t\t\tif (isNeighbourSeparator) {\n\t\t\t\t\toutScore += SEPARATOR_BONUS;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// First letter\n\t\t\t\toutScore += FIRST_LETTER_BONUS;\n\t\t\t}\n\t\t}\n\n\t\t// Return best result\n\t\tif (recursiveMatch && (!matched || bestRecursiveScore > outScore)) {\n\t\t\t// Recursive score is better than \"this\"\n\t\t\tmatches = [...bestRecursiveMatches];\n\t\t\toutScore = bestRecursiveScore;\n\t\t\treturn { matched: true, outScore };\n\t\t} else if (matched) {\n\t\t\t// \"this\" score is better than recursive\n\t\t\treturn { matched: true, outScore };\n\t\t} else {\n\t\t\treturn { matched: false, outScore };\n\t\t}\n\t}\n\treturn { matched: false, outScore };\n}\n\n/**\n * Does a fuzzy search to find pattern inside a string.\n * @param {*} pattern string pattern to search for\n * @param {*} target string string which is being searched\n * @returns [boolean, number] a boolean which tells if pattern was\n * found or not and a search score\n */\nfunction fuzzyMatch(pattern: string, target: string): { matched: boolean; outScore: number } {\n\tconst recursionCount = 0;\n\tconst recursionLimit = 5;\n\tconst matches: number[] = [];\n\tconst maxMatches = 256;\n\n\treturn fuzzyMatchRecursive(\n\t\tpattern,\n\t\ttarget,\n\t\t0 /* patternCurIndex */,\n\t\t0 /* strCurrIndex */,\n\t\tnull /* srcMatces */,\n\t\tmatches,\n\t\tmaxMatches,\n\t\t0 /* nextMatch */,\n\t\trecursionCount,\n\t\trecursionLimit,\n\t);\n}\n\n// prop = 'key'\n// prop = 'key1.key2'\n// prop = ['key1', 'key2']\nfunction getValue<T extends object>(obj: T, prop: string): unknown {\n\tif (obj.hasOwnProperty(prop)) {\n\t\treturn obj[prop as keyof T];\n\t}\n\n\tconst segments = prop.split('.');\n\n\tlet result = obj;\n\tlet i = 0;\n\twhile (result && i < segments.length) {\n\t\tconst key = segments[i] as keyof T;\n\t\tresult = result[key] as T;\n\t\ti++;\n\t}\n\treturn result;\n}\n\nexport function sublimeSearch<T extends object>(\n\tfilter: string,\n\tdata: readonly T[],\n\tkeys: Array<{ key: string; weight: number }> = DEFAULT_KEYS,\n): Array<{ score: number; item: T }> {\n\tconst results = data.reduce((accu: Array<{ score: number; item: T }>, item: T) => {\n\t\tlet values: Array<{ value: string; weight: number }> = [];\n\t\tkeys.forEach(({ key, weight }) => {\n\t\t\tconst value = getValue(item, key);\n\t\t\tif (Array.isArray(value)) {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tvalues = values.concat(value.map((v) => ({ value: v, weight })));\n\t\t\t} else if (typeof value === 'string') {\n\t\t\t\tvalues.push({\n\t\t\t\t\tvalue,\n\t\t\t\t\tweight,\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\n\t\t// for each item, check every key and get maximum score\n\t\tconst itemMatch = values.reduce(\n\t\t\t(\n\t\t\t\tresult: null | { matched: boolean; outScore: number },\n\t\t\t\t{ value, weight }: { value: string; weight: number },\n\t\t\t) => {\n\t\t\t\tif (!fuzzyMatchSimple(filter, value)) {\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\n\t\t\t\tconst match = fuzzyMatch(filter, value);\n\t\t\t\tmatch.outScore *= weight;\n\n\t\t\t\tconst { matched, outScore } = match;\n\t\t\t\tif (!result && matched) {\n\t\t\t\t\treturn match;\n\t\t\t\t}\n\t\t\t\tif (matched && result && outScore > result.outScore) {\n\t\t\t\t\treturn match;\n\t\t\t\t}\n\t\t\t\treturn result;\n\t\t\t},\n\t\t\tnull,\n\t\t);\n\n\t\tif (itemMatch) {\n\t\t\taccu.push({\n\t\t\t\tscore: itemMatch.outScore,\n\t\t\t\titem,\n\t\t\t});\n\t\t}\n\n\t\treturn accu;\n\t}, []);\n\n\tresults.sort((a, b) => {\n\t\treturn b.score - a.score;\n\t});\n\n\treturn results;\n}\n"],"mappings":";;AAKA,MAAM,mBAAmB;AACzB,MAAM,kBAAkB;AACxB,MAAM,cAAc;AACpB,MAAM,qBAAqB;AAE3B,MAAM,yBAAyB;AAC/B,MAAM,6BAA6B;AACnC,MAAM,2BAA2B;AAEjC,MAAa,eAAe,CAC3B;CAAE,KAAK;CAA0B,QAAQ;CAAK,EAC9C;CAAE,KAAK;CA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QACA,MACA,OAA+C,cACX;CACpC,MAAM,UAAU,KAAK,QAAQ,MAAyC,SAAY;EACjF,IAAIC,SAAmD,EAAE;AACzD,OAAK,SAAS,EAAE,KAAK,aAAa;GACjC,MAAM,QAAQ,SAAS,MAAM,IAAI;AACjC,OAAI,MAAM,QAAQ,MAAM,CAEvB,UAAS,OAAO,OAAO,MAAM,KAAK,OAAO;IAAE,OAAO;IAAG;IAAQ,EAAE,CAAC;YACtD,OAAO,UAAU,SAC3B,QAAO,KAAK;IACX;IACA;IACA,CAAC;IAEF;EAGF,MAAM,YAAY,OAAO,QAEvB,QACA,EAAE,OAAO,aACL;AACJ,OAAI,CAAC,iBAAiB,QAAQ,MAAM,CACnC,QAAO;GAGR,MAAM,QAAQ,WAAW,QAAQ,MAAM;AACvC,SAAM,YAAY;GAElB,MAAM,EAAE,SAAS,aAAa;AAC9B,OAAI,CAAC,UAAU,QACd,QAAO;AAER,OAAI,WAAW,UAAU,WAAW,OAAO,SAC1C,QAAO;AAER,UAAO;KAER,KACA;AAED,MAAI,UACH,MAAK,KAAK;GACT,OAAO,UAAU;GACjB;GACA,CAAC;AAGH,SAAO;IACL,EAAE,CAAC;AAEN,SAAQ,MAAM,GAAG,MAAM;AACtB,SAAO,EAAE,QAAQ,EAAE;GAClB;AAEF,QAAO"}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
//#region src/search/sublimeSearch.d.ts
|
|
1
2
|
declare const DEFAULT_KEYS: {
|
|
2
|
-
|
|
3
|
-
|
|
3
|
+
key: string;
|
|
4
|
+
weight: number;
|
|
4
5
|
}[];
|
|
5
6
|
declare function sublimeSearch<T extends object>(filter: string, data: readonly T[], keys?: Array<{
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
key: string;
|
|
8
|
+
weight: number;
|
|
8
9
|
}>): Array<{
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
score: number;
|
|
11
|
+
item: T;
|
|
11
12
|
}>;
|
|
12
|
-
|
|
13
|
+
//#endregion
|
|
13
14
|
export { DEFAULT_KEYS, sublimeSearch };
|
|
15
|
+
//# sourceMappingURL=sublimeSearch.d.cts.map
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
//#region src/search/sublimeSearch.d.ts
|
|
1
2
|
declare const DEFAULT_KEYS: {
|
|
2
|
-
|
|
3
|
-
|
|
3
|
+
key: string;
|
|
4
|
+
weight: number;
|
|
4
5
|
}[];
|
|
5
6
|
declare function sublimeSearch<T extends object>(filter: string, data: readonly T[], keys?: Array<{
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
key: string;
|
|
8
|
+
weight: number;
|
|
8
9
|
}>): Array<{
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
score: number;
|
|
11
|
+
item: T;
|
|
11
12
|
}>;
|
|
12
|
-
|
|
13
|
+
//#endregion
|
|
13
14
|
export { DEFAULT_KEYS, sublimeSearch };
|
|
15
|
+
//# sourceMappingURL=sublimeSearch.d.ts.map
|