@startinblox/core 2.0.0-beta.3 → 2.0.0-beta.5
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/helpers-BPYDgD10.js +355 -0
- package/dist/helpers.js +1 -1
- package/dist/index.js +13 -3
- package/dist/slimselect-Bx1deYT1.js +4 -0
- package/package.json +1 -2
- package/dist/helpers-Bt3iZe79.js +0 -1661
- package/dist/slimselect-WIZK5Hmq.js +0 -4
package/dist/helpers-Bt3iZe79.js
DELETED
|
@@ -1,1661 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __typeError = (msg) => {
|
|
3
|
-
throw TypeError(msg);
|
|
4
|
-
};
|
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
-
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
8
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
9
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
10
|
-
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
11
|
-
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
12
|
-
var _values, _resolve, _AsyncIterableBuilder_instances, createIterable_fn, next_fn, nextPromise_fn;
|
|
13
|
-
function isArray(value) {
|
|
14
|
-
return !Array.isArray ? getTag(value) === "[object Array]" : Array.isArray(value);
|
|
15
|
-
}
|
|
16
|
-
const INFINITY = 1 / 0;
|
|
17
|
-
function baseToString(value) {
|
|
18
|
-
if (typeof value == "string") {
|
|
19
|
-
return value;
|
|
20
|
-
}
|
|
21
|
-
let result = value + "";
|
|
22
|
-
return result == "0" && 1 / value == -INFINITY ? "-0" : result;
|
|
23
|
-
}
|
|
24
|
-
function toString(value) {
|
|
25
|
-
return value == null ? "" : baseToString(value);
|
|
26
|
-
}
|
|
27
|
-
function isString(value) {
|
|
28
|
-
return typeof value === "string";
|
|
29
|
-
}
|
|
30
|
-
function isNumber(value) {
|
|
31
|
-
return typeof value === "number";
|
|
32
|
-
}
|
|
33
|
-
function isBoolean(value) {
|
|
34
|
-
return value === true || value === false || isObjectLike(value) && getTag(value) == "[object Boolean]";
|
|
35
|
-
}
|
|
36
|
-
function isObject(value) {
|
|
37
|
-
return typeof value === "object";
|
|
38
|
-
}
|
|
39
|
-
function isObjectLike(value) {
|
|
40
|
-
return isObject(value) && value !== null;
|
|
41
|
-
}
|
|
42
|
-
function isDefined(value) {
|
|
43
|
-
return value !== void 0 && value !== null;
|
|
44
|
-
}
|
|
45
|
-
function isBlank(value) {
|
|
46
|
-
return !value.trim().length;
|
|
47
|
-
}
|
|
48
|
-
function getTag(value) {
|
|
49
|
-
return value == null ? value === void 0 ? "[object Undefined]" : "[object Null]" : Object.prototype.toString.call(value);
|
|
50
|
-
}
|
|
51
|
-
const EXTENDED_SEARCH_UNAVAILABLE = "Extended search is not available";
|
|
52
|
-
const INCORRECT_INDEX_TYPE = "Incorrect 'index' type";
|
|
53
|
-
const LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY = (key) => `Invalid value for key ${key}`;
|
|
54
|
-
const PATTERN_LENGTH_TOO_LARGE = (max) => `Pattern length exceeds max of ${max}.`;
|
|
55
|
-
const MISSING_KEY_PROPERTY = (name) => `Missing ${name} property in key`;
|
|
56
|
-
const INVALID_KEY_WEIGHT_VALUE = (key) => `Property 'weight' in key '${key}' must be a positive integer`;
|
|
57
|
-
const hasOwn = Object.prototype.hasOwnProperty;
|
|
58
|
-
class KeyStore {
|
|
59
|
-
constructor(keys) {
|
|
60
|
-
this._keys = [];
|
|
61
|
-
this._keyMap = {};
|
|
62
|
-
let totalWeight = 0;
|
|
63
|
-
keys.forEach((key) => {
|
|
64
|
-
let obj = createKey(key);
|
|
65
|
-
this._keys.push(obj);
|
|
66
|
-
this._keyMap[obj.id] = obj;
|
|
67
|
-
totalWeight += obj.weight;
|
|
68
|
-
});
|
|
69
|
-
this._keys.forEach((key) => {
|
|
70
|
-
key.weight /= totalWeight;
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
get(keyId) {
|
|
74
|
-
return this._keyMap[keyId];
|
|
75
|
-
}
|
|
76
|
-
keys() {
|
|
77
|
-
return this._keys;
|
|
78
|
-
}
|
|
79
|
-
toJSON() {
|
|
80
|
-
return JSON.stringify(this._keys);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
function createKey(key) {
|
|
84
|
-
let path = null;
|
|
85
|
-
let id = null;
|
|
86
|
-
let src = null;
|
|
87
|
-
let weight = 1;
|
|
88
|
-
let getFn = null;
|
|
89
|
-
if (isString(key) || isArray(key)) {
|
|
90
|
-
src = key;
|
|
91
|
-
path = createKeyPath(key);
|
|
92
|
-
id = createKeyId(key);
|
|
93
|
-
} else {
|
|
94
|
-
if (!hasOwn.call(key, "name")) {
|
|
95
|
-
throw new Error(MISSING_KEY_PROPERTY("name"));
|
|
96
|
-
}
|
|
97
|
-
const name = key.name;
|
|
98
|
-
src = name;
|
|
99
|
-
if (hasOwn.call(key, "weight")) {
|
|
100
|
-
weight = key.weight;
|
|
101
|
-
if (weight <= 0) {
|
|
102
|
-
throw new Error(INVALID_KEY_WEIGHT_VALUE(name));
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
path = createKeyPath(name);
|
|
106
|
-
id = createKeyId(name);
|
|
107
|
-
getFn = key.getFn;
|
|
108
|
-
}
|
|
109
|
-
return { path, id, weight, src, getFn };
|
|
110
|
-
}
|
|
111
|
-
function createKeyPath(key) {
|
|
112
|
-
return isArray(key) ? key : key.split(".");
|
|
113
|
-
}
|
|
114
|
-
function createKeyId(key) {
|
|
115
|
-
return isArray(key) ? key.join(".") : key;
|
|
116
|
-
}
|
|
117
|
-
function get(obj, path) {
|
|
118
|
-
let list = [];
|
|
119
|
-
let arr = false;
|
|
120
|
-
const deepGet = (obj2, path2, index) => {
|
|
121
|
-
if (!isDefined(obj2)) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
if (!path2[index]) {
|
|
125
|
-
list.push(obj2);
|
|
126
|
-
} else {
|
|
127
|
-
let key = path2[index];
|
|
128
|
-
const value = obj2[key];
|
|
129
|
-
if (!isDefined(value)) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
if (index === path2.length - 1 && (isString(value) || isNumber(value) || isBoolean(value))) {
|
|
133
|
-
list.push(toString(value));
|
|
134
|
-
} else if (isArray(value)) {
|
|
135
|
-
arr = true;
|
|
136
|
-
for (let i = 0, len = value.length; i < len; i += 1) {
|
|
137
|
-
deepGet(value[i], path2, index + 1);
|
|
138
|
-
}
|
|
139
|
-
} else if (path2.length) {
|
|
140
|
-
deepGet(value, path2, index + 1);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
deepGet(obj, isString(path) ? path.split(".") : path, 0);
|
|
145
|
-
return arr ? list : list[0];
|
|
146
|
-
}
|
|
147
|
-
const MatchOptions = {
|
|
148
|
-
// Whether the matches should be included in the result set. When `true`, each record in the result
|
|
149
|
-
// set will include the indices of the matched characters.
|
|
150
|
-
// These can consequently be used for highlighting purposes.
|
|
151
|
-
includeMatches: false,
|
|
152
|
-
// When `true`, the matching function will continue to the end of a search pattern even if
|
|
153
|
-
// a perfect match has already been located in the string.
|
|
154
|
-
findAllMatches: false,
|
|
155
|
-
// Minimum number of characters that must be matched before a result is considered a match
|
|
156
|
-
minMatchCharLength: 1
|
|
157
|
-
};
|
|
158
|
-
const BasicOptions = {
|
|
159
|
-
// When `true`, the algorithm continues searching to the end of the input even if a perfect
|
|
160
|
-
// match is found before the end of the same input.
|
|
161
|
-
isCaseSensitive: false,
|
|
162
|
-
// When true, the matching function will continue to the end of a search pattern even if
|
|
163
|
-
includeScore: false,
|
|
164
|
-
// List of properties that will be searched. This also supports nested properties.
|
|
165
|
-
keys: [],
|
|
166
|
-
// Whether to sort the result list, by score
|
|
167
|
-
shouldSort: true,
|
|
168
|
-
// Default sort function: sort by ascending score, ascending index
|
|
169
|
-
sortFn: (a, b) => a.score === b.score ? a.idx < b.idx ? -1 : 1 : a.score < b.score ? -1 : 1
|
|
170
|
-
};
|
|
171
|
-
const FuzzyOptions = {
|
|
172
|
-
// Approximately where in the text is the pattern expected to be found?
|
|
173
|
-
location: 0,
|
|
174
|
-
// At what point does the match algorithm give up. A threshold of '0.0' requires a perfect match
|
|
175
|
-
// (of both letters and location), a threshold of '1.0' would match anything.
|
|
176
|
-
threshold: 0.6,
|
|
177
|
-
// Determines how close the match must be to the fuzzy location (specified above).
|
|
178
|
-
// An exact letter match which is 'distance' characters away from the fuzzy location
|
|
179
|
-
// would score as a complete mismatch. A distance of '0' requires the match be at
|
|
180
|
-
// the exact location specified, a threshold of '1000' would require a perfect match
|
|
181
|
-
// to be within 800 characters of the fuzzy location to be found using a 0.8 threshold.
|
|
182
|
-
distance: 100
|
|
183
|
-
};
|
|
184
|
-
const AdvancedOptions = {
|
|
185
|
-
// When `true`, it enables the use of unix-like search commands
|
|
186
|
-
useExtendedSearch: false,
|
|
187
|
-
// The get function to use when fetching an object's properties.
|
|
188
|
-
// The default will search nested paths *ie foo.bar.baz*
|
|
189
|
-
getFn: get,
|
|
190
|
-
// When `true`, search will ignore `location` and `distance`, so it won't matter
|
|
191
|
-
// where in the string the pattern appears.
|
|
192
|
-
// More info: https://fusejs.io/concepts/scoring-theory.html#fuzziness-score
|
|
193
|
-
ignoreLocation: false,
|
|
194
|
-
// When `true`, the calculation for the relevance score (used for sorting) will
|
|
195
|
-
// ignore the field-length norm.
|
|
196
|
-
// More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
|
|
197
|
-
ignoreFieldNorm: false,
|
|
198
|
-
// The weight to determine how much field length norm effects scoring.
|
|
199
|
-
fieldNormWeight: 1
|
|
200
|
-
};
|
|
201
|
-
var Config = {
|
|
202
|
-
...BasicOptions,
|
|
203
|
-
...MatchOptions,
|
|
204
|
-
...FuzzyOptions,
|
|
205
|
-
...AdvancedOptions
|
|
206
|
-
};
|
|
207
|
-
const SPACE = /[^ ]+/g;
|
|
208
|
-
function norm(weight = 1, mantissa = 3) {
|
|
209
|
-
const cache = /* @__PURE__ */ new Map();
|
|
210
|
-
const m = Math.pow(10, mantissa);
|
|
211
|
-
return {
|
|
212
|
-
get(value) {
|
|
213
|
-
const numTokens = value.match(SPACE).length;
|
|
214
|
-
if (cache.has(numTokens)) {
|
|
215
|
-
return cache.get(numTokens);
|
|
216
|
-
}
|
|
217
|
-
const norm2 = 1 / Math.pow(numTokens, 0.5 * weight);
|
|
218
|
-
const n = parseFloat(Math.round(norm2 * m) / m);
|
|
219
|
-
cache.set(numTokens, n);
|
|
220
|
-
return n;
|
|
221
|
-
},
|
|
222
|
-
clear() {
|
|
223
|
-
cache.clear();
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
class FuseIndex {
|
|
228
|
-
constructor({
|
|
229
|
-
getFn = Config.getFn,
|
|
230
|
-
fieldNormWeight = Config.fieldNormWeight
|
|
231
|
-
} = {}) {
|
|
232
|
-
this.norm = norm(fieldNormWeight, 3);
|
|
233
|
-
this.getFn = getFn;
|
|
234
|
-
this.isCreated = false;
|
|
235
|
-
this.setIndexRecords();
|
|
236
|
-
}
|
|
237
|
-
setSources(docs = []) {
|
|
238
|
-
this.docs = docs;
|
|
239
|
-
}
|
|
240
|
-
setIndexRecords(records = []) {
|
|
241
|
-
this.records = records;
|
|
242
|
-
}
|
|
243
|
-
setKeys(keys = []) {
|
|
244
|
-
this.keys = keys;
|
|
245
|
-
this._keysMap = {};
|
|
246
|
-
keys.forEach((key, idx) => {
|
|
247
|
-
this._keysMap[key.id] = idx;
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
create() {
|
|
251
|
-
if (this.isCreated || !this.docs.length) {
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
this.isCreated = true;
|
|
255
|
-
if (isString(this.docs[0])) {
|
|
256
|
-
this.docs.forEach((doc, docIndex) => {
|
|
257
|
-
this._addString(doc, docIndex);
|
|
258
|
-
});
|
|
259
|
-
} else {
|
|
260
|
-
this.docs.forEach((doc, docIndex) => {
|
|
261
|
-
this._addObject(doc, docIndex);
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
this.norm.clear();
|
|
265
|
-
}
|
|
266
|
-
// Adds a doc to the end of the index
|
|
267
|
-
add(doc) {
|
|
268
|
-
const idx = this.size();
|
|
269
|
-
if (isString(doc)) {
|
|
270
|
-
this._addString(doc, idx);
|
|
271
|
-
} else {
|
|
272
|
-
this._addObject(doc, idx);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
// Removes the doc at the specified index of the index
|
|
276
|
-
removeAt(idx) {
|
|
277
|
-
this.records.splice(idx, 1);
|
|
278
|
-
for (let i = idx, len = this.size(); i < len; i += 1) {
|
|
279
|
-
this.records[i].i -= 1;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
getValueForItemAtKeyId(item, keyId) {
|
|
283
|
-
return item[this._keysMap[keyId]];
|
|
284
|
-
}
|
|
285
|
-
size() {
|
|
286
|
-
return this.records.length;
|
|
287
|
-
}
|
|
288
|
-
_addString(doc, docIndex) {
|
|
289
|
-
if (!isDefined(doc) || isBlank(doc)) {
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
let record = {
|
|
293
|
-
v: doc,
|
|
294
|
-
i: docIndex,
|
|
295
|
-
n: this.norm.get(doc)
|
|
296
|
-
};
|
|
297
|
-
this.records.push(record);
|
|
298
|
-
}
|
|
299
|
-
_addObject(doc, docIndex) {
|
|
300
|
-
let record = { i: docIndex, $: {} };
|
|
301
|
-
this.keys.forEach((key, keyIndex) => {
|
|
302
|
-
let value = key.getFn ? key.getFn(doc) : this.getFn(doc, key.path);
|
|
303
|
-
if (!isDefined(value)) {
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
if (isArray(value)) {
|
|
307
|
-
let subRecords = [];
|
|
308
|
-
const stack = [{ nestedArrIndex: -1, value }];
|
|
309
|
-
while (stack.length) {
|
|
310
|
-
const { nestedArrIndex, value: value2 } = stack.pop();
|
|
311
|
-
if (!isDefined(value2)) {
|
|
312
|
-
continue;
|
|
313
|
-
}
|
|
314
|
-
if (isString(value2) && !isBlank(value2)) {
|
|
315
|
-
let subRecord = {
|
|
316
|
-
v: value2,
|
|
317
|
-
i: nestedArrIndex,
|
|
318
|
-
n: this.norm.get(value2)
|
|
319
|
-
};
|
|
320
|
-
subRecords.push(subRecord);
|
|
321
|
-
} else if (isArray(value2)) {
|
|
322
|
-
value2.forEach((item, k) => {
|
|
323
|
-
stack.push({
|
|
324
|
-
nestedArrIndex: k,
|
|
325
|
-
value: item
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
} else ;
|
|
329
|
-
}
|
|
330
|
-
record.$[keyIndex] = subRecords;
|
|
331
|
-
} else if (isString(value) && !isBlank(value)) {
|
|
332
|
-
let subRecord = {
|
|
333
|
-
v: value,
|
|
334
|
-
n: this.norm.get(value)
|
|
335
|
-
};
|
|
336
|
-
record.$[keyIndex] = subRecord;
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
this.records.push(record);
|
|
340
|
-
}
|
|
341
|
-
toJSON() {
|
|
342
|
-
return {
|
|
343
|
-
keys: this.keys,
|
|
344
|
-
records: this.records
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
function createIndex(keys, docs, { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
|
|
349
|
-
const myIndex = new FuseIndex({ getFn, fieldNormWeight });
|
|
350
|
-
myIndex.setKeys(keys.map(createKey));
|
|
351
|
-
myIndex.setSources(docs);
|
|
352
|
-
myIndex.create();
|
|
353
|
-
return myIndex;
|
|
354
|
-
}
|
|
355
|
-
function parseIndex(data, { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}) {
|
|
356
|
-
const { keys, records } = data;
|
|
357
|
-
const myIndex = new FuseIndex({ getFn, fieldNormWeight });
|
|
358
|
-
myIndex.setKeys(keys);
|
|
359
|
-
myIndex.setIndexRecords(records);
|
|
360
|
-
return myIndex;
|
|
361
|
-
}
|
|
362
|
-
function computeScore$1(pattern, {
|
|
363
|
-
errors = 0,
|
|
364
|
-
currentLocation = 0,
|
|
365
|
-
expectedLocation = 0,
|
|
366
|
-
distance = Config.distance,
|
|
367
|
-
ignoreLocation = Config.ignoreLocation
|
|
368
|
-
} = {}) {
|
|
369
|
-
const accuracy = errors / pattern.length;
|
|
370
|
-
if (ignoreLocation) {
|
|
371
|
-
return accuracy;
|
|
372
|
-
}
|
|
373
|
-
const proximity = Math.abs(expectedLocation - currentLocation);
|
|
374
|
-
if (!distance) {
|
|
375
|
-
return proximity ? 1 : accuracy;
|
|
376
|
-
}
|
|
377
|
-
return accuracy + proximity / distance;
|
|
378
|
-
}
|
|
379
|
-
function convertMaskToIndices(matchmask = [], minMatchCharLength = Config.minMatchCharLength) {
|
|
380
|
-
let indices = [];
|
|
381
|
-
let start = -1;
|
|
382
|
-
let end = -1;
|
|
383
|
-
let i = 0;
|
|
384
|
-
for (let len = matchmask.length; i < len; i += 1) {
|
|
385
|
-
let match = matchmask[i];
|
|
386
|
-
if (match && start === -1) {
|
|
387
|
-
start = i;
|
|
388
|
-
} else if (!match && start !== -1) {
|
|
389
|
-
end = i - 1;
|
|
390
|
-
if (end - start + 1 >= minMatchCharLength) {
|
|
391
|
-
indices.push([start, end]);
|
|
392
|
-
}
|
|
393
|
-
start = -1;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
if (matchmask[i - 1] && i - start >= minMatchCharLength) {
|
|
397
|
-
indices.push([start, i - 1]);
|
|
398
|
-
}
|
|
399
|
-
return indices;
|
|
400
|
-
}
|
|
401
|
-
const MAX_BITS = 32;
|
|
402
|
-
function search(text, pattern, patternAlphabet, {
|
|
403
|
-
location = Config.location,
|
|
404
|
-
distance = Config.distance,
|
|
405
|
-
threshold = Config.threshold,
|
|
406
|
-
findAllMatches = Config.findAllMatches,
|
|
407
|
-
minMatchCharLength = Config.minMatchCharLength,
|
|
408
|
-
includeMatches = Config.includeMatches,
|
|
409
|
-
ignoreLocation = Config.ignoreLocation
|
|
410
|
-
} = {}) {
|
|
411
|
-
if (pattern.length > MAX_BITS) {
|
|
412
|
-
throw new Error(PATTERN_LENGTH_TOO_LARGE(MAX_BITS));
|
|
413
|
-
}
|
|
414
|
-
const patternLen = pattern.length;
|
|
415
|
-
const textLen = text.length;
|
|
416
|
-
const expectedLocation = Math.max(0, Math.min(location, textLen));
|
|
417
|
-
let currentThreshold = threshold;
|
|
418
|
-
let bestLocation = expectedLocation;
|
|
419
|
-
const computeMatches = minMatchCharLength > 1 || includeMatches;
|
|
420
|
-
const matchMask = computeMatches ? Array(textLen) : [];
|
|
421
|
-
let index;
|
|
422
|
-
while ((index = text.indexOf(pattern, bestLocation)) > -1) {
|
|
423
|
-
let score = computeScore$1(pattern, {
|
|
424
|
-
currentLocation: index,
|
|
425
|
-
expectedLocation,
|
|
426
|
-
distance,
|
|
427
|
-
ignoreLocation
|
|
428
|
-
});
|
|
429
|
-
currentThreshold = Math.min(score, currentThreshold);
|
|
430
|
-
bestLocation = index + patternLen;
|
|
431
|
-
if (computeMatches) {
|
|
432
|
-
let i = 0;
|
|
433
|
-
while (i < patternLen) {
|
|
434
|
-
matchMask[index + i] = 1;
|
|
435
|
-
i += 1;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
bestLocation = -1;
|
|
440
|
-
let lastBitArr = [];
|
|
441
|
-
let finalScore = 1;
|
|
442
|
-
let binMax = patternLen + textLen;
|
|
443
|
-
const mask = 1 << patternLen - 1;
|
|
444
|
-
for (let i = 0; i < patternLen; i += 1) {
|
|
445
|
-
let binMin = 0;
|
|
446
|
-
let binMid = binMax;
|
|
447
|
-
while (binMin < binMid) {
|
|
448
|
-
const score2 = computeScore$1(pattern, {
|
|
449
|
-
errors: i,
|
|
450
|
-
currentLocation: expectedLocation + binMid,
|
|
451
|
-
expectedLocation,
|
|
452
|
-
distance,
|
|
453
|
-
ignoreLocation
|
|
454
|
-
});
|
|
455
|
-
if (score2 <= currentThreshold) {
|
|
456
|
-
binMin = binMid;
|
|
457
|
-
} else {
|
|
458
|
-
binMax = binMid;
|
|
459
|
-
}
|
|
460
|
-
binMid = Math.floor((binMax - binMin) / 2 + binMin);
|
|
461
|
-
}
|
|
462
|
-
binMax = binMid;
|
|
463
|
-
let start = Math.max(1, expectedLocation - binMid + 1);
|
|
464
|
-
let finish = findAllMatches ? textLen : Math.min(expectedLocation + binMid, textLen) + patternLen;
|
|
465
|
-
let bitArr = Array(finish + 2);
|
|
466
|
-
bitArr[finish + 1] = (1 << i) - 1;
|
|
467
|
-
for (let j = finish; j >= start; j -= 1) {
|
|
468
|
-
let currentLocation = j - 1;
|
|
469
|
-
let charMatch = patternAlphabet[text.charAt(currentLocation)];
|
|
470
|
-
if (computeMatches) {
|
|
471
|
-
matchMask[currentLocation] = +!!charMatch;
|
|
472
|
-
}
|
|
473
|
-
bitArr[j] = (bitArr[j + 1] << 1 | 1) & charMatch;
|
|
474
|
-
if (i) {
|
|
475
|
-
bitArr[j] |= (lastBitArr[j + 1] | lastBitArr[j]) << 1 | 1 | lastBitArr[j + 1];
|
|
476
|
-
}
|
|
477
|
-
if (bitArr[j] & mask) {
|
|
478
|
-
finalScore = computeScore$1(pattern, {
|
|
479
|
-
errors: i,
|
|
480
|
-
currentLocation,
|
|
481
|
-
expectedLocation,
|
|
482
|
-
distance,
|
|
483
|
-
ignoreLocation
|
|
484
|
-
});
|
|
485
|
-
if (finalScore <= currentThreshold) {
|
|
486
|
-
currentThreshold = finalScore;
|
|
487
|
-
bestLocation = currentLocation;
|
|
488
|
-
if (bestLocation <= expectedLocation) {
|
|
489
|
-
break;
|
|
490
|
-
}
|
|
491
|
-
start = Math.max(1, 2 * expectedLocation - bestLocation);
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
const score = computeScore$1(pattern, {
|
|
496
|
-
errors: i + 1,
|
|
497
|
-
currentLocation: expectedLocation,
|
|
498
|
-
expectedLocation,
|
|
499
|
-
distance,
|
|
500
|
-
ignoreLocation
|
|
501
|
-
});
|
|
502
|
-
if (score > currentThreshold) {
|
|
503
|
-
break;
|
|
504
|
-
}
|
|
505
|
-
lastBitArr = bitArr;
|
|
506
|
-
}
|
|
507
|
-
const result = {
|
|
508
|
-
isMatch: bestLocation >= 0,
|
|
509
|
-
// Count exact matches (those with a score of 0) to be "almost" exact
|
|
510
|
-
score: Math.max(1e-3, finalScore)
|
|
511
|
-
};
|
|
512
|
-
if (computeMatches) {
|
|
513
|
-
const indices = convertMaskToIndices(matchMask, minMatchCharLength);
|
|
514
|
-
if (!indices.length) {
|
|
515
|
-
result.isMatch = false;
|
|
516
|
-
} else if (includeMatches) {
|
|
517
|
-
result.indices = indices;
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
return result;
|
|
521
|
-
}
|
|
522
|
-
function createPatternAlphabet(pattern) {
|
|
523
|
-
let mask = {};
|
|
524
|
-
for (let i = 0, len = pattern.length; i < len; i += 1) {
|
|
525
|
-
const char = pattern.charAt(i);
|
|
526
|
-
mask[char] = (mask[char] || 0) | 1 << len - i - 1;
|
|
527
|
-
}
|
|
528
|
-
return mask;
|
|
529
|
-
}
|
|
530
|
-
class BitapSearch {
|
|
531
|
-
constructor(pattern, {
|
|
532
|
-
location = Config.location,
|
|
533
|
-
threshold = Config.threshold,
|
|
534
|
-
distance = Config.distance,
|
|
535
|
-
includeMatches = Config.includeMatches,
|
|
536
|
-
findAllMatches = Config.findAllMatches,
|
|
537
|
-
minMatchCharLength = Config.minMatchCharLength,
|
|
538
|
-
isCaseSensitive = Config.isCaseSensitive,
|
|
539
|
-
ignoreLocation = Config.ignoreLocation
|
|
540
|
-
} = {}) {
|
|
541
|
-
this.options = {
|
|
542
|
-
location,
|
|
543
|
-
threshold,
|
|
544
|
-
distance,
|
|
545
|
-
includeMatches,
|
|
546
|
-
findAllMatches,
|
|
547
|
-
minMatchCharLength,
|
|
548
|
-
isCaseSensitive,
|
|
549
|
-
ignoreLocation
|
|
550
|
-
};
|
|
551
|
-
this.pattern = isCaseSensitive ? pattern : pattern.toLowerCase();
|
|
552
|
-
this.chunks = [];
|
|
553
|
-
if (!this.pattern.length) {
|
|
554
|
-
return;
|
|
555
|
-
}
|
|
556
|
-
const addChunk = (pattern2, startIndex) => {
|
|
557
|
-
this.chunks.push({
|
|
558
|
-
pattern: pattern2,
|
|
559
|
-
alphabet: createPatternAlphabet(pattern2),
|
|
560
|
-
startIndex
|
|
561
|
-
});
|
|
562
|
-
};
|
|
563
|
-
const len = this.pattern.length;
|
|
564
|
-
if (len > MAX_BITS) {
|
|
565
|
-
let i = 0;
|
|
566
|
-
const remainder = len % MAX_BITS;
|
|
567
|
-
const end = len - remainder;
|
|
568
|
-
while (i < end) {
|
|
569
|
-
addChunk(this.pattern.substr(i, MAX_BITS), i);
|
|
570
|
-
i += MAX_BITS;
|
|
571
|
-
}
|
|
572
|
-
if (remainder) {
|
|
573
|
-
const startIndex = len - MAX_BITS;
|
|
574
|
-
addChunk(this.pattern.substr(startIndex), startIndex);
|
|
575
|
-
}
|
|
576
|
-
} else {
|
|
577
|
-
addChunk(this.pattern, 0);
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
searchIn(text) {
|
|
581
|
-
const { isCaseSensitive, includeMatches } = this.options;
|
|
582
|
-
if (!isCaseSensitive) {
|
|
583
|
-
text = text.toLowerCase();
|
|
584
|
-
}
|
|
585
|
-
if (this.pattern === text) {
|
|
586
|
-
let result2 = {
|
|
587
|
-
isMatch: true,
|
|
588
|
-
score: 0
|
|
589
|
-
};
|
|
590
|
-
if (includeMatches) {
|
|
591
|
-
result2.indices = [[0, text.length - 1]];
|
|
592
|
-
}
|
|
593
|
-
return result2;
|
|
594
|
-
}
|
|
595
|
-
const {
|
|
596
|
-
location,
|
|
597
|
-
distance,
|
|
598
|
-
threshold,
|
|
599
|
-
findAllMatches,
|
|
600
|
-
minMatchCharLength,
|
|
601
|
-
ignoreLocation
|
|
602
|
-
} = this.options;
|
|
603
|
-
let allIndices = [];
|
|
604
|
-
let totalScore = 0;
|
|
605
|
-
let hasMatches = false;
|
|
606
|
-
this.chunks.forEach(({ pattern, alphabet, startIndex }) => {
|
|
607
|
-
const { isMatch, score, indices } = search(text, pattern, alphabet, {
|
|
608
|
-
location: location + startIndex,
|
|
609
|
-
distance,
|
|
610
|
-
threshold,
|
|
611
|
-
findAllMatches,
|
|
612
|
-
minMatchCharLength,
|
|
613
|
-
includeMatches,
|
|
614
|
-
ignoreLocation
|
|
615
|
-
});
|
|
616
|
-
if (isMatch) {
|
|
617
|
-
hasMatches = true;
|
|
618
|
-
}
|
|
619
|
-
totalScore += score;
|
|
620
|
-
if (isMatch && indices) {
|
|
621
|
-
allIndices = [...allIndices, ...indices];
|
|
622
|
-
}
|
|
623
|
-
});
|
|
624
|
-
let result = {
|
|
625
|
-
isMatch: hasMatches,
|
|
626
|
-
score: hasMatches ? totalScore / this.chunks.length : 1
|
|
627
|
-
};
|
|
628
|
-
if (hasMatches && includeMatches) {
|
|
629
|
-
result.indices = allIndices;
|
|
630
|
-
}
|
|
631
|
-
return result;
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
class BaseMatch {
|
|
635
|
-
constructor(pattern) {
|
|
636
|
-
this.pattern = pattern;
|
|
637
|
-
}
|
|
638
|
-
static isMultiMatch(pattern) {
|
|
639
|
-
return getMatch(pattern, this.multiRegex);
|
|
640
|
-
}
|
|
641
|
-
static isSingleMatch(pattern) {
|
|
642
|
-
return getMatch(pattern, this.singleRegex);
|
|
643
|
-
}
|
|
644
|
-
search() {
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
function getMatch(pattern, exp) {
|
|
648
|
-
const matches = pattern.match(exp);
|
|
649
|
-
return matches ? matches[1] : null;
|
|
650
|
-
}
|
|
651
|
-
class ExactMatch extends BaseMatch {
|
|
652
|
-
constructor(pattern) {
|
|
653
|
-
super(pattern);
|
|
654
|
-
}
|
|
655
|
-
static get type() {
|
|
656
|
-
return "exact";
|
|
657
|
-
}
|
|
658
|
-
static get multiRegex() {
|
|
659
|
-
return /^="(.*)"$/;
|
|
660
|
-
}
|
|
661
|
-
static get singleRegex() {
|
|
662
|
-
return /^=(.*)$/;
|
|
663
|
-
}
|
|
664
|
-
search(text) {
|
|
665
|
-
const isMatch = text === this.pattern;
|
|
666
|
-
return {
|
|
667
|
-
isMatch,
|
|
668
|
-
score: isMatch ? 0 : 1,
|
|
669
|
-
indices: [0, this.pattern.length - 1]
|
|
670
|
-
};
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
class InverseExactMatch extends BaseMatch {
|
|
674
|
-
constructor(pattern) {
|
|
675
|
-
super(pattern);
|
|
676
|
-
}
|
|
677
|
-
static get type() {
|
|
678
|
-
return "inverse-exact";
|
|
679
|
-
}
|
|
680
|
-
static get multiRegex() {
|
|
681
|
-
return /^!"(.*)"$/;
|
|
682
|
-
}
|
|
683
|
-
static get singleRegex() {
|
|
684
|
-
return /^!(.*)$/;
|
|
685
|
-
}
|
|
686
|
-
search(text) {
|
|
687
|
-
const index = text.indexOf(this.pattern);
|
|
688
|
-
const isMatch = index === -1;
|
|
689
|
-
return {
|
|
690
|
-
isMatch,
|
|
691
|
-
score: isMatch ? 0 : 1,
|
|
692
|
-
indices: [0, text.length - 1]
|
|
693
|
-
};
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
class PrefixExactMatch extends BaseMatch {
|
|
697
|
-
constructor(pattern) {
|
|
698
|
-
super(pattern);
|
|
699
|
-
}
|
|
700
|
-
static get type() {
|
|
701
|
-
return "prefix-exact";
|
|
702
|
-
}
|
|
703
|
-
static get multiRegex() {
|
|
704
|
-
return /^\^"(.*)"$/;
|
|
705
|
-
}
|
|
706
|
-
static get singleRegex() {
|
|
707
|
-
return /^\^(.*)$/;
|
|
708
|
-
}
|
|
709
|
-
search(text) {
|
|
710
|
-
const isMatch = text.startsWith(this.pattern);
|
|
711
|
-
return {
|
|
712
|
-
isMatch,
|
|
713
|
-
score: isMatch ? 0 : 1,
|
|
714
|
-
indices: [0, this.pattern.length - 1]
|
|
715
|
-
};
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
class InversePrefixExactMatch extends BaseMatch {
|
|
719
|
-
constructor(pattern) {
|
|
720
|
-
super(pattern);
|
|
721
|
-
}
|
|
722
|
-
static get type() {
|
|
723
|
-
return "inverse-prefix-exact";
|
|
724
|
-
}
|
|
725
|
-
static get multiRegex() {
|
|
726
|
-
return /^!\^"(.*)"$/;
|
|
727
|
-
}
|
|
728
|
-
static get singleRegex() {
|
|
729
|
-
return /^!\^(.*)$/;
|
|
730
|
-
}
|
|
731
|
-
search(text) {
|
|
732
|
-
const isMatch = !text.startsWith(this.pattern);
|
|
733
|
-
return {
|
|
734
|
-
isMatch,
|
|
735
|
-
score: isMatch ? 0 : 1,
|
|
736
|
-
indices: [0, text.length - 1]
|
|
737
|
-
};
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
class SuffixExactMatch extends BaseMatch {
|
|
741
|
-
constructor(pattern) {
|
|
742
|
-
super(pattern);
|
|
743
|
-
}
|
|
744
|
-
static get type() {
|
|
745
|
-
return "suffix-exact";
|
|
746
|
-
}
|
|
747
|
-
static get multiRegex() {
|
|
748
|
-
return /^"(.*)"\$$/;
|
|
749
|
-
}
|
|
750
|
-
static get singleRegex() {
|
|
751
|
-
return /^(.*)\$$/;
|
|
752
|
-
}
|
|
753
|
-
search(text) {
|
|
754
|
-
const isMatch = text.endsWith(this.pattern);
|
|
755
|
-
return {
|
|
756
|
-
isMatch,
|
|
757
|
-
score: isMatch ? 0 : 1,
|
|
758
|
-
indices: [text.length - this.pattern.length, text.length - 1]
|
|
759
|
-
};
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
class InverseSuffixExactMatch extends BaseMatch {
|
|
763
|
-
constructor(pattern) {
|
|
764
|
-
super(pattern);
|
|
765
|
-
}
|
|
766
|
-
static get type() {
|
|
767
|
-
return "inverse-suffix-exact";
|
|
768
|
-
}
|
|
769
|
-
static get multiRegex() {
|
|
770
|
-
return /^!"(.*)"\$$/;
|
|
771
|
-
}
|
|
772
|
-
static get singleRegex() {
|
|
773
|
-
return /^!(.*)\$$/;
|
|
774
|
-
}
|
|
775
|
-
search(text) {
|
|
776
|
-
const isMatch = !text.endsWith(this.pattern);
|
|
777
|
-
return {
|
|
778
|
-
isMatch,
|
|
779
|
-
score: isMatch ? 0 : 1,
|
|
780
|
-
indices: [0, text.length - 1]
|
|
781
|
-
};
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
class FuzzyMatch extends BaseMatch {
|
|
785
|
-
constructor(pattern, {
|
|
786
|
-
location = Config.location,
|
|
787
|
-
threshold = Config.threshold,
|
|
788
|
-
distance = Config.distance,
|
|
789
|
-
includeMatches = Config.includeMatches,
|
|
790
|
-
findAllMatches = Config.findAllMatches,
|
|
791
|
-
minMatchCharLength = Config.minMatchCharLength,
|
|
792
|
-
isCaseSensitive = Config.isCaseSensitive,
|
|
793
|
-
ignoreLocation = Config.ignoreLocation
|
|
794
|
-
} = {}) {
|
|
795
|
-
super(pattern);
|
|
796
|
-
this._bitapSearch = new BitapSearch(pattern, {
|
|
797
|
-
location,
|
|
798
|
-
threshold,
|
|
799
|
-
distance,
|
|
800
|
-
includeMatches,
|
|
801
|
-
findAllMatches,
|
|
802
|
-
minMatchCharLength,
|
|
803
|
-
isCaseSensitive,
|
|
804
|
-
ignoreLocation
|
|
805
|
-
});
|
|
806
|
-
}
|
|
807
|
-
static get type() {
|
|
808
|
-
return "fuzzy";
|
|
809
|
-
}
|
|
810
|
-
static get multiRegex() {
|
|
811
|
-
return /^"(.*)"$/;
|
|
812
|
-
}
|
|
813
|
-
static get singleRegex() {
|
|
814
|
-
return /^(.*)$/;
|
|
815
|
-
}
|
|
816
|
-
search(text) {
|
|
817
|
-
return this._bitapSearch.searchIn(text);
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
class IncludeMatch extends BaseMatch {
|
|
821
|
-
constructor(pattern) {
|
|
822
|
-
super(pattern);
|
|
823
|
-
}
|
|
824
|
-
static get type() {
|
|
825
|
-
return "include";
|
|
826
|
-
}
|
|
827
|
-
static get multiRegex() {
|
|
828
|
-
return /^'"(.*)"$/;
|
|
829
|
-
}
|
|
830
|
-
static get singleRegex() {
|
|
831
|
-
return /^'(.*)$/;
|
|
832
|
-
}
|
|
833
|
-
search(text) {
|
|
834
|
-
let location = 0;
|
|
835
|
-
let index;
|
|
836
|
-
const indices = [];
|
|
837
|
-
const patternLen = this.pattern.length;
|
|
838
|
-
while ((index = text.indexOf(this.pattern, location)) > -1) {
|
|
839
|
-
location = index + patternLen;
|
|
840
|
-
indices.push([index, location - 1]);
|
|
841
|
-
}
|
|
842
|
-
const isMatch = !!indices.length;
|
|
843
|
-
return {
|
|
844
|
-
isMatch,
|
|
845
|
-
score: isMatch ? 0 : 1,
|
|
846
|
-
indices
|
|
847
|
-
};
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
const searchers = [
|
|
851
|
-
ExactMatch,
|
|
852
|
-
IncludeMatch,
|
|
853
|
-
PrefixExactMatch,
|
|
854
|
-
InversePrefixExactMatch,
|
|
855
|
-
InverseSuffixExactMatch,
|
|
856
|
-
SuffixExactMatch,
|
|
857
|
-
InverseExactMatch,
|
|
858
|
-
FuzzyMatch
|
|
859
|
-
];
|
|
860
|
-
const searchersLen = searchers.length;
|
|
861
|
-
const SPACE_RE = / +(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/;
|
|
862
|
-
const OR_TOKEN = "|";
|
|
863
|
-
function parseQuery(pattern, options = {}) {
|
|
864
|
-
return pattern.split(OR_TOKEN).map((item) => {
|
|
865
|
-
let query = item.trim().split(SPACE_RE).filter((item2) => item2 && !!item2.trim());
|
|
866
|
-
let results = [];
|
|
867
|
-
for (let i = 0, len = query.length; i < len; i += 1) {
|
|
868
|
-
const queryItem = query[i];
|
|
869
|
-
let found = false;
|
|
870
|
-
let idx = -1;
|
|
871
|
-
while (!found && ++idx < searchersLen) {
|
|
872
|
-
const searcher = searchers[idx];
|
|
873
|
-
let token = searcher.isMultiMatch(queryItem);
|
|
874
|
-
if (token) {
|
|
875
|
-
results.push(new searcher(token, options));
|
|
876
|
-
found = true;
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
if (found) {
|
|
880
|
-
continue;
|
|
881
|
-
}
|
|
882
|
-
idx = -1;
|
|
883
|
-
while (++idx < searchersLen) {
|
|
884
|
-
const searcher = searchers[idx];
|
|
885
|
-
let token = searcher.isSingleMatch(queryItem);
|
|
886
|
-
if (token) {
|
|
887
|
-
results.push(new searcher(token, options));
|
|
888
|
-
break;
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
return results;
|
|
893
|
-
});
|
|
894
|
-
}
|
|
895
|
-
const MultiMatchSet = /* @__PURE__ */ new Set([FuzzyMatch.type, IncludeMatch.type]);
|
|
896
|
-
class ExtendedSearch {
|
|
897
|
-
constructor(pattern, {
|
|
898
|
-
isCaseSensitive = Config.isCaseSensitive,
|
|
899
|
-
includeMatches = Config.includeMatches,
|
|
900
|
-
minMatchCharLength = Config.minMatchCharLength,
|
|
901
|
-
ignoreLocation = Config.ignoreLocation,
|
|
902
|
-
findAllMatches = Config.findAllMatches,
|
|
903
|
-
location = Config.location,
|
|
904
|
-
threshold = Config.threshold,
|
|
905
|
-
distance = Config.distance
|
|
906
|
-
} = {}) {
|
|
907
|
-
this.query = null;
|
|
908
|
-
this.options = {
|
|
909
|
-
isCaseSensitive,
|
|
910
|
-
includeMatches,
|
|
911
|
-
minMatchCharLength,
|
|
912
|
-
findAllMatches,
|
|
913
|
-
ignoreLocation,
|
|
914
|
-
location,
|
|
915
|
-
threshold,
|
|
916
|
-
distance
|
|
917
|
-
};
|
|
918
|
-
this.pattern = isCaseSensitive ? pattern : pattern.toLowerCase();
|
|
919
|
-
this.query = parseQuery(this.pattern, this.options);
|
|
920
|
-
}
|
|
921
|
-
static condition(_, options) {
|
|
922
|
-
return options.useExtendedSearch;
|
|
923
|
-
}
|
|
924
|
-
searchIn(text) {
|
|
925
|
-
const query = this.query;
|
|
926
|
-
if (!query) {
|
|
927
|
-
return {
|
|
928
|
-
isMatch: false,
|
|
929
|
-
score: 1
|
|
930
|
-
};
|
|
931
|
-
}
|
|
932
|
-
const { includeMatches, isCaseSensitive } = this.options;
|
|
933
|
-
text = isCaseSensitive ? text : text.toLowerCase();
|
|
934
|
-
let numMatches = 0;
|
|
935
|
-
let allIndices = [];
|
|
936
|
-
let totalScore = 0;
|
|
937
|
-
for (let i = 0, qLen = query.length; i < qLen; i += 1) {
|
|
938
|
-
const searchers2 = query[i];
|
|
939
|
-
allIndices.length = 0;
|
|
940
|
-
numMatches = 0;
|
|
941
|
-
for (let j = 0, pLen = searchers2.length; j < pLen; j += 1) {
|
|
942
|
-
const searcher = searchers2[j];
|
|
943
|
-
const { isMatch, indices, score } = searcher.search(text);
|
|
944
|
-
if (isMatch) {
|
|
945
|
-
numMatches += 1;
|
|
946
|
-
totalScore += score;
|
|
947
|
-
if (includeMatches) {
|
|
948
|
-
const type = searcher.constructor.type;
|
|
949
|
-
if (MultiMatchSet.has(type)) {
|
|
950
|
-
allIndices = [...allIndices, ...indices];
|
|
951
|
-
} else {
|
|
952
|
-
allIndices.push(indices);
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
} else {
|
|
956
|
-
totalScore = 0;
|
|
957
|
-
numMatches = 0;
|
|
958
|
-
allIndices.length = 0;
|
|
959
|
-
break;
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
if (numMatches) {
|
|
963
|
-
let result = {
|
|
964
|
-
isMatch: true,
|
|
965
|
-
score: totalScore / numMatches
|
|
966
|
-
};
|
|
967
|
-
if (includeMatches) {
|
|
968
|
-
result.indices = allIndices;
|
|
969
|
-
}
|
|
970
|
-
return result;
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
return {
|
|
974
|
-
isMatch: false,
|
|
975
|
-
score: 1
|
|
976
|
-
};
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
const registeredSearchers = [];
|
|
980
|
-
function register(...args) {
|
|
981
|
-
registeredSearchers.push(...args);
|
|
982
|
-
}
|
|
983
|
-
function createSearcher(pattern, options) {
|
|
984
|
-
for (let i = 0, len = registeredSearchers.length; i < len; i += 1) {
|
|
985
|
-
let searcherClass = registeredSearchers[i];
|
|
986
|
-
if (searcherClass.condition(pattern, options)) {
|
|
987
|
-
return new searcherClass(pattern, options);
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
return new BitapSearch(pattern, options);
|
|
991
|
-
}
|
|
992
|
-
const LogicalOperator = {
|
|
993
|
-
AND: "$and",
|
|
994
|
-
OR: "$or"
|
|
995
|
-
};
|
|
996
|
-
const KeyType = {
|
|
997
|
-
PATH: "$path",
|
|
998
|
-
PATTERN: "$val"
|
|
999
|
-
};
|
|
1000
|
-
const isExpression = (query) => !!(query[LogicalOperator.AND] || query[LogicalOperator.OR]);
|
|
1001
|
-
const isPath = (query) => !!query[KeyType.PATH];
|
|
1002
|
-
const isLeaf = (query) => !isArray(query) && isObject(query) && !isExpression(query);
|
|
1003
|
-
const convertToExplicit = (query) => ({
|
|
1004
|
-
[LogicalOperator.AND]: Object.keys(query).map((key) => ({
|
|
1005
|
-
[key]: query[key]
|
|
1006
|
-
}))
|
|
1007
|
-
});
|
|
1008
|
-
function parse(query, options, { auto = true } = {}) {
|
|
1009
|
-
const next = (query2) => {
|
|
1010
|
-
let keys = Object.keys(query2);
|
|
1011
|
-
const isQueryPath = isPath(query2);
|
|
1012
|
-
if (!isQueryPath && keys.length > 1 && !isExpression(query2)) {
|
|
1013
|
-
return next(convertToExplicit(query2));
|
|
1014
|
-
}
|
|
1015
|
-
if (isLeaf(query2)) {
|
|
1016
|
-
const key = isQueryPath ? query2[KeyType.PATH] : keys[0];
|
|
1017
|
-
const pattern = isQueryPath ? query2[KeyType.PATTERN] : query2[key];
|
|
1018
|
-
if (!isString(pattern)) {
|
|
1019
|
-
throw new Error(LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key));
|
|
1020
|
-
}
|
|
1021
|
-
const obj = {
|
|
1022
|
-
keyId: createKeyId(key),
|
|
1023
|
-
pattern
|
|
1024
|
-
};
|
|
1025
|
-
if (auto) {
|
|
1026
|
-
obj.searcher = createSearcher(pattern, options);
|
|
1027
|
-
}
|
|
1028
|
-
return obj;
|
|
1029
|
-
}
|
|
1030
|
-
let node = {
|
|
1031
|
-
children: [],
|
|
1032
|
-
operator: keys[0]
|
|
1033
|
-
};
|
|
1034
|
-
keys.forEach((key) => {
|
|
1035
|
-
const value = query2[key];
|
|
1036
|
-
if (isArray(value)) {
|
|
1037
|
-
value.forEach((item) => {
|
|
1038
|
-
node.children.push(next(item));
|
|
1039
|
-
});
|
|
1040
|
-
}
|
|
1041
|
-
});
|
|
1042
|
-
return node;
|
|
1043
|
-
};
|
|
1044
|
-
if (!isExpression(query)) {
|
|
1045
|
-
query = convertToExplicit(query);
|
|
1046
|
-
}
|
|
1047
|
-
return next(query);
|
|
1048
|
-
}
|
|
1049
|
-
function computeScore(results, { ignoreFieldNorm = Config.ignoreFieldNorm }) {
|
|
1050
|
-
results.forEach((result) => {
|
|
1051
|
-
let totalScore = 1;
|
|
1052
|
-
result.matches.forEach(({ key, norm: norm2, score }) => {
|
|
1053
|
-
const weight = key ? key.weight : null;
|
|
1054
|
-
totalScore *= Math.pow(
|
|
1055
|
-
score === 0 && weight ? Number.EPSILON : score,
|
|
1056
|
-
(weight || 1) * (ignoreFieldNorm ? 1 : norm2)
|
|
1057
|
-
);
|
|
1058
|
-
});
|
|
1059
|
-
result.score = totalScore;
|
|
1060
|
-
});
|
|
1061
|
-
}
|
|
1062
|
-
function transformMatches(result, data) {
|
|
1063
|
-
const matches = result.matches;
|
|
1064
|
-
data.matches = [];
|
|
1065
|
-
if (!isDefined(matches)) {
|
|
1066
|
-
return;
|
|
1067
|
-
}
|
|
1068
|
-
matches.forEach((match) => {
|
|
1069
|
-
if (!isDefined(match.indices) || !match.indices.length) {
|
|
1070
|
-
return;
|
|
1071
|
-
}
|
|
1072
|
-
const { indices, value } = match;
|
|
1073
|
-
let obj = {
|
|
1074
|
-
indices,
|
|
1075
|
-
value
|
|
1076
|
-
};
|
|
1077
|
-
if (match.key) {
|
|
1078
|
-
obj.key = match.key.src;
|
|
1079
|
-
}
|
|
1080
|
-
if (match.idx > -1) {
|
|
1081
|
-
obj.refIndex = match.idx;
|
|
1082
|
-
}
|
|
1083
|
-
data.matches.push(obj);
|
|
1084
|
-
});
|
|
1085
|
-
}
|
|
1086
|
-
function transformScore(result, data) {
|
|
1087
|
-
data.score = result.score;
|
|
1088
|
-
}
|
|
1089
|
-
function format(results, docs, {
|
|
1090
|
-
includeMatches = Config.includeMatches,
|
|
1091
|
-
includeScore = Config.includeScore
|
|
1092
|
-
} = {}) {
|
|
1093
|
-
const transformers = [];
|
|
1094
|
-
if (includeMatches) transformers.push(transformMatches);
|
|
1095
|
-
if (includeScore) transformers.push(transformScore);
|
|
1096
|
-
return results.map((result) => {
|
|
1097
|
-
const { idx } = result;
|
|
1098
|
-
const data = {
|
|
1099
|
-
item: docs[idx],
|
|
1100
|
-
refIndex: idx
|
|
1101
|
-
};
|
|
1102
|
-
if (transformers.length) {
|
|
1103
|
-
transformers.forEach((transformer) => {
|
|
1104
|
-
transformer(result, data);
|
|
1105
|
-
});
|
|
1106
|
-
}
|
|
1107
|
-
return data;
|
|
1108
|
-
});
|
|
1109
|
-
}
|
|
1110
|
-
class Fuse {
|
|
1111
|
-
constructor(docs, options = {}, index) {
|
|
1112
|
-
this.options = { ...Config, ...options };
|
|
1113
|
-
if (this.options.useExtendedSearch && false) {
|
|
1114
|
-
throw new Error(EXTENDED_SEARCH_UNAVAILABLE);
|
|
1115
|
-
}
|
|
1116
|
-
this._keyStore = new KeyStore(this.options.keys);
|
|
1117
|
-
this.setCollection(docs, index);
|
|
1118
|
-
}
|
|
1119
|
-
setCollection(docs, index) {
|
|
1120
|
-
this._docs = docs;
|
|
1121
|
-
if (index && !(index instanceof FuseIndex)) {
|
|
1122
|
-
throw new Error(INCORRECT_INDEX_TYPE);
|
|
1123
|
-
}
|
|
1124
|
-
this._myIndex = index || createIndex(this.options.keys, this._docs, {
|
|
1125
|
-
getFn: this.options.getFn,
|
|
1126
|
-
fieldNormWeight: this.options.fieldNormWeight
|
|
1127
|
-
});
|
|
1128
|
-
}
|
|
1129
|
-
add(doc) {
|
|
1130
|
-
if (!isDefined(doc)) {
|
|
1131
|
-
return;
|
|
1132
|
-
}
|
|
1133
|
-
this._docs.push(doc);
|
|
1134
|
-
this._myIndex.add(doc);
|
|
1135
|
-
}
|
|
1136
|
-
remove(predicate = () => false) {
|
|
1137
|
-
const results = [];
|
|
1138
|
-
for (let i = 0, len = this._docs.length; i < len; i += 1) {
|
|
1139
|
-
const doc = this._docs[i];
|
|
1140
|
-
if (predicate(doc, i)) {
|
|
1141
|
-
this.removeAt(i);
|
|
1142
|
-
i -= 1;
|
|
1143
|
-
len -= 1;
|
|
1144
|
-
results.push(doc);
|
|
1145
|
-
}
|
|
1146
|
-
}
|
|
1147
|
-
return results;
|
|
1148
|
-
}
|
|
1149
|
-
removeAt(idx) {
|
|
1150
|
-
this._docs.splice(idx, 1);
|
|
1151
|
-
this._myIndex.removeAt(idx);
|
|
1152
|
-
}
|
|
1153
|
-
getIndex() {
|
|
1154
|
-
return this._myIndex;
|
|
1155
|
-
}
|
|
1156
|
-
search(query, { limit = -1 } = {}) {
|
|
1157
|
-
const {
|
|
1158
|
-
includeMatches,
|
|
1159
|
-
includeScore,
|
|
1160
|
-
shouldSort,
|
|
1161
|
-
sortFn,
|
|
1162
|
-
ignoreFieldNorm
|
|
1163
|
-
} = this.options;
|
|
1164
|
-
let results = isString(query) ? isString(this._docs[0]) ? this._searchStringList(query) : this._searchObjectList(query) : this._searchLogical(query);
|
|
1165
|
-
computeScore(results, { ignoreFieldNorm });
|
|
1166
|
-
if (shouldSort) {
|
|
1167
|
-
results.sort(sortFn);
|
|
1168
|
-
}
|
|
1169
|
-
if (isNumber(limit) && limit > -1) {
|
|
1170
|
-
results = results.slice(0, limit);
|
|
1171
|
-
}
|
|
1172
|
-
return format(results, this._docs, {
|
|
1173
|
-
includeMatches,
|
|
1174
|
-
includeScore
|
|
1175
|
-
});
|
|
1176
|
-
}
|
|
1177
|
-
_searchStringList(query) {
|
|
1178
|
-
const searcher = createSearcher(query, this.options);
|
|
1179
|
-
const { records } = this._myIndex;
|
|
1180
|
-
const results = [];
|
|
1181
|
-
records.forEach(({ v: text, i: idx, n: norm2 }) => {
|
|
1182
|
-
if (!isDefined(text)) {
|
|
1183
|
-
return;
|
|
1184
|
-
}
|
|
1185
|
-
const { isMatch, score, indices } = searcher.searchIn(text);
|
|
1186
|
-
if (isMatch) {
|
|
1187
|
-
results.push({
|
|
1188
|
-
item: text,
|
|
1189
|
-
idx,
|
|
1190
|
-
matches: [{ score, value: text, norm: norm2, indices }]
|
|
1191
|
-
});
|
|
1192
|
-
}
|
|
1193
|
-
});
|
|
1194
|
-
return results;
|
|
1195
|
-
}
|
|
1196
|
-
_searchLogical(query) {
|
|
1197
|
-
const expression = parse(query, this.options);
|
|
1198
|
-
const evaluate = (node, item, idx) => {
|
|
1199
|
-
if (!node.children) {
|
|
1200
|
-
const { keyId, searcher } = node;
|
|
1201
|
-
const matches = this._findMatches({
|
|
1202
|
-
key: this._keyStore.get(keyId),
|
|
1203
|
-
value: this._myIndex.getValueForItemAtKeyId(item, keyId),
|
|
1204
|
-
searcher
|
|
1205
|
-
});
|
|
1206
|
-
if (matches && matches.length) {
|
|
1207
|
-
return [
|
|
1208
|
-
{
|
|
1209
|
-
idx,
|
|
1210
|
-
item,
|
|
1211
|
-
matches
|
|
1212
|
-
}
|
|
1213
|
-
];
|
|
1214
|
-
}
|
|
1215
|
-
return [];
|
|
1216
|
-
}
|
|
1217
|
-
const res = [];
|
|
1218
|
-
for (let i = 0, len = node.children.length; i < len; i += 1) {
|
|
1219
|
-
const child = node.children[i];
|
|
1220
|
-
const result = evaluate(child, item, idx);
|
|
1221
|
-
if (result.length) {
|
|
1222
|
-
res.push(...result);
|
|
1223
|
-
} else if (node.operator === LogicalOperator.AND) {
|
|
1224
|
-
return [];
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
return res;
|
|
1228
|
-
};
|
|
1229
|
-
const records = this._myIndex.records;
|
|
1230
|
-
const resultMap = {};
|
|
1231
|
-
const results = [];
|
|
1232
|
-
records.forEach(({ $: item, i: idx }) => {
|
|
1233
|
-
if (isDefined(item)) {
|
|
1234
|
-
let expResults = evaluate(expression, item, idx);
|
|
1235
|
-
if (expResults.length) {
|
|
1236
|
-
if (!resultMap[idx]) {
|
|
1237
|
-
resultMap[idx] = { idx, item, matches: [] };
|
|
1238
|
-
results.push(resultMap[idx]);
|
|
1239
|
-
}
|
|
1240
|
-
expResults.forEach(({ matches }) => {
|
|
1241
|
-
resultMap[idx].matches.push(...matches);
|
|
1242
|
-
});
|
|
1243
|
-
}
|
|
1244
|
-
}
|
|
1245
|
-
});
|
|
1246
|
-
return results;
|
|
1247
|
-
}
|
|
1248
|
-
_searchObjectList(query) {
|
|
1249
|
-
const searcher = createSearcher(query, this.options);
|
|
1250
|
-
const { keys, records } = this._myIndex;
|
|
1251
|
-
const results = [];
|
|
1252
|
-
records.forEach(({ $: item, i: idx }) => {
|
|
1253
|
-
if (!isDefined(item)) {
|
|
1254
|
-
return;
|
|
1255
|
-
}
|
|
1256
|
-
let matches = [];
|
|
1257
|
-
keys.forEach((key, keyIndex) => {
|
|
1258
|
-
matches.push(
|
|
1259
|
-
...this._findMatches({
|
|
1260
|
-
key,
|
|
1261
|
-
value: item[keyIndex],
|
|
1262
|
-
searcher
|
|
1263
|
-
})
|
|
1264
|
-
);
|
|
1265
|
-
});
|
|
1266
|
-
if (matches.length) {
|
|
1267
|
-
results.push({
|
|
1268
|
-
idx,
|
|
1269
|
-
item,
|
|
1270
|
-
matches
|
|
1271
|
-
});
|
|
1272
|
-
}
|
|
1273
|
-
});
|
|
1274
|
-
return results;
|
|
1275
|
-
}
|
|
1276
|
-
_findMatches({ key, value, searcher }) {
|
|
1277
|
-
if (!isDefined(value)) {
|
|
1278
|
-
return [];
|
|
1279
|
-
}
|
|
1280
|
-
let matches = [];
|
|
1281
|
-
if (isArray(value)) {
|
|
1282
|
-
value.forEach(({ v: text, i: idx, n: norm2 }) => {
|
|
1283
|
-
if (!isDefined(text)) {
|
|
1284
|
-
return;
|
|
1285
|
-
}
|
|
1286
|
-
const { isMatch, score, indices } = searcher.searchIn(text);
|
|
1287
|
-
if (isMatch) {
|
|
1288
|
-
matches.push({
|
|
1289
|
-
score,
|
|
1290
|
-
key,
|
|
1291
|
-
value: text,
|
|
1292
|
-
idx,
|
|
1293
|
-
norm: norm2,
|
|
1294
|
-
indices
|
|
1295
|
-
});
|
|
1296
|
-
}
|
|
1297
|
-
});
|
|
1298
|
-
} else {
|
|
1299
|
-
const { v: text, n: norm2 } = value;
|
|
1300
|
-
const { isMatch, score, indices } = searcher.searchIn(text);
|
|
1301
|
-
if (isMatch) {
|
|
1302
|
-
matches.push({ score, key, value: text, norm: norm2, indices });
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
return matches;
|
|
1306
|
-
}
|
|
1307
|
-
}
|
|
1308
|
-
Fuse.version = "7.0.0";
|
|
1309
|
-
Fuse.createIndex = createIndex;
|
|
1310
|
-
Fuse.parseIndex = parseIndex;
|
|
1311
|
-
Fuse.config = Config;
|
|
1312
|
-
{
|
|
1313
|
-
Fuse.parseQuery = parse;
|
|
1314
|
-
}
|
|
1315
|
-
{
|
|
1316
|
-
register(ExtendedSearch);
|
|
1317
|
-
}
|
|
1318
|
-
function uniqID() {
|
|
1319
|
-
return `_${(Math.random() * 36 ** 20).toString(36).slice(0, 10)}`;
|
|
1320
|
-
}
|
|
1321
|
-
function stringToDom(html) {
|
|
1322
|
-
const template = document.createElement("template");
|
|
1323
|
-
template.innerHTML = html;
|
|
1324
|
-
return template.content;
|
|
1325
|
-
}
|
|
1326
|
-
const AsyncFunction = Object.getPrototypeOf(async () => {
|
|
1327
|
-
}).constructor;
|
|
1328
|
-
async function evalTemplateString(str, variables = {}) {
|
|
1329
|
-
const keys = Object.keys(variables);
|
|
1330
|
-
const values = keys.map((key) => variables[key]);
|
|
1331
|
-
try {
|
|
1332
|
-
const func = AsyncFunction.call(null, ...keys, `return \`${str}\``);
|
|
1333
|
-
return await func(...values);
|
|
1334
|
-
} catch (e) {
|
|
1335
|
-
console.log(e);
|
|
1336
|
-
throw new SyntaxError(`\`${str}\``);
|
|
1337
|
-
}
|
|
1338
|
-
}
|
|
1339
|
-
function importCSS(...stylesheets) {
|
|
1340
|
-
const linksElements = [];
|
|
1341
|
-
for (let url of stylesheets) {
|
|
1342
|
-
url = relativeSource(url);
|
|
1343
|
-
let link = Array.from(document.head.querySelectorAll("link")).find(
|
|
1344
|
-
(link2) => link2.href === url
|
|
1345
|
-
);
|
|
1346
|
-
if (link) return link;
|
|
1347
|
-
link = document.createElement("link");
|
|
1348
|
-
link.rel = "stylesheet";
|
|
1349
|
-
link.href = url;
|
|
1350
|
-
document.head.appendChild(link);
|
|
1351
|
-
linksElements.push(link);
|
|
1352
|
-
}
|
|
1353
|
-
return linksElements;
|
|
1354
|
-
}
|
|
1355
|
-
function importInlineCSS(id, importer) {
|
|
1356
|
-
id = `sib-inline-css-${id}`;
|
|
1357
|
-
let style = document.head.querySelector(`style#${id}`);
|
|
1358
|
-
if (style) return style;
|
|
1359
|
-
style = document.createElement("style");
|
|
1360
|
-
style.id = id;
|
|
1361
|
-
document.head.appendChild(style);
|
|
1362
|
-
(async () => {
|
|
1363
|
-
let textContent;
|
|
1364
|
-
if (typeof importer === "string") textContent = importer;
|
|
1365
|
-
else {
|
|
1366
|
-
const imported = await importer();
|
|
1367
|
-
if (typeof imported === "string") textContent = imported;
|
|
1368
|
-
else textContent = imported.default || "";
|
|
1369
|
-
}
|
|
1370
|
-
style.textContent = textContent;
|
|
1371
|
-
})();
|
|
1372
|
-
return style;
|
|
1373
|
-
}
|
|
1374
|
-
function importJS(...plugins) {
|
|
1375
|
-
return plugins.map((url) => {
|
|
1376
|
-
url = new URL(url, document.baseURI).href;
|
|
1377
|
-
let script = Array.from(document.querySelectorAll("script")).find(
|
|
1378
|
-
(script2) => script2.src === url
|
|
1379
|
-
);
|
|
1380
|
-
if (script) return script;
|
|
1381
|
-
script = document.createElement("script");
|
|
1382
|
-
script.src = url;
|
|
1383
|
-
document.head.appendChild(script);
|
|
1384
|
-
return script;
|
|
1385
|
-
});
|
|
1386
|
-
}
|
|
1387
|
-
function relativeSource(source) {
|
|
1388
|
-
if (!source.match(/^\..?\//)) return new URL(source, document.baseURI).href;
|
|
1389
|
-
const e = new Error();
|
|
1390
|
-
if (!e.stack) return source;
|
|
1391
|
-
const f2 = e.stack.split("\n").filter((l) => l.includes(":"))[2];
|
|
1392
|
-
const line = f2.match(/[a-z]+:.*$/);
|
|
1393
|
-
if (!line) return source;
|
|
1394
|
-
const calledFile = line[0].replace(/(\:[0-9]+){2}\)?$/, "");
|
|
1395
|
-
source = new URL(source, calledFile).href;
|
|
1396
|
-
return source;
|
|
1397
|
-
}
|
|
1398
|
-
function loadScript(source) {
|
|
1399
|
-
source = relativeSource(source);
|
|
1400
|
-
return new Promise((resolve) => {
|
|
1401
|
-
const script = document.createElement("script");
|
|
1402
|
-
const head = document.querySelector("head");
|
|
1403
|
-
script.async = true;
|
|
1404
|
-
script.onload = () => setTimeout(resolve, 0);
|
|
1405
|
-
script.src = source;
|
|
1406
|
-
if (head) head.appendChild(script);
|
|
1407
|
-
});
|
|
1408
|
-
}
|
|
1409
|
-
function domIsReady() {
|
|
1410
|
-
return new Promise((resolve) => {
|
|
1411
|
-
if (document.readyState === "complete") {
|
|
1412
|
-
resolve();
|
|
1413
|
-
} else {
|
|
1414
|
-
document.addEventListener("DOMContentLoaded", () => resolve());
|
|
1415
|
-
}
|
|
1416
|
-
});
|
|
1417
|
-
}
|
|
1418
|
-
function setDeepProperty(obj, path, value) {
|
|
1419
|
-
const name = path.shift();
|
|
1420
|
-
if (name) {
|
|
1421
|
-
if (!(name in obj)) obj[name] = {};
|
|
1422
|
-
if (path.length > 0) setDeepProperty(obj[name], path, value);
|
|
1423
|
-
else obj[name] = value;
|
|
1424
|
-
}
|
|
1425
|
-
}
|
|
1426
|
-
function parseFieldsString(fields) {
|
|
1427
|
-
if (!fields) return [];
|
|
1428
|
-
while (fields.indexOf("(") > 0) {
|
|
1429
|
-
const firstBracket = fields.indexOf("(");
|
|
1430
|
-
const noset = fields.substring(
|
|
1431
|
-
firstBracket,
|
|
1432
|
-
findClosingBracketMatchIndex(fields, firstBracket) + 1
|
|
1433
|
-
);
|
|
1434
|
-
fields = fields.replace(noset, "");
|
|
1435
|
-
}
|
|
1436
|
-
const re = /((^\s*|,)\s*)(("(\\"|[^"])*")|('(\\'|[^'])*')|[^,]*)/gm;
|
|
1437
|
-
const fieldsArray = fields.match(re) || [];
|
|
1438
|
-
if (!fieldsArray) return [];
|
|
1439
|
-
return fieldsArray.map((a) => a.replace(/^[\s,]+/, ""));
|
|
1440
|
-
}
|
|
1441
|
-
function findClosingBracketMatchIndex(str, pos) {
|
|
1442
|
-
if (str[pos] !== "(") throw new Error(`No '(' at index ${pos}`);
|
|
1443
|
-
let depth = 1;
|
|
1444
|
-
for (let i = pos + 1; i < str.length; i++) {
|
|
1445
|
-
switch (str[i]) {
|
|
1446
|
-
case "(":
|
|
1447
|
-
depth++;
|
|
1448
|
-
break;
|
|
1449
|
-
case ")":
|
|
1450
|
-
if (--depth === 0) return i;
|
|
1451
|
-
break;
|
|
1452
|
-
}
|
|
1453
|
-
}
|
|
1454
|
-
return -1;
|
|
1455
|
-
}
|
|
1456
|
-
function defineComponent(tagName, componentClass) {
|
|
1457
|
-
if (!customElements.get(tagName)) {
|
|
1458
|
-
customElements.define(tagName, componentClass);
|
|
1459
|
-
} else {
|
|
1460
|
-
console.warn(
|
|
1461
|
-
`Warning: the component "${tagName}" has already been loaded in another version of sib-core.`
|
|
1462
|
-
);
|
|
1463
|
-
}
|
|
1464
|
-
}
|
|
1465
|
-
function fuzzyCompare(subject, search2) {
|
|
1466
|
-
const fuse = new Fuse([subject], {
|
|
1467
|
-
shouldSort: false,
|
|
1468
|
-
threshold: 0.37
|
|
1469
|
-
}).search(search2);
|
|
1470
|
-
return fuse.length > 0;
|
|
1471
|
-
}
|
|
1472
|
-
const compare = {
|
|
1473
|
-
string(subject, query) {
|
|
1474
|
-
if (query === "") return true;
|
|
1475
|
-
if (subject.toString().toLowerCase().includes(String(query).toLowerCase()))
|
|
1476
|
-
return true;
|
|
1477
|
-
return fuzzyCompare(subject, query);
|
|
1478
|
-
},
|
|
1479
|
-
boolean(subject, query) {
|
|
1480
|
-
if (!query) return true;
|
|
1481
|
-
return subject;
|
|
1482
|
-
},
|
|
1483
|
-
number(subject, query) {
|
|
1484
|
-
return subject === query;
|
|
1485
|
-
},
|
|
1486
|
-
list(subject, list) {
|
|
1487
|
-
return list.includes(subject);
|
|
1488
|
-
},
|
|
1489
|
-
range(subject, range) {
|
|
1490
|
-
return (range[0] == null || range[0] === "" || subject >= range[0]) && (range[1] == null || range[1] === "" || subject <= range[1]);
|
|
1491
|
-
},
|
|
1492
|
-
resource(subject, query) {
|
|
1493
|
-
if (query === "") return true;
|
|
1494
|
-
if (!query["@id"]) return false;
|
|
1495
|
-
const ret = subject["@id"] === query["@id"];
|
|
1496
|
-
return ret;
|
|
1497
|
-
}
|
|
1498
|
-
};
|
|
1499
|
-
function generalComparator(a, b, order = "asc") {
|
|
1500
|
-
if (order === "desc") return generalComparator(b, a);
|
|
1501
|
-
if (a == null && b == null) return 0;
|
|
1502
|
-
if (a === b || Object.is(a, b)) return 0;
|
|
1503
|
-
if (typeof a === "boolean" && typeof b === "boolean") {
|
|
1504
|
-
return a === b ? 0 : a ? 1 : -1;
|
|
1505
|
-
}
|
|
1506
|
-
if (!Number.isNaN(Number(a)) && !Number.isNaN(Number(b))) {
|
|
1507
|
-
return Number(a) - Number(b);
|
|
1508
|
-
}
|
|
1509
|
-
if (Array.isArray(a) && Array.isArray(b)) {
|
|
1510
|
-
return a.length - b.length;
|
|
1511
|
-
}
|
|
1512
|
-
const dateA = Date.parse(String(a));
|
|
1513
|
-
const dateB = Date.parse(String(b));
|
|
1514
|
-
if (!Number.isNaN(dateA) && !Number.isNaN(dateB)) {
|
|
1515
|
-
return dateA - dateB;
|
|
1516
|
-
}
|
|
1517
|
-
if (a && b && typeof a === "object" && typeof b === "object") {
|
|
1518
|
-
const aKeys = Object.keys(a);
|
|
1519
|
-
const bKeys = Object.keys(b);
|
|
1520
|
-
return aKeys.length - bKeys.length;
|
|
1521
|
-
}
|
|
1522
|
-
if (a == null) return -1;
|
|
1523
|
-
if (b == null) return 1;
|
|
1524
|
-
return String(a).localeCompare(String(b));
|
|
1525
|
-
}
|
|
1526
|
-
function transformArrayToContainer(resource) {
|
|
1527
|
-
const newValue = { ...resource };
|
|
1528
|
-
for (const predicate of Object.keys(newValue)) {
|
|
1529
|
-
const predicateValue = newValue[predicate];
|
|
1530
|
-
if (!predicateValue || typeof predicateValue !== "object") continue;
|
|
1531
|
-
if (["permissions", "@context"].includes(predicate)) continue;
|
|
1532
|
-
if (!Array.isArray(predicateValue) && predicateValue["@id"]) {
|
|
1533
|
-
newValue[predicate] = transformArrayToContainer(resource[predicate]);
|
|
1534
|
-
}
|
|
1535
|
-
if (Array.isArray(predicateValue) && predicateValue["@id"]) {
|
|
1536
|
-
newValue[predicate] = {
|
|
1537
|
-
"@id": predicateValue["@id"],
|
|
1538
|
-
"ldp:contains": [...predicateValue]
|
|
1539
|
-
};
|
|
1540
|
-
newValue[predicate]["ldp:contains"].forEach(
|
|
1541
|
-
(childPredicate, index) => {
|
|
1542
|
-
newValue[predicate]["ldp:contains"][index] = transformArrayToContainer(childPredicate);
|
|
1543
|
-
}
|
|
1544
|
-
);
|
|
1545
|
-
}
|
|
1546
|
-
}
|
|
1547
|
-
return newValue;
|
|
1548
|
-
}
|
|
1549
|
-
class AsyncIterableBuilder {
|
|
1550
|
-
constructor() {
|
|
1551
|
-
__privateAdd(this, _AsyncIterableBuilder_instances);
|
|
1552
|
-
__privateAdd(this, _values, []);
|
|
1553
|
-
__privateAdd(this, _resolve);
|
|
1554
|
-
__publicField(this, "iterable");
|
|
1555
|
-
__publicField(this, "next");
|
|
1556
|
-
__privateMethod(this, _AsyncIterableBuilder_instances, nextPromise_fn).call(this);
|
|
1557
|
-
this.iterable = __privateMethod(this, _AsyncIterableBuilder_instances, createIterable_fn).call(this);
|
|
1558
|
-
this.next = __privateMethod(this, _AsyncIterableBuilder_instances, next_fn).bind(this);
|
|
1559
|
-
}
|
|
1560
|
-
}
|
|
1561
|
-
_values = new WeakMap();
|
|
1562
|
-
_resolve = new WeakMap();
|
|
1563
|
-
_AsyncIterableBuilder_instances = new WeakSet();
|
|
1564
|
-
createIterable_fn = async function* () {
|
|
1565
|
-
for (let index = 0; ; index++) {
|
|
1566
|
-
const { value, done } = await __privateGet(this, _values)[index];
|
|
1567
|
-
delete __privateGet(this, _values)[index];
|
|
1568
|
-
yield value;
|
|
1569
|
-
if (done) return;
|
|
1570
|
-
}
|
|
1571
|
-
};
|
|
1572
|
-
next_fn = function(value, done = false) {
|
|
1573
|
-
__privateGet(this, _resolve).call(this, { value, done });
|
|
1574
|
-
__privateMethod(this, _AsyncIterableBuilder_instances, nextPromise_fn).call(this);
|
|
1575
|
-
};
|
|
1576
|
-
nextPromise_fn = function() {
|
|
1577
|
-
__privateGet(this, _values).push(
|
|
1578
|
-
new Promise((resolve) => {
|
|
1579
|
-
__privateSet(this, _resolve, resolve);
|
|
1580
|
-
})
|
|
1581
|
-
);
|
|
1582
|
-
};
|
|
1583
|
-
const asyncQuerySelector = (selector, parent = document) => new Promise((resolve) => {
|
|
1584
|
-
const element = parent.querySelector(selector);
|
|
1585
|
-
if (element) return resolve(element);
|
|
1586
|
-
const observer = new MutationObserver(() => {
|
|
1587
|
-
const element2 = parent.querySelector(selector);
|
|
1588
|
-
if (!element2) return;
|
|
1589
|
-
observer.disconnect();
|
|
1590
|
-
return resolve(element2);
|
|
1591
|
-
});
|
|
1592
|
-
observer.observe(parent, {
|
|
1593
|
-
subtree: true,
|
|
1594
|
-
childList: true,
|
|
1595
|
-
attributes: true
|
|
1596
|
-
});
|
|
1597
|
-
});
|
|
1598
|
-
const asyncQuerySelectorAll = (selector, parent = document) => {
|
|
1599
|
-
const delivered = /* @__PURE__ */ new WeakSet();
|
|
1600
|
-
const { next, iterable } = new AsyncIterableBuilder();
|
|
1601
|
-
function checkNewElement() {
|
|
1602
|
-
for (const element of parent.querySelectorAll(selector)) {
|
|
1603
|
-
if (delivered.has(element)) continue;
|
|
1604
|
-
delivered.add(element);
|
|
1605
|
-
next(element);
|
|
1606
|
-
}
|
|
1607
|
-
}
|
|
1608
|
-
checkNewElement();
|
|
1609
|
-
const observer = new MutationObserver(checkNewElement);
|
|
1610
|
-
observer.observe(parent, {
|
|
1611
|
-
subtree: true,
|
|
1612
|
-
childList: true,
|
|
1613
|
-
attributes: true
|
|
1614
|
-
});
|
|
1615
|
-
return iterable;
|
|
1616
|
-
};
|
|
1617
|
-
const helpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
1618
|
-
__proto__: null,
|
|
1619
|
-
AsyncIterableBuilder,
|
|
1620
|
-
asyncQuerySelector,
|
|
1621
|
-
asyncQuerySelectorAll,
|
|
1622
|
-
compare,
|
|
1623
|
-
default: AsyncIterableBuilder,
|
|
1624
|
-
defineComponent,
|
|
1625
|
-
domIsReady,
|
|
1626
|
-
evalTemplateString,
|
|
1627
|
-
findClosingBracketMatchIndex,
|
|
1628
|
-
fuzzyCompare,
|
|
1629
|
-
generalComparator,
|
|
1630
|
-
importCSS,
|
|
1631
|
-
importInlineCSS,
|
|
1632
|
-
importJS,
|
|
1633
|
-
loadScript,
|
|
1634
|
-
parseFieldsString,
|
|
1635
|
-
setDeepProperty,
|
|
1636
|
-
stringToDom,
|
|
1637
|
-
transformArrayToContainer,
|
|
1638
|
-
uniqID
|
|
1639
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
1640
|
-
export {
|
|
1641
|
-
AsyncIterableBuilder as A,
|
|
1642
|
-
asyncQuerySelector as a,
|
|
1643
|
-
fuzzyCompare as b,
|
|
1644
|
-
compare as c,
|
|
1645
|
-
defineComponent as d,
|
|
1646
|
-
evalTemplateString as e,
|
|
1647
|
-
findClosingBracketMatchIndex as f,
|
|
1648
|
-
generalComparator as g,
|
|
1649
|
-
importCSS as h,
|
|
1650
|
-
importInlineCSS as i,
|
|
1651
|
-
helpers as j,
|
|
1652
|
-
stringToDom as k,
|
|
1653
|
-
importJS as l,
|
|
1654
|
-
loadScript as m,
|
|
1655
|
-
domIsReady as n,
|
|
1656
|
-
asyncQuerySelectorAll as o,
|
|
1657
|
-
parseFieldsString as p,
|
|
1658
|
-
setDeepProperty as s,
|
|
1659
|
-
transformArrayToContainer as t,
|
|
1660
|
-
uniqID as u
|
|
1661
|
-
};
|