tom-select-rails 0.2.0 → 2.2.0

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