@orchidjs/sifter 0.9.3 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/cjs/sifter.js +776 -49
- package/dist/cjs/sifter.js.map +1 -1
- package/dist/esm/lib/sifter.js +368 -0
- package/dist/esm/lib/sifter.js.map +1 -0
- package/dist/{cjs → esm/lib}/utils.js +5 -24
- package/dist/esm/lib/utils.js.map +1 -0
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js +508 -0
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js.map +1 -0
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js +113 -0
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js.map +1 -0
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js +29 -0
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js.map +1 -0
- package/dist/esm/sifter.js +32 -36
- package/dist/esm/sifter.js.map +1 -1
- package/dist/esm/utils.js +3 -12
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/sifter.d.ts +16 -9
- package/dist/types/types.d.ts +20 -8
- package/dist/types/utils.d.ts +2 -6
- package/dist/umd/sifter.js +1101 -638
- package/dist/umd/sifter.js.map +1 -1
- package/dist/umd/sifter.min.js +110 -78
- package/dist/umd/sifter.min.js.map +1 -1
- package/lib/sifter.ts +44 -48
- package/lib/types.ts +28 -10
- package/lib/utils.ts +4 -14
- package/package.json +6 -3
- package/dist/cjs/diacritics.js +0 -182
- package/dist/cjs/diacritics.js.map +0 -1
- package/dist/cjs/utils.js.map +0 -1
- package/dist/esm/diacritics.js +0 -173
- package/dist/esm/diacritics.js.map +0 -1
- package/dist/types/diacritics.d.ts +0 -35
- package/lib/diacritics.ts +0 -196
package/dist/cjs/diacritics.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
-
|
|
6
|
-
var utils = require('./utils.js');
|
|
7
|
-
|
|
8
|
-
// @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
|
|
9
|
-
// https://github.com/andrewrk/node-diacritics/blob/master/index.js
|
|
10
|
-
var latin_pat;
|
|
11
|
-
const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}]'; // \u{2bc}
|
|
12
|
-
|
|
13
|
-
const accent_reg = new RegExp(accent_pat, 'gu');
|
|
14
|
-
var diacritic_patterns;
|
|
15
|
-
const latin_convert = {
|
|
16
|
-
'æ': 'ae',
|
|
17
|
-
'ⱥ': 'a',
|
|
18
|
-
'ø': 'o'
|
|
19
|
-
};
|
|
20
|
-
const convert_pat = new RegExp(Object.keys(latin_convert).join('|'), 'gu');
|
|
21
|
-
const code_points = [[0, 65535]];
|
|
22
|
-
/**
|
|
23
|
-
* Remove accents
|
|
24
|
-
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
const asciifold = str => {
|
|
29
|
-
return str.normalize('NFKD').replace(accent_reg, '').toLowerCase().replace(convert_pat, function (foreignletter) {
|
|
30
|
-
return latin_convert[foreignletter] || foreignletter;
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* Convert array of strings to a regular expression
|
|
35
|
-
* ex ['ab','a'] => (?:ab|a)
|
|
36
|
-
* ex ['a','b'] => [ab]
|
|
37
|
-
*
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
const arrayToPattern = (chars, glue = '|') => {
|
|
41
|
-
if (chars.length === 1 && chars[0] != undefined) {
|
|
42
|
-
return chars[0];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
var longest = 1;
|
|
46
|
-
chars.forEach(a => {
|
|
47
|
-
longest = Math.max(longest, a.length);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
if (longest == 1) {
|
|
51
|
-
return '[' + chars.join('') + ']';
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return '(?:' + chars.join(glue) + ')';
|
|
55
|
-
};
|
|
56
|
-
const escapeToPattern = chars => {
|
|
57
|
-
const escaped = chars.map(diacritic => utils.escape_regex(diacritic));
|
|
58
|
-
return arrayToPattern(escaped);
|
|
59
|
-
};
|
|
60
|
-
/**
|
|
61
|
-
* Get all possible combinations of substrings that add up to the given string
|
|
62
|
-
* https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
|
|
63
|
-
*
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
const allSubstrings = input => {
|
|
67
|
-
if (input.length === 1) return [[input]];
|
|
68
|
-
var result = [];
|
|
69
|
-
allSubstrings(input.substring(1)).forEach(function (subresult) {
|
|
70
|
-
var tmp = subresult.slice(0);
|
|
71
|
-
tmp[0] = input.charAt(0) + tmp[0];
|
|
72
|
-
result.push(tmp);
|
|
73
|
-
tmp = subresult.slice(0);
|
|
74
|
-
tmp.unshift(input.charAt(0));
|
|
75
|
-
result.push(tmp);
|
|
76
|
-
});
|
|
77
|
-
return result;
|
|
78
|
-
};
|
|
79
|
-
/**
|
|
80
|
-
* Generate a list of diacritics from the list of code points
|
|
81
|
-
*
|
|
82
|
-
*/
|
|
83
|
-
|
|
84
|
-
const generateDiacritics = code_points => {
|
|
85
|
-
var diacritics = {};
|
|
86
|
-
code_points.forEach(code_range => {
|
|
87
|
-
for (let i = code_range[0]; i <= code_range[1]; i++) {
|
|
88
|
-
let diacritic = String.fromCharCode(i);
|
|
89
|
-
let latin = asciifold(diacritic);
|
|
90
|
-
|
|
91
|
-
if (latin == diacritic.toLowerCase()) {
|
|
92
|
-
continue;
|
|
93
|
-
} // skip when latin is a string longer than 3 characters long
|
|
94
|
-
// bc the resulting regex patterns will be long
|
|
95
|
-
// eg:
|
|
96
|
-
// latin صلى الله عليه وسلم length 18 code point 65018
|
|
97
|
-
// latin جل جلاله length 8 code point 65019
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (latin.length > 3) {
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const latin_diacritics = diacritics[latin] || [latin];
|
|
105
|
-
const patt = new RegExp(escapeToPattern(latin_diacritics), 'iu');
|
|
106
|
-
|
|
107
|
-
if (diacritic.match(patt)) {
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
latin_diacritics.push(diacritic);
|
|
112
|
-
diacritics[latin] = latin_diacritics;
|
|
113
|
-
}
|
|
114
|
-
}); // filter out if there's only one character in the list
|
|
115
|
-
// todo: this may not be needed
|
|
116
|
-
|
|
117
|
-
Object.keys(diacritics).forEach(latin => {
|
|
118
|
-
const latin_diacritics = diacritics[latin] || [];
|
|
119
|
-
|
|
120
|
-
if (latin_diacritics.length < 2) {
|
|
121
|
-
delete diacritics[latin];
|
|
122
|
-
}
|
|
123
|
-
}); // latin character pattern
|
|
124
|
-
// match longer substrings first
|
|
125
|
-
|
|
126
|
-
let latin_chars = Object.keys(diacritics).sort((a, b) => b.length - a.length);
|
|
127
|
-
latin_pat = new RegExp('(' + escapeToPattern(latin_chars) + accent_pat + '*)', 'gu'); // build diacritic patterns
|
|
128
|
-
// ae needs:
|
|
129
|
-
// (?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))
|
|
130
|
-
|
|
131
|
-
var diacritic_patterns = {};
|
|
132
|
-
latin_chars.sort((a, b) => a.length - b.length).forEach(latin => {
|
|
133
|
-
var substrings = allSubstrings(latin);
|
|
134
|
-
var pattern = substrings.map(sub_pat => {
|
|
135
|
-
sub_pat = sub_pat.map(l => {
|
|
136
|
-
if (diacritics.hasOwnProperty(l)) {
|
|
137
|
-
return escapeToPattern(diacritics[l]);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return l;
|
|
141
|
-
});
|
|
142
|
-
return arrayToPattern(sub_pat, '');
|
|
143
|
-
});
|
|
144
|
-
diacritic_patterns[latin] = arrayToPattern(pattern);
|
|
145
|
-
});
|
|
146
|
-
return diacritic_patterns;
|
|
147
|
-
};
|
|
148
|
-
/**
|
|
149
|
-
* Expand a regular expression pattern to include diacritics
|
|
150
|
-
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
|
|
151
|
-
*
|
|
152
|
-
*/
|
|
153
|
-
|
|
154
|
-
const diacriticRegexPoints = regex => {
|
|
155
|
-
if (diacritic_patterns === undefined) {
|
|
156
|
-
diacritic_patterns = generateDiacritics(code_points);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const decomposed = regex.normalize('NFKD').toLowerCase();
|
|
160
|
-
return decomposed.split(latin_pat).map(part => {
|
|
161
|
-
// "ffl" or "ffl"
|
|
162
|
-
const no_accent = asciifold(part);
|
|
163
|
-
|
|
164
|
-
if (no_accent == '') {
|
|
165
|
-
return '';
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (diacritic_patterns.hasOwnProperty(no_accent)) {
|
|
169
|
-
return diacritic_patterns[no_accent];
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return part;
|
|
173
|
-
}).join('');
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
exports.allSubstrings = allSubstrings;
|
|
177
|
-
exports.arrayToPattern = arrayToPattern;
|
|
178
|
-
exports.asciifold = asciifold;
|
|
179
|
-
exports.diacriticRegexPoints = diacriticRegexPoints;
|
|
180
|
-
exports.escapeToPattern = escapeToPattern;
|
|
181
|
-
exports.generateDiacritics = generateDiacritics;
|
|
182
|
-
//# sourceMappingURL=diacritics.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"diacritics.js","sources":["../../lib/diacritics.ts"],"sourcesContent":["\n// @ts-ignore TS2691 \"An import path cannot end with a '.ts' extension\"\nimport { escape_regex } from './utils.ts';\n\ntype TDiacraticList = {[key:string]:string};\n\n// https://github.com/andrewrk/node-diacritics/blob/master/index.js\n\nvar latin_pat:RegExp;\nconst accent_pat = '[\\u0300-\\u036F\\u{b7}\\u{2be}]'; // \\u{2bc}\nconst accent_reg = new RegExp(accent_pat,'gu');\nvar diacritic_patterns:TDiacraticList;\n\nconst latin_convert:TDiacraticList = {\n\t'æ': 'ae',\n\t'ⱥ': 'a',\n\t'ø': 'o',\n};\n\nconst convert_pat = new RegExp(Object.keys(latin_convert).join('|'),'gu');\n\nconst code_points:[[number,number]] = [[ 0, 65535 ]];\n\n/**\n * Remove accents\n * via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703\n *\n */\nexport const asciifold = (str:string):string => {\n\treturn str\n\t\t.normalize('NFKD')\n\t\t.replace(accent_reg, '')\n\t\t.toLowerCase()\n\t\t.replace(convert_pat,function(foreignletter) {\n\t\t\treturn latin_convert[foreignletter] || foreignletter;\n\t\t});\n};\n\n/**\n * Convert array of strings to a regular expression\n *\tex ['ab','a'] => (?:ab|a)\n * \tex ['a','b'] => [ab]\n *\n */\nexport const arrayToPattern = (chars:string[],glue:string='|'):string =>{\n\n\tif( chars.length === 1 && chars[0] != undefined ){\n\t\treturn chars[0];\n\t}\n\n\tvar longest = 1;\n\tchars.forEach((a)=>{longest = Math.max(longest,a.length)});\n\n\tif( longest == 1 ){\n\t\treturn '['+chars.join('')+']';\n\t}\n\n\treturn '(?:'+chars.join(glue)+')';\n};\n\nexport const escapeToPattern = (chars:string[]):string =>{\n\tconst escaped = chars.map((diacritic) => escape_regex(diacritic));\n\treturn arrayToPattern(escaped);\n};\n\n/**\n * Get all possible combinations of substrings that add up to the given string\n * https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string\n *\n */\nexport const allSubstrings = (input:string):string[][] => {\n\n if( input.length === 1) return [[input]];\n\n var result:string[][] = [];\n allSubstrings(input.substring(1)).forEach(function(subresult) {\n var tmp = subresult.slice(0);\n tmp[0] = input.charAt(0) + tmp[0];\n result.push(tmp);\n\n tmp = subresult.slice(0);\n tmp.unshift(input.charAt(0));\n result.push(tmp);\n });\n\n return result;\n}\n\n/**\n * Generate a list of diacritics from the list of code points\n *\n */\nexport const generateDiacritics = (code_points:[[number,number]]):TDiacraticList => {\n\n\tvar diacritics:{[key:string]:string[]} = {};\n\tcode_points.forEach((code_range)=>{\n\n\t\tfor(let i = code_range[0]; i <= code_range[1]; i++){\n\n\t\t\tlet diacritic\t= String.fromCharCode(i);\n\t\t\tlet\tlatin\t\t= asciifold(diacritic);\n\n\t\t\tif( latin == diacritic.toLowerCase() ){\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// skip when latin is a string longer than 3 characters long\n\t\t\t// bc the resulting regex patterns will be long\n\t\t\t// eg:\n\t\t\t// latin صلى الله عليه وسلم length 18 code point 65018\n\t\t\t// latin جل جلاله length 8 code point 65019\n\t\t\tif( latin.length > 3 ){\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst latin_diacritics:string[] = diacritics[latin] || [latin];\n\t\t\tconst patt = new RegExp( escapeToPattern(latin_diacritics),'iu');\n\t\t\tif( diacritic.match(patt) ){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tlatin_diacritics.push(diacritic);\n\t\t\tdiacritics[latin] = latin_diacritics;\n\t\t}\n\t});\n\n\t// filter out if there's only one character in the list\n\t// todo: this may not be needed\n\tObject.keys(diacritics).forEach(latin => {\n\t\tconst latin_diacritics = diacritics[latin] || [];\n\t\tif( latin_diacritics.length < 2 ){\n\t\t\tdelete diacritics[latin];\n\t\t}\n\t});\n\n\n\t// latin character pattern\n\t// match longer substrings first\n\tlet latin_chars\t= Object.keys(diacritics).sort((a, b) => b.length - a.length );\n\tlatin_pat\t\t= new RegExp('('+ escapeToPattern(latin_chars) + accent_pat + '*)','gu');\n\n\n\t// build diacritic patterns\n\t// ae needs:\n\t//\t(?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))\n\tvar diacritic_patterns:TDiacraticList = {};\n\tlatin_chars.sort((a,b) => a.length -b.length).forEach((latin)=>{\n\n\t\tvar substrings\t= allSubstrings(latin);\n\t\tvar pattern = substrings.map((sub_pat)=>{\n\n\t\t\tsub_pat = sub_pat.map((l)=>{\n\t\t\t\tif( diacritics.hasOwnProperty(l) ){\n\t\t\t\t\treturn escapeToPattern(diacritics[l]!);\n\t\t\t\t}\n\t\t\t\treturn l;\n\t\t\t});\n\n\t\t\treturn arrayToPattern(sub_pat,'');\n\t\t});\n\n\t\tdiacritic_patterns[latin] = arrayToPattern(pattern);\n\t});\n\n\n\treturn diacritic_patterns;\n}\n\n/**\n * Expand a regular expression pattern to include diacritics\n * \teg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/\n *\n */\nexport const diacriticRegexPoints = (regex:string):string => {\n\n\tif( diacritic_patterns === undefined ){\n\t\tdiacritic_patterns = generateDiacritics(code_points);\n\t}\n\n\tconst decomposed\t\t= regex.normalize('NFKD').toLowerCase();\n\n\treturn decomposed.split(latin_pat).map((part:string)=>{\n\n\t\t// \"ffl\" or \"ffl\"\n\t\tconst no_accent = asciifold(part);\n\t\tif( no_accent == '' ){\n\t\t\treturn '';\n\t\t}\n\n\t\tif( diacritic_patterns.hasOwnProperty(no_accent) ){\n\t\t\treturn diacritic_patterns[no_accent];\n\t\t}\n\n\t\treturn part;\n\t}).join('');\n\n}\n"],"names":["latin_pat","accent_pat","accent_reg","RegExp","diacritic_patterns","latin_convert","convert_pat","Object","keys","join","code_points","asciifold","str","normalize","replace","toLowerCase","foreignletter","arrayToPattern","chars","glue","length","undefined","longest","forEach","a","Math","max","escapeToPattern","escaped","map","diacritic","escape_regex","allSubstrings","input","result","substring","subresult","tmp","slice","charAt","push","unshift","generateDiacritics","diacritics","code_range","i","String","fromCharCode","latin","latin_diacritics","patt","match","latin_chars","sort","b","substrings","pattern","sub_pat","l","hasOwnProperty","diacriticRegexPoints","regex","decomposed","split","part","no_accent"],"mappings":";;;;;;;AACA;AAKA;AAEA,IAAIA,SAAJ;AACA,MAAMC,UAAU,GAAG,8BAAnB;;AACA,MAAMC,UAAU,GAAG,IAAIC,MAAJ,CAAWF,UAAX,EAAsB,IAAtB,CAAnB;AACA,IAAIG,kBAAJ;AAEA,MAAMC,aAA4B,GAAG;AACpC,OAAK,IAD+B;AAEpC,OAAK,GAF+B;AAGpC,OAAK;AAH+B,CAArC;AAMA,MAAMC,WAAW,GAAG,IAAIH,MAAJ,CAAWI,MAAM,CAACC,IAAP,CAAYH,aAAZ,EAA2BI,IAA3B,CAAgC,GAAhC,CAAX,EAAgD,IAAhD,CAApB;AAEA,MAAMC,WAA6B,GAAG,CAAC,CAAE,CAAF,EAAK,KAAL,CAAD,CAAtC;AAEA;AACA;AACA;AACA;AACA;;MACaC,SAAS,GAAIC,GAAD,IAAuB;AAC/C,SAAOA,GAAG,CACRC,SADK,CACK,MADL,EAELC,OAFK,CAEGZ,UAFH,EAEe,EAFf,EAGLa,WAHK,GAILD,OAJK,CAIGR,WAJH,EAIe,UAASU,aAAT,EAAwB;AAC5C,WAAOX,aAAa,CAACW,aAAD,CAAb,IAAgCA,aAAvC;AACA,GANK,CAAP;AAOA;AAED;AACA;AACA;AACA;AACA;AACA;;MACaC,cAAc,GAAG,CAACC,KAAD,EAAgBC,IAAW,GAAC,GAA5B,KAA0C;AAEvE,MAAID,KAAK,CAACE,MAAN,KAAiB,CAAjB,IAAsBF,KAAK,CAAC,CAAD,CAAL,IAAYG,SA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oB,GAAIC,KAAD,IAAyB;AAE5D,MAAIzD,kBAAkB,KAAKiB,SAA3B,EAAsC;AACrCjB,IAAAA,kBAAkB,GAAGsC,kBAAkB,CAAChC,WAAD,CAAvC;AACA;;AAED,QAAMoD,UAAU,GAAID,KAAK,CAAChD,SAAN,CAAgB,MAAhB,EAAwBE,WAAxB,EAApB;AAEA,SAAO+C,UAAU,CAACC,KAAX,CAAiB/D,SAAjB,EAA4B6B,GAA5B,CAAiCmC,IAAD,IAAe;AAErD;AACA,UAAMC,SAAS,GAAGtD,SAAS,CAACqD,IAAD,CAA3B;;AACA,QAAIC,SAAS,IAAI,EAAjB,EAAqB;AACpB,aAAO,EAAP;AACA;;AAED,QAAI7D,kBAAkB,CAACuD,cAAnB,CAAkCM,SAAlC,CAAJ,EAAkD;AACjD,aAAO7D,kBAAkB,CAAC6D,SAAD,CAAzB;AACA;;AAED,WAAOD,IAAP;AACA,GAbM,EAaJvD,IAbI,CAaC,EAbD,CAAP;AAeA;;;;;;;;;"}
|
package/dist/cjs/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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 * 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 => {\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\n\tif( typeof value == 'function' ) return value;\n\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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,CAAC,GAAGD,CAAR,EAAW,OAAO,CAAC,CAAR;AACX,SAAO,CAAP;AACA;;;;;;;;;;"}
|
package/dist/esm/diacritics.js
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
/*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */
|
|
2
|
-
import { escape_regex } from './utils.js';
|
|
3
|
-
|
|
4
|
-
// @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
|
|
5
|
-
// https://github.com/andrewrk/node-diacritics/blob/master/index.js
|
|
6
|
-
var latin_pat;
|
|
7
|
-
const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}]'; // \u{2bc}
|
|
8
|
-
|
|
9
|
-
const accent_reg = new RegExp(accent_pat, 'gu');
|
|
10
|
-
var diacritic_patterns;
|
|
11
|
-
const latin_convert = {
|
|
12
|
-
'æ': 'ae',
|
|
13
|
-
'ⱥ': 'a',
|
|
14
|
-
'ø': 'o'
|
|
15
|
-
};
|
|
16
|
-
const convert_pat = new RegExp(Object.keys(latin_convert).join('|'), 'gu');
|
|
17
|
-
const code_points = [[0, 65535]];
|
|
18
|
-
/**
|
|
19
|
-
* Remove accents
|
|
20
|
-
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
|
|
21
|
-
*
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
const asciifold = str => {
|
|
25
|
-
return str.normalize('NFKD').replace(accent_reg, '').toLowerCase().replace(convert_pat, function (foreignletter) {
|
|
26
|
-
return latin_convert[foreignletter] || foreignletter;
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Convert array of strings to a regular expression
|
|
31
|
-
* ex ['ab','a'] => (?:ab|a)
|
|
32
|
-
* ex ['a','b'] => [ab]
|
|
33
|
-
*
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
const arrayToPattern = (chars, glue = '|') => {
|
|
37
|
-
if (chars.length === 1 && chars[0] != undefined) {
|
|
38
|
-
return chars[0];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
var longest = 1;
|
|
42
|
-
chars.forEach(a => {
|
|
43
|
-
longest = Math.max(longest, a.length);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
if (longest == 1) {
|
|
47
|
-
return '[' + chars.join('') + ']';
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return '(?:' + chars.join(glue) + ')';
|
|
51
|
-
};
|
|
52
|
-
const escapeToPattern = chars => {
|
|
53
|
-
const escaped = chars.map(diacritic => escape_regex(diacritic));
|
|
54
|
-
return arrayToPattern(escaped);
|
|
55
|
-
};
|
|
56
|
-
/**
|
|
57
|
-
* Get all possible combinations of substrings that add up to the given string
|
|
58
|
-
* https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
|
|
59
|
-
*
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
-
const allSubstrings = input => {
|
|
63
|
-
if (input.length === 1) return [[input]];
|
|
64
|
-
var result = [];
|
|
65
|
-
allSubstrings(input.substring(1)).forEach(function (subresult) {
|
|
66
|
-
var tmp = subresult.slice(0);
|
|
67
|
-
tmp[0] = input.charAt(0) + tmp[0];
|
|
68
|
-
result.push(tmp);
|
|
69
|
-
tmp = subresult.slice(0);
|
|
70
|
-
tmp.unshift(input.charAt(0));
|
|
71
|
-
result.push(tmp);
|
|
72
|
-
});
|
|
73
|
-
return result;
|
|
74
|
-
};
|
|
75
|
-
/**
|
|
76
|
-
* Generate a list of diacritics from the list of code points
|
|
77
|
-
*
|
|
78
|
-
*/
|
|
79
|
-
|
|
80
|
-
const generateDiacritics = code_points => {
|
|
81
|
-
var diacritics = {};
|
|
82
|
-
code_points.forEach(code_range => {
|
|
83
|
-
for (let i = code_range[0]; i <= code_range[1]; i++) {
|
|
84
|
-
let diacritic = String.fromCharCode(i);
|
|
85
|
-
let latin = asciifold(diacritic);
|
|
86
|
-
|
|
87
|
-
if (latin == diacritic.toLowerCase()) {
|
|
88
|
-
continue;
|
|
89
|
-
} // skip when latin is a string longer than 3 characters long
|
|
90
|
-
// bc the resulting regex patterns will be long
|
|
91
|
-
// eg:
|
|
92
|
-
// latin صلى الله عليه وسلم length 18 code point 65018
|
|
93
|
-
// latin جل جلاله length 8 code point 65019
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (latin.length > 3) {
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const latin_diacritics = diacritics[latin] || [latin];
|
|
101
|
-
const patt = new RegExp(escapeToPattern(latin_diacritics), 'iu');
|
|
102
|
-
|
|
103
|
-
if (diacritic.match(patt)) {
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
latin_diacritics.push(diacritic);
|
|
108
|
-
diacritics[latin] = latin_diacritics;
|
|
109
|
-
}
|
|
110
|
-
}); // filter out if there's only one character in the list
|
|
111
|
-
// todo: this may not be needed
|
|
112
|
-
|
|
113
|
-
Object.keys(diacritics).forEach(latin => {
|
|
114
|
-
const latin_diacritics = diacritics[latin] || [];
|
|
115
|
-
|
|
116
|
-
if (latin_diacritics.length < 2) {
|
|
117
|
-
delete diacritics[latin];
|
|
118
|
-
}
|
|
119
|
-
}); // latin character pattern
|
|
120
|
-
// match longer substrings first
|
|
121
|
-
|
|
122
|
-
let latin_chars = Object.keys(diacritics).sort((a, b) => b.length - a.length);
|
|
123
|
-
latin_pat = new RegExp('(' + escapeToPattern(latin_chars) + accent_pat + '*)', 'gu'); // build diacritic patterns
|
|
124
|
-
// ae needs:
|
|
125
|
-
// (?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))
|
|
126
|
-
|
|
127
|
-
var diacritic_patterns = {};
|
|
128
|
-
latin_chars.sort((a, b) => a.length - b.length).forEach(latin => {
|
|
129
|
-
var substrings = allSubstrings(latin);
|
|
130
|
-
var pattern = substrings.map(sub_pat => {
|
|
131
|
-
sub_pat = sub_pat.map(l => {
|
|
132
|
-
if (diacritics.hasOwnProperty(l)) {
|
|
133
|
-
return escapeToPattern(diacritics[l]);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return l;
|
|
137
|
-
});
|
|
138
|
-
return arrayToPattern(sub_pat, '');
|
|
139
|
-
});
|
|
140
|
-
diacritic_patterns[latin] = arrayToPattern(pattern);
|
|
141
|
-
});
|
|
142
|
-
return diacritic_patterns;
|
|
143
|
-
};
|
|
144
|
-
/**
|
|
145
|
-
* Expand a regular expression pattern to include diacritics
|
|
146
|
-
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
|
|
147
|
-
*
|
|
148
|
-
*/
|
|
149
|
-
|
|
150
|
-
const diacriticRegexPoints = regex => {
|
|
151
|
-
if (diacritic_patterns === undefined) {
|
|
152
|
-
diacritic_patterns = generateDiacritics(code_points);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const decomposed = regex.normalize('NFKD').toLowerCase();
|
|
156
|
-
return decomposed.split(latin_pat).map(part => {
|
|
157
|
-
// "ffl" or "ffl"
|
|
158
|
-
const no_accent = asciifold(part);
|
|
159
|
-
|
|
160
|
-
if (no_accent == '') {
|
|
161
|
-
return '';
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (diacritic_patterns.hasOwnProperty(no_accent)) {
|
|
165
|
-
return diacritic_patterns[no_accent];
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return part;
|
|
169
|
-
}).join('');
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
export { allSubstrings, arrayToPattern, asciifold, diacriticRegexPoints, escapeToPattern, generateDiacritics };
|
|
173
|
-
//# sourceMappingURL=diacritics.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"diacritics.js","sources":["../../lib/diacritics.ts"],"sourcesContent":["\n// @ts-ignore TS2691 \"An import path cannot end with a '.ts' extension\"\nimport { escape_regex } from './utils.ts';\n\ntype TDiacraticList = {[key:string]:string};\n\n// https://github.com/andrewrk/node-diacritics/blob/master/index.js\n\nvar latin_pat:RegExp;\nconst accent_pat = '[\\u0300-\\u036F\\u{b7}\\u{2be}]'; // \\u{2bc}\nconst accent_reg = new RegExp(accent_pat,'gu');\nvar diacritic_patterns:TDiacraticList;\n\nconst latin_convert:TDiacraticList = {\n\t'æ': 'ae',\n\t'ⱥ': 'a',\n\t'ø': 'o',\n};\n\nconst convert_pat = new RegExp(Object.keys(latin_convert).join('|'),'gu');\n\nconst code_points:[[number,number]] = [[ 0, 65535 ]];\n\n/**\n * Remove accents\n * via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703\n *\n */\nexport const asciifold = (str:string):string => {\n\treturn str\n\t\t.normalize('NFKD')\n\t\t.replace(accent_reg, '')\n\t\t.toLowerCase()\n\t\t.replace(convert_pat,function(foreignletter) {\n\t\t\treturn latin_convert[foreignletter] || foreignletter;\n\t\t});\n};\n\n/**\n * Convert array of strings to a regular expression\n *\tex ['ab','a'] => (?:ab|a)\n * \tex ['a','b'] => [ab]\n *\n */\nexport const arrayToPattern = (chars:string[],glue:string='|'):string =>{\n\n\tif( chars.length === 1 && chars[0] != undefined ){\n\t\treturn chars[0];\n\t}\n\n\tvar longest = 1;\n\tchars.forEach((a)=>{longest = Math.max(longest,a.length)});\n\n\tif( longest == 1 ){\n\t\treturn '['+chars.join('')+']';\n\t}\n\n\treturn '(?:'+chars.join(glue)+')';\n};\n\nexport const escapeToPattern = (chars:string[]):string =>{\n\tconst escaped = chars.map((diacritic) => escape_regex(diacritic));\n\treturn arrayToPattern(escaped);\n};\n\n/**\n * Get all possible combinations of substrings that add up to the given string\n * https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string\n *\n */\nexport const allSubstrings = (input:string):string[][] => {\n\n if( input.length === 1) return [[input]];\n\n var result:string[][] = [];\n allSubstrings(input.substring(1)).forEach(function(subresult) {\n var tmp = subresult.slice(0);\n tmp[0] = input.charAt(0) + tmp[0];\n result.push(tmp);\n\n tmp = subresult.slice(0);\n tmp.unshift(input.charAt(0));\n result.push(tmp);\n });\n\n return result;\n}\n\n/**\n * Generate a list of diacritics from the list of code points\n *\n */\nexport const generateDiacritics = (code_points:[[number,number]]):TDiacraticList => {\n\n\tvar diacritics:{[key:string]:string[]} = {};\n\tcode_points.forEach((code_range)=>{\n\n\t\tfor(let i = code_range[0]; i <= code_range[1]; i++){\n\n\t\t\tlet diacritic\t= String.fromCharCode(i);\n\t\t\tlet\tlatin\t\t= asciifold(diacritic);\n\n\t\t\tif( latin == diacritic.toLowerCase() ){\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// skip when latin is a string longer than 3 characters long\n\t\t\t// bc the resulting regex patterns will be long\n\t\t\t// eg:\n\t\t\t// latin صلى الله عليه وسلم length 18 code point 65018\n\t\t\t// latin جل جلاله length 8 code point 65019\n\t\t\tif( latin.length > 3 ){\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst latin_diacritics:string[] = diacritics[latin] || [latin];\n\t\t\tconst patt = new RegExp( escapeToPattern(latin_diacritics),'iu');\n\t\t\tif( diacritic.match(patt) ){\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tlatin_diacritics.push(diacritic);\n\t\t\tdiacritics[latin] = latin_diacritics;\n\t\t}\n\t});\n\n\t// filter out if there's only one character in the list\n\t// todo: this may not be needed\n\tObject.keys(diacritics).forEach(latin => {\n\t\tconst latin_diacritics = diacritics[latin] || [];\n\t\tif( latin_diacritics.length < 2 ){\n\t\t\tdelete diacritics[latin];\n\t\t}\n\t});\n\n\n\t// latin character pattern\n\t// match longer substrings first\n\tlet latin_chars\t= Object.keys(diacritics).sort((a, b) => b.length - a.length );\n\tlatin_pat\t\t= new RegExp('('+ escapeToPattern(latin_chars) + accent_pat + '*)','gu');\n\n\n\t// build diacritic patterns\n\t// ae needs:\n\t//\t(?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))\n\tvar diacritic_patterns:TDiacraticList = {};\n\tlatin_chars.sort((a,b) => a.length -b.length).forEach((latin)=>{\n\n\t\tvar substrings\t= allSubstrings(latin);\n\t\tvar pattern = substrings.map((sub_pat)=>{\n\n\t\t\tsub_pat = sub_pat.map((l)=>{\n\t\t\t\tif( diacritics.hasOwnProperty(l) ){\n\t\t\t\t\treturn escapeToPattern(diacritics[l]!);\n\t\t\t\t}\n\t\t\t\treturn l;\n\t\t\t});\n\n\t\t\treturn arrayToPattern(sub_pat,'');\n\t\t});\n\n\t\tdiacritic_patterns[latin] = arrayToPattern(pattern);\n\t});\n\n\n\treturn diacritic_patterns;\n}\n\n/**\n * Expand a regular expression pattern to include diacritics\n * \teg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/\n *\n */\nexport const diacriticRegexPoints = (regex:string):string => {\n\n\tif( diacritic_patterns === undefined ){\n\t\tdiacritic_patterns = generateDiacritics(code_points);\n\t}\n\n\tconst decomposed\t\t= regex.normalize('NFKD').toLowerCase();\n\n\treturn decomposed.split(latin_pat).map((part:string)=>{\n\n\t\t// \"ffl\" or \"ffl\"\n\t\tconst no_accent = asciifold(part);\n\t\tif( no_accent == '' ){\n\t\t\treturn '';\n\t\t}\n\n\t\tif( diacritic_patterns.hasOwnProperty(no_accent) ){\n\t\t\treturn diacritic_patterns[no_accent];\n\t\t}\n\n\t\treturn part;\n\t}).join('');\n\n}\n"],"names":["latin_pat","accent_pat","accent_reg","RegExp","diacritic_patterns","latin_convert","convert_pat","Object","keys","join","code_points","asciifold","str","normalize","replace","toLowerCase","foreignletter","arrayToPattern","chars","glue","length","undefined","longest","forEach","a","Math","max","escapeToPattern","escaped","map","diacritic","escape_regex","allSubstrings","input","result","substring","subresult","tmp","slice","charAt","push","unshift","generateDiacritics","diacritics","code_range","i","String","fromCharCode","latin","latin_diacritics","patt","match","latin_chars","sort","b","substrings","pattern","sub_pat","l","hasOwnProperty","diacriticRegexPoints","regex","decomposed","split","part","no_accent"],"mappings":";;;AACA;AAKA;AAEA,IAAIA,SAAJ;AACA,MAAMC,UAAU,GAAG,8BAAnB;;AACA,MAAMC,UAAU,GAAG,IAAIC,MAAJ,CAAWF,UAAX,EAAsB,IAAtB,CAAnB;AACA,IAAIG,kBAAJ;AAEA,MAAMC,aAA4B,GAAG;AACpC,OAAK,IAD+B;AAEpC,OAAK,GAF+B;AAGpC,OAAK;AAH+B,CAArC;AAMA,MAAMC,WAAW,GAAG,IAAIH,MAAJ,CAAWI,MAAM,CAACC,IAAP,CAAYH,aAAZ,EAA2BI,IAA3B,CAAgC,GAAhC,CAAX,EAAgD,IAAhD,CAApB;AAEA,MAAMC,WAA6B,GAAG,CAAC,CAAE,CAAF,EAAK,KAAL,CAAD,CAAtC;AAEA;AACA;AACA;AACA;AACA;;MACaC,SAAS,GAAIC,GAAD,IAAuB;AAC/C,SAAOA,GAAG,CACRC,SADK,CACK,MADL,EAELC,OAFK,CAEGZ,UAFH,EAEe,EAFf,EAGLa,WAHK,GAILD,OAJK,CAIGR,WAJH,EAIe,UAASU,aAAT,EAAwB;AAC5C,WAAOX,aAAa,CAACW,aAAD,CAAb,IAAgCA,aAAvC;AACA,GANK,CAAP;AAOA;AAED;AACA;AACA;AACA;AACA;AACA;;MACaC,cAAc,GAAG,CAACC,KAAD,EAAgBC,IAAW,GAAC,GAA5B,KAA0C;AAEvE,MAAID,KAAK,CAACE,MAAN,KAAiB,CAAjB,IAAsBF,KAAK,CAAC,CAAD,CAAL,IAAYG,SAAtC,EAAiD;AAChD,WAAOH,KAAK,CAAC,CAAD,CAAZ;AACA;;AAED,MAAII,OAAO,GAAG,CAA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A,kBAAkB,GAAGsC,kBAAkB,CAAChC,WAAD,CAAvC;AACA;;AAED,QAAMoD,UAAU,GAAID,KAAK,CAAChD,SAAN,CAAgB,MAAhB,EAAwBE,WAAxB,EAApB;AAEA,SAAO+C,UAAU,CAACC,KAAX,CAAiB/D,SAAjB,EAA4B6B,GAA5B,CAAiCmC,IAAD,IAAe;AAErD;AACA,UAAMC,SAAS,GAAGtD,SAAS,CAACqD,IAAD,CAA3B;;AACA,QAAIC,SAAS,IAAI,EAAjB,EAAqB;AACpB,aAAO,EAAP;AACA;;AAED,QAAI7D,kBAAkB,CAACuD,cAAnB,CAAkCM,SAAlC,CAAJ,EAAkD;AACjD,aAAO7D,kBAAkB,CAAC6D,SAAD,CAAzB;AACA;;AAED,WAAOD,IAAP;AACA,GAbM,EAaJvD,IAbI,CAaC,EAbD,CAAP;AAeA;;;;"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
declare type TDiacraticList = {
|
|
2
|
-
[key: string]: string;
|
|
3
|
-
};
|
|
4
|
-
/**
|
|
5
|
-
* Remove accents
|
|
6
|
-
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
export declare const asciifold: (str: string) => string;
|
|
10
|
-
/**
|
|
11
|
-
* Convert array of strings to a regular expression
|
|
12
|
-
* ex ['ab','a'] => (?:ab|a)
|
|
13
|
-
* ex ['a','b'] => [ab]
|
|
14
|
-
*
|
|
15
|
-
*/
|
|
16
|
-
export declare const arrayToPattern: (chars: string[], glue?: string) => string;
|
|
17
|
-
export declare const escapeToPattern: (chars: string[]) => string;
|
|
18
|
-
/**
|
|
19
|
-
* Get all possible combinations of substrings that add up to the given string
|
|
20
|
-
* https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
|
|
21
|
-
*
|
|
22
|
-
*/
|
|
23
|
-
export declare const allSubstrings: (input: string) => string[][];
|
|
24
|
-
/**
|
|
25
|
-
* Generate a list of diacritics from the list of code points
|
|
26
|
-
*
|
|
27
|
-
*/
|
|
28
|
-
export declare const generateDiacritics: (code_points: [[number, number]]) => TDiacraticList;
|
|
29
|
-
/**
|
|
30
|
-
* Expand a regular expression pattern to include diacritics
|
|
31
|
-
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
|
|
32
|
-
*
|
|
33
|
-
*/
|
|
34
|
-
export declare const diacriticRegexPoints: (regex: string) => string;
|
|
35
|
-
export {};
|
package/lib/diacritics.ts
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
// @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
|
|
3
|
-
import { escape_regex } from './utils.ts';
|
|
4
|
-
|
|
5
|
-
type TDiacraticList = {[key:string]:string};
|
|
6
|
-
|
|
7
|
-
// https://github.com/andrewrk/node-diacritics/blob/master/index.js
|
|
8
|
-
|
|
9
|
-
var latin_pat:RegExp;
|
|
10
|
-
const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}]'; // \u{2bc}
|
|
11
|
-
const accent_reg = new RegExp(accent_pat,'gu');
|
|
12
|
-
var diacritic_patterns:TDiacraticList;
|
|
13
|
-
|
|
14
|
-
const latin_convert:TDiacraticList = {
|
|
15
|
-
'æ': 'ae',
|
|
16
|
-
'ⱥ': 'a',
|
|
17
|
-
'ø': 'o',
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const convert_pat = new RegExp(Object.keys(latin_convert).join('|'),'gu');
|
|
21
|
-
|
|
22
|
-
const code_points:[[number,number]] = [[ 0, 65535 ]];
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Remove accents
|
|
26
|
-
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
|
|
27
|
-
*
|
|
28
|
-
*/
|
|
29
|
-
export const asciifold = (str:string):string => {
|
|
30
|
-
return str
|
|
31
|
-
.normalize('NFKD')
|
|
32
|
-
.replace(accent_reg, '')
|
|
33
|
-
.toLowerCase()
|
|
34
|
-
.replace(convert_pat,function(foreignletter) {
|
|
35
|
-
return latin_convert[foreignletter] || foreignletter;
|
|
36
|
-
});
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Convert array of strings to a regular expression
|
|
41
|
-
* ex ['ab','a'] => (?:ab|a)
|
|
42
|
-
* ex ['a','b'] => [ab]
|
|
43
|
-
*
|
|
44
|
-
*/
|
|
45
|
-
export const arrayToPattern = (chars:string[],glue:string='|'):string =>{
|
|
46
|
-
|
|
47
|
-
if( chars.length === 1 && chars[0] != undefined ){
|
|
48
|
-
return chars[0];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
var longest = 1;
|
|
52
|
-
chars.forEach((a)=>{longest = Math.max(longest,a.length)});
|
|
53
|
-
|
|
54
|
-
if( longest == 1 ){
|
|
55
|
-
return '['+chars.join('')+']';
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return '(?:'+chars.join(glue)+')';
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export const escapeToPattern = (chars:string[]):string =>{
|
|
62
|
-
const escaped = chars.map((diacritic) => escape_regex(diacritic));
|
|
63
|
-
return arrayToPattern(escaped);
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Get all possible combinations of substrings that add up to the given string
|
|
68
|
-
* https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
|
|
69
|
-
*
|
|
70
|
-
*/
|
|
71
|
-
export const allSubstrings = (input:string):string[][] => {
|
|
72
|
-
|
|
73
|
-
if( input.length === 1) return [[input]];
|
|
74
|
-
|
|
75
|
-
var result:string[][] = [];
|
|
76
|
-
allSubstrings(input.substring(1)).forEach(function(subresult) {
|
|
77
|
-
var tmp = subresult.slice(0);
|
|
78
|
-
tmp[0] = input.charAt(0) + tmp[0];
|
|
79
|
-
result.push(tmp);
|
|
80
|
-
|
|
81
|
-
tmp = subresult.slice(0);
|
|
82
|
-
tmp.unshift(input.charAt(0));
|
|
83
|
-
result.push(tmp);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
return result;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Generate a list of diacritics from the list of code points
|
|
91
|
-
*
|
|
92
|
-
*/
|
|
93
|
-
export const generateDiacritics = (code_points:[[number,number]]):TDiacraticList => {
|
|
94
|
-
|
|
95
|
-
var diacritics:{[key:string]:string[]} = {};
|
|
96
|
-
code_points.forEach((code_range)=>{
|
|
97
|
-
|
|
98
|
-
for(let i = code_range[0]; i <= code_range[1]; i++){
|
|
99
|
-
|
|
100
|
-
let diacritic = String.fromCharCode(i);
|
|
101
|
-
let latin = asciifold(diacritic);
|
|
102
|
-
|
|
103
|
-
if( latin == diacritic.toLowerCase() ){
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// skip when latin is a string longer than 3 characters long
|
|
108
|
-
// bc the resulting regex patterns will be long
|
|
109
|
-
// eg:
|
|
110
|
-
// latin صلى الله عليه وسلم length 18 code point 65018
|
|
111
|
-
// latin جل جلاله length 8 code point 65019
|
|
112
|
-
if( latin.length > 3 ){
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const latin_diacritics:string[] = diacritics[latin] || [latin];
|
|
117
|
-
const patt = new RegExp( escapeToPattern(latin_diacritics),'iu');
|
|
118
|
-
if( diacritic.match(patt) ){
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
latin_diacritics.push(diacritic);
|
|
122
|
-
diacritics[latin] = latin_diacritics;
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// filter out if there's only one character in the list
|
|
127
|
-
// todo: this may not be needed
|
|
128
|
-
Object.keys(diacritics).forEach(latin => {
|
|
129
|
-
const latin_diacritics = diacritics[latin] || [];
|
|
130
|
-
if( latin_diacritics.length < 2 ){
|
|
131
|
-
delete diacritics[latin];
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
// latin character pattern
|
|
137
|
-
// match longer substrings first
|
|
138
|
-
let latin_chars = Object.keys(diacritics).sort((a, b) => b.length - a.length );
|
|
139
|
-
latin_pat = new RegExp('('+ escapeToPattern(latin_chars) + accent_pat + '*)','gu');
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
// build diacritic patterns
|
|
143
|
-
// ae needs:
|
|
144
|
-
// (?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))
|
|
145
|
-
var diacritic_patterns:TDiacraticList = {};
|
|
146
|
-
latin_chars.sort((a,b) => a.length -b.length).forEach((latin)=>{
|
|
147
|
-
|
|
148
|
-
var substrings = allSubstrings(latin);
|
|
149
|
-
var pattern = substrings.map((sub_pat)=>{
|
|
150
|
-
|
|
151
|
-
sub_pat = sub_pat.map((l)=>{
|
|
152
|
-
if( diacritics.hasOwnProperty(l) ){
|
|
153
|
-
return escapeToPattern(diacritics[l]!);
|
|
154
|
-
}
|
|
155
|
-
return l;
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
return arrayToPattern(sub_pat,'');
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
diacritic_patterns[latin] = arrayToPattern(pattern);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return diacritic_patterns;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Expand a regular expression pattern to include diacritics
|
|
170
|
-
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
|
|
171
|
-
*
|
|
172
|
-
*/
|
|
173
|
-
export const diacriticRegexPoints = (regex:string):string => {
|
|
174
|
-
|
|
175
|
-
if( diacritic_patterns === undefined ){
|
|
176
|
-
diacritic_patterns = generateDiacritics(code_points);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const decomposed = regex.normalize('NFKD').toLowerCase();
|
|
180
|
-
|
|
181
|
-
return decomposed.split(latin_pat).map((part:string)=>{
|
|
182
|
-
|
|
183
|
-
// "ffl" or "ffl"
|
|
184
|
-
const no_accent = asciifold(part);
|
|
185
|
-
if( no_accent == '' ){
|
|
186
|
-
return '';
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if( diacritic_patterns.hasOwnProperty(no_accent) ){
|
|
190
|
-
return diacritic_patterns[no_accent];
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return part;
|
|
194
|
-
}).join('');
|
|
195
|
-
|
|
196
|
-
}
|