@orchidjs/sifter 0.7.3 → 0.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +25 -3
  2. package/dist/cjs/diacritics.js +118 -68
  3. package/dist/cjs/diacritics.js.map +1 -1
  4. package/dist/cjs/sifter.js +9 -7
  5. package/dist/cjs/sifter.js.map +1 -1
  6. package/dist/cjs/utils.js +21 -15
  7. package/dist/cjs/utils.js.map +1 -1
  8. package/dist/esm/diacritics.js +116 -69
  9. package/dist/esm/diacritics.js.map +1 -1
  10. package/dist/esm/sifter.js +10 -8
  11. package/dist/esm/sifter.js.map +1 -1
  12. package/dist/esm/utils.js +21 -15
  13. package/dist/esm/utils.js.map +1 -1
  14. package/dist/types/diacritics.d.ts +40 -0
  15. package/dist/types/sifter.d.ts +64 -0
  16. package/dist/types/types.d.ts +45 -0
  17. package/dist/types/utils.d.ts +52 -0
  18. package/dist/umd/sifter.js +144 -89
  19. package/dist/umd/sifter.js.map +1 -1
  20. package/dist/umd/sifter.min.js +56 -35
  21. package/dist/umd/sifter.min.js.map +1 -1
  22. package/lib/diacritics.ts +145 -81
  23. package/lib/sifter.ts +32 -83
  24. package/lib/types.ts +54 -0
  25. package/lib/utils.ts +16 -9
  26. package/package.json +8 -3
  27. package/.config/babel.config.json +0 -17
  28. package/.config/rollup.config.js +0 -95
  29. package/.config/tsconfig.json +0 -16
  30. package/.travis.yml +0 -9
  31. package/Makefile +0 -27
  32. package/build/cjs/diacritics.js +0 -123
  33. package/build/cjs/diacritics.js.map +0 -1
  34. package/build/cjs/sifter.js +0 -375
  35. package/build/cjs/sifter.js.map +0 -1
  36. package/build/cjs/utils.js +0 -108
  37. package/build/cjs/utils.js.map +0 -1
  38. package/build/esm/diacritics.js +0 -117
  39. package/build/esm/diacritics.js.map +0 -1
  40. package/build/esm/sifter.js +0 -373
  41. package/build/esm/sifter.js.map +0 -1
  42. package/build/esm/utils.js +0 -98
  43. package/build/esm/utils.js.map +0 -1
  44. package/build/umd/sifter.js +0 -585
  45. package/build/umd/sifter.js.map +0 -1
  46. package/build/umd/sifter.min.js +0 -63
  47. package/build/umd/sifter.min.js.map +0 -1
  48. package/coverage/clover.xml +0 -207
  49. package/coverage/coverage-final.json +0 -4
  50. package/coverage/lcov-report/base.css +0 -224
  51. package/coverage/lcov-report/block-navigation.js +0 -79
  52. package/coverage/lcov-report/diacritics.ts.html +0 -929
  53. package/coverage/lcov-report/favicon.png +0 -0
  54. package/coverage/lcov-report/index.html +0 -141
  55. package/coverage/lcov-report/prettify.css +0 -1
  56. package/coverage/lcov-report/prettify.js +0 -2
  57. package/coverage/lcov-report/sifter.ts.html +0 -1322
  58. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  59. package/coverage/lcov-report/sorter.js +0 -170
  60. package/coverage/lcov-report/utils.ts.html +0 -398
  61. package/coverage/lcov.info +0 -419
  62. package/release.sh +0 -112
  63. package/test/getScoreFunction.js +0 -55
  64. package/test/prepareSearch.js +0 -40
  65. package/test/search.js +0 -307
  66. package/test/sorting.js +0 -193
  67. package/test/tokenize.js +0 -63
  68. package/test/utils.js +0 -28
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sources":["../../lib/utils.ts"],"sourcesContent":["\n// @ts-ignore TS2691 \"An import path cannot end with a '.ts' extension\"\nimport { asciifold } from './diacritics.ts';\n\n// @ts-ignore TS2691 \"An import path cannot end with a '.ts' extension\"\nimport { TToken } from './sifter.ts';\n\n\n/**\n * A property getter resolving dot-notation\n * @param {Object} obj The root object to fetch property on\n * @param {String} name The optionally dotted property name to fetch\n * @return {Object} The resolved property value\n */\nexport function getAttr(obj:{[key:string]:any}, name:string ) {\n if (!obj ) return;\n return obj[name];\n};\n\n/**\n * A property getter resolving dot-notation\n * @param {Object} obj The root object to fetch property on\n * @param {String} name The optionally dotted property name to fetch\n * @return {Object} The resolved property value\n */\nexport function getAttrNesting(obj:{[key:string]:any}, name:string ) {\n if (!obj ) return;\n var part, names = name.split(\".\");\n\twhile( (part = names.shift()) && (obj = obj[part]));\n return obj;\n};\n\n/**\n * Calculates how close of a match the\n * given value is against a search token.\n *\n */\nexport function scoreValue(value:string, token:TToken, weight:number ):number {\n\tvar score, pos;\n\n\tif (!value) return 0;\n\n\tvalue = value + '';\n\tpos = value.search(token.regex);\n\tif (pos === -1) return 0;\n\n\tscore = token.string.length / value.length;\n\tif (pos === 0) score += 0.5;\n\n\treturn score * weight;\n};\n\nexport function escape_regex(str:string):string {\n\treturn (str + '').replace(/([.?*+^$[\\]\\\\(){}|-])/g, '\\\\$1');\n};\n\n\n/**\n * Cast object property to an array if it exists and has a value\n *\n */\nexport function propToArray(obj:{[key:string]:any}, key:string){\n\tvar value = obj[key];\n\tif( value && !Array.isArray(value) ){\n\t\tobj[key] = [value];\n\t}\n}\n\n\n/**\n * Iterates over arrays and hashes.\n *\n * ```\n * iterate(this.items, function(item, id) {\n * // invoked for each item\n * });\n * ```\n *\n */\nexport function iterate(object:[]|{[key:string]:any}, callback:(value:any,key:number|string)=>any) {\n\n\tif ( Array.isArray(object)) {\n\t\tobject.forEach(callback);\n\n\t}else{\n\n\t\tfor (var key in object) {\n\t\t\tif (object.hasOwnProperty(key)) {\n\t\t\t\tcallback(object[key], key);\n\t\t\t}\n\t\t}\n\t}\n};\n\n\n\nexport function cmp(a:number|string, b:number|string) {\n\tif (typeof a === 'number' && typeof b === 'number') {\n\t\treturn a > b ? 1 : (a < b ? -1 : 0);\n\t}\n\ta = asciifold(a + '').toLowerCase();\n\tb = asciifold(b + '').toLowerCase();\n\tif (a > b) return 1;\n\tif (b > a) return -1;\n\treturn 0;\n};\n"],"names":["getAttr","obj","name","getAttrNesting","part","names","split","shift","scoreValue","value","token","weight","score","pos","search","regex","string","length","escape_regex","str","replace","propToArray","key","Array","isArray","iterate","object","callback","forEach","hasOwnProperty","cmp","a","b","asciifold","toLowerCase"],"mappings":";;;AACA;;AAOA;AACA;AACA;AACA;AACA;AACA;AAC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TS2691 \"An import path cannot end with a '.ts' extension\"\nimport { asciifold } from './diacritics.ts';\n\n// @ts-ignore TS2691 \"An import path cannot end with a '.ts' extension\"\nimport * as T from './types.ts';\n\n\n/**\n * A property getter resolving dot-notation\n * @param {Object} obj The root object to fetch property on\n * @param {String} name The optionally dotted property name to fetch\n * @return {Object} The resolved property value\n */\nexport const getAttr = (obj:{[key:string]:any}, name:string ) => {\n if (!obj ) return;\n return obj[name];\n};\n\n/**\n * A property getter resolving dot-notation\n * @param {Object} obj The root object to fetch property on\n * @param {String} name The optionally dotted property name to fetch\n * @return {Object} The resolved property value\n */\nexport const getAttrNesting = (obj:{[key:string]:any}, name:string ) => {\n if (!obj ) return;\n var part, names = name.split(\".\");\n\twhile( (part = names.shift()) && (obj = obj[part]));\n return obj;\n};\n\n/**\n * Calculates how close of a match the\n * given value is against a search token.\n *\n */\nexport const scoreValue = (value:string, token:T.Token, weight:number ):number => {\n\tvar score, pos;\n\n\tif (!value) return 0;\n\n\tvalue = value + '';\n\tpos = value.search(token.regex);\n\tif (pos === -1) return 0;\n\n\tscore = token.string.length / value.length;\n\tif (pos === 0) score += 0.5;\n\n\treturn score * weight;\n};\n\n/**\n *\n * https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error\n */\nexport const escape_regex = (str:string):string => {\t\n\treturn (str + '').replace(/([\\$\\(\\)\\*\\+\\.\\?\\[\\]\\^\\{\\|\\}\\\\])/gu, '\\\\$1');\n};\n\n\n/**\n * Cast object property to an array if it exists and has a value\n *\n */\nexport const propToArray = (obj:{[key:string]:any}, key:string) => {\n\tvar value = obj[key];\n\t\n\tif( typeof value == 'function' ) return value;\n\t\n\tif( value && !Array.isArray(value) ){\n\t\tobj[key] = [value];\n\t}\n}\n\n\n/**\n * Iterates over arrays and hashes.\n *\n * ```\n * iterate(this.items, function(item, id) {\n * // invoked for each item\n * });\n * ```\n *\n */\nexport const iterate = (object:[]|{[key:string]:any}, callback:(value:any,key:number|string)=>any) => {\n\n\tif ( Array.isArray(object)) {\n\t\tobject.forEach(callback);\n\n\t}else{\n\n\t\tfor (var key in object) {\n\t\t\tif (object.hasOwnProperty(key)) {\n\t\t\t\tcallback(object[key], key);\n\t\t\t}\n\t\t}\n\t}\n};\n\n\n\nexport const cmp = (a:number|string, b:number|string) => {\n\tif (typeof a === 'number' && typeof b === 'number') {\n\t\treturn a > b ? 1 : (a < b ? -1 : 0);\n\t}\n\ta = asciifold(a + '').toLowerCase();\n\tb = asciifold(b + '').toLowerCase();\n\tif (a > b) return 1;\n\tif (b > a) return -1;\n\treturn 0;\n};\n"],"names":["getAttr","obj","name","getAttrNesting","part","names","split","shift","scoreValue","value","token","weight","score","pos","search","regex","string","length","escape_regex","str","replace","propToArray","key","Array","isArray","iterate","object","callback","forEach","hasOwnProperty","cmp","a","b","asciifold","toLowerCase"],"mappings":";;;AACA;;AAOA;AACA;AACA;AACA;AACA;AACA;MACaA,OAAO,GAAG,CAACC,GAAD,EAAyBC,IAAzB,KAA0C;AAC7D,MAAI,CAACD,GAAL,EAAW;AACX,SAAOA,GAAG,CAACC,IAAD,CAAV;AACH;AAED;AACA;AACA;AACA;AACA;AACA;;MACaC,cAAc,GAAG,CAACF,GAAD,EAAyBC,IAAzB,KAA0C;AACpE,MAAI,CAACD,GAAL,EAAW;AACX,MAAIG,IAAJ;AAAA,MAAUC,KAAK,GAAGH,IAAI,CAACI,KAAL,CAAW,GAAX,CAAlB;;AACH,SAAO,CAACF,IAAI,GAAGC,KAAK,CAACE,KAAN,EAAR,MAA2BN,GAAG,GAAGA,GAAG,CAACG,IAAD,CAApC,CAAP,CAAmD;;AAChD,SAAOH,GAAP;AACH;AAED;AACA;AACA;AACA;AACA;;MACaO,UAAU,GAAG,CAACC,KAAD,EAAeC,KAAf,EAA8BC,MAA9B,KAAwD;AACjF,MAAIC,KAAJ,EAAWC,GAAX;AAEA,MAAI,CAACJ,KAAL,EAAY,OAAO,CAAP;AAEZA,EAAAA,KAAK,GAAGA,KAAK,GAAG,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;AACD;MAIYQ,GAAG,GAAG,CAACC,CAAD,EAAkBC,CAAlB,KAAsC;AACxD,MAAI,OAAOD,CAAP,KAAa,QAAb,IAAyB,OAAOC,CAAP,KAAa,QAA1C,EAAoD;AACnD,WAAOD,CAAC,GAAGC,CAAJ,GAAQ,CAAR,GAAaD,CAAC,GAAGC,CAAJ,GAAQ,CAAC,CAAT,GAAa,CAAjC;AACA;;AACDD,EAAAA,CAAC,GAAGE,SAAS,CAACF,CAAC,GAAG,EAAL,CAAT,CAAkBG,WAAlB,EAAJ;AACAF,EAAAA,CAAC,GAAGC,SAAS,CAACD,CAAC,GAAG,EAAL,CAAT,CAAkBE,WAAlB,EAAJ;AACA,MAAIH,CAAC,GAAGC,CAAR,EAAW,OAAO,CAAP;AACX,MAAIA,CAAC,GAAGD,CAAR,EAAW,OAAO,CAAC,CAAR;AACX,SAAO,CAAP;AACA;;;;"}
@@ -0,0 +1,40 @@
1
+ declare type TDiacraticList = {
2
+ [key: string]: string;
3
+ };
4
+ export declare const DIACRITICS: TDiacraticList;
5
+ /**
6
+ * code points generated from toCodePoints();
7
+ * removed 65339 to 65345
8
+ */
9
+ export declare const code_points: number[][];
10
+ /**
11
+ * Remove accents
12
+ * via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
13
+ *
14
+ */
15
+ export declare const asciifold: (str: string) => string;
16
+ /**
17
+ * Convert array of strings to a regular expression
18
+ * ex ['ab','a'] => (?:ab|a)
19
+ * ex ['a','b'] => [ab]
20
+ *
21
+ */
22
+ export declare const arrayToPattern: (chars: string[], glue?: string) => string;
23
+ /**
24
+ * Get all possible combinations of substrings that add up to the given string
25
+ * https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
26
+ *
27
+ */
28
+ export declare const allSubstrings: (input: string) => string[][];
29
+ /**
30
+ * Generate a list of diacritics from the list of code points
31
+ *
32
+ */
33
+ export declare const generateDiacritics: () => TDiacraticList;
34
+ /**
35
+ * Expand a regular expression pattern to include diacritics
36
+ * eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
37
+ *
38
+ */
39
+ export declare const diacriticRegexPoints: (regex: string) => string;
40
+ export {};
@@ -0,0 +1,64 @@
1
+ /**
2
+ * sifter.js
3
+ * Copyright (c) 2013–2020 Brian Reavis & contributors
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6
+ * file except in compliance with the License. You may obtain a copy of the License at:
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under
10
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ * ANY KIND, either express or implied. See the License for the specific language
12
+ * governing permissions and limitations under the License.
13
+ *
14
+ * @author Brian Reavis <brian@thirdroute.com>
15
+ */
16
+ import * as T from 'types.ts';
17
+ export default class Sifter {
18
+ items: any;
19
+ settings: T.Settings;
20
+ /**
21
+ * Textually searches arrays and hashes of objects
22
+ * by property (or multiple properties). Designed
23
+ * specifically for autocomplete.
24
+ *
25
+ */
26
+ constructor(items: any, settings: T.Settings);
27
+ /**
28
+ * Splits a search string into an array of individual
29
+ * regexps to be used to match results.
30
+ *
31
+ */
32
+ tokenize(query: string, respect_word_boundaries?: boolean, weights?: T.Weights): T.Token[];
33
+ /**
34
+ * Returns a function to be used to score individual results.
35
+ *
36
+ * Good matches will have a higher score than poor matches.
37
+ * If an item is not a match, 0 will be returned by the function.
38
+ *
39
+ * @returns {function}
40
+ */
41
+ getScoreFunction(query: string, options: T.Options): (data: {}) => any;
42
+ _getScoreFunction(search: T.PrepareObj): (data: {}) => any;
43
+ /**
44
+ * Returns a function that can be used to compare two
45
+ * results, for sorting purposes. If no sorting should
46
+ * be performed, `null` will be returned.
47
+ *
48
+ * @return function(a,b)
49
+ */
50
+ getSortFunction(query: string, options: T.Options): any;
51
+ _getSortFunction(search: T.PrepareObj): any;
52
+ /**
53
+ * Parses a search query and returns an object
54
+ * with tokens and fields ready to be populated
55
+ * with results.
56
+ *
57
+ */
58
+ prepareSearch(query: string, optsUser: Partial<T.Options>): T.PrepareObj;
59
+ /**
60
+ * Searches through all items and returns a sorted array of matches.
61
+ *
62
+ */
63
+ search(query: string, options: T.Options): T.PrepareObj;
64
+ }
@@ -0,0 +1,45 @@
1
+ import Sifter from 'sifter.ts';
2
+ export declare type Field = {
3
+ field: string;
4
+ weight: number;
5
+ };
6
+ export declare type Sort = {
7
+ field: string;
8
+ direction?: string;
9
+ };
10
+ export declare type SortFn = (this: Sifter, a: ResultItem, b: ResultItem) => number;
11
+ export declare type Options = {
12
+ fields: Field[];
13
+ score: () => any;
14
+ filter: boolean;
15
+ limit: number;
16
+ sort: SortFn | Sort[];
17
+ sort_empty: SortFn | Sort[];
18
+ nesting: boolean;
19
+ respect_word_boundaries: boolean;
20
+ conjunction: string;
21
+ };
22
+ export declare type Token = {
23
+ string: string;
24
+ regex: RegExp | null;
25
+ field: string | null;
26
+ };
27
+ export declare type Weights = {
28
+ [key: string]: number;
29
+ };
30
+ export declare type PrepareObj = {
31
+ options: Options;
32
+ query: string;
33
+ tokens: Token[];
34
+ total: number;
35
+ items: ResultItem[];
36
+ weights: Weights;
37
+ getAttrFn: (data: any, field: string) => any;
38
+ };
39
+ export declare type Settings = {
40
+ diacritics: boolean;
41
+ };
42
+ export declare type ResultItem = {
43
+ score: number;
44
+ id: number | string;
45
+ };
@@ -0,0 +1,52 @@
1
+ /**
2
+ * A property getter resolving dot-notation
3
+ * @param {Object} obj The root object to fetch property on
4
+ * @param {String} name The optionally dotted property name to fetch
5
+ * @return {Object} The resolved property value
6
+ */
7
+ export declare const getAttr: (obj: {
8
+ [key: string]: any;
9
+ }, name: string) => any;
10
+ /**
11
+ * A property getter resolving dot-notation
12
+ * @param {Object} obj The root object to fetch property on
13
+ * @param {String} name The optionally dotted property name to fetch
14
+ * @return {Object} The resolved property value
15
+ */
16
+ export declare const getAttrNesting: (obj: {
17
+ [key: string]: any;
18
+ }, name: string) => {
19
+ [key: string]: any;
20
+ } | undefined;
21
+ /**
22
+ * Calculates how close of a match the
23
+ * given value is against a search token.
24
+ *
25
+ */
26
+ export declare const scoreValue: (value: string, token: any, weight: number) => number;
27
+ /**
28
+ *
29
+ * https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error
30
+ */
31
+ export declare const escape_regex: (str: string) => string;
32
+ /**
33
+ * Cast object property to an array if it exists and has a value
34
+ *
35
+ */
36
+ export declare const propToArray: (obj: {
37
+ [key: string]: any;
38
+ }, key: string) => any;
39
+ /**
40
+ * Iterates over arrays and hashes.
41
+ *
42
+ * ```
43
+ * iterate(this.items, function(item, id) {
44
+ * // invoked for each item
45
+ * });
46
+ * ```
47
+ *
48
+ */
49
+ export declare const iterate: (object: [] | {
50
+ [key: string]: any;
51
+ }, callback: (value: any, key: number | string) => any) => void;
52
+ export declare const cmp: (a: number | string, b: number | string) => 1 | -1 | 0;
@@ -6,118 +6,165 @@
6
6
  }(this, (function () { 'use strict';
7
7
 
8
8
  // https://github.com/andrewrk/node-diacritics/blob/master/index.js
9
+ var latin_pat;
10
+ const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}]'; // \u{2bc}
11
+
12
+ const accent_reg = new RegExp(accent_pat, 'g');
13
+ var diacritic_patterns;
14
+ const latin_convert = {
15
+ 'æ': 'ae',
16
+ 'ⱥ': 'a',
17
+ 'ø': 'o'
18
+ };
19
+ const convert_pat = new RegExp(Object.keys(latin_convert).join('|'), 'g');
9
20
  /**
10
21
  * code points generated from toCodePoints();
11
22
  * removed 65339 to 65345
12
23
  */
13
24
 
14
- var code_points = [[67, 67], [160, 160], [192, 438], [452, 652], [961, 961], [1019, 1019], [1083, 1083], [1281, 1289], [1984, 1984], [5095, 5095], [7429, 7441], [7545, 7549], [7680, 7935], [8580, 8580], [9398, 9449], [11360, 11391], [42792, 42793], [42802, 42851], [42873, 42897], [42912, 42922], [64256, 64260], [65313, 65338], [65345, 65370]];
25
+ const code_points = [[67, 67], [160, 160], [192, 438], [452, 652], [961, 961], [1019, 1019], [1083, 1083], [1281, 1289], [1984, 1984], [5095, 5095], [7429, 7441], [7545, 7549], [7680, 7935], [8580, 8580], [9398, 9449], [11360, 11391], [42792, 42793], [42802, 42851], [42873, 42897], [42912, 42922], [64256, 64260], [65313, 65338], [65345, 65370]];
15
26
  /**
16
27
  * Remove accents
17
28
  * via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
18
29
  *
19
30
  */
20
31
 
21
- function asciifold(str) {
22
- return str.normalize('NFD').replace(/[\u0300-\u036F]/g, '').normalize('NFKD').toLowerCase();
23
- }
32
+ const asciifold = str => {
33
+ return str.normalize('NFKD').replace(accent_reg, '').toLowerCase().replace(convert_pat, function (foreignletter) {
34
+ return latin_convert[foreignletter];
35
+ });
36
+ };
24
37
  /**
25
- * Generate a list of diacritics from the list of code points
38
+ * Convert array of strings to a regular expression
39
+ * ex ['ab','a'] => (?:ab|a)
40
+ * ex ['a','b'] => [ab]
26
41
  *
27
42
  */
28
43
 
29
44
 
30
- function generateDiacritics() {
31
- var latin_convert = {
32
- 'l·': 'l',
33
- 'ʼn': 'n',
34
- 'æ': 'ae',
35
- 'ø': 'o',
36
- 'aʾ': 'a',
37
- 'dž': 'dz'
38
- };
39
- var diacritics = {}; //var no_latin = [];
45
+ const arrayToPattern = (chars, glue = '|') => {
46
+ if (chars.length == 1) {
47
+ return chars[0];
48
+ }
49
+
50
+ var longest = 1;
51
+ chars.forEach(a => {
52
+ longest = Math.max(longest, a.length);
53
+ });
40
54
 
55
+ if (longest == 1) {
56
+ return '[' + chars.join('') + ']';
57
+ }
58
+
59
+ return '(?:' + chars.join(glue) + ')';
60
+ };
61
+ /**
62
+ * Get all possible combinations of substrings that add up to the given string
63
+ * https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
64
+ *
65
+ */
66
+
67
+ const allSubstrings = input => {
68
+ if (input.length === 1) return [[input]];
69
+ var result = [];
70
+ allSubstrings(input.substring(1)).forEach(function (subresult) {
71
+ var tmp = subresult.slice(0);
72
+ tmp[0] = input.charAt(0) + tmp[0];
73
+ result.push(tmp);
74
+ tmp = subresult.slice(0);
75
+ tmp.unshift(input.charAt(0));
76
+ result.push(tmp);
77
+ });
78
+ return result;
79
+ };
80
+ /**
81
+ * Generate a list of diacritics from the list of code points
82
+ *
83
+ */
84
+
85
+ const generateDiacritics = () => {
86
+ var diacritics = {};
41
87
  code_points.forEach(code_range => {
42
88
  for (let i = code_range[0]; i <= code_range[1]; i++) {
43
89
  let diacritic = String.fromCharCode(i);
44
- let latin = diacritic.normalize('NFD').replace(/[\u0300-\u036F]/g, '').normalize('NFKD');
90
+ let latin = asciifold(diacritic);
45
91
 
46
- if (latin == diacritic) {
47
- //no_latin.push(diacritic);
92
+ if (latin == diacritic.toLowerCase()) {
48
93
  continue;
49
94
  }
50
95
 
51
- latin = latin.toLowerCase();
52
-
53
- if (latin in latin_convert) {
54
- latin = latin_convert[latin];
96
+ if (!(latin in diacritics)) {
97
+ diacritics[latin] = [latin];
55
98
  }
56
99
 
57
- if (!(latin in diacritics)) {
58
- diacritics[latin] = latin + latin.toUpperCase();
100
+ var patt = new RegExp(arrayToPattern(diacritics[latin]), 'iu');
101
+
102
+ if (diacritic.match(patt)) {
103
+ continue;
59
104
  }
60
105
 
61
- diacritics[latin] += diacritic;
106
+ diacritics[latin].push(diacritic);
62
107
  }
63
- }); //console.log('no_latin',JSON.stringify(no_latin));
108
+ });
109
+ var latin_chars = Object.keys(diacritics); // latin character pattern
110
+ // match longer substrings first
111
+
112
+ latin_chars = latin_chars.sort((a, b) => b.length - a.length);
113
+ latin_pat = new RegExp('(' + arrayToPattern(latin_chars) + accent_pat + '*)', 'g'); // build diacritic patterns
114
+ // ae needs:
115
+ // (?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))
116
+
117
+ var diacritic_patterns = {};
118
+ latin_chars.sort((a, b) => a.length - b.length).forEach(latin => {
119
+ var substrings = allSubstrings(latin);
120
+ var pattern = substrings.map(sub_pat => {
121
+ sub_pat = sub_pat.map(l => {
122
+ if (diacritics.hasOwnProperty(l)) {
123
+ return arrayToPattern(diacritics[l]);
124
+ }
64
125
 
65
- return diacritics;
66
- }
126
+ return l;
127
+ });
128
+ return arrayToPattern(sub_pat, '');
129
+ });
130
+ diacritic_patterns[latin] = arrayToPattern(pattern);
131
+ });
132
+ return diacritic_patterns;
133
+ };
67
134
  /**
68
135
  * Expand a regular expression pattern to include diacritics
69
136
  * eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
70
137
  *
71
138
  */
72
139
 
73
- var diacritics = null;
74
- function diacriticRegexPoints(regex) {
75
- if (diacritics === null) {
76
- diacritics = generateDiacritics();
140
+ const diacriticRegexPoints = regex => {
141
+ if (diacritic_patterns === undefined) {
142
+ diacritic_patterns = generateDiacritics();
77
143
  }
78
144
 
79
- for (let latin in diacritics) {
80
- if (diacritics.hasOwnProperty(latin)) {
81
- regex = regex.replace(new RegExp(latin, 'g'), '[' + diacritics[latin] + ']');
82
- }
83
- }
145
+ const decomposed = regex.normalize('NFKD').toLowerCase();
146
+ return decomposed.split(latin_pat).map(part => {
147
+ if (part == '') {
148
+ return '';
149
+ } // "ffl" or "ffl"
84
150
 
85
- return regex;
86
- }
87
- /**
88
- * Expand a regular expression pattern to include diacritics
89
- * eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
90
- *
91
- * rollup will bundle this function (and the DIACRITICS constant) unless commented out
92
- *
93
- var diacriticRegex = (function() {
94
151
 
95
- var list = [];
96
- for( let letter in DIACRITICS ){
152
+ const no_accent = asciifold(part);
97
153
 
98
- if( letter.toLowerCase() != letter && letter.toLowerCase() in DIACRITICS ){
99
- continue;
100
- }
154
+ if (diacritic_patterns.hasOwnProperty(no_accent)) {
155
+ return diacritic_patterns[no_accent];
156
+ } // 'أهلا' (\u{623}\u{647}\u{644}\u{627}) or 'أهلا' (\u{627}\u{654}\u{647}\u{644}\u{627})
101
157
 
102
- if( DIACRITICS.hasOwnProperty(letter) ){
103
158
 
104
- var replace = letter + DIACRITICS[letter];
105
- if( letter.toUpperCase() in DIACRITICS ){
106
- replace += letter.toUpperCase() + DIACRITICS[letter.toUpperCase()];
107
- }
159
+ const composed_part = part.normalize('NFC');
108
160
 
109
- list.push({let:letter,pat:'['+replace+']'});
110
- }
111
- }
161
+ if (composed_part != part) {
162
+ return arrayToPattern([part, composed_part]);
163
+ }
112
164
 
113
- return function(regex:string):string{
114
- list.forEach((item)=>{
115
- regex = regex.replace( new RegExp(item.let,'g'),item.pat);
116
- });
117
- return regex;
118
- }
119
- })();
120
- */
165
+ return part;
166
+ }).join('');
167
+ };
121
168
 
122
169
  // @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
123
170
 
@@ -127,10 +174,10 @@
127
174
  * @param {String} name The optionally dotted property name to fetch
128
175
  * @return {Object} The resolved property value
129
176
  */
130
- function getAttr(obj, name) {
177
+ const getAttr = (obj, name) => {
131
178
  if (!obj) return;
132
179
  return obj[name];
133
- }
180
+ };
134
181
  /**
135
182
  * A property getter resolving dot-notation
136
183
  * @param {Object} obj The root object to fetch property on
@@ -138,7 +185,7 @@
138
185
  * @return {Object} The resolved property value
139
186
  */
140
187
 
141
- function getAttrNesting(obj, name) {
188
+ const getAttrNesting = (obj, name) => {
142
189
  if (!obj) return;
143
190
  var part,
144
191
  names = name.split(".");
@@ -146,14 +193,14 @@
146
193
  while ((part = names.shift()) && (obj = obj[part]));
147
194
 
148
195
  return obj;
149
- }
196
+ };
150
197
  /**
151
198
  * Calculates how close of a match the
152
199
  * given value is against a search token.
153
200
  *
154
201
  */
155
202
 
156
- function scoreValue(value, token, weight) {
203
+ const scoreValue = (value, token, weight) => {
157
204
  var score, pos;
158
205
  if (!value) return 0;
159
206
  value = value + '';
@@ -162,22 +209,28 @@
162
209
  score = token.string.length / value.length;
163
210
  if (pos === 0) score += 0.5;
164
211
  return score * weight;
165
- }
166
- function escape_regex(str) {
167
- return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
168
- }
212
+ };
213
+ /**
214
+ *
215
+ * https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error
216
+ */
217
+
218
+ const escape_regex = str => {
219
+ return (str + '').replace(/([\$\(-\+\.\?\[-\^\{-\}])/g, '\\$1');
220
+ };
169
221
  /**
170
222
  * Cast object property to an array if it exists and has a value
171
223
  *
172
224
  */
173
225
 
174
- function propToArray(obj, key) {
226
+ const propToArray = (obj, key) => {
175
227
  var value = obj[key];
228
+ if (typeof value == 'function') return value;
176
229
 
177
230
  if (value && !Array.isArray(value)) {
178
231
  obj[key] = [value];
179
232
  }
180
- }
233
+ };
181
234
  /**
182
235
  * Iterates over arrays and hashes.
183
236
  *
@@ -189,7 +242,7 @@
189
242
  *
190
243
  */
191
244
 
192
- function iterate(object, callback) {
245
+ const iterate = (object, callback) => {
193
246
  if (Array.isArray(object)) {
194
247
  object.forEach(callback);
195
248
  } else {
@@ -199,8 +252,8 @@
199
252
  }
200
253
  }
201
254
  }
202
- }
203
- function cmp(a, b) {
255
+ };
256
+ const cmp = (a, b) => {
204
257
  if (typeof a === 'number' && typeof b === 'number') {
205
258
  return a > b ? 1 : a < b ? -1 : 0;
206
259
  }
@@ -210,7 +263,7 @@
210
263
  if (a > b) return 1;
211
264
  if (b > a) return -1;
212
265
  return 0;
213
- }
266
+ };
214
267
 
215
268
  /**
216
269
  * sifter.js
@@ -227,6 +280,7 @@
227
280
  *
228
281
  * @author Brian Reavis <brian@thirdroute.com>
229
282
  */
283
+
230
284
  class Sifter {
231
285
  // []|{};
232
286
 
@@ -282,7 +336,7 @@
282
336
 
283
337
  tokens.push({
284
338
  string: word,
285
- regex: regex ? new RegExp(regex, 'i') : null,
339
+ regex: regex ? new RegExp(regex, 'iu') : null,
286
340
  field: field
287
341
  });
288
342
  });
@@ -326,9 +380,6 @@
326
380
  * Calculates the score of an object
327
381
  * against the search query.
328
382
  *
329
- * @param {TToken} token
330
- * @param {object} data
331
- * @return {number}
332
383
  */
333
384
 
334
385
 
@@ -411,12 +462,17 @@
411
462
  sort = !search.query && options.sort_empty ? options.sort_empty : options.sort,
412
463
  sort_flds = [],
413
464
  multipliers = [];
465
+
466
+ if (typeof sort == 'function') {
467
+ return sort.bind(this);
468
+ }
414
469
  /**
415
470
  * Fetches the specified sort field value
416
471
  * from a search result item.
417
472
  *
418
473
  */
419
474
 
475
+
420
476
  const get_field = function get_field(name, result) {
421
477
  if (name === '$score') return result.score;
422
478
  return search.getAttrFn(self.items[result.id], name);
@@ -517,10 +573,9 @@
517
573
  options.fields = fields;
518
574
  }
519
575
 
520
- query = asciifold(query + '').toLowerCase().trim();
521
576
  return {
522
577
  options: options,
523
- query: query,
578
+ query: query.toLowerCase().trim(),
524
579
  tokens: this.tokenize(query, options.respect_word_boundaries, weights),
525
580
  total: 0,
526
581
  items: [],
@@ -556,7 +611,7 @@
556
611
  }
557
612
  });
558
613
  } else {
559
- iterate(self.items, (item, id) => {
614
+ iterate(self.items, (_, id) => {
560
615
  search.items.push({
561
616
  'score': 1,
562
617
  'id': id