tom-select-rails 0.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tom-select-rails/version.rb +1 -1
  3. data/vendor/assets/javascripts/tom-select-rails/cjs/tom-select.complete.js +761 -273
  4. data/vendor/assets/javascripts/tom-select-rails/cjs/tom-select.complete.js.map +1 -1
  5. data/vendor/assets/javascripts/tom-select-rails/cjs/tom-select.js +756 -272
  6. data/vendor/assets/javascripts/tom-select-rails/cjs/tom-select.js.map +1 -1
  7. data/vendor/assets/javascripts/tom-select-rails/cjs/tom-select.popular.js +753 -269
  8. data/vendor/assets/javascripts/tom-select-rails/cjs/tom-select.popular.js.map +1 -1
  9. data/vendor/assets/javascripts/tom-select-rails/cjs/utils.js +1 -1
  10. data/vendor/assets/javascripts/tom-select-rails/cjs/utils.js.map +1 -1
  11. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/caret_position/plugin.js +9 -5
  12. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/caret_position/plugin.js.map +1 -1
  13. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/change_listener/plugin.js +1 -1
  14. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/change_listener/plugin.js.map +1 -1
  15. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/checkbox_options/plugin.js +12 -7
  16. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/checkbox_options/plugin.js.map +1 -1
  17. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/clear_button/plugin.js +13 -8
  18. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/clear_button/plugin.js.map +1 -1
  19. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/drag_drop/plugin.js +1 -1
  20. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/drag_drop/plugin.js.map +1 -1
  21. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/dropdown_header/plugin.js +12 -7
  22. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/dropdown_header/plugin.js.map +1 -1
  23. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/dropdown_input/plugin.js +12 -8
  24. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/dropdown_input/plugin.js.map +1 -1
  25. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/input_autogrow/plugin.js +1 -1
  26. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/input_autogrow/plugin.js.map +1 -1
  27. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/no_active_items/plugin.js +1 -1
  28. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/no_active_items/plugin.js.map +1 -1
  29. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/no_backspace_delete/plugin.js +1 -1
  30. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/no_backspace_delete/plugin.js.map +1 -1
  31. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/optgroup_columns/plugin.js +9 -4
  32. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/optgroup_columns/plugin.js.map +1 -1
  33. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/remove_button/plugin.js +12 -7
  34. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/remove_button/plugin.js.map +1 -1
  35. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/restore_on_backspace/plugin.js +1 -1
  36. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/restore_on_backspace/plugin.js.map +1 -1
  37. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/virtual_scroll/plugin.js +16 -8
  38. data/vendor/assets/javascripts/tom-select-rails/esm/plugins/virtual_scroll/plugin.js.map +1 -1
  39. data/vendor/assets/javascripts/tom-select-rails/esm/tom-select.complete.js +761 -273
  40. data/vendor/assets/javascripts/tom-select-rails/esm/tom-select.complete.js.map +1 -1
  41. data/vendor/assets/javascripts/tom-select-rails/esm/tom-select.js +756 -272
  42. data/vendor/assets/javascripts/tom-select-rails/esm/tom-select.js.map +1 -1
  43. data/vendor/assets/javascripts/tom-select-rails/esm/tom-select.popular.js +753 -269
  44. data/vendor/assets/javascripts/tom-select-rails/esm/tom-select.popular.js.map +1 -1
  45. data/vendor/assets/javascripts/tom-select-rails/esm/utils.js +1 -1
  46. data/vendor/assets/javascripts/tom-select-rails/esm/utils.js.map +1 -1
  47. data/vendor/assets/javascripts/tom-select-rails/js/plugins/caret_position.js +164 -160
  48. data/vendor/assets/javascripts/tom-select-rails/js/plugins/caret_position.js.map +1 -1
  49. data/vendor/assets/javascripts/tom-select-rails/js/plugins/change_listener.js +1 -1
  50. data/vendor/assets/javascripts/tom-select-rails/js/plugins/change_listener.js.map +1 -1
  51. data/vendor/assets/javascripts/tom-select-rails/js/plugins/checkbox_options.js +12 -7
  52. data/vendor/assets/javascripts/tom-select-rails/js/plugins/checkbox_options.js.map +1 -1
  53. data/vendor/assets/javascripts/tom-select-rails/js/plugins/clear_button.js +94 -89
  54. data/vendor/assets/javascripts/tom-select-rails/js/plugins/clear_button.js.map +1 -1
  55. data/vendor/assets/javascripts/tom-select-rails/js/plugins/drag_drop.js +1 -1
  56. data/vendor/assets/javascripts/tom-select-rails/js/plugins/drag_drop.js.map +1 -1
  57. data/vendor/assets/javascripts/tom-select-rails/js/plugins/dropdown_header.js +121 -116
  58. data/vendor/assets/javascripts/tom-select-rails/js/plugins/dropdown_header.js.map +1 -1
  59. data/vendor/assets/javascripts/tom-select-rails/js/plugins/dropdown_input.js +234 -230
  60. data/vendor/assets/javascripts/tom-select-rails/js/plugins/dropdown_input.js.map +1 -1
  61. data/vendor/assets/javascripts/tom-select-rails/js/plugins/input_autogrow.js +1 -1
  62. data/vendor/assets/javascripts/tom-select-rails/js/plugins/input_autogrow.js.map +1 -1
  63. data/vendor/assets/javascripts/tom-select-rails/js/plugins/no_active_items.js +1 -1
  64. data/vendor/assets/javascripts/tom-select-rails/js/plugins/no_active_items.js.map +1 -1
  65. data/vendor/assets/javascripts/tom-select-rails/js/plugins/no_backspace_delete.js +1 -1
  66. data/vendor/assets/javascripts/tom-select-rails/js/plugins/no_backspace_delete.js.map +1 -1
  67. data/vendor/assets/javascripts/tom-select-rails/js/plugins/optgroup_columns.js +112 -107
  68. data/vendor/assets/javascripts/tom-select-rails/js/plugins/optgroup_columns.js.map +1 -1
  69. data/vendor/assets/javascripts/tom-select-rails/js/plugins/remove_button.js +149 -144
  70. data/vendor/assets/javascripts/tom-select-rails/js/plugins/remove_button.js.map +1 -1
  71. data/vendor/assets/javascripts/tom-select-rails/js/plugins/restore_on_backspace.js +1 -1
  72. data/vendor/assets/javascripts/tom-select-rails/js/plugins/restore_on_backspace.js.map +1 -1
  73. data/vendor/assets/javascripts/tom-select-rails/js/plugins/virtual_scroll.js +277 -269
  74. data/vendor/assets/javascripts/tom-select-rails/js/plugins/virtual_scroll.js.map +1 -1
  75. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.base.js +756 -272
  76. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.base.js.map +1 -1
  77. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.base.min.js +329 -272
  78. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.base.min.js.map +1 -1
  79. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.complete.js +761 -273
  80. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.complete.js.map +1 -1
  81. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.complete.min.js +268 -209
  82. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.complete.min.js.map +1 -1
  83. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.popular.js +753 -269
  84. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.popular.js.map +1 -1
  85. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.popular.min.js +348 -291
  86. data/vendor/assets/javascripts/tom-select-rails/js/tom-select.popular.min.js.map +1 -1
  87. data/vendor/assets/javascripts/tom-select-rails/types/tom-select.d.ts +6 -6
  88. data/vendor/assets/javascripts/tom-select-rails/types/types/settings.d.ts +19 -22
  89. data/vendor/assets/javascripts/tom-select-rails/types/utils.d.ts +5 -5
  90. data/vendor/assets/javascripts/tom-select-rails/types/vanilla.d.ts +2 -2
  91. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.bootstrap4.css +41 -19
  92. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.bootstrap4.css.map +1 -1
  93. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.bootstrap4.min.css +1 -1
  94. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.bootstrap4.min.css.map +1 -1
  95. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.bootstrap5.css +61 -34
  96. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.bootstrap5.css.map +1 -1
  97. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.bootstrap5.min.css +1 -1
  98. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.bootstrap5.min.css.map +1 -1
  99. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.css +39 -17
  100. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.css.map +1 -1
  101. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.default.css +41 -19
  102. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.default.css.map +1 -1
  103. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.default.min.css +1 -1
  104. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.default.min.css.map +1 -1
  105. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.min.css +1 -1
  106. data/vendor/assets/stylesheets/tom-select-rails/css/tom-select.min.css.map +1 -1
  107. data/vendor/assets/stylesheets/tom-select-rails/scss/plugins/clear_button.scss +11 -10
  108. data/vendor/assets/stylesheets/tom-select-rails/scss/plugins/remove_button.scss +34 -8
  109. data/vendor/assets/stylesheets/tom-select-rails/scss/tom-select.bootstrap4.scss +1 -1
  110. data/vendor/assets/stylesheets/tom-select-rails/scss/tom-select.bootstrap5.scss +20 -10
  111. data/vendor/assets/stylesheets/tom-select-rails/scss/tom-select.default.scss +1 -1
  112. data/vendor/assets/stylesheets/tom-select-rails/scss/tom-select.scss +11 -2
  113. metadata +3 -3
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Tom Select v2.1.0
2
+ * Tom Select v2.2.1
3
3
  * Licensed under the Apache License, Version 2.0 (the "License");
