-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (53 loc) · 2.49 KB
/
Copy pathindex.js
File metadata and controls
53 lines (53 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"use strict";
exports.__esModule = true;
exports.createSearch = void 0;
var hangul_js_1 = require("hangul-js");
var getChars = function (keyword) {
var chars = {};
(0, hangul_js_1.disassemble)(keyword).map(function (char) {
return (chars[char] = typeof chars[char] !== 'undefined' ? chars[char] + 1 : 1);
});
return chars;
};
var createSearch = function (items) {
var itemCounts = items.map(function (item) { return getChars(item); });
var search = function (keyword, options) {
var keywordCounts = getChars((0, hangul_js_1.disassemble)(keyword).join(''));
var founds = [];
itemCounts.map(function (itemCount, index) {
var matchCount = 0;
var exactFullCheck = (options === null || options === void 0 ? void 0 : options.exact) === true ? Object.keys(keywordCounts).length : 0;
var exactCheck = 0;
var exactOrderCheck = [];
Object.entries(itemCount).map(function (_a) {
var itemKey = _a[0], itemValue = _a[1];
Object.entries(keywordCounts).map(function (_a) {
var keywordKey = _a[0], keywordValue = _a[1];
if (itemKey === keywordKey && itemValue >= keywordValue) {
matchCount += keywordValue;
if ((options === null || options === void 0 ? void 0 : options.exact) === true)
exactCheck += 1;
if ((options === null || options === void 0 ? void 0 : options.order) === true)
exactOrderCheck.push(itemKey);
}
});
});
if ((options === null || options === void 0 ? void 0 : options.order) === true) {
if (!Object.keys(keywordCounts).every(function (element, index) { return element === exactOrderCheck[index]; }))
return;
}
if ((options === null || options === void 0 ? void 0 : options.exact) === true) {
if (matchCount !== 0 && exactFullCheck === exactCheck)
founds.push([items[index], matchCount]);
}
else {
if (matchCount !== 0)
founds.push([items[index], matchCount]);
}
});
return founds.sort(function (min, max) { return max[1] - min[1]; }).map(function (found) { return found[0]; });
};
return search;
};
exports.createSearch = createSearch;
exports["default"] = exports.createSearch;