@orchidjs/sifter 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sifter.d.ts +72 -0
- package/dist/cjs/sifter.js +290 -1110
- package/dist/cjs/sifter.js.map +1 -1
- package/dist/cjs/types.d.ts +57 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/utils.d.ts +48 -0
- package/dist/cjs/utils.js +102 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/sifter.d.ts +72 -0
- package/dist/esm/sifter.js +267 -330
- package/dist/esm/sifter.js.map +1 -1
- package/dist/esm/types.d.ts +57 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/utils.d.ts +48 -0
- package/dist/esm/utils.js +54 -56
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/sifter.d.ts +3 -2
- package/dist/types/types.d.ts +12 -12
- package/dist/types/utils.d.ts +2 -2
- package/dist/umd/package.json +1 -0
- package/dist/umd/sifter.js +794 -1043
- package/dist/umd/sifter.js.map +1 -1
- package/dist/umd/sifter.min.js +72 -77
- package/dist/umd/sifter.min.js.map +1 -1
- package/lib/sifter.ts +3 -2
- package/lib/types.ts +1 -1
- package/lib/utils.ts +1 -1
- package/package.json +31 -21
- package/dist/esm/lib/sifter.js +0 -368
- package/dist/esm/lib/sifter.js.map +0 -1
- package/dist/esm/lib/utils.js +0 -95
- package/dist/esm/lib/utils.js.map +0 -1
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js +0 -556
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js.map +0 -1
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js +0 -113
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js.map +0 -1
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js +0 -29
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js.map +0 -1
package/dist/umd/sifter.js
CHANGED
|
@@ -1,1154 +1,905 @@
|
|
|
1
1
|
/*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */
|
|
2
2
|
(function (global, factory) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}(this, (function (exports) { 'use strict';
|
|
3
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
4
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
5
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.sifter = {}));
|
|
6
|
+
})(this, (function (exports) { 'use strict';
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const arrayToPattern = chars => {
|
|
18
|
-
chars = chars.filter(Boolean);
|
|
19
|
-
|
|
20
|
-
if (chars.length < 2) {
|
|
21
|
-
return chars[0] || '';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return maxValueLength(chars) == 1 ? '[' + chars.join('') + ']' : '(?:' + chars.join('|') + ')';
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* @param {string[]} array
|
|
28
|
-
* @return {string}
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const sequencePattern = array => {
|
|
33
|
-
if (!hasDuplicates(array)) {
|
|
34
|
-
return array.join('');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let pattern = '';
|
|
38
|
-
let prev_char_count = 0;
|
|
39
|
-
|
|
40
|
-
const prev_pattern = () => {
|
|
41
|
-
if (prev_char_count > 1) {
|
|
42
|
-
pattern += '{' + prev_char_count + '}';
|
|
8
|
+
/**
|
|
9
|
+
* Convert array of strings to a regular expression
|
|
10
|
+
* ex ['ab','a'] => (?:ab|a)
|
|
11
|
+
* ex ['a','b'] => [ab]
|
|
12
|
+
*/
|
|
13
|
+
const arrayToPattern = chars => {
|
|
14
|
+
chars = chars.filter(Boolean);
|
|
15
|
+
if (chars.length < 2) {
|
|
16
|
+
return chars[0] || '';
|
|
43
17
|
}
|
|
18
|
+
return maxValueLength(chars) == 1 ? '[' + chars.join('') + ']' : '(?:' + chars.join('|') + ')';
|
|
44
19
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
prev_char_count++;
|
|
49
|
-
return;
|
|
20
|
+
const sequencePattern = array => {
|
|
21
|
+
if (!hasDuplicates(array)) {
|
|
22
|
+
return array.join('');
|
|
50
23
|
}
|
|
51
|
-
|
|
24
|
+
let pattern = '';
|
|
25
|
+
let prev_char_count = 0;
|
|
26
|
+
const prev_pattern = () => {
|
|
27
|
+
if (prev_char_count > 1) {
|
|
28
|
+
pattern += '{' + prev_char_count + '}';
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
array.forEach((char, i) => {
|
|
32
|
+
if (char === array[i - 1]) {
|
|
33
|
+
prev_char_count++;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
prev_pattern();
|
|
37
|
+
pattern += char;
|
|
38
|
+
prev_char_count = 1;
|
|
39
|
+
});
|
|
52
40
|
prev_pattern();
|
|
53
|
-
pattern
|
|
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
|
-
* @return {string}
|
|
86
|
-
*/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const escape_regex = str => {
|
|
90
|
-
return (str + '').replace(/([\$\(-\+\.\?\[-\^\{-\}])/g, '\\$1');
|
|
91
|
-
};
|
|
92
|
-
/**
|
|
93
|
-
* Return the max length of array values
|
|
94
|
-
* @param {string[]} array
|
|
95
|
-
*
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const maxValueLength = array => {
|
|
100
|
-
return array.reduce((longest, value) => Math.max(longest, unicodeLength(value)), 0);
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* @param {string} str
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const unicodeLength = str => {
|
|
108
|
-
return toArray(str).length;
|
|
109
|
-
};
|
|
110
|
-
/**
|
|
111
|
-
* @param {any} p
|
|
112
|
-
* @return {any[]}
|
|
113
|
-
*/
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const toArray = p => Array.from(p);
|
|
117
|
-
|
|
118
|
-
/*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Get all possible combinations of substrings that add up to the given string
|
|
122
|
-
* https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
|
|
123
|
-
* @param {string} input
|
|
124
|
-
* @return {string[][]}
|
|
125
|
-
*/
|
|
126
|
-
const allSubstrings = input => {
|
|
127
|
-
if (input.length === 1) return [[input]];
|
|
128
|
-
/** @type {string[][]} */
|
|
129
|
-
|
|
130
|
-
let result = [];
|
|
131
|
-
const start = input.substring(1);
|
|
132
|
-
const suba = allSubstrings(start);
|
|
133
|
-
suba.forEach(function (subresult) {
|
|
134
|
-
let tmp = subresult.slice(0);
|
|
135
|
-
tmp[0] = input.charAt(0) + tmp[0];
|
|
136
|
-
result.push(tmp);
|
|
137
|
-
tmp = subresult.slice(0);
|
|
138
|
-
tmp.unshift(input.charAt(0));
|
|
139
|
-
result.push(tmp);
|
|
140
|
-
});
|
|
141
|
-
return result;
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
|
|
145
|
-
/**
|
|
146
|
-
* @typedef {{[key:string]:string}} TUnicodeMap
|
|
147
|
-
* @typedef {{[key:string]:Set<string>}} TUnicodeSets
|
|
148
|
-
* @typedef {[[number,number]]} TCodePoints
|
|
149
|
-
* @typedef {{folded:string,composed:string,code_point:number}} TCodePointObj
|
|
150
|
-
* @typedef {{start:number,end:number,length:number,substr:string}} TSequencePart
|
|
151
|
-
*/
|
|
152
|
-
|
|
153
|
-
/** @type {TCodePoints} */
|
|
154
|
-
|
|
155
|
-
const code_points = [[0, 65535]];
|
|
156
|
-
const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}\u{2bc}]';
|
|
157
|
-
/** @type {TUnicodeMap} */
|
|
158
|
-
|
|
159
|
-
let unicode_map;
|
|
160
|
-
/** @type {RegExp} */
|
|
161
|
-
|
|
162
|
-
let multi_char_reg;
|
|
163
|
-
const max_char_length = 3;
|
|
164
|
-
/** @type {TUnicodeMap} */
|
|
165
|
-
|
|
166
|
-
const latin_convert = {};
|
|
167
|
-
/** @type {TUnicodeMap} */
|
|
168
|
-
|
|
169
|
-
const latin_condensed = {
|
|
170
|
-
'/': '⁄∕',
|
|
171
|
-
'0': '߀',
|
|
172
|
-
"a": "ⱥɐɑ",
|
|
173
|
-
"aa": "ꜳ",
|
|
174
|
-
"ae": "æǽǣ",
|
|
175
|
-
"ao": "ꜵ",
|
|
176
|
-
"au": "ꜷ",
|
|
177
|
-
"av": "ꜹꜻ",
|
|
178
|
-
"ay": "ꜽ",
|
|
179
|
-
"b": "ƀɓƃ",
|
|
180
|
-
"c": "ꜿƈȼↄ",
|
|
181
|
-
"d": "đɗɖᴅƌꮷԁɦ",
|
|
182
|
-
"e": "ɛǝᴇɇ",
|
|
183
|
-
"f": "ꝼƒ",
|
|
184
|
-
"g": "ǥɠꞡᵹꝿɢ",
|
|
185
|
-
"h": "ħⱨⱶɥ",
|
|
186
|
-
"i": "i̇ɨı",
|
|
187
|
-
"j": "ɉȷ",
|
|
188
|
-
"k": "ƙⱪꝁꝃꝅꞣ",
|
|
189
|
-
"l": "łƚɫⱡꝉꝇꞁɭ",
|
|
190
|
-
"m": "ɱɯϻ",
|
|
191
|
-
"n": "ꞥƞɲꞑᴎлԉ",
|
|
192
|
-
"o": "øǿɔɵꝋꝍᴑ",
|
|
193
|
-
"oe": "œ",
|
|
194
|
-
"oi": "ƣ",
|
|
195
|
-
"oo": "ꝏ",
|
|
196
|
-
"ou": "ȣ",
|
|
197
|
-
"p": "ƥᵽꝑꝓꝕρ",
|
|
198
|
-
"q": "ꝗꝙɋ",
|
|
199
|
-
"r": "ɍɽꝛꞧꞃ",
|
|
200
|
-
"s": "ßȿꞩꞅʂṧṩ",
|
|
201
|
-
"t": "ŧƭʈⱦꞇ",
|
|
202
|
-
"th": "þ",
|
|
203
|
-
"tz": "ꜩ",
|
|
204
|
-
"u": "ʉ",
|
|
205
|
-
"v": "ʋꝟʌ",
|
|
206
|
-
"vy": "ꝡ",
|
|
207
|
-
"w": "ⱳ",
|
|
208
|
-
"y": "ƴɏỿ",
|
|
209
|
-
"z": "ƶȥɀⱬꝣ",
|
|
210
|
-
"hv": "ƕ"
|
|
211
|
-
};
|
|
41
|
+
return pattern;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Convert array of strings to a regular expression
|
|
45
|
+
* ex ['ab','a'] => (?:ab|a)
|
|
46
|
+
* ex ['a','b'] => [ab]
|
|
47
|
+
*/
|
|
48
|
+
const setToPattern = chars => {
|
|
49
|
+
let array = Array.from(chars);
|
|
50
|
+
return arrayToPattern(array);
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values
|
|
54
|
+
*/
|
|
55
|
+
const hasDuplicates = array => {
|
|
56
|
+
return new Set(array).size !== array.length;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error
|
|
60
|
+
*/
|
|
61
|
+
const escape_regex = str => {
|
|
62
|
+
return (str + '').replace(/([\$\(-\+\.\?\[-\^\{-\}])/g, '\\$1');
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Return the max length of array values
|
|
66
|
+
*/
|
|
67
|
+
const maxValueLength = array => {
|
|
68
|
+
return array.reduce((longest, value) => Math.max(longest, unicodeLength(value)), 0);
|
|
69
|
+
};
|
|
70
|
+
const unicodeLength = str => {
|
|
71
|
+
return Array.from(str).length;
|
|
72
|
+
};
|
|
212
73
|
|
|
213
|
-
|
|
214
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Get all possible combinations of substrings that add up to the given string
|
|
76
|
+
* https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
|
|
77
|
+
*/
|
|
78
|
+
const allSubstrings = input => {
|
|
79
|
+
if (input.length === 1) return [[input]];
|
|
80
|
+
let result = [];
|
|
81
|
+
const start = input.substring(1);
|
|
82
|
+
const suba = allSubstrings(start);
|
|
83
|
+
suba.forEach(function (subresult) {
|
|
84
|
+
let tmp = subresult.slice(0);
|
|
85
|
+
tmp[0] = input.charAt(0) + tmp[0];
|
|
86
|
+
result.push(tmp);
|
|
87
|
+
tmp = subresult.slice(0);
|
|
88
|
+
tmp.unshift(input.charAt(0));
|
|
89
|
+
result.push(tmp);
|
|
90
|
+
});
|
|
91
|
+
return result;
|
|
92
|
+
};
|
|
215
93
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
94
|
+
const code_points = [[0, 65535]];
|
|
95
|
+
const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}\u{2bc}]';
|
|
96
|
+
let unicode_map;
|
|
97
|
+
let multi_char_reg;
|
|
98
|
+
const max_char_length = 3;
|
|
99
|
+
const latin_convert = {};
|
|
100
|
+
const latin_condensed = {
|
|
101
|
+
'/': '⁄∕',
|
|
102
|
+
'0': '߀',
|
|
103
|
+
"a": "ⱥɐɑ",
|
|
104
|
+
"aa": "ꜳ",
|
|
105
|
+
"ae": "æǽǣ",
|
|
106
|
+
"ao": "ꜵ",
|
|
107
|
+
"au": "ꜷ",
|
|
108
|
+
"av": "ꜹꜻ",
|
|
109
|
+
"ay": "ꜽ",
|
|
110
|
+
"b": "ƀɓƃ",
|
|
111
|
+
"c": "ꜿƈȼↄ",
|
|
112
|
+
"d": "đɗɖᴅƌꮷԁɦ",
|
|
113
|
+
"e": "ɛǝᴇɇ",
|
|
114
|
+
"f": "ꝼƒ",
|
|
115
|
+
"g": "ǥɠꞡᵹꝿɢ",
|
|
116
|
+
"h": "ħⱨⱶɥ",
|
|
117
|
+
"i": "ɨı",
|
|
118
|
+
"j": "ɉȷ",
|
|
119
|
+
"k": "ƙⱪꝁꝃꝅꞣ",
|
|
120
|
+
"l": "łƚɫⱡꝉꝇꞁɭ",
|
|
121
|
+
"m": "ɱɯϻ",
|
|
122
|
+
"n": "ꞥƞɲꞑᴎлԉ",
|
|
123
|
+
"o": "øǿɔɵꝋꝍᴑ",
|
|
124
|
+
"oe": "œ",
|
|
125
|
+
"oi": "ƣ",
|
|
126
|
+
"oo": "ꝏ",
|
|
127
|
+
"ou": "ȣ",
|
|
128
|
+
"p": "ƥᵽꝑꝓꝕρ",
|
|
129
|
+
"q": "ꝗꝙɋ",
|
|
130
|
+
"r": "ɍɽꝛꞧꞃ",
|
|
131
|
+
"s": "ßȿꞩꞅʂ",
|
|
132
|
+
"t": "ŧƭʈⱦꞇ",
|
|
133
|
+
"th": "þ",
|
|
134
|
+
"tz": "ꜩ",
|
|
135
|
+
"u": "ʉ",
|
|
136
|
+
"v": "ʋꝟʌ",
|
|
137
|
+
"vy": "ꝡ",
|
|
138
|
+
"w": "ⱳ",
|
|
139
|
+
"y": "ƴɏỿ",
|
|
140
|
+
"z": "ƶȥɀⱬꝣ",
|
|
141
|
+
"hv": "ƕ"
|
|
142
|
+
};
|
|
143
|
+
for (let latin in latin_condensed) {
|
|
144
|
+
let unicode = latin_condensed[latin] || '';
|
|
145
|
+
for (let i = 0; i < unicode.length; i++) {
|
|
146
|
+
let char = unicode.substring(i, i + 1);
|
|
147
|
+
latin_convert[char] = latin;
|
|
148
|
+
}
|
|
219
149
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const normalize = (str, form = 'NFKD') => str.normalize(form);
|
|
242
|
-
/**
|
|
243
|
-
* Compatibility Decomposition without reordering string
|
|
244
|
-
* calling str.normalize('NFKD') on \u{594}\u{595}\u{596} becomes \u{596}\u{594}\u{595}
|
|
245
|
-
* @param {string} str
|
|
246
|
-
*/
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
const decompose = str => {
|
|
250
|
-
if (str.match(/[\u0f71-\u0f81]/)) {
|
|
251
|
-
return toArray(str).reduce(
|
|
150
|
+
const convert_pat = new RegExp(Object.keys(latin_convert).join('|') + '|' + accent_pat, 'gu');
|
|
151
|
+
/**
|
|
152
|
+
* Initialize the unicode_map from the give code point ranges
|
|
153
|
+
*/
|
|
154
|
+
const initialize = _code_points => {
|
|
155
|
+
if (unicode_map !== undefined) return;
|
|
156
|
+
unicode_map = generateMap(code_points);
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Helper method for normalize a string
|
|
160
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
|
|
161
|
+
*/
|
|
162
|
+
const normalize = (str, form = 'NFKD') => str.normalize(form);
|
|
163
|
+
/**
|
|
164
|
+
* Remove accents without reordering string
|
|
165
|
+
* calling str.normalize('NFKD') on \u{594}\u{595}\u{596} becomes \u{596}\u{594}\u{595}
|
|
166
|
+
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
|
|
167
|
+
*/
|
|
168
|
+
const asciifold = str => {
|
|
169
|
+
return Array.from(str).reduce(
|
|
252
170
|
/**
|
|
253
171
|
* @param {string} result
|
|
254
172
|
* @param {string} char
|
|
255
173
|
*/
|
|
256
174
|
(result, char) => {
|
|
257
|
-
return result +
|
|
175
|
+
return result + _asciifold(char);
|
|
258
176
|
}, '');
|
|
177
|
+
};
|
|
178
|
+
const _asciifold = str => {
|
|
179
|
+
str = normalize(str).toLowerCase().replace(convert_pat, (/** @type {string} */char) => {
|
|
180
|
+
return latin_convert[char] || '';
|
|
181
|
+
});
|
|
182
|
+
//return str;
|
|
183
|
+
return normalize(str, 'NFC');
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Generate a list of unicode variants from the list of code points
|
|
187
|
+
*/
|
|
188
|
+
function* generator(code_points) {
|
|
189
|
+
for (const [code_point_min, code_point_max] of code_points) {
|
|
190
|
+
for (let i = code_point_min; i <= code_point_max; i++) {
|
|
191
|
+
let composed = String.fromCharCode(i);
|
|
192
|
+
let folded = asciifold(composed);
|
|
193
|
+
if (folded == composed.toLowerCase()) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
// skip when folded is a string longer than 3 characters long
|
|
197
|
+
// bc the resulting regex patterns will be long
|
|
198
|
+
// eg:
|
|
199
|
+
// folded صلى الله عليه وسلم length 18 code point 65018
|
|
200
|
+
// folded جل جلاله length 8 code point 65019
|
|
201
|
+
if (folded.length > max_char_length) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (folded.length == 0) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
yield {
|
|
208
|
+
folded: folded,
|
|
209
|
+
composed: composed,
|
|
210
|
+
code_point: i
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
259
214
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const asciifold = str => {
|
|
272
|
-
return decompose(str).toLowerCase().replace(convert_pat,
|
|
273
|
-
/** @type {string} */
|
|
274
|
-
char => {
|
|
275
|
-
return latin_convert[char] || '';
|
|
276
|
-
});
|
|
277
|
-
};
|
|
278
|
-
/**
|
|
279
|
-
* Generate a list of unicode variants from the list of code points
|
|
280
|
-
* @param {TCodePoints} code_points
|
|
281
|
-
* @yield {TCodePointObj}
|
|
282
|
-
*/
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
function* generator(code_points) {
|
|
286
|
-
for (const [code_point_min, code_point_max] of code_points) {
|
|
287
|
-
for (let i = code_point_min; i <= code_point_max; i++) {
|
|
288
|
-
let composed = String.fromCharCode(i);
|
|
289
|
-
let folded = asciifold(composed);
|
|
290
|
-
|
|
291
|
-
if (folded == composed.toLowerCase()) {
|
|
292
|
-
continue;
|
|
293
|
-
} // skip when folded is a string longer than 3 characters long
|
|
294
|
-
// bc the resulting regex patterns will be long
|
|
295
|
-
// eg:
|
|
296
|
-
// folded صلى الله عليه وسلم length 18 code point 65018
|
|
297
|
-
// folded جل جلاله length 8 code point 65019
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
if (folded.length > max_char_length) {
|
|
301
|
-
continue;
|
|
215
|
+
/**
|
|
216
|
+
* Generate a unicode map from the list of code points
|
|
217
|
+
*/
|
|
218
|
+
const generateSets = code_points => {
|
|
219
|
+
const unicode_sets = {};
|
|
220
|
+
const addMatching = (folded, to_add) => {
|
|
221
|
+
/** @type {Set<string>} */
|
|
222
|
+
const folded_set = unicode_sets[folded] || new Set();
|
|
223
|
+
const patt = new RegExp('^' + setToPattern(folded_set) + '$', 'iu');
|
|
224
|
+
if (to_add.match(patt)) {
|
|
225
|
+
return;
|
|
302
226
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
227
|
+
folded_set.add(escape_regex(to_add));
|
|
228
|
+
unicode_sets[folded] = folded_set;
|
|
229
|
+
};
|
|
230
|
+
for (let value of generator(code_points)) {
|
|
231
|
+
addMatching(value.folded, value.folded);
|
|
232
|
+
addMatching(value.folded, value.composed);
|
|
233
|
+
}
|
|
234
|
+
return unicode_sets;
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* Generate a unicode map from the list of code points
|
|
238
|
+
* ae => (?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))
|
|
239
|
+
*/
|
|
240
|
+
const generateMap = code_points => {
|
|
241
|
+
const unicode_sets = generateSets(code_points);
|
|
242
|
+
const unicode_map = {};
|
|
243
|
+
let multi_char = [];
|
|
244
|
+
for (let folded in unicode_sets) {
|
|
245
|
+
let set = unicode_sets[folded];
|
|
246
|
+
if (set) {
|
|
247
|
+
unicode_map[folded] = setToPattern(set);
|
|
306
248
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
let recomposed = normalize(decomposed, 'NFC');
|
|
310
|
-
|
|
311
|
-
if (recomposed === composed && folded === decomposed) {
|
|
312
|
-
continue;
|
|
249
|
+
if (folded.length > 1) {
|
|
250
|
+
multi_char.push(escape_regex(folded));
|
|
313
251
|
}
|
|
314
|
-
|
|
315
|
-
yield {
|
|
316
|
-
folded: folded,
|
|
317
|
-
composed: composed,
|
|
318
|
-
code_point: i
|
|
319
|
-
};
|
|
320
252
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
* @return {TUnicodeSets}
|
|
327
|
-
*/
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
const generateSets = code_points => {
|
|
331
|
-
/** @type {{[key:string]:Set<string>}} */
|
|
332
|
-
const unicode_sets = {};
|
|
253
|
+
multi_char.sort((a, b) => b.length - a.length);
|
|
254
|
+
const multi_char_patt = arrayToPattern(multi_char);
|
|
255
|
+
multi_char_reg = new RegExp('^' + multi_char_patt, 'u');
|
|
256
|
+
return unicode_map;
|
|
257
|
+
};
|
|
333
258
|
/**
|
|
334
|
-
*
|
|
335
|
-
* @param {string} to_add
|
|
259
|
+
* Map each element of an array from its folded value to all possible unicode matches
|
|
336
260
|
*/
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
261
|
+
const mapSequence = (strings, min_replacement = 1) => {
|
|
262
|
+
let chars_replaced = 0;
|
|
263
|
+
strings = strings.map(str => {
|
|
264
|
+
if (unicode_map[str]) {
|
|
265
|
+
chars_replaced += str.length;
|
|
266
|
+
}
|
|
267
|
+
return unicode_map[str] || str;
|
|
268
|
+
});
|
|
269
|
+
if (chars_replaced >= min_replacement) {
|
|
270
|
+
return sequencePattern(strings);
|
|
345
271
|
}
|
|
346
|
-
|
|
347
|
-
folded_set.add(escape_regex(to_add));
|
|
348
|
-
unicode_sets[folded] = folded_set;
|
|
272
|
+
return '';
|
|
349
273
|
};
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
274
|
+
/**
|
|
275
|
+
* Convert a short string and split it into all possible patterns
|
|
276
|
+
* Keep a pattern only if min_replacement is met
|
|
277
|
+
*
|
|
278
|
+
* 'abc'
|
|
279
|
+
* => [['abc'],['ab','c'],['a','bc'],['a','b','c']]
|
|
280
|
+
* => ['abc-pattern','ab-c-pattern'...]
|
|
281
|
+
*/
|
|
282
|
+
const substringsToPattern = (str, min_replacement = 1) => {
|
|
283
|
+
min_replacement = Math.max(min_replacement, str.length - 1);
|
|
284
|
+
return arrayToPattern(allSubstrings(str).map(sub_pat => {
|
|
285
|
+
return mapSequence(sub_pat, min_replacement);
|
|
286
|
+
}));
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* Convert an array of sequences into a pattern
|
|
290
|
+
* [{start:0,end:3,length:3,substr:'iii'}...] => (?:iii...)
|
|
291
|
+
*/
|
|
292
|
+
const sequencesToPattern = (sequences, all = true) => {
|
|
293
|
+
let min_replacement = sequences.length > 1 ? 1 : 0;
|
|
294
|
+
return arrayToPattern(sequences.map(sequence => {
|
|
295
|
+
let seq = [];
|
|
296
|
+
const len = all ? sequence.length() : sequence.length() - 1;
|
|
297
|
+
for (let j = 0; j < len; j++) {
|
|
298
|
+
seq.push(substringsToPattern(sequence.substrs[j] || '', min_replacement));
|
|
299
|
+
}
|
|
300
|
+
return sequencePattern(seq);
|
|
301
|
+
}));
|
|
302
|
+
};
|
|
303
|
+
/**
|
|
304
|
+
* Return true if the sequence is already in the sequences
|
|
305
|
+
*/
|
|
306
|
+
const inSequences = (needle_seq, sequences) => {
|
|
307
|
+
for (const seq of sequences) {
|
|
308
|
+
if (seq.start != needle_seq.start || seq.end != needle_seq.end) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (seq.substrs.join('') !== needle_seq.substrs.join('')) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
let needle_parts = needle_seq.parts;
|
|
315
|
+
const filter = part => {
|
|
316
|
+
for (const needle_part of needle_parts) {
|
|
317
|
+
if (needle_part.start === part.start && needle_part.substr === part.substr) {
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
if (part.length == 1 || needle_part.length == 1) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
// check for overlapping parts
|
|
324
|
+
// a = ['::=','==']
|
|
325
|
+
// b = ['::','===']
|
|
326
|
+
// a = ['r','sm']
|
|
327
|
+
// b = ['rs','m']
|
|
328
|
+
if (part.start < needle_part.start && part.end > needle_part.start) {
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
if (needle_part.start < part.start && needle_part.end > part.start) {
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return false;
|
|
336
|
+
};
|
|
337
|
+
let filtered = seq.parts.filter(filter);
|
|
338
|
+
if (filtered.length > 0) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
return true;
|
|
386
342
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
*/
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
const mapSequence = (strings, min_replacement = 1) => {
|
|
403
|
-
let chars_replaced = 0;
|
|
404
|
-
strings = strings.map(str => {
|
|
405
|
-
if (unicode_map[str]) {
|
|
406
|
-
chars_replaced += str.length;
|
|
343
|
+
return false;
|
|
344
|
+
};
|
|
345
|
+
class Sequence {
|
|
346
|
+
constructor() {
|
|
347
|
+
this.parts = void 0;
|
|
348
|
+
this.substrs = void 0;
|
|
349
|
+
this.start = void 0;
|
|
350
|
+
this.end = void 0;
|
|
351
|
+
this.parts = [];
|
|
352
|
+
this.substrs = [];
|
|
353
|
+
this.start = 0;
|
|
354
|
+
this.end = 0;
|
|
407
355
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
return '';
|
|
417
|
-
};
|
|
418
|
-
/**
|
|
419
|
-
* Convert a short string and split it into all possible patterns
|
|
420
|
-
* Keep a pattern only if min_replacement is met
|
|
421
|
-
*
|
|
422
|
-
* 'abc'
|
|
423
|
-
* => [['abc'],['ab','c'],['a','bc'],['a','b','c']]
|
|
424
|
-
* => ['abc-pattern','ab-c-pattern'...]
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
* @param {string} str
|
|
428
|
-
* @param {number} min_replacement
|
|
429
|
-
* @return {string}
|
|
430
|
-
*/
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
const substringsToPattern = (str, min_replacement = 1) => {
|
|
434
|
-
min_replacement = Math.max(min_replacement, str.length - 1);
|
|
435
|
-
return arrayToPattern(allSubstrings(str).map(sub_pat => {
|
|
436
|
-
return mapSequence(sub_pat, min_replacement);
|
|
437
|
-
}));
|
|
438
|
-
};
|
|
439
|
-
/**
|
|
440
|
-
* Convert an array of sequences into a pattern
|
|
441
|
-
* [{start:0,end:3,length:3,substr:'iii'}...] => (?:iii...)
|
|
442
|
-
*
|
|
443
|
-
* @param {Sequence[]} sequences
|
|
444
|
-
* @param {boolean} all
|
|
445
|
-
*/
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
const sequencesToPattern = (sequences, all = true) => {
|
|
449
|
-
let min_replacement = sequences.length > 1 ? 1 : 0;
|
|
450
|
-
return arrayToPattern(sequences.map(sequence => {
|
|
451
|
-
let seq = [];
|
|
452
|
-
const len = all ? sequence.length() : sequence.length() - 1;
|
|
453
|
-
|
|
454
|
-
for (let j = 0; j < len; j++) {
|
|
455
|
-
seq.push(substringsToPattern(sequence.substrs[j] || '', min_replacement));
|
|
356
|
+
add(part) {
|
|
357
|
+
if (part) {
|
|
358
|
+
this.parts.push(part);
|
|
359
|
+
this.substrs.push(part.substr);
|
|
360
|
+
this.start = Math.min(part.start, this.start);
|
|
361
|
+
this.end = Math.max(part.end, this.end);
|
|
362
|
+
}
|
|
456
363
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}));
|
|
460
|
-
};
|
|
461
|
-
/**
|
|
462
|
-
* Return true if the sequence is already in the sequences
|
|
463
|
-
* @param {Sequence} needle_seq
|
|
464
|
-
* @param {Sequence[]} sequences
|
|
465
|
-
*/
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
const inSequences = (needle_seq, sequences) => {
|
|
469
|
-
for (const seq of sequences) {
|
|
470
|
-
if (seq.start != needle_seq.start || seq.end != needle_seq.end) {
|
|
471
|
-
continue;
|
|
364
|
+
last() {
|
|
365
|
+
return this.parts[this.parts.length - 1];
|
|
472
366
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
continue;
|
|
367
|
+
length() {
|
|
368
|
+
return this.parts.length;
|
|
476
369
|
}
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
const filter = part => {
|
|
484
|
-
for (const needle_part of needle_parts) {
|
|
485
|
-
if (needle_part.start === part.start && needle_part.substr === part.substr) {
|
|
486
|
-
return false;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
if (part.length == 1 || needle_part.length == 1) {
|
|
490
|
-
continue;
|
|
491
|
-
} // check for overlapping parts
|
|
492
|
-
// a = ['::=','==']
|
|
493
|
-
// b = ['::','===']
|
|
494
|
-
// a = ['r','sm']
|
|
495
|
-
// b = ['rs','m']
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
if (part.start < needle_part.start && part.end > needle_part.start) {
|
|
499
|
-
return true;
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
if (needle_part.start < part.start && needle_part.end > part.start) {
|
|
503
|
-
return true;
|
|
504
|
-
}
|
|
370
|
+
clone(position, last_piece) {
|
|
371
|
+
let clone = new Sequence();
|
|
372
|
+
let parts = JSON.parse(JSON.stringify(this.parts));
|
|
373
|
+
let last_part = parts.pop();
|
|
374
|
+
for (const part of parts) {
|
|
375
|
+
clone.add(part);
|
|
505
376
|
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
return true;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
return false;
|
|
520
|
-
};
|
|
521
|
-
|
|
522
|
-
class Sequence {
|
|
523
|
-
constructor() {
|
|
524
|
-
/** @type {TSequencePart[]} */
|
|
525
|
-
this.parts = [];
|
|
526
|
-
/** @type {string[]} */
|
|
527
|
-
|
|
528
|
-
this.substrs = [];
|
|
529
|
-
this.start = 0;
|
|
530
|
-
this.end = 0;
|
|
531
|
-
}
|
|
532
|
-
/**
|
|
533
|
-
* @param {TSequencePart|undefined} part
|
|
534
|
-
*/
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
add(part) {
|
|
538
|
-
if (part) {
|
|
539
|
-
this.parts.push(part);
|
|
540
|
-
this.substrs.push(part.substr);
|
|
541
|
-
this.start = Math.min(part.start, this.start);
|
|
542
|
-
this.end = Math.max(part.end, this.end);
|
|
377
|
+
let last_substr = last_piece.substr.substring(0, position - last_part.start);
|
|
378
|
+
let clone_last_len = last_substr.length;
|
|
379
|
+
clone.add({
|
|
380
|
+
start: last_part.start,
|
|
381
|
+
end: last_part.start + clone_last_len,
|
|
382
|
+
length: clone_last_len,
|
|
383
|
+
substr: last_substr
|
|
384
|
+
});
|
|
385
|
+
return clone;
|
|
543
386
|
}
|
|
544
387
|
}
|
|
545
|
-
|
|
546
|
-
last() {
|
|
547
|
-
return this.parts[this.parts.length - 1];
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
length() {
|
|
551
|
-
return this.parts.length;
|
|
552
|
-
}
|
|
553
388
|
/**
|
|
554
|
-
*
|
|
555
|
-
*
|
|
389
|
+
* Expand a regular expression pattern to include unicode variants
|
|
390
|
+
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
|
|
391
|
+
*
|
|
392
|
+
* Issue:
|
|
393
|
+
* ﺊﺋ [ 'ﺊ = \\u{fe8a}', 'ﺋ = \\u{fe8b}' ]
|
|
394
|
+
* becomes: ئئ [ 'ي = \\u{64a}', 'ٔ = \\u{654}', 'ي = \\u{64a}', 'ٔ = \\u{654}' ]
|
|
395
|
+
*
|
|
396
|
+
* İIJ = IIJ = ⅡJ
|
|
397
|
+
*
|
|
398
|
+
* 1/2/4
|
|
556
399
|
*/
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
let
|
|
561
|
-
let
|
|
562
|
-
let
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
const getPattern = str => {
|
|
598
|
-
initialize();
|
|
599
|
-
str = asciifold(str);
|
|
600
|
-
let pattern = '';
|
|
601
|
-
let sequences = [new Sequence()];
|
|
602
|
-
|
|
603
|
-
for (let i = 0; i < str.length; i++) {
|
|
604
|
-
let substr = str.substring(i);
|
|
605
|
-
let match = substr.match(multi_char_reg);
|
|
606
|
-
const char = str.substring(i, i + 1);
|
|
607
|
-
const match_str = match ? match[0] : null; // loop through sequences
|
|
608
|
-
// add either the char or multi_match
|
|
609
|
-
|
|
610
|
-
let overlapping = [];
|
|
611
|
-
let added_types = new Set();
|
|
612
|
-
|
|
613
|
-
for (const sequence of sequences) {
|
|
614
|
-
const last_piece = sequence.last();
|
|
615
|
-
|
|
616
|
-
if (!last_piece || last_piece.length == 1 || last_piece.end <= i) {
|
|
617
|
-
// if we have a multi match
|
|
618
|
-
if (match_str) {
|
|
400
|
+
const getPattern = str => {
|
|
401
|
+
initialize();
|
|
402
|
+
str = asciifold(str);
|
|
403
|
+
let pattern = '';
|
|
404
|
+
let sequences = [new Sequence()];
|
|
405
|
+
for (let i = 0; i < str.length; i++) {
|
|
406
|
+
let substr = str.substring(i);
|
|
407
|
+
let match = substr.match(multi_char_reg);
|
|
408
|
+
const char = str.substring(i, i + 1);
|
|
409
|
+
const match_str = match ? match[0] : null;
|
|
410
|
+
// loop through sequences
|
|
411
|
+
// add either the char or multi_match
|
|
412
|
+
let overlapping = [];
|
|
413
|
+
let added_types = new Set();
|
|
414
|
+
for (const sequence of sequences) {
|
|
415
|
+
const last_piece = sequence.last();
|
|
416
|
+
if (!last_piece || last_piece.length == 1 || last_piece.end <= i) {
|
|
417
|
+
// if we have a multi match
|
|
418
|
+
if (match_str) {
|
|
419
|
+
const len = match_str.length;
|
|
420
|
+
sequence.add({
|
|
421
|
+
start: i,
|
|
422
|
+
end: i + len,
|
|
423
|
+
length: len,
|
|
424
|
+
substr: match_str
|
|
425
|
+
});
|
|
426
|
+
added_types.add('1');
|
|
427
|
+
} else {
|
|
428
|
+
sequence.add({
|
|
429
|
+
start: i,
|
|
430
|
+
end: i + 1,
|
|
431
|
+
length: 1,
|
|
432
|
+
substr: char
|
|
433
|
+
});
|
|
434
|
+
added_types.add('2');
|
|
435
|
+
}
|
|
436
|
+
} else if (match_str) {
|
|
437
|
+
let clone = sequence.clone(i, last_piece);
|
|
619
438
|
const len = match_str.length;
|
|
620
|
-
|
|
439
|
+
clone.add({
|
|
621
440
|
start: i,
|
|
622
441
|
end: i + len,
|
|
623
442
|
length: len,
|
|
624
443
|
substr: match_str
|
|
625
444
|
});
|
|
626
|
-
|
|
445
|
+
overlapping.push(clone);
|
|
627
446
|
} else {
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
length: 1,
|
|
632
|
-
substr: char
|
|
633
|
-
});
|
|
634
|
-
added_types.add('2');
|
|
447
|
+
// don't add char
|
|
448
|
+
// adding would create invalid patterns: 234 => [2,34,4]
|
|
449
|
+
added_types.add('3');
|
|
635
450
|
}
|
|
636
|
-
} else if (match_str) {
|
|
637
|
-
let clone = sequence.clone(i, last_piece);
|
|
638
|
-
const len = match_str.length;
|
|
639
|
-
clone.add({
|
|
640
|
-
start: i,
|
|
641
|
-
end: i + len,
|
|
642
|
-
length: len,
|
|
643
|
-
substr: match_str
|
|
644
|
-
});
|
|
645
|
-
overlapping.push(clone);
|
|
646
|
-
} else {
|
|
647
|
-
// don't add char
|
|
648
|
-
// adding would create invalid patterns: 234 => [2,34,4]
|
|
649
|
-
added_types.add('3');
|
|
650
451
|
}
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
continue;
|
|
452
|
+
// if we have overlapping
|
|
453
|
+
if (overlapping.length > 0) {
|
|
454
|
+
// ['ii','iii'] before ['i','i','iii']
|
|
455
|
+
overlapping = overlapping.sort((a, b) => {
|
|
456
|
+
return a.length() - b.length();
|
|
457
|
+
});
|
|
458
|
+
for (let clone of overlapping) {
|
|
459
|
+
// don't add if we already have an equivalent sequence
|
|
460
|
+
if (inSequences(clone, sequences)) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
sequences.push(clone);
|
|
664
464
|
}
|
|
665
|
-
|
|
666
|
-
sequences.push(clone);
|
|
465
|
+
continue;
|
|
667
466
|
}
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
if (old_seq) {
|
|
682
|
-
new_seq.add(old_seq.last());
|
|
467
|
+
// if we haven't done anything unique
|
|
468
|
+
// clean up the patterns
|
|
469
|
+
// helps keep patterns smaller
|
|
470
|
+
// if str = 'r₨㎧aarss', pattern will be 446 instead of 655
|
|
471
|
+
if (i > 0 && added_types.size == 1 && !added_types.has('3')) {
|
|
472
|
+
pattern += sequencesToPattern(sequences, false);
|
|
473
|
+
let new_seq = new Sequence();
|
|
474
|
+
const old_seq = sequences[0];
|
|
475
|
+
if (old_seq) {
|
|
476
|
+
new_seq.add(old_seq.last());
|
|
477
|
+
}
|
|
478
|
+
sequences = [new_seq];
|
|
683
479
|
}
|
|
684
|
-
|
|
685
|
-
sequences = [new_seq];
|
|
686
480
|
}
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
return pattern;
|
|
691
|
-
};
|
|
481
|
+
pattern += sequencesToPattern(sequences, true);
|
|
482
|
+
return pattern;
|
|
483
|
+
};
|
|
692
484
|
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
/**
|
|
704
|
-
* A property getter resolving dot-notation
|
|
705
|
-
* @param {Object} obj The root object to fetch property on
|
|
706
|
-
* @param {String} name The optionally dotted property name to fetch
|
|
707
|
-
* @return {Object} The resolved property value
|
|
708
|
-
*/
|
|
485
|
+
/**
|
|
486
|
+
* A property getter resolving dot-notation
|
|
487
|
+
* @param {Object} obj The root object to fetch property on
|
|
488
|
+
* @param {String} name The optionally dotted property name to fetch
|
|
489
|
+
* @return {Object} The resolved property value
|
|
490
|
+
*/
|
|
491
|
+
const getAttr = (obj, name) => {
|
|
492
|
+
if (!obj) return;
|
|
493
|
+
return obj[name];
|
|
494
|
+
};
|
|
709
495
|
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
496
|
+
/**
|
|
497
|
+
* A property getter resolving dot-notation
|
|
498
|
+
* @param {Object} obj The root object to fetch property on
|
|
499
|
+
* @param {String} name The optionally dotted property name to fetch
|
|
500
|
+
* @return {Object} The resolved property value
|
|
501
|
+
*/
|
|
502
|
+
const getAttrNesting = (obj, name) => {
|
|
503
|
+
if (!obj) return;
|
|
504
|
+
var part,
|
|
713
505
|
names = name.split(".");
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
return obj;
|
|
718
|
-
};
|
|
719
|
-
/**
|
|
720
|
-
* Calculates how close of a match the
|
|
721
|
-
* given value is against a search token.
|
|
722
|
-
*
|
|
723
|
-
*/
|
|
724
|
-
|
|
725
|
-
const scoreValue = (value, token, weight) => {
|
|
726
|
-
var score, pos;
|
|
727
|
-
if (!value) return 0;
|
|
728
|
-
value = value + '';
|
|
729
|
-
if (token.regex == null) return 0;
|
|
730
|
-
pos = value.search(token.regex);
|
|
731
|
-
if (pos === -1) return 0;
|
|
732
|
-
score = token.string.length / value.length;
|
|
733
|
-
if (pos === 0) score += 0.5;
|
|
734
|
-
return score * weight;
|
|
735
|
-
};
|
|
736
|
-
/**
|
|
737
|
-
* Cast object property to an array if it exists and has a value
|
|
738
|
-
*
|
|
739
|
-
*/
|
|
740
|
-
|
|
741
|
-
const propToArray = (obj, key) => {
|
|
742
|
-
var value = obj[key];
|
|
743
|
-
if (typeof value == 'function') return value;
|
|
744
|
-
|
|
745
|
-
if (value && !Array.isArray(value)) {
|
|
746
|
-
obj[key] = [value];
|
|
747
|
-
}
|
|
748
|
-
};
|
|
749
|
-
/**
|
|
750
|
-
* Iterates over arrays and hashes.
|
|
751
|
-
*
|
|
752
|
-
* ```
|
|
753
|
-
* iterate(this.items, function(item, id) {
|
|
754
|
-
* // invoked for each item
|
|
755
|
-
* });
|
|
756
|
-
* ```
|
|
757
|
-
*
|
|
758
|
-
*/
|
|
759
|
-
|
|
760
|
-
const iterate = (object, callback) => {
|
|
761
|
-
if (Array.isArray(object)) {
|
|
762
|
-
object.forEach(callback);
|
|
763
|
-
} else {
|
|
764
|
-
for (var key in object) {
|
|
765
|
-
if (object.hasOwnProperty(key)) {
|
|
766
|
-
callback(object[key], key);
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
};
|
|
771
|
-
const cmp = (a, b) => {
|
|
772
|
-
if (typeof a === 'number' && typeof b === 'number') {
|
|
773
|
-
return a > b ? 1 : a < b ? -1 : 0;
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
a = asciifold(a + '').toLowerCase();
|
|
777
|
-
b = asciifold(b + '').toLowerCase();
|
|
778
|
-
if (a > b) return 1;
|
|
779
|
-
if (b > a) return -1;
|
|
780
|
-
return 0;
|
|
781
|
-
};
|
|
782
|
-
|
|
783
|
-
/**
|
|
784
|
-
* sifter.js
|
|
785
|
-
* Copyright (c) 2013–2020 Brian Reavis & contributors
|
|
786
|
-
*
|
|
787
|
-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
|
|
788
|
-
* file except in compliance with the License. You may obtain a copy of the License at:
|
|
789
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
790
|
-
*
|
|
791
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
792
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
|
793
|
-
* ANY KIND, either express or implied. See the License for the specific language
|
|
794
|
-
* governing permissions and limitations under the License.
|
|
795
|
-
*
|
|
796
|
-
* @author Brian Reavis <brian@thirdroute.com>
|
|
797
|
-
*/
|
|
798
|
-
|
|
799
|
-
class Sifter {
|
|
800
|
-
// []|{};
|
|
506
|
+
while ((part = names.shift()) && (obj = obj[part]));
|
|
507
|
+
return obj;
|
|
508
|
+
};
|
|
801
509
|
|
|
802
510
|
/**
|
|
803
|
-
*
|
|
804
|
-
*
|
|
805
|
-
* specifically for autocomplete.
|
|
511
|
+
* Calculates how close of a match the
|
|
512
|
+
* given value is against a search token.
|
|
806
513
|
*
|
|
807
514
|
*/
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
515
|
+
const scoreValue = (value, token, weight) => {
|
|
516
|
+
var score, pos;
|
|
517
|
+
if (!value) return 0;
|
|
518
|
+
value = value + '';
|
|
519
|
+
if (token.regex == null) return 0;
|
|
520
|
+
pos = value.search(token.regex);
|
|
521
|
+
if (pos === -1) return 0;
|
|
522
|
+
score = token.string.length / value.length;
|
|
523
|
+
if (pos === 0) score += 0.5;
|
|
524
|
+
return score * weight;
|
|
525
|
+
};
|
|
816
526
|
|
|
817
527
|
/**
|
|
818
|
-
*
|
|
819
|
-
* regexps to be used to match results.
|
|
528
|
+
* Cast object property to an array if it exists and has a value
|
|
820
529
|
*
|
|
821
530
|
*/
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
if (weights) {
|
|
829
|
-
field_regex = new RegExp('^(' + Object.keys(weights).map(escape_regex).join('|') + ')\:(.*)$');
|
|
531
|
+
const propToArray = (obj, key) => {
|
|
532
|
+
var value = obj[key];
|
|
533
|
+
if (typeof value == 'function') return value;
|
|
534
|
+
if (value && !Array.isArray(value)) {
|
|
535
|
+
obj[key] = [value];
|
|
830
536
|
}
|
|
537
|
+
};
|
|
831
538
|
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
539
|
+
/**
|
|
540
|
+
* Iterates over arrays and hashes.
|
|
541
|
+
*
|
|
542
|
+
* ```
|
|
543
|
+
* iterate(this.items, function(item, id) {
|
|
544
|
+
* // invoked for each item
|
|
545
|
+
* });
|
|
546
|
+
* ```
|
|
547
|
+
*
|
|
548
|
+
*/
|
|
549
|
+
const iterate = (object, callback) => {
|
|
550
|
+
if (Array.isArray(object)) {
|
|
551
|
+
object.forEach(callback);
|
|
552
|
+
} else {
|
|
553
|
+
for (var key in object) {
|
|
554
|
+
if (object.hasOwnProperty(key)) {
|
|
555
|
+
callback(object[key], key);
|
|
847
556
|
}
|
|
848
|
-
|
|
849
|
-
if (regex && respect_word_boundaries) regex = "\\b" + regex;
|
|
850
557
|
}
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
const cmp = (a, b) => {
|
|
561
|
+
if (typeof a === 'number' && typeof b === 'number') {
|
|
562
|
+
return a > b ? 1 : a < b ? -1 : 0;
|
|
563
|
+
}
|
|
564
|
+
a = asciifold(a + '').toLowerCase();
|
|
565
|
+
b = asciifold(b + '').toLowerCase();
|
|
566
|
+
if (a > b) return 1;
|
|
567
|
+
if (b > a) return -1;
|
|
568
|
+
return 0;
|
|
569
|
+
};
|
|
860
570
|
|
|
861
571
|
/**
|
|
862
|
-
*
|
|
572
|
+
* sifter.js
|
|
573
|
+
* Copyright (c) 2013–2020 Brian Reavis & contributors
|
|
863
574
|
*
|
|
864
|
-
*
|
|
865
|
-
*
|
|
575
|
+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
|
|
576
|
+
* file except in compliance with the License. You may obtain a copy of the License at:
|
|
577
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
866
578
|
*
|
|
867
|
-
*
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
return this._getScoreFunction(search);
|
|
872
|
-
}
|
|
873
|
-
/**
|
|
874
|
-
* @returns {T.ScoreFn}
|
|
579
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
580
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
|
581
|
+
* ANY KIND, either express or implied. See the License for the specific language
|
|
582
|
+
* governing permissions and limitations under the License.
|
|
875
583
|
*
|
|
584
|
+
* @author Brian Reavis <brian@thirdroute.com>
|
|
876
585
|
*/
|
|
877
586
|
|
|
587
|
+
class Sifter {
|
|
588
|
+
// []|{};
|
|
878
589
|
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
const fields = search.options.fields,
|
|
890
|
-
weights = search.weights,
|
|
891
|
-
field_count = fields.length,
|
|
892
|
-
getAttrFn = search.getAttrFn;
|
|
893
|
-
|
|
894
|
-
if (!field_count) {
|
|
895
|
-
return function () {
|
|
896
|
-
return 1;
|
|
590
|
+
/**
|
|
591
|
+
* Textually searches arrays and hashes of objects
|
|
592
|
+
* by property (or multiple properties). Designed
|
|
593
|
+
* specifically for autocomplete.
|
|
594
|
+
*
|
|
595
|
+
*/
|
|
596
|
+
constructor(items, settings) {
|
|
597
|
+
this.items = items;
|
|
598
|
+
this.settings = settings || {
|
|
599
|
+
diacritics: true
|
|
897
600
|
};
|
|
898
601
|
}
|
|
899
602
|
/**
|
|
900
|
-
*
|
|
901
|
-
*
|
|
603
|
+
* Splits a search string into an array of individual
|
|
604
|
+
* regexps to be used to match results.
|
|
902
605
|
*
|
|
903
606
|
*/
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
};
|
|
607
|
+
tokenize(query, respect_word_boundaries, weights) {
|
|
608
|
+
if (!query || !query.length) return [];
|
|
609
|
+
const tokens = [];
|
|
610
|
+
const words = query.split(/\s+/);
|
|
611
|
+
var field_regex;
|
|
612
|
+
if (weights) {
|
|
613
|
+
field_regex = new RegExp('^(' + Object.keys(weights).map(escape_regex).join('|') + ')\:(.*)$');
|
|
912
614
|
}
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
615
|
+
words.forEach(word => {
|
|
616
|
+
let field_match;
|
|
617
|
+
let field = null;
|
|
618
|
+
let regex = null;
|
|
619
|
+
|
|
620
|
+
// look for "field:query" tokens
|
|
621
|
+
if (field_regex && (field_match = word.match(field_regex))) {
|
|
622
|
+
field = field_match[1];
|
|
623
|
+
word = field_match[2];
|
|
624
|
+
}
|
|
625
|
+
if (word.length > 0) {
|
|
626
|
+
if (this.settings.diacritics) {
|
|
627
|
+
regex = getPattern(word) || null;
|
|
922
628
|
} else {
|
|
923
|
-
|
|
629
|
+
regex = escape_regex(word);
|
|
924
630
|
}
|
|
925
|
-
|
|
926
|
-
iterate(weights, (weight, field) => {
|
|
927
|
-
sum += scoreValue(getAttrFn(data, field), token, weight);
|
|
928
|
-
});
|
|
631
|
+
if (regex && respect_word_boundaries) regex = "\\b" + regex;
|
|
929
632
|
}
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
if (token_count === 1) {
|
|
936
|
-
return function (data) {
|
|
937
|
-
return scoreObject(tokens[0], data);
|
|
938
|
-
};
|
|
939
|
-
}
|
|
940
|
-
|
|
941
|
-
if (search.options.conjunction === 'and') {
|
|
942
|
-
return function (data) {
|
|
943
|
-
var score,
|
|
944
|
-
sum = 0;
|
|
945
|
-
|
|
946
|
-
for (let token of tokens) {
|
|
947
|
-
score = scoreObject(token, data);
|
|
948
|
-
if (score <= 0) return 0;
|
|
949
|
-
sum += score;
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
return sum / token_count;
|
|
953
|
-
};
|
|
954
|
-
} else {
|
|
955
|
-
return function (data) {
|
|
956
|
-
var sum = 0;
|
|
957
|
-
iterate(tokens, token => {
|
|
958
|
-
sum += scoreObject(token, data);
|
|
633
|
+
tokens.push({
|
|
634
|
+
string: word,
|
|
635
|
+
regex: regex ? new RegExp(regex, 'iu') : null,
|
|
636
|
+
field: field
|
|
959
637
|
});
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
/**
|
|
966
|
-
* Returns a function that can be used to compare two
|
|
967
|
-
* results, for sorting purposes. If no sorting should
|
|
968
|
-
* be performed, `null` will be returned.
|
|
969
|
-
*
|
|
970
|
-
* @return function(a,b)
|
|
971
|
-
*/
|
|
972
|
-
getSortFunction(query, options) {
|
|
973
|
-
var search = this.prepareSearch(query, options);
|
|
974
|
-
return this._getSortFunction(search);
|
|
975
|
-
}
|
|
976
|
-
|
|
977
|
-
_getSortFunction(search) {
|
|
978
|
-
var implicit_score,
|
|
979
|
-
sort_flds = [];
|
|
980
|
-
const self = this,
|
|
981
|
-
options = search.options,
|
|
982
|
-
sort = !search.query && options.sort_empty ? options.sort_empty : options.sort;
|
|
983
|
-
|
|
984
|
-
if (typeof sort == 'function') {
|
|
985
|
-
return sort.bind(this);
|
|
638
|
+
});
|
|
639
|
+
return tokens;
|
|
986
640
|
}
|
|
987
641
|
/**
|
|
988
|
-
*
|
|
989
|
-
* from a search result item.
|
|
642
|
+
* Returns a function to be used to score individual results.
|
|
990
643
|
*
|
|
644
|
+
* Good matches will have a higher score than poor matches.
|
|
645
|
+
* If an item is not a match, 0 will be returned by the function.
|
|
646
|
+
*
|
|
647
|
+
* @returns {T.ScoreFn}
|
|
991
648
|
*/
|
|
649
|
+
getScoreFunction(query, options) {
|
|
650
|
+
var search = this.prepareSearch(query, options);
|
|
651
|
+
return this._getScoreFunction(search);
|
|
652
|
+
}
|
|
992
653
|
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
654
|
+
/**
|
|
655
|
+
* @returns {T.ScoreFn}
|
|
656
|
+
*
|
|
657
|
+
*/
|
|
658
|
+
_getScoreFunction(search) {
|
|
659
|
+
const tokens = search.tokens,
|
|
660
|
+
token_count = tokens.length;
|
|
661
|
+
if (!token_count) {
|
|
662
|
+
return function () {
|
|
663
|
+
return 0;
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
const fields = search.options.fields,
|
|
667
|
+
weights = search.weights,
|
|
668
|
+
field_count = fields.length,
|
|
669
|
+
getAttrFn = search.getAttrFn;
|
|
670
|
+
if (!field_count) {
|
|
671
|
+
return function () {
|
|
672
|
+
return 1;
|
|
673
|
+
};
|
|
1005
674
|
}
|
|
1006
|
-
} // the "$score" field is implied to be the primary
|
|
1007
|
-
// sort field, unless it's manually specified
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
if (search.query) {
|
|
1011
|
-
implicit_score = true;
|
|
1012
675
|
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
676
|
+
/**
|
|
677
|
+
* Calculates the score of an object
|
|
678
|
+
* against the search query.
|
|
679
|
+
*
|
|
680
|
+
*/
|
|
681
|
+
const scoreObject = function () {
|
|
682
|
+
if (field_count === 1) {
|
|
683
|
+
return function (token, data) {
|
|
684
|
+
const field = fields[0].field;
|
|
685
|
+
return scoreValue(getAttrFn(data, field), token, weights[field] || 1);
|
|
686
|
+
};
|
|
1017
687
|
}
|
|
688
|
+
return function (token, data) {
|
|
689
|
+
var sum = 0;
|
|
690
|
+
|
|
691
|
+
// is the token specific to a field?
|
|
692
|
+
if (token.field) {
|
|
693
|
+
const value = getAttrFn(data, token.field);
|
|
694
|
+
if (!token.regex && value) {
|
|
695
|
+
sum += 1 / field_count;
|
|
696
|
+
} else {
|
|
697
|
+
sum += scoreValue(value, token, 1);
|
|
698
|
+
}
|
|
699
|
+
} else {
|
|
700
|
+
iterate(weights, (weight, field) => {
|
|
701
|
+
sum += scoreValue(getAttrFn(data, field), token, weight);
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
return sum / field_count;
|
|
705
|
+
};
|
|
706
|
+
}();
|
|
707
|
+
if (token_count === 1) {
|
|
708
|
+
return function (data) {
|
|
709
|
+
return scoreObject(tokens[0], data);
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
if (search.options.conjunction === 'and') {
|
|
713
|
+
return function (data) {
|
|
714
|
+
var score,
|
|
715
|
+
sum = 0;
|
|
716
|
+
for (let token of tokens) {
|
|
717
|
+
score = scoreObject(token, data);
|
|
718
|
+
if (score <= 0) return 0;
|
|
719
|
+
sum += score;
|
|
720
|
+
}
|
|
721
|
+
return sum / token_count;
|
|
722
|
+
};
|
|
723
|
+
} else {
|
|
724
|
+
return function (data) {
|
|
725
|
+
var sum = 0;
|
|
726
|
+
iterate(tokens, token => {
|
|
727
|
+
sum += scoreObject(token, data);
|
|
728
|
+
});
|
|
729
|
+
return sum / token_count;
|
|
730
|
+
};
|
|
1018
731
|
}
|
|
1019
|
-
|
|
1020
|
-
if (implicit_score) {
|
|
1021
|
-
sort_flds.unshift({
|
|
1022
|
-
field: '$score',
|
|
1023
|
-
direction: 'desc'
|
|
1024
|
-
});
|
|
1025
|
-
} // without a search.query, all items will have the same score
|
|
1026
|
-
|
|
1027
|
-
} else {
|
|
1028
|
-
sort_flds = sort_flds.filter(fld => fld.field !== '$score');
|
|
1029
|
-
} // build function
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
const sort_flds_count = sort_flds.length;
|
|
1033
|
-
|
|
1034
|
-
if (!sort_flds_count) {
|
|
1035
|
-
return null;
|
|
1036
732
|
}
|
|
733
|
+
/**
|
|
734
|
+
* Returns a function that can be used to compare two
|
|
735
|
+
* results, for sorting purposes. If no sorting should
|
|
736
|
+
* be performed, `null` will be returned.
|
|
737
|
+
*
|
|
738
|
+
* @return function(a,b)
|
|
739
|
+
*/
|
|
740
|
+
getSortFunction(query, options) {
|
|
741
|
+
var search = this.prepareSearch(query, options);
|
|
742
|
+
return this._getSortFunction(search);
|
|
743
|
+
}
|
|
744
|
+
_getSortFunction(search) {
|
|
745
|
+
var implicit_score,
|
|
746
|
+
sort_flds = [];
|
|
747
|
+
const self = this,
|
|
748
|
+
options = search.options,
|
|
749
|
+
sort = !search.query && options.sort_empty ? options.sort_empty : options.sort;
|
|
750
|
+
if (typeof sort == 'function') {
|
|
751
|
+
return sort.bind(this);
|
|
752
|
+
}
|
|
1037
753
|
|
|
1038
|
-
|
|
1039
|
-
|
|
754
|
+
/**
|
|
755
|
+
* Fetches the specified sort field value
|
|
756
|
+
* from a search result item.
|
|
757
|
+
*
|
|
758
|
+
*/
|
|
759
|
+
const get_field = function get_field(name, result) {
|
|
760
|
+
if (name === '$score') return result.score;
|
|
761
|
+
return search.getAttrFn(self.items[result.id], name);
|
|
762
|
+
};
|
|
1040
763
|
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
let
|
|
1044
|
-
|
|
1045
|
-
|
|
764
|
+
// parse options
|
|
765
|
+
if (sort) {
|
|
766
|
+
for (let s of sort) {
|
|
767
|
+
if (search.query || s.field !== '$score') {
|
|
768
|
+
sort_flds.push(s);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
1046
771
|
}
|
|
1047
772
|
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
773
|
+
// the "$score" field is implied to be the primary
|
|
774
|
+
// sort field, unless it's manually specified
|
|
775
|
+
if (search.query) {
|
|
776
|
+
implicit_score = true;
|
|
777
|
+
for (let fld of sort_flds) {
|
|
778
|
+
if (fld.field === '$score') {
|
|
779
|
+
implicit_score = false;
|
|
780
|
+
break;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
if (implicit_score) {
|
|
784
|
+
sort_flds.unshift({
|
|
785
|
+
field: '$score',
|
|
786
|
+
direction: 'desc'
|
|
787
|
+
});
|
|
788
|
+
}
|
|
1051
789
|
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
*
|
|
1057
|
-
*/
|
|
1058
|
-
prepareSearch(query, optsUser) {
|
|
1059
|
-
const weights = {};
|
|
1060
|
-
var options = Object.assign({}, optsUser);
|
|
1061
|
-
propToArray(options, 'sort');
|
|
1062
|
-
propToArray(options, 'sort_empty'); // convert fields to new format
|
|
790
|
+
// without a search.query, all items will have the same score
|
|
791
|
+
} else {
|
|
792
|
+
sort_flds = sort_flds.filter(fld => fld.field !== '$score');
|
|
793
|
+
}
|
|
1063
794
|
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
795
|
+
// build function
|
|
796
|
+
const sort_flds_count = sort_flds.length;
|
|
797
|
+
if (!sort_flds_count) {
|
|
798
|
+
return null;
|
|
799
|
+
}
|
|
800
|
+
return function (a, b) {
|
|
801
|
+
var result, field;
|
|
802
|
+
for (let sort_fld of sort_flds) {
|
|
803
|
+
field = sort_fld.field;
|
|
804
|
+
let multiplier = sort_fld.direction === 'desc' ? -1 : 1;
|
|
805
|
+
result = multiplier * cmp(get_field(field, a), get_field(field, b));
|
|
806
|
+
if (result) return result;
|
|
1073
807
|
}
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
weights[field.field] = 'weight' in field ? field.weight : 1;
|
|
1077
|
-
});
|
|
1078
|
-
options.fields = fields;
|
|
808
|
+
return 0;
|
|
809
|
+
};
|
|
1079
810
|
}
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
weights
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
811
|
+
/**
|
|
812
|
+
* Parses a search query and returns an object
|
|
813
|
+
* with tokens and fields ready to be populated
|
|
814
|
+
* with results.
|
|
815
|
+
*
|
|
816
|
+
*/
|
|
817
|
+
prepareSearch(query, optsUser) {
|
|
818
|
+
const weights = {};
|
|
819
|
+
var options = Object.assign({}, optsUser);
|
|
820
|
+
propToArray(options, 'sort');
|
|
821
|
+
propToArray(options, 'sort_empty');
|
|
822
|
+
|
|
823
|
+
// convert fields to new format
|
|
824
|
+
if (options.fields) {
|
|
825
|
+
propToArray(options, 'fields');
|
|
826
|
+
const fields = [];
|
|
827
|
+
options.fields.forEach(field => {
|
|
828
|
+
if (typeof field == 'string') {
|
|
829
|
+
field = {
|
|
830
|
+
field: field,
|
|
831
|
+
weight: 1
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
fields.push(field);
|
|
835
|
+
weights[field.field] = 'weight' in field ? field.weight : 1;
|
|
836
|
+
});
|
|
837
|
+
options.fields = fields;
|
|
838
|
+
}
|
|
839
|
+
return {
|
|
840
|
+
options: options,
|
|
841
|
+
query: query.toLowerCase().trim(),
|
|
842
|
+
tokens: this.tokenize(query, options.respect_word_boundaries, weights),
|
|
843
|
+
total: 0,
|
|
844
|
+
items: [],
|
|
845
|
+
weights: weights,
|
|
846
|
+
getAttrFn: options.nesting ? getAttrNesting : getAttr
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
/**
|
|
850
|
+
* Searches through all items and returns a sorted array of matches.
|
|
851
|
+
*
|
|
852
|
+
*/
|
|
853
|
+
search(query, options) {
|
|
854
|
+
var self = this,
|
|
1098
855
|
score,
|
|
1099
856
|
search;
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
857
|
+
search = this.prepareSearch(query, options);
|
|
858
|
+
options = search.options;
|
|
859
|
+
query = search.query;
|
|
860
|
+
|
|
861
|
+
// generate result scoring function
|
|
862
|
+
const fn_score = options.score || self._getScoreFunction(search);
|
|
863
|
+
|
|
864
|
+
// perform search and sort
|
|
865
|
+
if (query.length) {
|
|
866
|
+
iterate(self.items, (item, id) => {
|
|
867
|
+
score = fn_score(item);
|
|
868
|
+
if (options.filter === false || score > 0) {
|
|
869
|
+
search.items.push({
|
|
870
|
+
'score': score,
|
|
871
|
+
'id': id
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
});
|
|
875
|
+
} else {
|
|
876
|
+
iterate(self.items, (_, id) => {
|
|
1112
877
|
search.items.push({
|
|
1113
|
-
'score':
|
|
878
|
+
'score': 1,
|
|
1114
879
|
'id': id
|
|
1115
880
|
});
|
|
1116
|
-
}
|
|
1117
|
-
});
|
|
1118
|
-
} else {
|
|
1119
|
-
iterate(self.items, (_, id) => {
|
|
1120
|
-
search.items.push({
|
|
1121
|
-
'score': 1,
|
|
1122
|
-
'id': id
|
|
1123
881
|
});
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
const fn_sort = self._getSortFunction(search);
|
|
1128
|
-
|
|
1129
|
-
if (fn_sort) search.items.sort(fn_sort); // apply limits
|
|
1130
|
-
|
|
1131
|
-
search.total = search.items.length;
|
|
882
|
+
}
|
|
883
|
+
const fn_sort = self._getSortFunction(search);
|
|
884
|
+
if (fn_sort) search.items.sort(fn_sort);
|
|
1132
885
|
|
|
1133
|
-
|
|
1134
|
-
search.
|
|
886
|
+
// apply limits
|
|
887
|
+
search.total = search.items.length;
|
|
888
|
+
if (typeof options.limit === 'number') {
|
|
889
|
+
search.items = search.items.slice(0, options.limit);
|
|
890
|
+
}
|
|
891
|
+
return search;
|
|
1135
892
|
}
|
|
1136
|
-
|
|
1137
|
-
return search;
|
|
1138
893
|
}
|
|
1139
894
|
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
exports.propToArray = propToArray;
|
|
1149
|
-
exports.scoreValue = scoreValue;
|
|
1150
|
-
|
|
1151
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
895
|
+
exports.Sifter = Sifter;
|
|
896
|
+
exports.cmp = cmp;
|
|
897
|
+
exports.getAttr = getAttr;
|
|
898
|
+
exports.getAttrNesting = getAttrNesting;
|
|
899
|
+
exports.getPattern = getPattern;
|
|
900
|
+
exports.iterate = iterate;
|
|
901
|
+
exports.propToArray = propToArray;
|
|
902
|
+
exports.scoreValue = scoreValue;
|
|
1152
903
|
|
|
1153
|
-
}))
|
|
904
|
+
}));
|
|
1154
905
|
//# sourceMappingURL=sifter.js.map
|