4
4
  */
5
5
 
@@ -31,9 +31,9 @@ class MicroEvent {
31
31
 
32
32
  on(events, fct) {
33
33
  forEvents(events, event => {
34
- this._events[event] = this._events[event] || [];
35
-
36
- this._events[event].push(fct);
34
+ const event_array = this._events[event] || [];
35
+ event_array.push(fct);
36
+ this._events[event] = event_array;
37
37
  });
38
38
  }
39
39
 
@@ -46,21 +46,26 @@ class MicroEvent {
46
46
  }
47
47
 
48
48
  forEvents(events, event => {
49
- if (n === 1) return delete this._events[event];
50
- if (event in this._events === false) return;
49
+ if (n === 1) {
50
+ delete this._events[event];
51
+ return;
52
+ }
51
53
 
52
- this._events[event].splice(this._events[event].indexOf(fct), 1);
54
+ const event_array = this._events[event];
55
+ if (event_array === undefined) return;
56
+ event_array.splice(event_array.indexOf(fct), 1);
57
+ this._events[event] = event_array;
53
58
  });
54
59
  }
55
60
 
56
61
  trigger(events, ...args) {
57
62
  var self = this;
58
63
  forEvents(events, event => {
59
- if (event in self._events === false) return;
60
-
61
- for (let fct of self._events[event]) {
64
+ const event_array = self._events[event];
65
+ if (event_array === undefined) return;
66
+ event_array.forEach(fct => {
62
67
  fct.apply(self, args);
63
- }
68
+ });
64
69
  });
65
70
  }
66
71
 
@@ -187,69 +192,124 @@ function MicroPlugin(Interface) {
187
192
  };
188
193
  }
189
194
 
