@koine/utils 2.0.0-beta.2 → 2.0.0-beta.4
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/Defer.js +23 -18
- package/Defer.mjs +6 -5
- package/Emitter.js +31 -20
- package/Emitter.mjs +13 -16
- package/README.md +5 -1
- package/accentSets.js +129 -37
- package/accentSets.mjs +113 -29
- package/addOrReplaceAtIdx.js +27 -10
- package/addOrReplaceAtIdx.mjs +9 -6
- package/areEqual.js +35 -41
- package/areEqual.mjs +16 -22
- package/arrayOfAll.js +20 -11
- package/arrayOfAll.mjs +1 -6
- package/arraySum.js +20 -9
- package/arraySum.mjs +1 -4
- package/arrayToLookup.js +21 -9
- package/arrayToLookup.mjs +2 -4
- package/buildUrlQueryString.js +32 -29
- package/buildUrlQueryString.mjs +11 -14
- package/capitalize.js +21 -9
- package/capitalize.mjs +3 -5
- package/changeUrlPath.js +23 -17
- package/changeUrlPath.mjs +2 -3
- package/chunkByChunks.js +35 -24
- package/chunkByChunks.mjs +17 -20
- package/chunkBySize.js +21 -8
- package/chunkBySize.mjs +3 -4
- package/clamp.js +19 -6
- package/clamp.mjs +1 -2
- package/clsx.js +29 -23
- package/clsx.mjs +11 -14
- package/convertRange.js +20 -7
- package/convertRange.mjs +2 -3
- package/cookie.js +13 -5
- package/cookie.mjs +3 -2
- package/createPalette.js +27 -19
- package/createPalette.mjs +9 -6
- package/debounce.js +27 -21
- package/debounce.mjs +9 -17
- package/debouncePromise.js +20 -11
- package/debouncePromise.mjs +1 -2
- package/debounceRaf.js +23 -14
- package/debounceRaf.mjs +5 -10
- package/decode.js +20 -7
- package/decode.mjs +2 -3
- package/encode.js +20 -7
- package/encode.mjs +2 -3
- package/ensureInt.js +20 -9
- package/ensureInt.mjs +1 -4
- package/env.js +1 -1
- package/env.mjs +0 -1
- package/errorToString.js +21 -14
- package/errorToString.mjs +1 -4
- package/findDuplicatedIndexes.js +22 -9
- package/findDuplicatedIndexes.mjs +4 -5
- package/forin.js +20 -7
- package/forin.mjs +2 -3
- package/gbToBytes.js +20 -7
- package/gbToBytes.mjs +1 -2
- package/getEmptyArray.js +25 -16
- package/getEmptyArray.mjs +4 -3
- package/getKeys.js +20 -6
- package/getKeys.mjs +1 -2
- package/getMediaQueryWidthResolvers.js +62 -53
- package/getMediaQueryWidthResolvers.mjs +44 -49
- package/getMediaQueryWidthTailwindScreens.js +36 -18
- package/getMediaQueryWidthTailwindScreens.mjs +18 -14
- package/getNonce.js +20 -10
- package/getNonce.mjs +1 -2
- package/getParamAmong.js +22 -19
- package/getParamAmong.mjs +2 -4
- package/getParamAsInt.js +22 -19
- package/getParamAsInt.mjs +2 -4
- package/getParamAsString.js +21 -16
- package/getParamAsString.mjs +1 -2
- package/getType.js +19 -12
- package/getType.mjs +2 -4
- package/getUrlHashParams.js +20 -13
- package/getUrlHashParams.mjs +2 -4
- package/getUrlHashPathname.js +20 -8
- package/getUrlHashPathname.mjs +1 -3
- package/getUrlPathnameParts.js +23 -23
- package/getUrlPathnameParts.mjs +2 -7
- package/getUrlQueryParams.js +26 -33
- package/getUrlQueryParams.mjs +7 -18
- package/imgEmptyPixel.js +20 -6
- package/imgEmptyPixel.mjs +1 -2
- package/index.js +531 -266
- package/index.mjs +2 -2
- package/invariant.js +24 -12
- package/invariant.mjs +6 -8
- package/isAnyObject.js +21 -12
- package/isAnyObject.mjs +1 -2
- package/isArray.js +21 -12
- package/isArray.mjs +1 -2
- package/isBlob.js +21 -12
- package/isBlob.mjs +1 -2
- package/isBoolean.js +21 -12
- package/isBoolean.mjs +1 -2
- package/isBrowser.js +20 -6
- package/isBrowser.mjs +1 -2
- package/isBrowserNow.js +21 -11
- package/isBrowserNow.mjs +1 -2
- package/isDate.js +21 -12
- package/isDate.mjs +1 -2
- package/isEmptyArray.js +21 -12
- package/isEmptyArray.mjs +1 -2
- package/isEmptyObject.js +21 -12
- package/isEmptyObject.mjs +1 -2
- package/isEmptyString.js +19 -6
- package/isEmptyString.mjs +1 -2
- package/isError.js +21 -12
- package/isError.mjs +1 -2
- package/isExternalUrl.js +24 -18
- package/isExternalUrl.mjs +4 -6
- package/isFile.js +21 -12
- package/isFile.mjs +1 -2
- package/isFloat.js +21 -12
- package/isFloat.mjs +1 -2
- package/isFormData.js +21 -12
- package/isFormData.mjs +1 -2
- package/isFullArray.js +21 -12
- package/isFullArray.mjs +1 -2
- package/isFullObject.js +21 -12
- package/isFullObject.mjs +1 -2
- package/isFullString.js +21 -12
- package/isFullString.mjs +1 -2
- package/isFunction.js +18 -9
- package/isFunction.mjs +1 -2
- package/isInt.js +21 -12
- package/isInt.mjs +1 -2
- package/isMap.js +21 -12
- package/isMap.mjs +1 -2
- package/isNaNValue.js +21 -12
- package/isNaNValue.mjs +1 -2
- package/isNegativeNumber.js +21 -12
- package/isNegativeNumber.mjs +1 -2
- package/isNull.js +21 -12
- package/isNull.mjs +1 -2
- package/isNullOrUndefined.js +23 -13
- package/isNullOrUndefined.mjs +1 -2
- package/isNumber.js +21 -14
- package/isNumber.mjs +1 -2
- package/isObject.js +21 -12
- package/isObject.mjs +1 -2
- package/isObjectLike.js +21 -14
- package/isObjectLike.mjs +1 -2
- package/isOneOf.js +19 -11
- package/isOneOf.mjs +1 -7
- package/isPlainObject.js +22 -15
- package/isPlainObject.mjs +3 -6
- package/isPositiveNumber.js +21 -12
- package/isPositiveNumber.mjs +1 -2
- package/isPrimitive.js +26 -22
- package/isPrimitive.mjs +5 -11
- package/isPromise.js +21 -12
- package/isPromise.mjs +1 -2
- package/isRegExp.js +21 -12
- package/isRegExp.mjs +1 -2
- package/isServer.js +22 -11
- package/isServer.mjs +1 -2
- package/isServerNow.js +21 -11
- package/isServerNow.mjs +1 -2
- package/isSet.js +21 -12
- package/isSet.mjs +1 -2
- package/isString.js +21 -12
- package/isString.mjs +1 -2
- package/isSymbol.js +21 -12
- package/isSymbol.mjs +1 -2
- package/isType.js +22 -17
- package/isType.mjs +3 -5
- package/isUndefined.js +21 -12
- package/isUndefined.mjs +1 -2
- package/isWeakMap.js +21 -12
- package/isWeakMap.mjs +1 -2
- package/isWeakSet.js +21 -12
- package/isWeakSet.mjs +1 -2
- package/kbToBytes.js +20 -7
- package/kbToBytes.mjs +1 -2
- package/location.js +6 -2
- package/location.mjs +3 -1
- package/lowercase.js +20 -9
- package/lowercase.mjs +1 -4
- package/mapListBy.js +21 -10
- package/mapListBy.mjs +2 -5
- package/matchSorter.js +132 -133
- package/matchSorter.mjs +108 -126
- package/mbToBytes.js +20 -7
- package/mbToBytes.mjs +1 -2
- package/mergeObjects.d.ts +1 -1
- package/mergeObjects.js +24 -26
- package/mergeObjects.mjs +12 -15
- package/mergeUrlQueryParams.js +25 -20
- package/mergeUrlQueryParams.mjs +4 -8
- package/moveSortableArrayItemByKey.js +26 -12
- package/moveSortableArrayItemByKey.mjs +8 -8
- package/noop.js +20 -7
- package/noop.mjs +1 -2
- package/normaliseUrl.js +22 -18
- package/normaliseUrl.mjs +1 -3
- package/normaliseUrlPathname.js +22 -17
- package/normaliseUrlPathname.mjs +1 -3
- package/objectOmit.js +21 -9
- package/objectOmit.mjs +3 -5
- package/objectPick.js +22 -9
- package/objectPick.mjs +4 -5
- package/package.json +11 -8
- package/parseCookie.js +29 -26
- package/parseCookie.mjs +11 -15
- package/parseURL.js +23 -10
- package/parseURL.mjs +5 -6
- package/quaranteneProps.js +24 -12
- package/quaranteneProps.mjs +6 -8
- package/randomInt.js +19 -6
- package/randomInt.mjs +1 -2
- package/randomKey.js +21 -8
- package/randomKey.mjs +3 -4
- package/readCookie.js +25 -12
- package/readCookie.mjs +7 -8
- package/removeAccents.js +24 -15
- package/removeAccents.mjs +5 -8
- package/removeCookie.js +26 -17
- package/removeCookie.mjs +6 -5
- package/removeDuplicates.js +1 -1
- package/removeDuplicates.mjs +0 -1
- package/removeDuplicatesByKey.js +24 -12
- package/removeDuplicatesByKey.mjs +5 -7
- package/removeDuplicatesComparing.js +22 -11
- package/removeDuplicatesComparing.mjs +3 -4
- package/removeIndexesFromArray.js +21 -8
- package/removeIndexesFromArray.mjs +3 -4
- package/removeTrailingSlash.js +20 -8
- package/removeTrailingSlash.mjs +1 -3
- package/removeUrlQueryParams.js +27 -20
- package/removeUrlQueryParams.mjs +5 -7
- package/render.js +61 -137
- package/render.mjs +43 -98
- package/roundTo.js +34 -22
- package/roundTo.mjs +15 -17
- package/serializeCookie.js +29 -35
- package/serializeCookie.mjs +9 -15
- package/setCookie.js +37 -31
- package/setCookie.mjs +13 -16
- package/shuffle.js +28 -14
- package/shuffle.mjs +10 -10
- package/slugify.js +33 -25
- package/slugify.mjs +13 -11
- package/split.js +18 -4
- package/swapMap.js +22 -10
- package/swapMap.mjs +3 -5
- package/throttle.d.ts +1 -0
- package/throttle.js +23 -15
- package/throttle.mjs +5 -11
- package/titleCase.js +20 -11
- package/titleCase.mjs +1 -6
- package/toNumber.js +20 -9
- package/toNumber.mjs +1 -2
- package/toRgba.js +22 -10
- package/toRgba.mjs +3 -5
- package/transformToUrlPathname.js +21 -18
- package/transformToUrlPathname.mjs +2 -5
- package/truncate.js +20 -13
- package/truncate.mjs +1 -8
- package/tryUntil.js +26 -15
- package/tryUntil.mjs +7 -10
- package/types.js +4 -1
- package/types.mjs +2 -1
- package/uid.js +21 -13
- package/uid.mjs +3 -5
- package/updateLinkParams.js +21 -14
- package/updateLinkParams.mjs +2 -3
- package/updateUrlQueryParams.js +26 -21
- package/updateUrlQueryParams.mjs +3 -7
- package/uppercase.js +20 -9
- package/uppercase.mjs +1 -4
- package/uuid.js +21 -10
- package/uuid.mjs +2 -5
- package/uuidNumeric.js +20 -7
- package/uuidNumeric.mjs +1 -2
- package/wait.js +20 -9
- package/wait.mjs +1 -4
- package/without.js +2 -3
- package/without.mjs +1 -3
package/matchSorter.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.defaultBaseSortFn = exports.matchSorter = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
1
|
/**
|
|
6
2
|
* @file
|
|
7
3
|
*
|
|
@@ -17,47 +13,61 @@ var tslib_1 = require("tslib");
|
|
|
17
13
|
* @license MIT license.
|
|
18
14
|
* @copyright (c) 2020 Kent C. Dodds
|
|
19
15
|
* @author Kent C. Dodds <me@kentcdodds.com> (https://kentcdodds.com)
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
var
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
16
|
+
*/ "use strict";
|
|
17
|
+
Object.defineProperty(exports, "__esModule", {
|
|
18
|
+
value: true
|
|
19
|
+
});
|
|
20
|
+
function _export(target, all) {
|
|
21
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: all[name]
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
_export(exports, {
|
|
27
|
+
matchSorter: function() {
|
|
28
|
+
return matchSorter;
|
|
29
|
+
},
|
|
30
|
+
defaultBaseSortFn: function() {
|
|
31
|
+
return defaultBaseSortFn;
|
|
32
|
+
},
|
|
33
|
+
default: function() {
|
|
34
|
+
return _default;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
|
|
38
|
+
const _removeAccents = /*#__PURE__*/ _interop_require_default._(require("./removeAccents"));
|
|
39
|
+
const RANKING_CASE_SENSITIVE_EQUAL = 7;
|
|
40
|
+
const RANKING_EQUAL = 6;
|
|
41
|
+
const RANKING_STARTS_WITH = 5;
|
|
42
|
+
const RANKING_WORD_STARTS_WITH = 4;
|
|
43
|
+
const RANKING_CONTAINS = 3;
|
|
44
|
+
const RANKING_ACRONYM = 2;
|
|
45
|
+
const RANKING_MATCHES = 1;
|
|
46
|
+
const RANKING_NO_MATCH = 0;
|
|
47
|
+
const defaultBaseSortFn = (a, b)=>String(a.rankedValue).localeCompare(String(b.rankedValue));
|
|
34
48
|
/**
|
|
35
49
|
* Takes an array of items and a value and returns a new array with the items that match the given value
|
|
36
50
|
* @param {Array} items - the items to sort
|
|
37
51
|
* @param {String} value - the value to use for ranking
|
|
38
52
|
* @param {Object} options - Some options to configure the sorter
|
|
39
53
|
* @return {Array} - the new sorted array
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return matchedItems.sort(function (a, b) { return sortRankedValues(a, b, baseSort); });
|
|
45
|
-
} : _c;
|
|
46
|
-
var matchedItems = items.reduce(reduceItemsToRanked, []);
|
|
47
|
-
return sorter(matchedItems).map(function (_a) {
|
|
48
|
-
var item = _a.item;
|
|
49
|
-
return item;
|
|
50
|
-
});
|
|
54
|
+
*/ function matchSorter(items, value, options = {}) {
|
|
55
|
+
const { keys, threshold = RANKING_MATCHES, baseSort = defaultBaseSortFn, sorter = (matchedItems)=>matchedItems.sort((a, b)=>sortRankedValues(a, b, baseSort)) } = options;
|
|
56
|
+
const matchedItems = items.reduce(reduceItemsToRanked, []);
|
|
57
|
+
return sorter(matchedItems).map(({ item })=>item);
|
|
51
58
|
function reduceItemsToRanked(matches, item, index) {
|
|
52
|
-
|
|
53
|
-
|
|
59
|
+
const rankingInfo = getHighestRanking(item, keys, value, options);
|
|
60
|
+
const { rank, keyThreshold = threshold } = rankingInfo;
|
|
54
61
|
if (rank >= keyThreshold) {
|
|
55
|
-
matches.push(
|
|
62
|
+
matches.push({
|
|
63
|
+
...rankingInfo,
|
|
64
|
+
item,
|
|
65
|
+
index
|
|
66
|
+
});
|
|
56
67
|
}
|
|
57
68
|
return matches;
|
|
58
69
|
}
|
|
59
70
|
}
|
|
60
|
-
exports.matchSorter = matchSorter;
|
|
61
71
|
/**
|
|
62
72
|
* Gets the highest ranking for value for the given item based on its values for the given keys
|
|
63
73
|
* @param {*} item - the item to rank
|
|
@@ -65,30 +75,26 @@ exports.matchSorter = matchSorter;
|
|
|
65
75
|
* @param {String} value - the value to rank against
|
|
66
76
|
* @param {Object} options - options to control the ranking
|
|
67
77
|
* @return {{rank: Number, keyIndex: Number, keyThreshold: Number}} - the highest ranking
|
|
68
|
-
*/
|
|
69
|
-
function getHighestRanking(item, keys, value, options) {
|
|
78
|
+
*/ function getHighestRanking(item, keys, value, options) {
|
|
70
79
|
if (!keys) {
|
|
71
80
|
// if keys is not specified, then we assume the item given is ready to be matched
|
|
72
|
-
|
|
81
|
+
const stringItem = item;
|
|
73
82
|
return {
|
|
74
83
|
// ends up being duplicate of 'item' in matches but consistent
|
|
75
84
|
rankedValue: stringItem,
|
|
76
85
|
rank: getMatchRanking(stringItem, value, options),
|
|
77
86
|
keyIndex: -1,
|
|
78
|
-
keyThreshold: options.threshold
|
|
87
|
+
keyThreshold: options.threshold
|
|
79
88
|
};
|
|
80
89
|
}
|
|
81
|
-
|
|
82
|
-
return valuesToRank.reduce(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
var newRankedValue = rankedValue;
|
|
87
|
-
var minRanking = attributes.minRanking, maxRanking = attributes.maxRanking, threshold = attributes.threshold;
|
|
90
|
+
const valuesToRank = getAllValuesToRank(item, keys);
|
|
91
|
+
return valuesToRank.reduce(({ rank, rankedValue, keyIndex, keyThreshold }, { itemValue, attributes }, i)=>{
|
|
92
|
+
let newRank = getMatchRanking(itemValue, value, options);
|
|
93
|
+
let newRankedValue = rankedValue;
|
|
94
|
+
const { minRanking, maxRanking, threshold } = attributes;
|
|
88
95
|
if (newRank < minRanking && newRank >= RANKING_MATCHES) {
|
|
89
96
|
newRank = minRanking;
|
|
90
|
-
}
|
|
91
|
-
else if (newRank > maxRanking) {
|
|
97
|
+
} else if (newRank > maxRanking) {
|
|
92
98
|
newRank = maxRanking;
|
|
93
99
|
}
|
|
94
100
|
if (newRank > rank) {
|
|
@@ -97,12 +103,17 @@ function getHighestRanking(item, keys, value, options) {
|
|
|
97
103
|
keyThreshold = threshold;
|
|
98
104
|
newRankedValue = itemValue;
|
|
99
105
|
}
|
|
100
|
-
return {
|
|
106
|
+
return {
|
|
107
|
+
rankedValue: newRankedValue,
|
|
108
|
+
rank,
|
|
109
|
+
keyIndex,
|
|
110
|
+
keyThreshold
|
|
111
|
+
};
|
|
101
112
|
}, {
|
|
102
113
|
rankedValue: item,
|
|
103
114
|
rank: RANKING_NO_MATCH,
|
|
104
115
|
keyIndex: -1,
|
|
105
|
-
keyThreshold: options.threshold
|
|
116
|
+
keyThreshold: options.threshold
|
|
106
117
|
});
|
|
107
118
|
}
|
|
108
119
|
/**
|
|
@@ -111,8 +122,7 @@ function getHighestRanking(item, keys, value, options) {
|
|
|
111
122
|
* @param {String} stringToRank - the string to rank
|
|
112
123
|
* @param {Object} options - options for the match (like keepDiacritics for comparison)
|
|
113
124
|
* @returns {Number} the ranking for how well stringToRank matches testString
|
|
114
|
-
*/
|
|
115
|
-
function getMatchRanking(testString, stringToRank, options) {
|
|
125
|
+
*/ function getMatchRanking(testString, stringToRank, options) {
|
|
116
126
|
testString = prepareValueForComparison(testString, options);
|
|
117
127
|
stringToRank = prepareValueForComparison(stringToRank, options);
|
|
118
128
|
// too long
|
|
@@ -135,14 +145,13 @@ function getMatchRanking(testString, stringToRank, options) {
|
|
|
135
145
|
return RANKING_STARTS_WITH;
|
|
136
146
|
}
|
|
137
147
|
// word starts with
|
|
138
|
-
if (testString.includes(
|
|
148
|
+
if (testString.includes(` ${stringToRank}`)) {
|
|
139
149
|
return RANKING_WORD_STARTS_WITH;
|
|
140
150
|
}
|
|
141
151
|
// contains
|
|
142
152
|
if (testString.includes(stringToRank)) {
|
|
143
153
|
return RANKING_CONTAINS;
|
|
144
|
-
}
|
|
145
|
-
else if (stringToRank.length === 1) {
|
|
154
|
+
} else if (stringToRank.length === 1) {
|
|
146
155
|
// If the only character in the given stringToRank
|
|
147
156
|
// isn't even contained in the testString, then
|
|
148
157
|
// it's definitely not a match.
|
|
@@ -161,13 +170,12 @@ function getMatchRanking(testString, stringToRank, options) {
|
|
|
161
170
|
*
|
|
162
171
|
* @param {String} string the string for which to produce the acronym
|
|
163
172
|
* @returns {String} the acronym
|
|
164
|
-
*/
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
splitByHyphenWords.forEach(function (splitByHyphenWord) {
|
|
173
|
+
*/ function getAcronym(string) {
|
|
174
|
+
let acronym = "";
|
|
175
|
+
const wordsInString = string.split(" ");
|
|
176
|
+
wordsInString.forEach((wordInString)=>{
|
|
177
|
+
const splitByHyphenWords = wordInString.split("-");
|
|
178
|
+
splitByHyphenWords.forEach((splitByHyphenWord)=>{
|
|
171
179
|
acronym += splitByHyphenWord.substring(0, 1);
|
|
172
180
|
});
|
|
173
181
|
});
|
|
@@ -182,13 +190,12 @@ function getAcronym(string) {
|
|
|
182
190
|
* @param {String} stringToRank - the string to rank
|
|
183
191
|
* @returns {Number} the number between RANKING_MATCHES and
|
|
184
192
|
* RANKING_MATCHES + 1 for how well stringToRank matches testString
|
|
185
|
-
*/
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
var charNumber = 0;
|
|
193
|
+
*/ function getClosenessRanking(testString, stringToRank) {
|
|
194
|
+
let matchingInOrderCharCount = 0;
|
|
195
|
+
let charNumber = 0;
|
|
189
196
|
function findMatchingCharacter(matchChar, string, index) {
|
|
190
|
-
for
|
|
191
|
-
|
|
197
|
+
for(let j = index, J = string.length; j < J; j++){
|
|
198
|
+
const stringChar = string[j];
|
|
192
199
|
if (stringChar === matchChar) {
|
|
193
200
|
matchingInOrderCharCount += 1;
|
|
194
201
|
return j + 1;
|
|
@@ -197,25 +204,25 @@ function getClosenessRanking(testString, stringToRank) {
|
|
|
197
204
|
return -1;
|
|
198
205
|
}
|
|
199
206
|
function getRanking(spread) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
207
|
+
const spreadPercentage = 1 / spread;
|
|
208
|
+
const inOrderPercentage = matchingInOrderCharCount / stringToRank.length;
|
|
209
|
+
const ranking = RANKING_MATCHES + inOrderPercentage * spreadPercentage;
|
|
203
210
|
return ranking;
|
|
204
211
|
}
|
|
205
|
-
|
|
212
|
+
const firstIndex = findMatchingCharacter(stringToRank[0], testString, 0);
|
|
206
213
|
if (firstIndex < 0) {
|
|
207
214
|
return RANKING_NO_MATCH;
|
|
208
215
|
}
|
|
209
216
|
charNumber = firstIndex;
|
|
210
|
-
for
|
|
211
|
-
|
|
217
|
+
for(let i = 1, I = stringToRank.length; i < I; i++){
|
|
218
|
+
const matchChar = stringToRank[i];
|
|
212
219
|
charNumber = findMatchingCharacter(matchChar, testString, charNumber);
|
|
213
|
-
|
|
220
|
+
const found = charNumber > -1;
|
|
214
221
|
if (!found) {
|
|
215
222
|
return RANKING_NO_MATCH;
|
|
216
223
|
}
|
|
217
224
|
}
|
|
218
|
-
|
|
225
|
+
const spread = charNumber - firstIndex;
|
|
219
226
|
return getRanking(spread);
|
|
220
227
|
}
|
|
221
228
|
/**
|
|
@@ -223,23 +230,20 @@ function getClosenessRanking(testString, stringToRank) {
|
|
|
223
230
|
* @param {Object} a - the first item to sort
|
|
224
231
|
* @param {Object} b - the second item to sort
|
|
225
232
|
* @return {Number} -1 if a should come first, 1 if b should come first, 0 if equal
|
|
226
|
-
*/
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
var same = aRank === bRank;
|
|
233
|
+
*/ function sortRankedValues(a, b, baseSort) {
|
|
234
|
+
const aFirst = -1;
|
|
235
|
+
const bFirst = 1;
|
|
236
|
+
const { rank: aRank, keyIndex: aKeyIndex } = a;
|
|
237
|
+
const { rank: bRank, keyIndex: bKeyIndex } = b;
|
|
238
|
+
const same = aRank === bRank;
|
|
233
239
|
if (same) {
|
|
234
240
|
if (aKeyIndex === bKeyIndex) {
|
|
235
241
|
// use the base sort function as a tie-breaker
|
|
236
242
|
return baseSort(a, b);
|
|
237
|
-
}
|
|
238
|
-
else {
|
|
243
|
+
} else {
|
|
239
244
|
return aKeyIndex < bKeyIndex ? aFirst : bFirst;
|
|
240
245
|
}
|
|
241
|
-
}
|
|
242
|
-
else {
|
|
246
|
+
} else {
|
|
243
247
|
return aRank > bRank ? aFirst : bFirst;
|
|
244
248
|
}
|
|
245
249
|
}
|
|
@@ -248,14 +252,12 @@ function sortRankedValues(a, b, baseSort) {
|
|
|
248
252
|
* @param {String} value - the value to clean
|
|
249
253
|
* @param {Object} options - {keepDiacritics: whether to remove diacritics}
|
|
250
254
|
* @return {String} the prepared value
|
|
251
|
-
*/
|
|
252
|
-
function prepareValueForComparison(value, _a) {
|
|
253
|
-
var keepDiacritics = _a.keepDiacritics;
|
|
255
|
+
*/ function prepareValueForComparison(value, { keepDiacritics }) {
|
|
254
256
|
// value might not actually be a string at this point (we don't get to choose)
|
|
255
257
|
// so part of preparing the value for comparison is ensure that it is a string
|
|
256
|
-
value =
|
|
258
|
+
value = `${value}`; // toString
|
|
257
259
|
if (!keepDiacritics) {
|
|
258
|
-
value = (0,
|
|
260
|
+
value = (0, _removeAccents.default)(value);
|
|
259
261
|
}
|
|
260
262
|
return value;
|
|
261
263
|
}
|
|
@@ -264,26 +266,21 @@ function prepareValueForComparison(value, _a) {
|
|
|
264
266
|
* @param {Object} item - the item
|
|
265
267
|
* @param {Object|Function} key - the potentially nested keypath or property callback
|
|
266
268
|
* @return {Array} - an array containing the value(s) at the nested keypath
|
|
267
|
-
*/
|
|
268
|
-
function getItemValues(item, key) {
|
|
269
|
+
*/ function getItemValues(item, key) {
|
|
269
270
|
if (typeof key === "object") {
|
|
270
271
|
key = key.key;
|
|
271
272
|
}
|
|
272
|
-
|
|
273
|
+
let value;
|
|
273
274
|
if (typeof key === "function") {
|
|
274
275
|
value = key(item);
|
|
275
|
-
}
|
|
276
|
-
else if (item == null) {
|
|
276
|
+
} else if (item == null) {
|
|
277
277
|
value = null;
|
|
278
|
-
}
|
|
279
|
-
else if (Object.hasOwnProperty.call(item, key)) {
|
|
278
|
+
} else if (Object.hasOwnProperty.call(item, key)) {
|
|
280
279
|
value = item[key];
|
|
281
|
-
}
|
|
282
|
-
else if (key.includes(".")) {
|
|
280
|
+
} else if (key.includes(".")) {
|
|
283
281
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
284
282
|
return getNestedValues(key, item);
|
|
285
|
-
}
|
|
286
|
-
else {
|
|
283
|
+
} else {
|
|
287
284
|
value = null;
|
|
288
285
|
}
|
|
289
286
|
// because `value` can also be undefined
|
|
@@ -293,7 +290,9 @@ function getItemValues(item, key) {
|
|
|
293
290
|
if (Array.isArray(value)) {
|
|
294
291
|
return value;
|
|
295
292
|
}
|
|
296
|
-
return [
|
|
293
|
+
return [
|
|
294
|
+
String(value)
|
|
295
|
+
];
|
|
297
296
|
}
|
|
298
297
|
/**
|
|
299
298
|
* Given path: "foo.bar.baz"
|
|
@@ -301,24 +300,23 @@ function getItemValues(item, key) {
|
|
|
301
300
|
* -> 'buzz'
|
|
302
301
|
* @param path a dot-separated set of keys
|
|
303
302
|
* @param item the item to get the value from
|
|
304
|
-
*/
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
303
|
+
*/ function getNestedValues(path, item) {
|
|
304
|
+
const keys = path.split(".");
|
|
305
|
+
let values = [
|
|
306
|
+
item
|
|
307
|
+
];
|
|
308
|
+
for(let i = 0, I = keys.length; i < I; i++){
|
|
309
|
+
const nestedKey = keys[i];
|
|
310
|
+
let nestedValues = [];
|
|
311
|
+
for(let j = 0, J = values.length; j < J; j++){
|
|
312
|
+
const nestedItem = values[j];
|
|
313
|
+
if (nestedItem == null) continue;
|
|
315
314
|
if (Object.hasOwnProperty.call(nestedItem, nestedKey)) {
|
|
316
|
-
|
|
315
|
+
const nestedValue = nestedItem[nestedKey];
|
|
317
316
|
if (nestedValue != null) {
|
|
318
317
|
nestedValues.push(nestedValue);
|
|
319
318
|
}
|
|
320
|
-
}
|
|
321
|
-
else if (nestedKey === "*") {
|
|
319
|
+
} else if (nestedKey === "*") {
|
|
322
320
|
// ensure that values is an array
|
|
323
321
|
nestedValues = nestedValues.concat(nestedItem);
|
|
324
322
|
}
|
|
@@ -328,8 +326,8 @@ function getNestedValues(path, item) {
|
|
|
328
326
|
if (Array.isArray(values[0])) {
|
|
329
327
|
// keep allowing the implicit wildcard for an array of strings at the end of
|
|
330
328
|
// the path; don't use `.flat()` because that's not available in node.js v10
|
|
331
|
-
|
|
332
|
-
return result.concat
|
|
329
|
+
const result = [];
|
|
330
|
+
return result.concat(...values);
|
|
333
331
|
}
|
|
334
332
|
// Based on our logic it should be an array of strings by now...
|
|
335
333
|
// assuming the user's path terminated in strings
|
|
@@ -340,35 +338,36 @@ function getNestedValues(path, item) {
|
|
|
340
338
|
* @param item - the item from which the values will be retrieved
|
|
341
339
|
* @param keys - the keys to use to retrieve the values
|
|
342
340
|
* @return objects with {itemValue, attributes}
|
|
343
|
-
*/
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
for (var i = 0, I = itemValues.length; i < I; i++) {
|
|
341
|
+
*/ function getAllValuesToRank(item, keys) {
|
|
342
|
+
const allValues = [];
|
|
343
|
+
for(let j = 0, J = keys.length; j < J; j++){
|
|
344
|
+
const key = keys[j];
|
|
345
|
+
const attributes = getKeyAttributes(key);
|
|
346
|
+
const itemValues = getItemValues(item, key);
|
|
347
|
+
for(let i = 0, I = itemValues.length; i < I; i++){
|
|
351
348
|
allValues.push({
|
|
352
349
|
itemValue: itemValues[i],
|
|
353
|
-
attributes
|
|
350
|
+
attributes
|
|
354
351
|
});
|
|
355
352
|
}
|
|
356
353
|
}
|
|
357
354
|
return allValues;
|
|
358
355
|
}
|
|
359
|
-
|
|
356
|
+
const defaultKeyAttributes = {
|
|
360
357
|
maxRanking: Infinity,
|
|
361
|
-
minRanking: -Infinity
|
|
358
|
+
minRanking: -Infinity
|
|
362
359
|
};
|
|
363
360
|
/**
|
|
364
361
|
* Gets all the attributes for the given key
|
|
365
362
|
* @param key - the key from which the attributes will be retrieved
|
|
366
363
|
* @return object containing the key's attributes
|
|
367
|
-
*/
|
|
368
|
-
function getKeyAttributes(key) {
|
|
364
|
+
*/ function getKeyAttributes(key) {
|
|
369
365
|
if (typeof key === "string") {
|
|
370
366
|
return defaultKeyAttributes;
|
|
371
367
|
}
|
|
372
|
-
return
|
|
368
|
+
return {
|
|
369
|
+
...defaultKeyAttributes,
|
|
370
|
+
...key
|
|
371
|
+
};
|
|
373
372
|
}
|
|
374
|
-
|
|
373
|
+
const _default = matchSorter;
|