@orchidjs/sifter 0.7.5 → 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.
- package/README.md +25 -3
- package/dist/cjs/diacritics.js +114 -64
- package/dist/cjs/diacritics.js.map +1 -1
- package/dist/cjs/sifter.js +8 -4
- package/dist/cjs/sifter.js.map +1 -1
- package/dist/cjs/utils.js +7 -1
- package/dist/cjs/utils.js.map +1 -1
- package/dist/esm/diacritics.js +112 -65
- package/dist/esm/diacritics.js.map +1 -1
- package/dist/esm/sifter.js +9 -5
- package/dist/esm/sifter.js.map +1 -1
- package/dist/esm/utils.js +7 -1
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/diacritics.d.ts +21 -32
- package/dist/types/sifter.d.ts +3 -3
- package/dist/types/types.d.ts +10 -8
- package/dist/types/utils.d.ts +5 -1
- package/dist/umd/sifter.js +125 -68
- package/dist/umd/sifter.js.map +1 -1
- package/dist/umd/sifter.min.js +48 -30
- package/dist/umd/sifter.min.js.map +1 -1
- package/lib/diacritics.ts +142 -78
- package/lib/sifter.ts +9 -6
- package/lib/types.ts +12 -8
- package/lib/utils.ts +9 -2
- package/package.json +7 -3
- package/.config/babel.config.json +0 -17
- package/.config/rollup.config.js +0 -95
- package/.config/tsconfig.json +0 -16
- package/.travis.yml +0 -9
- package/Makefile +0 -27
- package/build/cjs/diacritics.js +0 -123
- package/build/cjs/diacritics.js.map +0 -1
- package/build/cjs/sifter.js +0 -373
- package/build/cjs/sifter.js.map +0 -1
- package/build/cjs/utils.js +0 -108
- package/build/cjs/utils.js.map +0 -1
- package/build/esm/diacritics.js +0 -117
- package/build/esm/diacritics.js.map +0 -1
- package/build/esm/sifter.js +0 -371
- package/build/esm/sifter.js.map +0 -1
- package/build/esm/utils.js +0 -98
- package/build/esm/utils.js.map +0 -1
- package/build/umd/sifter.js +0 -583
- package/build/umd/sifter.js.map +0 -1
- package/build/umd/sifter.min.js +0 -66
- package/build/umd/sifter.min.js.map +0 -1
- package/coverage/clover.xml +0 -217
- package/coverage/coverage-final.json +0 -4
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -79
- package/coverage/lcov-report/diacritics.ts.html +0 -941
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -141
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sifter.ts.html +0 -1160
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -170
- package/coverage/lcov-report/utils.ts.html +0 -398
- package/coverage/lcov.info +0 -429
- package/release.sh +0 -119
- package/test/getScoreFunction.js +0 -55
- package/test/prepareSearch.js +0 -40
- package/test/search.js +0 -307
- package/test/sorting.js +0 -193
- package/test/tokenize.js +0 -63
- package/test/utils.js +0 -28
package/.config/rollup.config.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import babel from '@rollup/plugin-babel';
|
|
2
|
-
import { terser } from 'rollup-plugin-terser';
|
|
3
|
-
import pkg from '../package.json';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import fs from 'fs';
|
|
6
|
-
|
|
7
|
-
var configs = [];
|
|
8
|
-
const banner = `/*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */`;
|
|
9
|
-
|
|
10
|
-
const extensions = [
|
|
11
|
-
'.js', '.jsx', '.ts', '.tsx',
|
|
12
|
-
];
|
|
13
|
-
|
|
14
|
-
var babel_config = babel({
|
|
15
|
-
extensions: extensions,
|
|
16
|
-
babelHelpers: 'bundled',
|
|
17
|
-
configFile: path.resolve(__dirname,'babel.config.json'),
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
var terser_config = terser({
|
|
21
|
-
mangle: true,
|
|
22
|
-
format: {
|
|
23
|
-
semicolons: false,
|
|
24
|
-
comments: function (node, comment) {
|
|
25
|
-
var text = comment.value;
|
|
26
|
-
var type = comment.type;
|
|
27
|
-
if (type == "comment2") {
|
|
28
|
-
// multiline comment
|
|
29
|
-
return /\* sifter.js/i.test(text);
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// esm
|
|
37
|
-
configs.push({
|
|
38
|
-
input: path.resolve(__dirname,'../lib/sifter.ts'),
|
|
39
|
-
output:{
|
|
40
|
-
dir: path.resolve(__dirname,'../build/esm'),
|
|
41
|
-
format: 'esm',
|
|
42
|
-
preserveModules: true,
|
|
43
|
-
sourcemap: true,
|
|
44
|
-
banner: banner,
|
|
45
|
-
},
|
|
46
|
-
plugins:[babel_config] // resolve_config
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// cjs
|
|
50
|
-
configs.push({
|
|
51
|
-
input: path.resolve(__dirname,'../lib/sifter.ts'),
|
|
52
|
-
output:{
|
|
53
|
-
dir: path.resolve(__dirname,'../build/cjs'),
|
|
54
|
-
format: 'cjs',
|
|
55
|
-
preserveModules: true,
|
|
56
|
-
sourcemap: true,
|
|
57
|
-
banner: banner,
|
|
58
|
-
},
|
|
59
|
-
plugins:[babel_config] //resolve_config
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
// umd
|
|
64
|
-
configs.push({
|
|
65
|
-
input: path.resolve(__dirname,'../lib/sifter.ts'),
|
|
66
|
-
output: {
|
|
67
|
-
name: 'sifter',
|
|
68
|
-
file: `build/umd/sifter.js`,
|
|
69
|
-
format: 'umd',
|
|
70
|
-
sourcemap: true,
|
|
71
|
-
banner: banner
|
|
72
|
-
},
|
|
73
|
-
plugins:[
|
|
74
|
-
babel_config,
|
|
75
|
-
]
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// umd min
|
|
79
|
-
configs.push({
|
|
80
|
-
input: path.resolve(__dirname,'../lib/sifter.ts'),
|
|
81
|
-
output: {
|
|
82
|
-
name: 'sifter',
|
|
83
|
-
file: `build/umd/sifter.min.js`,
|
|
84
|
-
format: 'umd',
|
|
85
|
-
sourcemap: true,
|
|
86
|
-
banner: banner
|
|
87
|
-
},
|
|
88
|
-
plugins:[
|
|
89
|
-
babel_config,
|
|
90
|
-
terser_config
|
|
91
|
-
]
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
export default configs;
|
package/.config/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"include": ["../lib/**/*"],
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"allowJs": true,
|
|
5
|
-
"checkJs": true,
|
|
6
|
-
"strict": true,
|
|
7
|
-
"target": "esnext",
|
|
8
|
-
"module": "esnext",
|
|
9
|
-
"noUnusedLocals": true,
|
|
10
|
-
|
|
11
|
-
"declaration": true,
|
|
12
|
-
"declarationDir": "../dist/types",
|
|
13
|
-
"isolatedModules": true,
|
|
14
|
-
"moduleResolution": "node"
|
|
15
|
-
},
|
|
16
|
-
}
|
package/.travis.yml
DELETED
package/Makefile
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
.PHONY: compile release benchmark
|
|
2
|
-
|
|
3
|
-
all: compile
|
|
4
|
-
|
|
5
|
-
benchmark:
|
|
6
|
-
node --expose-gc benchmark/index.js
|
|
7
|
-
|
|
8
|
-
compile:
|
|
9
|
-
npm run build
|
|
10
|
-
|
|
11
|
-
release:
|
|
12
|
-
ifeq ($(strip $(version)),)
|
|
13
|
-
@echo "\033[31mERROR:\033[0;39m No version provided."
|
|
14
|
-
@echo "\033[1;30mmake release version=1.0.0\033[0;39m"
|
|
15
|
-
else
|
|
16
|
-
sed -i.bak 's/"version": "[^"]*"/"version": "$(version)"/' package.json
|
|
17
|
-
sed -i.bak 's/"version": "[^"]*"/"version": "$(version)"/' bower.json
|
|
18
|
-
rm *.bak
|
|
19
|
-
make compile
|
|
20
|
-
git add .
|
|
21
|
-
git commit -a -m "Released $(version)."
|
|
22
|
-
git tag v$(version)
|
|
23
|
-
git push origin master
|
|
24
|
-
git push origin --tags
|
|
25
|
-
npm publish
|
|
26
|
-
@echo "\033[32mv${version} released\033[0;39m"
|
|
27
|
-
endif
|
package/build/cjs/diacritics.js
DELETED
|
@@ -1,123 +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
|
-
// https://github.com/andrewrk/node-diacritics/blob/master/index.js
|
|
7
|
-
/**
|
|
8
|
-
* code points generated from toCodePoints();
|
|
9
|
-
* removed 65339 to 65345
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
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]];
|
|
13
|
-
/**
|
|
14
|
-
* Remove accents
|
|
15
|
-
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
|
|
16
|
-
*
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
const asciifold = str => {
|
|
20
|
-
return str.normalize('NFD').replace(/[\u0300-\u036F]/g, '').normalize('NFKD').toLowerCase();
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* Generate a list of diacritics from the list of code points
|
|
24
|
-
*
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const generateDiacritics = () => {
|
|
29
|
-
var latin_convert = {
|
|
30
|
-
'l·': 'l',
|
|
31
|
-
'ʼn': 'n',
|
|
32
|
-
'æ': 'ae',
|
|
33
|
-
'ø': 'o',
|
|
34
|
-
'aʾ': 'a',
|
|
35
|
-
'dž': 'dz'
|
|
36
|
-
};
|
|
37
|
-
var diacritics = {}; //var no_latin = [];
|
|
38
|
-
|
|
39
|
-
code_points.forEach(code_range => {
|
|
40
|
-
for (let i = code_range[0]; i <= code_range[1]; i++) {
|
|
41
|
-
let diacritic = String.fromCharCode(i);
|
|
42
|
-
let latin = diacritic.normalize('NFD').replace(/[\u0300-\u036F]/g, '').normalize('NFKD');
|
|
43
|
-
|
|
44
|
-
if (latin == diacritic) {
|
|
45
|
-
//no_latin.push(diacritic);
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
latin = latin.toLowerCase();
|
|
50
|
-
|
|
51
|
-
if (latin in latin_convert) {
|
|
52
|
-
latin = latin_convert[latin];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (!(latin in diacritics)) {
|
|
56
|
-
diacritics[latin] = latin + latin.toUpperCase();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
diacritics[latin] += diacritic;
|
|
60
|
-
}
|
|
61
|
-
}); //console.log('no_latin',JSON.stringify(no_latin));
|
|
62
|
-
|
|
63
|
-
return diacritics;
|
|
64
|
-
};
|
|
65
|
-
/**
|
|
66
|
-
* Expand a regular expression pattern to include diacritics
|
|
67
|
-
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
|
|
68
|
-
*
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
var diacritics = null;
|
|
72
|
-
const diacriticRegexPoints = regex => {
|
|
73
|
-
if (diacritics === null) {
|
|
74
|
-
diacritics = generateDiacritics();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
for (let latin in diacritics) {
|
|
78
|
-
if (diacritics.hasOwnProperty(latin)) {
|
|
79
|
-
regex = regex.replace(new RegExp(latin, 'g'), '[' + diacritics[latin] + ']');
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return regex;
|
|
84
|
-
};
|
|
85
|
-
/**
|
|
86
|
-
* Expand a regular expression pattern to include diacritics
|
|
87
|
-
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
|
|
88
|
-
*
|
|
89
|
-
* rollup will bundle this function (and the DIACRITICS constant) unless commented out
|
|
90
|
-
*
|
|
91
|
-
var diacriticRegex = (function() {
|
|
92
|
-
|
|
93
|
-
var list = [];
|
|
94
|
-
for( let letter in DIACRITICS ){
|
|
95
|
-
|
|
96
|
-
if( letter.toLowerCase() != letter && letter.toLowerCase() in DIACRITICS ){
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if( DIACRITICS.hasOwnProperty(letter) ){
|
|
101
|
-
|
|
102
|
-
var replace = letter + DIACRITICS[letter];
|
|
103
|
-
if( letter.toUpperCase() in DIACRITICS ){
|
|
104
|
-
replace += letter.toUpperCase() + DIACRITICS[letter.toUpperCase()];
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
list.push({let:letter,pat:'['+replace+']'});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return function(regex:string):string{
|
|
112
|
-
list.forEach((item)=>{
|
|
113
|
-
regex = regex.replace( new RegExp(item.let,'g'),item.pat);
|
|
114
|
-
});
|
|
115
|
-
return regex;
|
|
116
|
-
}
|
|
117
|
-
})();
|
|
118
|
-
*/
|
|
119
|
-
|
|
120
|
-
exports.asciifold = asciifold;
|
|
121
|
-
exports.diacriticRegexPoints = diacriticRegexPoints;
|
|
122
|
-
exports.generateDiacritics = generateDiacritics;
|
|
123
|
-
//# sourceMappingURL=diacritics.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"diacritics.js","sources":["../../lib/diacritics.ts"],"sourcesContent":["\ntype TDiacraticList = {[key:string]:string};\n\n// https://github.com/andrewrk/node-diacritics/blob/master/index.js\nexport const DIACRITICS:TDiacraticList = {\n\t\" \":\" \",\n\t0:\"߀\",\n\tA:\"ⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ\",\n\tAA:\"Ꜳ\",\n\tAE:\"ÆǼǢ\",\n\tAO:\"Ꜵ\",\n\tAU:\"Ꜷ\",\n\tAV:\"ꜸꜺ\",\n\tAY:\"Ꜽ\",\n\tB:\"ⒷBḂḄḆɃƁ\",\n\tC:\"ⒸCꜾḈĆCĈĊČÇƇȻ\",\n\tD:\"ⒹDḊĎḌḐḒḎĐƊƉᴅꝹ\",\n\tDh:\"Ð\",\n\tDZ:\"DZDŽ\",\n\tDz:\"DzDž\",\n\tE:\"ɛⒺEÈÉÊỀẾỄỂẼĒḔḖĔĖËẺĚȄȆẸỆȨḜĘḘḚƐƎᴇ\",\n\tF:\"ꝼⒻFḞƑꝻ\",\n\tG:\"ⒼGǴĜḠĞĠǦĢǤƓꞠꝽꝾɢ\",\n\tH:\"ⒽHĤḢḦȞḤḨḪĦⱧⱵꞍ\",\n\tI:\"ⒾIÌÍÎĨĪĬİÏḮỈǏȈȊỊĮḬƗ\",\n\tJ:\"ⒿJĴɈȷ\",\n\tK:\"ⓀKḰǨḲĶḴƘⱩꝀꝂꝄꞢ\",\n\tL:\"ⓁLĿĹĽḶḸĻḼḺŁȽⱢⱠꝈꝆꞀ\",\n\tLJ:\"LJ\",\n\tLj:\"Lj\",\n\tM:\"ⓂMḾṀṂⱮƜϻ\",\n\tN:\"ꞤȠⓃNǸŃÑṄŇṆŅṊṈƝꞐᴎ\",\n\tNJ:\"NJ\",\n\tNj:\"Nj\",\n\tO:\"ⓄOÒÓÔỒỐỖỔÕṌȬṎŌṐṒŎȮȰÖȪỎŐǑȌȎƠỜỚỠỞỢỌỘǪǬØǾƆƟꝊꝌ\",\n\tOE:\"Œ\",\n\tOI:\"Ƣ\",\n\tOO:\"Ꝏ\",\n\tOU:\"Ȣ\",\n\tP:\"ⓅPṔṖƤⱣꝐꝒꝔ\",\n\tQ:\"ⓆQꝖꝘɊ\",\n\tR:\"ⓇRŔṘŘȐȒṚṜŖṞɌⱤꝚꞦꞂ\",\n\tS:\"ⓈSẞŚṤŜṠŠṦṢṨȘŞⱾꞨꞄ\",\n\tT:\"ⓉTṪŤṬȚŢṰṮŦƬƮȾꞆ\",\n\tTh:\"Þ\",\n\tTZ:\"Ꜩ\",\n\tU:\"ⓊUÙÚÛŨṸŪṺŬÜǛǗǕǙỦŮŰǓȔȖƯỪỨỮỬỰỤṲŲṶṴɄ\",\n\tV:\"ⓋVṼṾƲꝞɅ\",\n\tVY:\"Ꝡ\",\n\tW:\"ⓌWẀẂŴẆẄẈⱲ\",\n\tX:\"ⓍXẊẌ\",\n\tY:\"ⓎYỲÝŶỸȲẎŸỶỴƳɎỾ\",\n\tZ:\"ⓏZŹẐŻŽẒẔƵȤⱿⱫꝢ\",\n\ta:\"ⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑ\",\n\taa:\"ꜳ\",\n\tae:\"æǽǣ\",\n\tao:\"ꜵ\",\n\tau:\"ꜷ\",\n\tav:\"ꜹꜻ\",\n\tay:\"ꜽ\",\n\tb:\"ⓑbḃḅḇƀƃɓƂ\",\n\tc:\"cⓒćĉċčçḉƈȼꜿↄ\",\n\td:\"ⓓdḋďḍḑḓḏđƌɖɗƋᏧԁꞪ\",\n\tdh:\"ð\",\n\tdz:\"dzdž\",\n\te:\"ⓔeèéêềếễểẽēḕḗĕėëẻěȅȇẹệȩḝęḙḛɇǝ\",\n\tf:\"ⓕfḟƒ\",\n\tff:\"ff\",\n\tfi:\"fi\",\n\tfl:\"fl\",\n\tffi:\"ffi\",\n\tffl:\"ffl\",\n\tg:\"ⓖgǵĝḡğġǧģǥɠꞡꝿᵹ\",\n\th:\"ⓗhĥḣḧȟḥḩḫẖħⱨⱶɥ\",\n\thv:\"ƕ\",\n\ti:\"ⓘiìíîĩīĭïḯỉǐȉȋịįḭɨı\",\n\tj:\"ⓙjĵǰɉ\",\n\tk:\"ⓚkḱǩḳķḵƙⱪꝁꝃꝅꞣ\",\n\tl:\"ⓛlŀĺľḷḹļḽḻſłƚɫⱡꝉꞁꝇɭ\",\n\tlj:\"lj\",\n\tm:\"ⓜmḿṁṃɱɯ\",\n\tn:\"ⓝnǹńñṅňṇņṋṉƞɲʼnꞑꞥлԉ\",\n\tnj:\"nj\",\n\to:\"ⓞoòóôồốỗổõṍȭṏōṑṓŏȯȱöȫỏőǒȍȏơờớỡởợọộǫǭøǿꝋꝍɵɔᴑ\",\n\toe:\"œ\",\n\toi:\"ƣ\",\n\too:\"ꝏ\",\n\tou:\"ȣ\",\n\tp:\"ⓟpṕṗƥᵽꝑꝓꝕρ\",\n\tq:\"ⓠqɋꝗꝙ\",\n\tr:\"ⓡrŕṙřȑȓṛṝŗṟɍɽꝛꞧꞃ\",\n\ts:\"ⓢsśṥŝṡšṧṣṩșşȿꞩꞅẛʂ\",\n\tss:\"ß\",\n\tt:\"ⓣtṫẗťṭțţṱṯŧƭʈⱦꞇ\",\n\tth:\"þ\",\n\ttz:\"ꜩ\",\n\tu:\"ⓤuùúûũṹūṻŭüǜǘǖǚủůűǔȕȗưừứữửựụṳųṷṵʉ\",\n\tv:\"ⓥvṽṿʋꝟʌ\",\n\tvy:\"ꝡ\",\n\tw:\"ⓦwẁẃŵẇẅẘẉⱳ\",\n\tx:\"ⓧxẋẍ\",\n\ty:\"ⓨyỳýŷỹȳẏÿỷẙỵƴɏỿ\",\n\tz:\"ⓩzźẑżžẓẕƶȥɀⱬꝣ\"\n}\n\n/**\n * code points generated from toCodePoints();\n * removed 65339 to 65345\n */\nvar code_points = [\n\t[ 67, 67 ],\n\t[ 160, 160 ],\n\t[ 192, 438 ],\n\t[ 452, 652 ],\n\t[ 961, 961 ],\n\t[ 1019, 1019 ],\n\t[ 1083, 1083 ],\n\t[ 1281, 1289 ],\n\t[ 1984, 1984 ],\n\t[ 5095, 5095 ],\n\t[ 7429, 7441 ],\n\t[ 7545, 7549 ],\n\t[ 7680, 7935 ],\n\t[ 8580, 8580 ],\n\t[ 9398, 9449 ],\n\t[ 11360, 11391 ],\n\t[ 42792, 42793 ],\n\t[ 42802, 42851 ],\n\t[ 42873, 42897 ],\n\t[ 42912, 42922 ],\n\t[ 64256, 64260 ],\n\t[ 65313, 65338 ],\n\t[ 65345, 65370 ]\n];\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.normalize('NFD').replace(/[\\u0300-\\u036F]/g, '').normalize('NFKD').toLowerCase();\n};\n\n\n/**\n * Convert list of diacritics to array of code points\n *\n */\n// @ts-ignore\nfunction toCodePoints(tolerance=8){\n\tvar char_codes:number[] = [];\n\n\tfor( let letter in DIACRITICS ){\n\t\tlet _diacritics = DIACRITICS[letter];\n\t\tfor( let n = 0; n < _diacritics.length; n++ ){\n\t\t\tvar code_point = _diacritics.codePointAt(n);\n\t\t\tif( code_point ) char_codes.push( code_point );\n\t\t}\n\t}\n\n\t//https://stackoverflow.com/questions/40431572/is-there-a-simple-way-to-group-js-array-values-by-range\n\tchar_codes.sort((a, b) => a - b);\n\tvar accumulator: number[][] = [];\n var result = char_codes.reduce(function (accumulator, currentValue, index, source) {\n\n\t\tif( !index ){\n\t\t\taccumulator.push( [currentValue,currentValue] );\n\n\t\t}else if( currentValue - source[index - 1] > tolerance ){\n\t\t\taccumulator.push( [currentValue,currentValue] );\n\n\t\t}else{\n\n\t\t\tlet range = accumulator.pop();\n\t\t\tif( range ){\n\t\t\t\taccumulator.push( [range[0],currentValue]);\n\t\t\t}\n\t\t}\n\n return accumulator;\n }, accumulator);\n\n\tconsole.log(`char_codes (${result.length})`,result);\n}\n\n/**\n * Generate a list of diacritics from the list of code points\n *\n */\nexport const generateDiacritics = ():TDiacraticList => {\n\n\tvar latin_convert:{[key:string]:string} = {\n\t\t'l·': 'l',\n\t\t'ʼn': 'n',\n\t\t'æ': 'ae',\n\t\t'ø': 'o',\n\t\t'aʾ': 'a',\n\t\t'dž': 'dz',\n\t};\n\n\tvar diacritics:{[key:string]:string} = {};\n\t//var no_latin\t= [];\n\tcode_points.forEach((code_range)=>{\n\n\t\tfor(let i = code_range[0]; i <= code_range[1]; i++){\n\t\t\tlet diacritic\t= String.fromCharCode(i);\n\t\t\tlet latin\t\t= diacritic.normalize('NFD').replace(/[\\u0300-\\u036F]/g, '').normalize('NFKD');\n\n\t\t\tif( latin == diacritic ){\n\t\t\t\t//no_latin.push(diacritic);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlatin = latin.toLowerCase();\n\n\t\t\tif( latin in latin_convert ){\n\t\t\t\tlatin = latin_convert[latin];\n\t\t\t}\n\n\t\t\tif( !(latin in diacritics) ){\n\t\t\t\tdiacritics[latin] = latin + latin.toUpperCase();\n\t\t\t}\n\t\t\tdiacritics[latin] += diacritic;\n\t\t}\n\t});\n\n\t//console.log('no_latin',JSON.stringify(no_latin));\n\n\treturn diacritics;\n}\n\n/**\n * Expand a regular expression pattern to include diacritics\n * \teg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/\n *\n */\nvar diacritics:null|TDiacraticList = null\nexport const diacriticRegexPoints = (regex:string):string => {\n\n\tif( diacritics === null ){\n\t\tdiacritics = generateDiacritics();\n\t}\n\n\tfor( let latin in diacritics ){\n\t\tif( diacritics.hasOwnProperty(latin) ){\n\t\t\tregex = regex.replace( new RegExp(latin,'g'), '['+diacritics[latin]+']');\n\t\t}\n\t}\n\treturn regex;\n}\n\n\n/**\n * Expand a regular expression pattern to include diacritics\n * \teg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/\n *\n * rollup will bundle this function (and the DIACRITICS constant) unless commented out\n *\nvar diacriticRegex = (function() {\n\n\tvar list = [];\n\tfor( let letter in DIACRITICS ){\n\n\t\tif( letter.toLowerCase() != letter && letter.toLowerCase() in DIACRITICS ){\n\t\t\tcontinue;\n\t\t}\n\n\t\tif( DIACRITICS.hasOwnProperty(letter) ){\n\n\t\t\tvar replace = letter + DIACRITICS[letter];\n\t\t\tif( letter.toUpperCase() in DIACRITICS ){\n\t\t\t\treplace += letter.toUpperCase() + DIACRITICS[letter.toUpperCase()];\n\t\t\t}\n\n\t\t\tlist.push({let:letter,pat:'['+replace+']'});\n\t\t}\n\t}\n\n\treturn function(regex:string):string{\n\t\tlist.forEach((item)=>{\n\t\t\tregex = regex.replace( new RegExp(item.let,'g'),item.pat);\n\t\t});\n\t\treturn regex;\n\t}\n})();\n*/\n"],"names":["code_points","asciifold","str","normalize","replace","toLowerCase","generateDiacritics","latin_convert","diacritics","forEach","code_range","i","diacritic","String","fromCharCode","latin","toUpperCase","diacriticRegexPoints","regex","hasOwnProperty","RegExp"],"mappings":"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"}
|
package/build/cjs/sifter.js
DELETED
|
@@ -1,373 +0,0 @@
|
|
|
1
|
-
/*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
var utils = require('./utils.js');
|
|
5
|
-
var diacritics = require('./diacritics.js');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* sifter.js
|
|
9
|
-
* Copyright (c) 2013–2020 Brian Reavis & contributors
|
|
10
|
-
*
|
|
11
|
-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
|
|
12
|
-
* file except in compliance with the License. You may obtain a copy of the License at:
|
|
13
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
-
*
|
|
15
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
16
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
|
17
|
-
* ANY KIND, either express or implied. See the License for the specific language
|
|
18
|
-
* governing permissions and limitations under the License.
|
|
19
|
-
*
|
|
20
|
-
* @author Brian Reavis <brian@thirdroute.com>
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
class Sifter {
|
|
24
|
-
// []|{};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Textually searches arrays and hashes of objects
|
|
28
|
-
* by property (or multiple properties). Designed
|
|
29
|
-
* specifically for autocomplete.
|
|
30
|
-
*
|
|
31
|
-
*/
|
|
32
|
-
constructor(items, settings) {
|
|
33
|
-
this.items = void 0;
|
|
34
|
-
this.settings = void 0;
|
|
35
|
-
this.items = items;
|
|
36
|
-
this.settings = settings || {
|
|
37
|
-
diacritics: true
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Splits a search string into an array of individual
|
|
43
|
-
* regexps to be used to match results.
|
|
44
|
-
*
|
|
45
|
-
*/
|
|
46
|
-
tokenize(query, respect_word_boundaries, weights) {
|
|
47
|
-
if (!query || !query.length) return [];
|
|
48
|
-
const tokens = [];
|
|
49
|
-
const words = query.split(/\s+/);
|
|
50
|
-
var field_regex;
|
|
51
|
-
|
|
52
|
-
if (weights) {
|
|
53
|
-
field_regex = new RegExp('^(' + Object.keys(weights).map(utils.escape_regex).join('|') + ')\:(.*)$');
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
words.forEach(word => {
|
|
57
|
-
let field_match;
|
|
58
|
-
let field = null;
|
|
59
|
-
let regex = null; // look for "field:query" tokens
|
|
60
|
-
|
|
61
|
-
if (field_regex && (field_match = word.match(field_regex))) {
|
|
62
|
-
field = field_match[1];
|
|
63
|
-
word = field_match[2];
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (word.length > 0) {
|
|
67
|
-
regex = utils.escape_regex(word);
|
|
68
|
-
|
|
69
|
-
if (this.settings.diacritics) {
|
|
70
|
-
regex = diacritics.diacriticRegexPoints(regex);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (respect_word_boundaries) regex = "\\b" + regex;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
tokens.push({
|
|
77
|
-
string: word,
|
|
78
|
-
regex: regex ? new RegExp(regex, 'i') : null,
|
|
79
|
-
field: field
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
return tokens;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Returns a function to be used to score individual results.
|
|
87
|
-
*
|
|
88
|
-
* Good matches will have a higher score than poor matches.
|
|
89
|
-
* If an item is not a match, 0 will be returned by the function.
|
|
90
|
-
*
|
|
91
|
-
* @returns {function}
|
|
92
|
-
*/
|
|
93
|
-
getScoreFunction(query, options) {
|
|
94
|
-
var search = this.prepareSearch(query, options);
|
|
95
|
-
return this._getScoreFunction(search);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
_getScoreFunction(search) {
|
|
99
|
-
const tokens = search.tokens,
|
|
100
|
-
token_count = tokens.length;
|
|
101
|
-
|
|
102
|
-
if (!token_count) {
|
|
103
|
-
return function () {
|
|
104
|
-
return 0;
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const fields = search.options.fields,
|
|
109
|
-
weights = search.weights,
|
|
110
|
-
field_count = fields.length,
|
|
111
|
-
getAttrFn = search.getAttrFn;
|
|
112
|
-
|
|
113
|
-
if (!field_count) {
|
|
114
|
-
return function () {
|
|
115
|
-
return 1;
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Calculates the score of an object
|
|
120
|
-
* against the search query.
|
|
121
|
-
*
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const scoreObject = function () {
|
|
126
|
-
if (field_count === 1) {
|
|
127
|
-
return function (token, data) {
|
|
128
|
-
const field = fields[0].field;
|
|
129
|
-
return utils.scoreValue(getAttrFn(data, field), token, weights[field]);
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return function (token, data) {
|
|
134
|
-
var sum = 0; // is the token specific to a field?
|
|
135
|
-
|
|
136
|
-
if (token.field) {
|
|
137
|
-
const value = getAttrFn(data, token.field);
|
|
138
|
-
|
|
139
|
-
if (!token.regex && value) {
|
|
140
|
-
sum += 1 / field_count;
|
|
141
|
-
} else {
|
|
142
|
-
sum += utils.scoreValue(value, token, 1);
|
|
143
|
-
}
|
|
144
|
-
} else {
|
|
145
|
-
utils.iterate(weights, (weight, field) => {
|
|
146
|
-
sum += utils.scoreValue(getAttrFn(data, field), token, weight);
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return sum / field_count;
|
|
151
|
-
};
|
|
152
|
-
}();
|
|
153
|
-
|
|
154
|
-
if (token_count === 1) {
|
|
155
|
-
return function (data) {
|
|
156
|
-
return scoreObject(tokens[0], data);
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (search.options.conjunction === 'and') {
|
|
161
|
-
return function (data) {
|
|
162
|
-
var i = 0,
|
|
163
|
-
score,
|
|
164
|
-
sum = 0;
|
|
165
|
-
|
|
166
|
-
for (; i < token_count; i++) {
|
|
167
|
-
score = scoreObject(tokens[i], data);
|
|
168
|
-
if (score <= 0) return 0;
|
|
169
|
-
sum += score;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return sum / token_count;
|
|
173
|
-
};
|
|
174
|
-
} else {
|
|
175
|
-
return function (data) {
|
|
176
|
-
var sum = 0;
|
|
177
|
-
utils.iterate(tokens, token => {
|
|
178
|
-
sum += scoreObject(token, data);
|
|
179
|
-
});
|
|
180
|
-
return sum / token_count;
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Returns a function that can be used to compare two
|
|
187
|
-
* results, for sorting purposes. If no sorting should
|
|
188
|
-
* be performed, `null` will be returned.
|
|
189
|
-
*
|
|
190
|
-
* @return function(a,b)
|
|
191
|
-
*/
|
|
192
|
-
getSortFunction(query, options) {
|
|
193
|
-
var search = this.prepareSearch(query, options);
|
|
194
|
-
return this._getSortFunction(search);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
_getSortFunction(search) {
|
|
198
|
-
var i, n, implicit_score;
|
|
199
|
-
const self = this,
|
|
200
|
-
options = search.options,
|
|
201
|
-
sort = !search.query && options.sort_empty ? options.sort_empty : options.sort,
|
|
202
|
-
sort_flds = [],
|
|
203
|
-
multipliers = [];
|
|
204
|
-
/**
|
|
205
|
-
* Fetches the specified sort field value
|
|
206
|
-
* from a search result item.
|
|
207
|
-
*
|
|
208
|
-
*/
|
|
209
|
-
|
|
210
|
-
const get_field = function get_field(name, result) {
|
|
211
|
-
if (name === '$score') return result.score;
|
|
212
|
-
return search.getAttrFn(self.items[result.id], name);
|
|
213
|
-
}; // parse options
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
if (sort) {
|
|
217
|
-
for (i = 0, n = sort.length; i < n; i++) {
|
|
218
|
-
if (search.query || sort[i].field !== '$score') {
|
|
219
|
-
sort_flds.push(sort[i]);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
} // the "$score" field is implied to be the primary
|
|
223
|
-
// sort field, unless it's manually specified
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (search.query) {
|
|
227
|
-
implicit_score = true;
|
|
228
|
-
|
|
229
|
-
for (i = 0, n = sort_flds.length; i < n; i++) {
|
|
230
|
-
if (sort_flds[i].field === '$score') {
|
|
231
|
-
implicit_score = false;
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
if (implicit_score) {
|
|
237
|
-
sort_flds.unshift({
|
|
238
|
-
field: '$score',
|
|
239
|
-
direction: 'desc'
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
} else {
|
|
243
|
-
for (i = 0, n = sort_flds.length; i < n; i++) {
|
|
244
|
-
if (sort_flds[i].field === '$score') {
|
|
245
|
-
sort_flds.splice(i, 1);
|
|
246
|
-
break;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
for (i = 0, n = sort_flds.length; i < n; i++) {
|
|
252
|
-
multipliers.push(sort_flds[i].direction === 'desc' ? -1 : 1);
|
|
253
|
-
} // build function
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const sort_flds_count = sort_flds.length;
|
|
257
|
-
|
|
258
|
-
if (!sort_flds_count) {
|
|
259
|
-
return null;
|
|
260
|
-
} else if (sort_flds_count === 1) {
|
|
261
|
-
const sort_fld = sort_flds[0].field;
|
|
262
|
-
const multiplier = multipliers[0];
|
|
263
|
-
return function (a, b) {
|
|
264
|
-
return multiplier * utils.cmp(get_field(sort_fld, a), get_field(sort_fld, b));
|
|
265
|
-
};
|
|
266
|
-
} else {
|
|
267
|
-
return function (a, b) {
|
|
268
|
-
var i, result, field;
|
|
269
|
-
|
|
270
|
-
for (i = 0; i < sort_flds_count; i++) {
|
|
271
|
-
field = sort_flds[i].field;
|
|
272
|
-
result = multipliers[i] * utils.cmp(get_field(field, a), get_field(field, b));
|
|
273
|
-
if (result) return result;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
return 0;
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Parses a search query and returns an object
|
|
283
|
-
* with tokens and fields ready to be populated
|
|
284
|
-
* with results.
|
|
285
|
-
*
|
|
286
|
-
*/
|
|
287
|
-
prepareSearch(query, optsUser) {
|
|
288
|
-
const weights = {};
|
|
289
|
-
var options = Object.assign({}, optsUser);
|
|
290
|
-
utils.propToArray(options, 'sort');
|
|
291
|
-
utils.propToArray(options, 'sort_empty'); // convert fields to new format
|
|
292
|
-
|
|
293
|
-
if (options.fields) {
|
|
294
|
-
utils.propToArray(options, 'fields');
|
|
295
|
-
const fields = [];
|
|
296
|
-
options.fields.forEach(field => {
|
|
297
|
-
if (typeof field == 'string') {
|
|
298
|
-
field = {
|
|
299
|
-
field: field,
|
|
300
|
-
weight: 1
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
fields.push(field);
|
|
305
|
-
weights[field.field] = 'weight' in field ? field.weight : 1;
|
|
306
|
-
});
|
|
307
|
-
options.fields = fields;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
query = diacritics.asciifold(query + '').toLowerCase().trim();
|
|
311
|
-
return {
|
|
312
|
-
options: options,
|
|
313
|
-
query: query,
|
|
314
|
-
tokens: this.tokenize(query, options.respect_word_boundaries, weights),
|
|
315
|
-
total: 0,
|
|
316
|
-
items: [],
|
|
317
|
-
weights: weights,
|
|
318
|
-
getAttrFn: options.nesting ? utils.getAttrNesting : utils.getAttr
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* Searches through all items and returns a sorted array of matches.
|
|
324
|
-
*
|
|
325
|
-
*/
|
|
326
|
-
search(query, options) {
|
|
327
|
-
var self = this,
|
|
328
|
-
score,
|
|
329
|
-
search;
|
|
330
|
-
search = this.prepareSearch(query, options);
|
|
331
|
-
options = search.options;
|
|
332
|
-
query = search.query; // generate result scoring function
|
|
333
|
-
|
|
334
|
-
const fn_score = options.score || self._getScoreFunction(search); // perform search and sort
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
if (query.length) {
|
|
338
|
-
utils.iterate(self.items, (item, id) => {
|
|
339
|
-
score = fn_score(item);
|
|
340
|
-
|
|
341
|
-
if (options.filter === false || score > 0) {
|
|
342
|
-
search.items.push({
|
|
343
|
-
'score': score,
|
|
344
|
-
'id': id
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
} else {
|
|
349
|
-
utils.iterate(self.items, (item, id) => {
|
|
350
|
-
search.items.push({
|
|
351
|
-
'score': 1,
|
|
352
|
-
'id': id
|
|
353
|
-
});
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const fn_sort = self._getSortFunction(search);
|
|
358
|
-
|
|
359
|
-
if (fn_sort) search.items.sort(fn_sort); // apply limits
|
|
360
|
-
|
|
361
|
-
search.total = search.items.length;
|
|
362
|
-
|
|
363
|
-
if (typeof options.limit === 'number') {
|
|
364
|
-
search.items = search.items.slice(0, options.limit);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
return search;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
module.exports = Sifter;
|
|
373
|
-
//# sourceMappingURL=sifter.js.map
|