190
- // @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
191
- // https://github.com/andrewrk/node-diacritics/blob/master/index.js
192
- var latin_pat;
193
- const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}]'; // \u{2bc}
195
+ /*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
196
+ /**
197
+ * Convert array of strings to a regular expression
198
+ * ex ['ab','a'] => (?:ab|a)
199
+ * ex ['a','b'] => [ab]
200
+ * @param {string[]} chars
201
+ * @return {string}
202
+ */
203
+ const arrayToPattern = chars => {
204
+ chars = chars.filter(Boolean);
194
205
 
195
- const accent_reg = new RegExp(accent_pat, 'gu');
196
- var diacritic_patterns;
197
- const latin_convert = {
198
- 'æ': 'ae',
199
- '': 'a',
200
- 'ø': 'o'
206
+ if (chars.length < 2) {
207
+ return chars[0] || '';
208
+ }
209
+
210
+ return maxValueLength(chars) == 1 ? '[' + chars.join('') + ']' : '(?:' + chars.join('|') + ')';
201
211
  };
202
- const convert_pat = new RegExp(Object.keys(latin_convert).join('|'), 'gu');
203
- const code_points = [[0, 65535]];
204
212
  /**
205
- * Remove accents
206
- * via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
207
- *
213
+ * @param {string[]} array
214
+ * @return {string}
208
215
  */
209
216
 
210
- const asciifold = str => {
211
- return str.normalize('NFKD').replace(accent_reg, '').toLowerCase().replace(convert_pat, function (foreignletter) {
212
- return latin_convert[foreignletter];
217
+ const sequencePattern = array => {
218
+ if (!hasDuplicates(array)) {
219
+ return array.join('');
220
+ }
221
+
222
+ let pattern = '';
223
+ let prev_char_count = 0;
224
+
225
+ const prev_pattern = () => {
226
+ if (prev_char_count > 1) {
227
+ pattern += '{' + prev_char_count + '}';
228
+ }
229
+ };
230
+
231
+ array.forEach((char, i) => {
232
+ if (char === array[i - 1]) {
233
+ prev_char_count++;
234
+ return;
235
+ }
236
+
237
+ prev_pattern();
238
+ pattern += char;
239
+ prev_char_count = 1;
213
240
  });
241
+ prev_pattern();
242
+ return pattern;
214
243
  };
215
244
  /**
216
245
  * Convert array of strings to a regular expression
217
246
  * ex ['ab','a'] => (?:ab|a)
218
247
  * ex ['a','b'] => [ab]
219
- *
248
+ * @param {Set<string>} chars
249
+ * @return {string}
220
250
  */
221
251
 
222
- const arrayToPattern = (chars, glue = '|') => {
223
- if (chars.length == 1) {
224
- return chars[0];
225
- }
252
+ const setToPattern = chars => {
253
+ let array = toArray(chars);
254
+ return arrayToPattern(array);
255
+ };
256
+ /**
257
+ *
258
+ * https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values
259
+ * @param {any[]} array
260
+ */
226
261
 
227
- var longest = 1;
228
- chars.forEach(a => {
229
- longest = Math.max(longest, a.length);
230
- });
262
+ const hasDuplicates = array => {
263
+ return new Set(array).size !== array.length;
264
+ };
265
+ /**
266
+ * https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error
267
+ * @param {string} str
268
+ * @return {string}
269
+ */
231
270
 
232
- if (longest == 1) {
233
- return '[' + chars.join('') + ']';
234
- }
271
+ const escape_regex = str => {
272
+ return (str + '').replace(/([\$\(\)\*\+\.\?\[\]\^\{\|\}\\])/gu, '\\$1');
273
+ };
274
+ /**
275
+ * Return the max length of array values
276
+ * @param {string[]} array
277
+ *
278
+ */
235
279
 
236
- return '(?:' + chars.join(glue) + ')';
280
+ const maxValueLength = array => {
281
+ return array.reduce((longest, value) => Math.max(longest, unicodeLength(value)), 0);
237
282
  };
238
- const escapeToPattern = chars => {
239
- const escaped = chars.map(diacritic => escape_regex(diacritic));
240
- return arrayToPattern(escaped);
283
+ /**
284
+ * @param {string} str
285
+ */
286
+
287
+ const unicodeLength = str => {
288
+ return toArray(str).length;
241
289
  };
290
+ /**
291
+ * @param {any} p
292
+ * @return {any[]}
293
+ */
294
+
295
+ const toArray = p => Array.from(p);
296
+
297
+ /*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
242
298
  /**
243
299
  * Get all possible combinations of substrings that add up to the given string
244
300
  * https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
245
- *
301
+ * @param {string} input
302
+ * @return {string[][]}
246
303
  */
247
-
248
304
  const allSubstrings = input => {
249
305
  if (input.length === 1) return [[input]];
250
- var result = [];
251
- allSubstrings(input.substring(1)).forEach(function (subresult) {
252
- var tmp = subresult.slice(0);
306
+ /** @type {string[][]} */
307
+
308
+ let result = [];
309
+ const start = input.substring(1);
310
+ const suba = allSubstrings(start);
311
+ suba.forEach(function (subresult) {
312
+ let tmp = subresult.slice(0);
253
313
  tmp[0] = input.charAt(0) + tmp[0];
254
314
  result.push(tmp);
255
315
  tmp = subresult.slice(0);
@@ -258,108 +318,500 @@ const allSubstrings = input => {
258
318
  });
259
319
  return result;
260
320
  };
321
+
322
+ /*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
323
+
324
+ /**
325
+ * @typedef {{[key:string]:string}} TUnicodeMap
326
+ * @typedef {{[key:string]:Set<string>}} TUnicodeSets
327
+ * @typedef {[[number,number]]} TCodePoints
328
+ * @typedef {{folded:string,composed:string,code_point:number}} TCodePointObj
329
+ * @typedef {{start:number,end:number,length:number,substr:string}} TSequencePart
330
+ */
331
+ /** @type {TCodePoints} */
332
+
333
+ const code_points = [[0, 65535]];
334
+ const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}]'; // \u{2bc}
335
+
336
+ /** @type {TUnicodeMap} */
337
+
338
+ let unicode_map;
339
+ /** @type {RegExp} */
340
+
341
+ let multi_char_reg;
342
+ const max_char_length = 3;
343
+ /** @type {TUnicodeMap} */
344
+
345
+ const latin_convert = {
346
+ 'æ': 'ae',
347
+ 'ⱥ': 'a',
348
+ 'ø': 'o',
349
+ '⁄': '/',
350
+ '∕': '/'
351
+ };
352
+ const convert_pat = new RegExp(Object.keys(latin_convert).join('|') + '|' + accent_pat, 'gu');
261
353
  /**
262
- * Generate a list of diacritics from the list of code points
354
+ * Initialize the unicode_map from the give code point ranges
263
355
  *
356
+ * @param {TCodePoints=} _code_points
357
+ */
358
+
359
+ const initialize = _code_points => {
360
+ if (unicode_map !== undefined) return;
361
+ unicode_map = generateMap(_code_points || code_points);
362
+ };
363
+ /**
364
+ * Helper method for normalize a string
365
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
366
+ * @param {string} str
367
+ * @param {string} form
368
+ */
369
+
370
+ const normalize = (str, form = 'NFKD') => str.normalize(form);
371
+ /**
372
+ * Compatibility Decomposition without reordering string
373
+ * calling str.normalize('NFKD') on \u{594}\u{595}\u{596} becomes \u{596}\u{594}\u{595}
374
+ * @param {string} str
375
+ */
376
+
377
+ const decompose = str => {
378
+ if (str.match(/[\u0f71-\u0f81]/)) {
379
+ return toArray(str).reduce(
380
+ /**
381
+ * @param {string} result
382
+ * @param {string} char
383
+ */
384
+ (result, char) => {
385
+ return result + normalize(char);
386
+ }, '');
387
+ }
388
+
389
+ return normalize(str);
390
+ };
391
+ /**
392
+ * Remove accents
393
+ * via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
394
+ * @param {string} str
395
+ * @return {string}
264
396
  */
265
397
 
266
- const generateDiacritics = code_points => {
267
- var diacritics = {};
268
- code_points.forEach(code_range => {
269
- for (let i = code_range[0]; i <= code_range[1]; i++) {
270
- let diacritic = String.fromCharCode(i);
271
- let latin = asciifold(diacritic);
398
+ const asciifold = str => {
399
+ return decompose(str).toLowerCase().replace(convert_pat, (
400
+ /** @type {string} */
401
+ char) => {
402
+ return latin_convert[char] || '';
403
+ });
404
+ };
405
+ /**
406
+ * Generate a list of unicode variants from the list of code points
407
+ * @param {TCodePoints} code_points
408
+ * @yield {TCodePointObj}
409
+ */
272
410
 
273
- if (latin == diacritic.toLowerCase()) {
411
+ function* generator(code_points) {
412
+ for (const [code_point_min, code_point_max] of code_points) {
413
+ for (let i = code_point_min; i <= code_point_max; i++) {
414
+ let composed = String.fromCharCode(i);
415
+ let folded = asciifold(composed);
416
+
417
+ if (folded == composed.toLowerCase()) {
274
418
  continue;
275
- } // skip when latin is a string longer than 3 characters long
419
+ } // skip when folded is a string longer than 3 characters long
276
420
  // bc the resulting regex patterns will be long
277
421
  // eg:
278
- // latin صلى الله عليه وسلم length 18 code point 65018
279
- // latin جل جلاله length 8 code point 65019
422
+ // folded صلى الله عليه وسلم length 18 code point 65018
423
+ // folded جل جلاله length 8 code point 65019
280
424
 
281
425
 
282
- if (latin.length > 3) {
426
+ if (folded.length > max_char_length) {
283
427
  continue;
284
428
  }
285
429
 
286
- if (!(latin in diacritics)) {
287
- diacritics[latin] = [latin];
430
+ if (folded.length == 0) {
431
+ continue;
288
432
  }
289
433
 
290
- var patt = new RegExp(escapeToPattern(diacritics[latin]), 'iu');
434
+ let decomposed = normalize(composed);
435
+ let recomposed = normalize(decomposed, 'NFC');
291
436
 
292
- if (diacritic.match(patt)) {
437
+ if (recomposed === composed && folded === decomposed) {
293
438
  continue;
294
439
  }
295
440
 
296
- diacritics[latin].push(diacritic);
441
+ yield {
442
+ folded: folded,
443
+ composed: composed,
444
+ code_point: i
445
+ };
446
+ }
447
+ }
448
+ }
449
+ /**
450
+ * Generate a unicode map from the list of code points
451
+ * @param {TCodePoints} code_points
452
+ * @return {TUnicodeSets}
453
+ */
454
+
455
+ const generateSets = code_points => {
456
+ /** @type {{[key:string]:Set<string>}} */
457
+ const unicode_sets = {};
458
+ /**
459
+ * @param {string} folded
460
+ * @param {string} to_add
461
+ */
462
+
463
+ const addMatching = (folded, to_add) => {
464
+ /** @type {Set<string>} */
465
+ const folded_set = unicode_sets[folded] || new Set();
466
+ const patt = new RegExp('^' + setToPattern(folded_set) + '$', 'iu');
467
+
468
+ if (to_add.match(patt)) {
469
+ return;
297
470
  }
298
- }); // filter out if there's only one character in the list
299
471
 
300
- let latin_chars = Object.keys(diacritics);
472
+ folded_set.add(escape_regex(to_add));
473
+ unicode_sets[folded] = folded_set;
474
+ };
301
475
 
302
- for (let i = 0; i < latin_chars.length; i++) {
303
- const latin = latin_chars[i];
476
+ for (let value of generator(code_points)) {
477
+ addMatching(value.folded, value.folded);
478
+ addMatching(value.folded, value.composed);
479
+ }
480
+
481
+ return unicode_sets;
482
+ };
483
+ /**
484
+ * Generate a unicode map from the list of code points
485
+ * ae => (?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))
486
+ *
487
+ * @param {TCodePoints} code_points
488
+ * @return {TUnicodeMap}
489
+ */
304
490
 
305
- if (diacritics[latin].length < 2) {
306
- delete diacritics[latin];
491
+ const generateMap = code_points => {
492
+ /** @type {TUnicodeSets} */
493
+ const unicode_sets = generateSets(code_points);
494
+ /** @type {TUnicodeMap} */
495
+
496
+ const unicode_map = {};
497
+ /** @type {string[]} */
498
+
499
+ let multi_char = [];
500
+
501
+ for (let folded in unicode_sets) {
502
+ let set = unicode_sets[folded];
503
+
504
+ if (set) {
505
+ unicode_map[folded] = setToPattern(set);
307
506
  }
308
- } // latin character pattern
309
- // match longer substrings first
310
-
311
-
312
- latin_chars = Object.keys(diacritics).sort((a, b) => b.length - a.length);
313
- latin_pat = new RegExp('(' + escapeToPattern(latin_chars) + accent_pat + '*)', 'gu'); // build diacritic patterns
314
- // ae needs:
315
- // (?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))
316
-
317
- var diacritic_patterns = {};
318
- latin_chars.sort((a, b) => a.length - b.length).forEach(latin => {
319
- var substrings = allSubstrings(latin);
320
- var pattern = substrings.map(sub_pat => {
321
- sub_pat = sub_pat.map(l => {
322
- if (diacritics.hasOwnProperty(l)) {
323
- return escapeToPattern(diacritics[l]);
324
- }
325
507
 
326
- return l;
327
- });
328
- return arrayToPattern(sub_pat, '');
329
- });
330
- diacritic_patterns[latin] = arrayToPattern(pattern);
508
+ if (folded.length > 1) {
509
+ multi_char.push(escape_regex(folded));
510
+ }
511
+ }
512
+
513
+ multi_char.sort((a, b) => b.length - a.length);
514
+ const multi_char_patt = arrayToPattern(multi_char);
515
+ multi_char_reg = new RegExp('^' + multi_char_patt, 'u');
516
+ return unicode_map;
517
+ };
518
+ /**
519
+ * Map each element of an array from it's folded value to all possible unicode matches
520
+ * @param {string[]} strings
521
+ * @param {number} min_replacement
522
+ * @return {string}
523
+ */
524
+
525
+ const mapSequence = (strings, min_replacement = 1) => {
526
+ let chars_replaced = 0;
527
+ strings = strings.map(str => {
528
+ if (unicode_map[str]) {
529
+ chars_replaced += str.length;
530
+ }
531
+
532
+ return unicode_map[str] || str;
331
533
  });
332
- return diacritic_patterns;
534
+
535
+ if (chars_replaced >= min_replacement) {
536
+ return sequencePattern(strings);
537
+ }
538
+
539
+ return '';
333
540
  };
334
541
  /**
335
- * Expand a regular expression pattern to include diacritics
336
- * eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
542
+ * Convert a short string and split it into all possible patterns
543
+ * Keep a pattern only if min_replacement is met
544
+ *
545
+ * 'abc'
546
+ * => [['abc'],['ab','c'],['a','bc'],['a','b','c']]
547
+ * => ['abc-pattern','ab-c-pattern'...]
337
548
  *
549
+ *
550
+ * @param {string} str
551
+ * @param {number} min_replacement
552
+ * @return {string}
553
+ */
554
+
555
+ const substringsToPattern = (str, min_replacement = 1) => {
556
+ min_replacement = Math.max(min_replacement, str.length - 1);
557
+ return arrayToPattern(allSubstrings(str).map(sub_pat => {
558
+ return mapSequence(sub_pat, min_replacement);
559
+ }));
560
+ };
561
+ /**
562
+ * Convert an array of sequences into a pattern
563
+ * [{start:0,end:3,length:3,substr:'iii'}...] => (?:iii...)
564
+ *
565
+ * @param {Sequence[]} sequences
566
+ * @param {boolean} all
567
+ */
568
+
569
+ const sequencesToPattern = (sequences, all = true) => {
570
+ let min_replacement = sequences.length > 1 ? 1 : 0;
571
+ return arrayToPattern(sequences.map(sequence => {
572
+ let seq = [];
573
+ const len = all ? sequence.length() : sequence.length() - 1;
574
+
575
+ for (let j = 0; j < len; j++) {
576
+ seq.push(substringsToPattern(sequence.substrs[j] || '', min_replacement));
577
+ }
578
+
579
+ return sequencePattern(seq);
580
+ }));
581
+ };
582
+ /**
583
+ * Return true if the sequence is already in the sequences
584
+ * @param {Sequence} needle_seq
585
+ * @param {Sequence[]} sequences
338
586
  */
339
587
 
340
- const diacriticRegexPoints = regex => {
341
- if (diacritic_patterns === undefined) {
342
- diacritic_patterns = generateDiacritics(code_points);
588
+
589
+ const inSequences = (needle_seq, sequences) => {
590
+ for (const seq of sequences) {
591
+ if (seq.start != needle_seq.start || seq.end != needle_seq.end) {
592
+ continue;
593
+ }
594
+
595
+ if (seq.substrs.join('') !== needle_seq.substrs.join('')) {
596
+ continue;
597
+ }
598
+
599
+ let needle_parts = needle_seq.parts;
600
+ /**
601
+ * @param {TSequencePart} part
602
+ */
603
+
604
+ const filter = part => {
605
+ for (const needle_part of needle_parts) {
606
+ if (needle_part.start === part.start && needle_part.substr === part.substr) {
607
+ return false;
608
+ }
609
+
610
+ if (part.length == 1 || needle_part.length == 1) {
611
+ continue;
612
+ } // check for overlapping parts
613
+ // a = ['::=','==']
614
+ // b = ['::','===']
615
+ // a = ['r','sm']
616
+ // b = ['rs','m']
617
+
618
+
619
+ if (part.start < needle_part.start && part.end > needle_part.start) {
620
+ return true;
621
+ }
622
+
623
+ if (needle_part.start < part.start && needle_part.end > part.start) {
624
+ return true;
625
+ }
626
+ }
627
+
628
+ return false;
629
+ };
630
+
631
+ let filtered = seq.parts.filter(filter);
632
+
633
+ if (filtered.length > 0) {
634
+ continue;
635
+ }
636
+
637
+ return true;
638
+ }
639
+
640
+ return false;
641
+ };
642
+
643
+ class Sequence {
644
+ constructor() {
645
+ /** @type {TSequencePart[]} */
646
+ this.parts = [];
647
+ /** @type {string[]} */
648
+
649
+ this.substrs = [];
650
+ this.start = 0;
651
+ this.end = 0;
652
+ }
653
+ /**
654
+ * @param {TSequencePart|undefined} part
655
+ */
656
+
657
+
658
+ add(part) {
659
+ if (part) {
660
+ this.parts.push(part);
661
+ this.substrs.push(part.substr);
662
+ this.start = Math.min(part.start, this.start);
663
+ this.end = Math.max(part.end, this.end);
664
+ }
665
+ }
666
+
667
+ last() {
668
+ return this.parts[this.parts.length - 1];
343
669
  }
344
670
 
345
- const decomposed = regex.normalize('NFKD').toLowerCase();
346
- return decomposed.split(latin_pat).map(part => {
347
- // "ffl" or "ffl"
348
- const no_accent = asciifold(part);
671
+ length() {
672
+ return this.parts.length;
673
+ }
674
+ /**
675
+ * @param {number} position
676
+ * @param {TSequencePart} last_piece
677
+ */
349
678
 
350
- if (no_accent == '') {
351
- return '';
679
+
680
+ clone(position, last_piece) {
681
+ let clone = new Sequence();
682
+ let parts = JSON.parse(JSON.stringify(this.parts));
683
+ let last_part = parts.pop();
684
+
685
+ for (const part of parts) {
686
+ clone.add(part);
352
687
  }
353
688
 
354
- if (diacritic_patterns.hasOwnProperty(no_accent)) {
355
- return diacritic_patterns[no_accent];
689
+ let last_substr = last_piece.substr.substring(0, position - last_part.start);
690
+ let clone_last_len = last_substr.length;
691
+ clone.add({
692
+ start: last_part.start,
693
+ end: last_part.start + clone_last_len,
694
+ length: clone_last_len,
695
+ substr: last_substr
696
+ });
697
+ return clone;
698
+ }
699
+
700
+ }
701
+ /**
702
+ * Expand a regular expression pattern to include unicode variants
703
+ * eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
704
+ *
705
+ * Issue:
706
+ * ﺊﺋ [ 'ﺊ = \\u{fe8a}', 'ﺋ = \\u{fe8b}' ]
707
+ * becomes: ئئ [ 'ي = \\u{64a}', 'ٔ = \\u{654}', 'ي = \\u{64a}', 'ٔ = \\u{654}' ]
708
+ *
709
+ * İIJ = IIJ = ⅡJ
710
+ *
711
+ * 1/2/4
712
+ *
713
+ * @param {string} str
714
+ * @return {string|undefined}
715
+ */
716
+
717
+
718
+ const getPattern = str => {
719
+ initialize();
720
+ str = asciifold(str);
721
+ let pattern = '';
722
+ let sequences = [new Sequence()];
723
+
724
+ for (let i = 0; i < str.length; i++) {
725
+ let substr = str.substring(i);
726
+ let match = substr.match(multi_char_reg);
727
+ const char = str.substring(i, i + 1);
728
+ const match_str = match ? match[0] : null; // loop through sequences
729
+ // add either the char or multi_match
730
+
731
+ let overlapping = [];
732
+ let added_types = new Set();
733
+
734
+ for (const sequence of sequences) {
735
+ const last_piece = sequence.last();
736
+
737
+ if (!last_piece || last_piece.length == 1 || last_piece.end <= i) {
738
+ // if we have a multi match
739
+ if (match_str) {
740
+ const len = match_str.length;
741
+ sequence.add({
742
+ start: i,
743
+ end: i + len,
744
+ length: len,
745
+ substr: match_str
746
+ });
747
+ added_types.add('1');
748
+ } else {
749
+ sequence.add({
750
+ start: i,
751
+ end: i + 1,
752
+ length: 1,
753
+ substr: char
754
+ });
755
+ added_types.add('2');
756
+ }
757
+ } else if (match_str) {
758
+ let clone = sequence.clone(i, last_piece);
759
+ const len = match_str.length;
760
+ clone.add({
761
+ start: i,
762
+ end: i + len,
763
+ length: len,
764
+ substr: match_str
765
+ });
766
+ overlapping.push(clone);
767
+ } else {
768
+ // don't add char
769
+ // adding would create invalid patterns: 234 => [2,34,4]
770
+ added_types.add('3');
771
+ }
772
+ } // if we have overlapping
773
+
774
+
775
+ if (overlapping.length > 0) {
776
+ // ['ii','iii'] before ['i','i','iii']
777
+ overlapping = overlapping.sort((a, b) => {
778
+ return a.length() - b.length();
779
+ });
780
+
781
+ for (let clone of overlapping) {
782
+ // don't add if we already have an equivalent sequence
783
+ if (inSequences(clone, sequences)) {
784
+ continue;
785
+ }
786
+
787
+ sequences.push(clone);
788
+ }
789
+
790
+ continue;
791
+ } // if we haven't done anything unique
792
+ // clean up the patterns
793
+ // helps keep patterns smaller
794
+ // if str = 'r₨㎧aarss', pattern will be 446 instead of 655
795
+
796
+
797
+ if (i > 0 && added_types.size == 1 && !added_types.has('3')) {
798
+ pattern += sequencesToPattern(sequences, false);
799
+ let new_seq = new Sequence();
800
+ const old_seq = sequences[0];
801
+
802
+ if (old_seq) {
803
+ new_seq.add(old_seq.last());
804
+ }
805
+
806
+ sequences = [new_seq];
356
807
  }
808
+ }
357
809
 
358
- return part;
359
- }).join('');
810
+ pattern += sequencesToPattern(sequences, true);
811
+ return pattern;
360
812
  };
361
813
 
362
- // @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
814
+ /*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */
363
815
 
364
816
  /**
365
817
  * A property getter resolving dot-notation
@@ -397,20 +849,13 @@ const scoreValue = (value, token, weight) => {
397
849
  var score, pos;
398
850
  if (!value) return 0;
399
851
  value = value + '';
852
+ if (token.regex == null) return 0;
400
853
  pos = value.search(token.regex);
401
854
  if (pos === -1) return 0;
402
855
  score = token.string.length / value.length;
403
856
  if (pos === 0) score += 0.5;
404
857
  return score * weight;
405
858
  };
406
- /**
407
- *
408
- * https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error
409
- */
410
-
411
- const escape_regex = str => {
412
- return (str + '').replace(/([\$\(-\+\.\?\[-\^\{-\}])/g, '\\$1');
413
- };
414
859
  /**
415
860
  * Cast object property to an array if it exists and has a value
416
861
  *
@@ -435,7 +880,7 @@ const propToArray = (obj, key) => {
435
880
  *
436
881
  */
437
882
 
438
- const iterate = (object, callback) => {
883
+ const iterate$1 = (object, callback) => {
439
884
  if (Array.isArray(object)) {
440
885
  object.forEach(callback);
441
886
  } else {
@@ -458,6 +903,8 @@ const cmp = (a, b) => {
458
903
  return 0;
459
904
  };
460
905
 
906
+ /*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */
907
+
461
908
  /**
462
909
  * sifter.js
463
910
  * Copyright (c) 2013–2020 Brian Reavis & contributors
@@ -519,12 +966,12 @@ class Sifter {
519
966
 
520
967
  if (word.length > 0) {
521
968
  if (this.settings.diacritics) {
522
- regex = diacriticRegexPoints(word);
969
+ regex = getPattern(word) || null;
523
970
  } else {
524
971
  regex = escape_regex(word);
525
972
  }
526
973
 
527
- if (respect_word_boundaries) regex = "\\b" + regex;
974
+ if (regex && respect_word_boundaries) regex = "\\b" + regex;
528
975
  }
529
976
 
530
977
  tokens.push({
@@ -542,12 +989,17 @@ class Sifter {
542
989
  * Good matches will have a higher score than poor matches.
543
990
  * If an item is not a match, 0 will be returned by the function.
544
991
  *
545
- * @returns {function}
992
+ * @returns {T.ScoreFn}
546
993
  */
547
994
  getScoreFunction(query, options) {
548
995
  var search = this.prepareSearch(query, options);
549
996
  return this._getScoreFunction(search);
550
997
  }
998
+ /**
999
+ * @returns {T.ScoreFn}
1000
+ *
1001
+ */
1002
+
551
1003
 
552
1004
  _getScoreFunction(search) {
553
1005
  const tokens = search.tokens,
@@ -580,7 +1032,7 @@ class Sifter {
580
1032
  if (field_count === 1) {
581
1033
  return function (token, data) {
582
1034
  const field = fields[0].field;
583
- return scoreValue(getAttrFn(data, field), token, weights[field]);
1035
+ return scoreValue(getAttrFn(data, field), token, weights[field] || 1);
584
1036
  };
585
1037
  }
586
1038
 
@@ -596,7 +1048,7 @@ class Sifter {
596
1048
  sum += scoreValue(value, token, 1);
597
1049
  }
598
1050
  } else {
599
- iterate(weights, (weight, field) => {
1051
+ iterate$1(weights, (weight, field) => {
600
1052
  sum += scoreValue(getAttrFn(data, field), token, weight);
601
1053
  });
602
1054
  }
@@ -613,12 +1065,11 @@ class Sifter {
613
1065
 
614
1066
  if (search.options.conjunction === 'and') {
615
1067
  return function (data) {
616
- var i = 0,
617
- score,
1068
+ var score,
618
1069
  sum = 0;
619
1070
 
620
- for (; i < token_count; i++) {
621
- score = scoreObject(tokens[i], data);
1071
+ for (let token of tokens) {
1072
+ score = scoreObject(token, data);
622
1073
  if (score <= 0) return 0;
623
1074
  sum += score;
624
1075
  }
@@ -628,7 +1079,7 @@ class Sifter {
628
1079
  } else {
629
1080
  return function (data) {
630
1081
  var sum = 0;
631
- iterate(tokens, token => {
1082
+ iterate$1(tokens, token => {
632
1083
  sum += scoreObject(token, data);
633
1084
  });
634
1085
  return sum / token_count;
@@ -649,12 +1100,11 @@ class Sifter {
649
1100
  }
650
1101
 
651
1102
  _getSortFunction(search) {
652
- var i, n, implicit_score;
1103
+ var implicit_score,
1104
+ sort_flds = [];
653
1105
  const self = this,
654
1106
  options = search.options,
655
- sort = !search.query && options.sort_empty ? options.sort_empty : options.sort,
656
- sort_flds = [],
657
- multipliers = [];
1107
+ sort = !search.query && options.sort_empty ? options.sort_empty : options.sort;
658
1108
 
659
1109
  if (typeof sort == 'function') {
660
1110
  return sort.bind(this);
@@ -673,9 +1123,9 @@ class Sifter {
673
1123
 
674
1124
 
675
1125
  if (sort) {
676
- for (i = 0, n = sort.length; i < n; i++) {
677
- if (search.query || sort[i].field !== '$score') {
678
- sort_flds.push(sort[i]);
1126
+ for (let s of sort) {
1127
+ if (search.query || s.field !== '$score') {
1128
+ sort_flds.push(s);
679
1129
  }
680
1130
  }
681
1131
  } // the "$score" field is implied to be the primary
@@ -685,8 +1135,8 @@ class Sifter {
685
1135
  if (search.query) {
686
1136
  implicit_score = true;
687
1137
 
688
- for (i = 0, n = sort_flds.length; i < n; i++) {
689
- if (sort_flds[i].field === '$score') {
1138
+ for (let fld of sort_flds) {
1139
+ if (fld.field === '$score') {
690
1140
  implicit_score = false;
691
1141
  break;
692
1142
  }
@@ -697,18 +1147,10 @@ class Sifter {
697
1147
  field: '$score',
698
1148
  direction: 'desc'
699
1149
  });
700
- }
701
- } else {
702
- for (i = 0, n = sort_flds.length; i < n; i++) {
703
- if (sort_flds[i].field === '$score') {
704
- sort_flds.splice(i, 1);
705
- break;
706
- }
707
- }
708
- }
1150
+ } // without a search.query, all items will have the same score
709
1151
 
710
- for (i = 0, n = sort_flds.length; i < n; i++) {
711
- multipliers.push(sort_flds[i].direction === 'desc' ? -1 : 1);
1152
+ } else {
1153
+ sort_flds = sort_flds.filter(fld => fld.field !== '$score');
712
1154
  } // build function
713
1155
 
714
1156
 
@@ -716,25 +1158,20 @@ class Sifter {
716
1158
 
717
1159
  if (!sort_flds_count) {
718
1160
  return null;
719
- } else if (sort_flds_count === 1) {
720
- const sort_fld = sort_flds[0].field;
721
- const multiplier = multipliers[0];
722
- return function (a, b) {
723
- return multiplier * cmp(get_field(sort_fld, a), get_field(sort_fld, b));
724
- };
725
- } else {
726
- return function (a, b) {
727
- var i, result, field;
1161
+ }
728
1162
 
729
- for (i = 0; i < sort_flds_count; i++) {
730
- field = sort_flds[i].field;
731
- result = multipliers[i] * cmp(get_field(field, a), get_field(field, b));
732
- if (result) return result;
733
- }
1163
+ return function (a, b) {
1164
+ var result, field;
734
1165
 
735
- return 0;
736
- };
737
- }
1166
+ for (let sort_fld of sort_flds) {
1167
+ field = sort_fld.field;
1168
+ let multiplier = sort_fld.direction === 'desc' ? -1 : 1;
1169
+ result = multiplier * cmp(get_field(field, a), get_field(field, b));
1170
+ if (result) return result;
1171
+ }
1172
+
1173
+ return 0;
1174
+ };
738
1175
  }
739
1176
 
740
1177
  /**
@@ -793,7 +1230,7 @@ class Sifter {
793
1230
 
794
1231
 
795
1232
  if (query.length) {
796
- iterate(self.items, (item, id) => {
1233
+ iterate$1(self.items, (item, id) => {
797
1234
  score = fn_score(item);
798
1235
 
799
1236
  if (options.filter === false || score > 0) {
@@ -804,7 +1241,7 @@ class Sifter {
804
1241
  }
805
1242
  });
806
1243
  } else {
807
- iterate(self.items, (_, id) => {
1244
+ iterate$1(self.items, (_, id) => {
808
1245
  search.items.push({
809
1246
  'score': 1,
810
1247
  'id': id
@@ -827,6 +1264,29 @@ class Sifter {
827
1264
 
828
1265
  }
829
1266
 
1267
+ /**
1268
+ * Iterates over arrays and hashes.
1269
+ *
1270
+ * ```
1271
+ * iterate(this.items, function(item, id) {
1272
+ * // invoked for each item
1273
+ * });
1274
+ * ```
1275
+ *
1276
+ */
1277
+
1278
+ const iterate = (object, callback) => {
1279
+ if (Array.isArray(object)) {
1280
+ object.forEach(callback);
1281
+ } else {
1282
+ for (var key in object) {
1283
+ if (object.hasOwnProperty(key)) {
1284
+ callback(object[key], key);
1285
+ }
1286
+ }
1287
+ }
1288
+ };
1289
+
830
1290
  /**
831
1291
  * Return a dom element from either a dom query string, jQuery object, a dom element or html string
832
1292
  * https://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro/35385518#35385518
@@ -844,10 +1304,10 @@ const getDom = query => {
844
1304
  }
845
1305
 
846
1306
  if (isHtmlString(query)) {
847
- let div = document.createElement('div');
848
- div.innerHTML = query.trim(); // Never return a text node of whitespace as the result
1307
+ var tpl = document.createElement('template');
1308
+ tpl.innerHTML = query.trim(); // Never return a text node of whitespace as the result
849
1309
 
850
- return div.firstChild;
1310
+ return tpl.content.firstChild;
851
1311
  }
852
1312
 
853
1313
  return document.querySelector(query);
@@ -1058,9 +1518,9 @@ const highlight = (element, regex) => {
1058
1518
 
1059
1519
  const highlightChildren = node => {
1060
1520
  if (node.nodeType === 1 && node.childNodes && !/(script|style)/i.test(node.tagName) && (node.className !== 'highlight' || node.tagName !== 'SPAN')) {
1061
- for (var i = 0; i < node.childNodes.length; ++i) {
1062
- i += highlightRecursive(node.childNodes[i]);
1063
- }
1521
+ Array.from(node.childNodes).forEach(element => {
1522
+ highlightRecursive(element);
1523
+ });
1064
1524
  }
1065
1525
  };
1066
1526
 
@@ -1633,7 +2093,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
1633
2093
  control_input = getDom(settings.controlInput); // set attributes
1634
2094
 
1635
2095
  var attrs = ['autocorrect', 'autocapitalize', 'autocomplete'];
1636
- iterate(attrs, attr => {
2096
+ iterate$1(attrs, attr => {
1637
2097
  if (input.getAttribute(attr)) {
1638
2098
  setAttr(control_input, {
1639
2099
  [attr]: input.getAttribute(attr)
@@ -1737,6 +2197,9 @@ class TomSelect extends MicroPlugin(MicroEvent) {
1737
2197
  }
1738
2198
 
1739
2199
  self.control_input.type = input.type;
2200
+ addEvent(dropdown, 'mousemove', () => {
2201
+ self.ignoreHover = false;
2202
+ });
1740
2203
  addEvent(dropdown, 'mouseenter', e => {
1741
2204
  var target_match = parentMatch(e.target, '[data-selectable]', dropdown);
1742
2205
  if (target_match) self.onOptionHover(e, target_match);
@@ -1809,18 +2272,12 @@ class TomSelect extends MicroPlugin(MicroEvent) {
1809
2272
  }
1810
2273
  };
1811
2274
 
1812
- const win_hover = () => {
1813
- self.ignoreHover = false;
1814
- };
1815
-
1816
2275
  addEvent(document, 'mousedown', doc_mousedown);
1817
2276
  addEvent(window, 'scroll', win_scroll, passive_event);
1818
2277
  addEvent(window, 'resize', win_scroll, passive_event);
1819
- addEvent(window, 'mousemove', win_hover, passive_event);
1820
2278
 
1821
2279
  this._destroy = () => {
1822
2280
  document.removeEventListener('mousedown', doc_mousedown);
1823
- window.removeEventListener('mousemove', win_hover);
1824
2281
  window.removeEventListener('scroll', win_scroll);
1825
2282
  window.removeEventListener('resize', win_scroll);
1826
2283
  if (label) label.removeEventListener('click', label_click);
@@ -1838,7 +2295,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
1838
2295
  settings.items = [];
1839
2296
  delete settings.optgroups;
1840
2297
  delete settings.options;
1841
- addEvent(input, 'invalid', e => {
2298
+ addEvent(input, 'invalid', () => {
1842
2299
  if (self.isValid) {
1843
2300
  self.isValid = false;
1844
2301
  self.isInvalid = true;
@@ -1875,7 +2332,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
1875
2332
  // build options table
1876
2333
  this.addOptions(options); // build optgroup table
1877
2334
 
1878
- iterate(optgroups, optgroup => {
2335
+ iterate$1(optgroups, optgroup => {
1879
2336
  this.registerOptionGroup(optgroup);
1880
2337
  });
1881
2338
  }
@@ -2039,13 +2496,15 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2039
2496
  }
2040
2497
 
2041
2498
  var splitInput = pastedText.trim().split(self.settings.splitOn);
2042
- iterate(splitInput, piece => {
2043
- piece = hash_key(piece);
2499
+ iterate$1(splitInput, piece => {
2500
+ const hash = hash_key(piece);
2044
2501
 
2045
- if (this.options[piece]) {
2046
- self.addItem(piece);
2047
- } else {
2048
- self.createItem(piece);
2502
+ if (hash) {
2503
+ if (this.options[piece]) {
2504
+ self.addItem(piece);
2505
+ } else {
2506
+ self.createItem(piece);
2507
+ }
2049
2508
  }
2050
2509
  });
2051
2510
  }, 0);
@@ -2273,7 +2732,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2273
2732
  };
2274
2733
 
2275
2734
  if (self.settings.create && self.settings.createOnBlur) {
2276
- self.createItem(null, false, deactivate);
2735
+ self.createItem(null, deactivate);
2277
2736
  } else {
2278
2737
  deactivate();
2279
2738
  }
@@ -2294,7 +2753,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2294
2753
  }
2295
2754
 
2296
2755
  if (option.classList.contains('create')) {
2297
- self.createItem(null, true, () => {
2756
+ self.createItem(null, () => {
2298
2757
  if (self.settings.closeAfterSelect) {
2299
2758
  self.close();
2300
2759
  }
@@ -2663,7 +3122,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2663
3122
  self.hideInput();
2664
3123
  self.close();
2665
3124
  self.activeItems = activeItems;
2666
- iterate(activeItems, item => {
3125
+ iterate$1(activeItems, item => {
2667
3126
  self.setActiveItemClass(item);
2668
3127
  });
2669
3128
  }
@@ -2799,7 +3258,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2799
3258
 
2800
3259
 
2801
3260
  search(query) {
2802
- var i, result, calculateScore;
3261
+ var result, calculateScore;
2803
3262
  var self = this;
2804
3263
  var options = this.getSearchOptions(); // validate user-provided result scoring function
2805
3264
 
@@ -2824,13 +3283,10 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2824
3283
 
2825
3284
 
2826
3285
  if (self.settings.hideSelected) {
2827
- for (i = result.items.length - 1; i >= 0; i--) {
2828
- let hashed = hash_key(result.items[i].id);
2829
-
2830
- if (hashed && self.items.indexOf(hashed) !== -1) {
2831
- result.items.splice(i, 1);
2832
- }
2833
- }
3286
+ result.items = result.items.filter(item => {
3287
+ let hashed = hash_key(item.id);
3288
+ return !(hashed && self.items.indexOf(hashed) !== -1);
3289
+ });
2834
3290
  }
2835
3291
 
2836
3292
  return result;
@@ -2843,21 +3299,24 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2843
3299
 
2844
3300
 
2845
3301
  refreshOptions(triggerDropdown = true) {
2846
- var i, j, k, n, optgroup, optgroups, html, has_create_option, active_value, active_group;
3302
+ var i, j, k, n, optgroup, optgroups, html, has_create_option, active_group;
2847
3303
  var create;
2848
3304
  const groups = {};
2849
3305
  const groups_order = [];
2850
3306
  var self = this;
2851
3307
  var query = self.inputValue();
3308
+ const same_query = query === self.lastQuery || query == '' && self.lastQuery == null;
2852
3309
  var results = self.search(query);
2853
- var active_option = null; //self.activeOption;
2854
-
3310
+ var active_option = null;
2855
3311
  var show_dropdown = self.settings.shouldOpen || false;
2856
3312
  var dropdown_content = self.dropdown_content;
2857
3313
 
2858
- if (self.activeOption) {
2859
- active_value = self.activeOption.dataset.value;
2860
- active_group = self.activeOption.closest('[data-group]');
3314
+ if (same_query) {
3315
+ active_option = self.activeOption;
3316
+
3317
+ if (active_option) {
3318
+ active_group = active_option.closest('[data-group]');
3319
+ }
2861
3320
  } // build markup
2862
3321
 
2863
3322
 
@@ -2874,12 +3333,16 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2874
3333
 
2875
3334
  for (i = 0; i < n; i++) {
2876
3335
  // get option dom element
2877
- let opt_value = results.items[i].id;
3336
+ let item = results.items[i];
3337
+ if (!item) continue;
3338
+ let opt_value = item.id;
2878
3339
  let option = self.options[opt_value];
2879
- let option_el = self.getOption(opt_value, true); // toggle 'selected' class
3340
+ if (option === undefined) continue;
3341
+ let opt_hash = get_hash(opt_value);
3342
+ let option_el = self.getOption(opt_hash, true); // toggle 'selected' class
2880
3343
 
2881
3344
  if (!self.settings.hideSelected) {
2882
- option_el.classList.toggle('selected', self.items.includes(opt_value));
3345
+ option_el.classList.toggle('selected', self.items.includes(opt_hash));
2883
3346
  }
2884
3347
 
2885
3348
  optgroup = option[self.settings.optgroupField] || '';
@@ -2892,8 +3355,10 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2892
3355
  optgroup = '';
2893
3356
  }
2894
3357
 
2895
- if (!groups.hasOwnProperty(optgroup)) {
2896
- groups[optgroup] = document.createDocumentFragment();
3358
+ let group_fragment = groups[optgroup];
3359
+
3360
+ if (group_fragment === undefined) {
3361
+ group_fragment = document.createDocumentFragment();
2897
3362
  groups_order.push(optgroup);
2898
3363
  } // nodes can only have one parent, so if the option is in mutple groups, we need a clone
2899
3364
 
@@ -2905,48 +3370,50 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2905
3370
  'aria-selected': null
2906
3371
  });
2907
3372
  option_el.classList.add('ts-cloned');
2908
- removeClasses(option_el, 'active');
2909
- } // make sure we keep the activeOption in the same group
2910
-
3373
+ removeClasses(option_el, 'active'); // make sure we keep the activeOption in the same group
2911
3374
 
2912
- if (!active_option && active_value == opt_value) {
2913
- if (active_group) {
2914
- if (active_group.dataset.group === optgroup) {
3375
+ if (self.activeOption && self.activeOption.dataset.value == opt_value) {
3376
+ if (active_group && active_group.dataset.group === optgroup.toString()) {
2915
3377
  active_option = option_el;
2916
3378
  }
2917
- } else {
2918
- active_option = option_el;
2919
3379
  }
2920
3380
  }
2921
3381
 
2922
- groups[optgroup].appendChild(option_el);
3382
+ group_fragment.appendChild(option_el);
3383
+ groups[optgroup] = group_fragment;
2923
3384
  }
2924
3385
  } // sort optgroups
2925
3386
 
2926
3387
 
2927
- if (this.settings.lockOptgroupOrder) {
3388
+ if (self.settings.lockOptgroupOrder) {
2928
3389
  groups_order.sort((a, b) => {
2929
- var a_order = self.optgroups[a] && self.optgroups[a].$order || 0;
2930
- var b_order = self.optgroups[b] && self.optgroups[b].$order || 0;
3390
+ const grp_a = self.optgroups[a];
3391
+ const grp_b = self.optgroups[b];
3392
+ const a_order = grp_a && grp_a.$order || 0;
3393
+ const b_order = grp_b && grp_b.$order || 0;
2931
3394
  return a_order - b_order;
2932
3395
  });
2933
3396
  } // render optgroup headers & join groups
2934
3397
 
2935
3398
 
2936
3399
  html = document.createDocumentFragment();
2937
- iterate(groups_order, optgroup => {
2938
- if (self.optgroups.hasOwnProperty(optgroup) && groups[optgroup].children.length) {
3400
+ iterate$1(groups_order, optgroup => {
3401
+ let group_fragment = groups[optgroup];
3402
+ if (!group_fragment || !group_fragment.children.length) return;
3403
+ let group_heading = self.optgroups[optgroup];
3404
+
3405
+ if (group_heading !== undefined) {
2939
3406
  let group_options = document.createDocumentFragment();
2940
- let header = self.render('optgroup_header', self.optgroups[optgroup]);
3407
+ let header = self.render('optgroup_header', group_heading);
2941
3408
  append(group_options, header);
2942
- append(group_options, groups[optgroup]);
3409
+ append(group_options, group_fragment);
2943
3410
  let group_html = self.render('optgroup', {
2944
- group: self.optgroups[optgroup],
3411
+ group: group_heading,
2945
3412
  options: group_options
2946
3413
  });
2947
3414
  append(html, group_html);
2948
3415
  } else {
2949
- append(html, groups[optgroup]);
3416
+ append(html, group_fragment);
2950
3417
  }
2951
3418
  });
2952
3419
  dropdown_content.innerHTML = '';
@@ -2956,7 +3423,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2956
3423
  removeHighlight(dropdown_content);
2957
3424
 
2958
3425
  if (results.query.length && results.tokens.length) {
2959
- iterate(results.tokens, tok => {
3426
+ iterate$1(results.tokens, tok => {
2960
3427
  highlight(dropdown_content, tok.regex);
2961
3428
  });
2962
3429
  }
@@ -2997,7 +3464,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
2997
3464
 
2998
3465
  if (show_dropdown) {
2999
3466
  if (results.items.length > 0) {
3000
- if (!active_option && self.settings.mode === 'single' && self.items.length) {
3467
+ if (!active_option && self.settings.mode === 'single' && self.items[0] != undefined) {
3001
3468
  active_option = self.getOption(self.items[0]);
3002
3469
  }
3003
3470
 
@@ -3084,7 +3551,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
3084
3551
 
3085
3552
 
3086
3553
  addOptions(data, user_created = false) {
3087
- iterate(data, dat => {
3554
+ iterate$1(data, dat => {
3088
3555
  this.addOption(dat, user_created);
3089
3556
  });
3090
3557
  }
@@ -3164,11 +3631,12 @@ class TomSelect extends MicroPlugin(MicroEvent) {
3164
3631
  const value_new = hash_key(data[self.settings.valueField]); // sanity checks
3165
3632
 
3166
3633
  if (value_old === null) return;
3167
- if (!self.options.hasOwnProperty(value_old)) return;
3634
+ const data_old = self.options[value_old];
3635
+ if (data_old == undefined) return;
3168
3636
  if (typeof value_new !== 'string') throw new Error('Value must be set in option data');
3169
3637
  const option = self.getOption(value_old);
3170
3638
  const item = self.getItem(value_old);
3171
- data.$order = data.$order || self.options[value_old].$order;
3639
+ data.$order = data.$order || data_old.$order;
3172
3640
  delete self.options[value_old]; // invalidate render cache
3173
3641
  // don't remove existing node yet, we'll remove it after replacing it
3174
3642
 
@@ -3232,9 +3700,9 @@ class TomSelect extends MicroPlugin(MicroEvent) {
3232
3700
  this.userOptions = {};
3233
3701
  this.clearCache();
3234
3702
  const selected = {};
3235
- iterate(this.options, (option, key) => {
3703
+ iterate$1(this.options, (option, key) => {
3236
3704
  if (boundFilter(option, key)) {
3237
- selected[key] = this.options[key];
3705
+ selected[key] = option;
3238
3706
  }
3239
3707
  });
3240
3708
  this.options = this.sifter.items = selected;
@@ -3264,10 +3732,10 @@ class TomSelect extends MicroPlugin(MicroEvent) {
3264
3732
 
3265
3733
  getOption(value, create = false) {
3266
3734
  const hashed = hash_key(value);
3735
+ if (hashed === null) return null;
3736
+ const option = this.options[hashed];
3267
3737
 
3268
- if (hashed !== null && this.options.hasOwnProperty(hashed)) {
3269
- const option = this.options[hashed];
3270
-
3738
+ if (option != undefined) {
3271
3739
  if (option.$div) {
3272
3740
  return option.$div;
3273
3741
  }
@@ -3340,11 +3808,11 @@ class TomSelect extends MicroPlugin(MicroEvent) {
3340
3808
  var self = this;
3341
3809
  var items = Array.isArray(values) ? values : [values];
3342
3810
  items = items.filter(x => self.items.indexOf(x) === -1);
3343
-
3344
- for (let i = 0, n = items.length; i < n; i++) {
3345
- self.isPending = i < n - 1;
3346
- self.addItem(items[i], silent);
3347
- }
3811
+ const last_item = items[items.length - 1];
3812
+ items.forEach(item => {
3813
+ self.isPending = item !== last_item;
3814
+ self.addItem(item, silent);
3815
+ });
3348
3816
  }
3349
3817
  /**
3350
3818
  * "Selects" an item. Adds it to the list
@@ -3475,7 +3943,16 @@ class TomSelect extends MicroPlugin(MicroEvent) {
3475
3943
  */
3476
3944
 
3477
3945
 
3478
- createItem(input = null, triggerDropdown = true, callback = () => {}) {
3946
+ createItem(input = null, callback = () => {}) {
3947
+ // triggerDropdown parameter @deprecated 2.1.1
3948
+ if (arguments.length === 3) {
3949
+ callback = arguments[2];
3950
+ }
3951
+
3952
+ if (typeof callback != 'function') {
3953
+ callback = () => {};
3954
+ }
3955
+
3479
3956
  var self = this;
3480
3957
  var caret = self.caretPos;
3481
3958
  var output;
@@ -3751,7 +4228,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
3751
4228
  var self = this;
3752
4229
  if (!self.items.length) return;
3753
4230
  var items = self.controlChildren();
3754
- iterate(items, item => {
4231
+ iterate$1(items, item => {
3755
4232
  self.removeItem(item, true);
3756
4233
  });
3757
4234
  self.showInput();
@@ -3769,7 +4246,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
3769
4246
  const self = this;
3770
4247
  const caret = self.caretPos;
3771
4248
  const target = self.control;
3772
- target.insertBefore(el, target.children[caret]);
4249
+ target.insertBefore(el, target.children[caret] || null);
3773
4250
  self.setCaret(caret + 1);
3774
4251
  }
3775
4252
  /**
@@ -3794,14 +4271,19 @@ class TomSelect extends MicroPlugin(MicroEvent) {
3794
4271
  caret++;
3795
4272
  }
3796
4273
 
3797
- iterate(self.activeItems, item => rm_items.push(item));
4274
+ iterate$1(self.activeItems, item => rm_items.push(item));
3798
4275
  } else if ((self.isFocused || self.settings.mode === 'single') && self.items.length) {
3799
4276
  const items = self.controlChildren();
4277
+ let rm_item;
3800
4278
 
3801
4279
  if (direction < 0 && selection.start === 0 && selection.length === 0) {
3802
- rm_items.push(items[self.caretPos - 1]);
4280
+ rm_item = items[self.caretPos - 1];
3803
4281
  } else if (direction > 0 && selection.start === self.inputValue().length) {
3804
- rm_items.push(items[self.caretPos]);
4282
+ rm_item = items[self.caretPos];
4283
+ }
4284
+
4285
+ if (rm_item !== undefined) {
4286
+ rm_items.push(rm_item);
3805
4287
  }
3806
4288
  }
3807
4289
 
@@ -4003,26 +4485,11 @@ class TomSelect extends MicroPlugin(MicroEvent) {
4003
4485
 
4004
4486
 
4005
4487
  render(templateName, data) {
4006
- if (typeof this.settings.render[templateName] !== 'function') {
4007
- return null;
4008
- }
4009
-
4010
- return this._render(templateName, data);
4011
- }
4012
- /**
4013
- * _render() can be called directly when we know we don't want to hit the cache
4014
- * return type could be null for some templates, we need https://github.com/microsoft/TypeScript/issues/33014
4015
- */
4016
-
4017
-
4018
- _render(templateName, data) {
4019
- var value = '',
4020
- id,
4021
- html;
4488
+ var id, html;
4022
4489
  const self = this;
4023
4490
 
4024
- if (templateName === 'option' || templateName == 'item') {
4025
- value = get_hash(data[self.settings.valueField]);
4491
+ if (typeof this.settings.render[templateName] !== 'function') {
4492
+ return null;
4026
4493
  } // render markup
4027
4494
 
4028
4495
 
@@ -4058,6 +4525,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
4058
4525
  }
4059
4526
 
4060
4527
  if (templateName === 'option' || templateName === 'item') {
4528
+ const value = get_hash(data[self.settings.valueField]);
4061
4529
  setAttr(html, {
4062
4530
  'data-value': value
4063
4531
  }); // make sure we have some classes if a template is overwritten
@@ -4074,12 +4542,28 @@ class TomSelect extends MicroPlugin(MicroEvent) {
4074
4542
  id: data.$id
4075
4543
  }); // update cache
4076
4544
 
4077
- self.options[value].$div = html;
4545
+ data.$div = html;
4546
+ self.options[value] = data;
4078
4547
  }
4079
4548
  }
4080
4549
 
4081
4550
  return html;
4082
4551
  }
4552
+ /**
4553
+ * Type guarded rendering
4554
+ *
4555
+ */
4556
+
4557
+
4558
+ _render(templateName, data) {
4559
+ const html = this.render(templateName, data);
4560
+
4561
+ if (html == null) {
4562
+ throw 'HTMLElement expected';
4563
+ }
4564
+
4565
+ return html;
4566
+ }
4083
4567
  /**
4084
4568
  * Clears the render cache for a template. If
4085
4569
  * no template is given, clears all render
@@ -4089,7 +4573,7 @@ class TomSelect extends MicroPlugin(MicroEvent) {
4089
4573
 
4090
4574
 
4091
4575
  clearCache() {
4092
- iterate(this.options, (option, value) => {
4576
+ iterate$1(this.options, option => {
4093
4577
  if (option.$div) {
4094
4578
  option.$div.remove();
4095
4579
  delete option.$div;