@superutils/core 1.0.5 → 1.0.7
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 +13 -6
- package/dist/index.d.ts +41 -23
- package/dist/index.js +94 -50
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ For full API reference check out the [docs page](https://alien45.github.io/super
|
|
|
15
15
|
- [`is`](#is): Type checkers
|
|
16
16
|
- [`deferred()`](#deferred): Debounce callbacks
|
|
17
17
|
- [`throttle()`](#throttle): Throttle callbacks
|
|
18
|
-
- [`fallbackIfFails()`](#fallback-if-fails): Gracefully invoke functions
|
|
18
|
+
- [`fallbackIfFails()`](#fallback-if-fails): Gracefully invoke functions or promises with a fallback
|
|
19
19
|
- [`objCopy()`](#obj-copy): Deep-copy objects
|
|
20
20
|
- [`search()`](#search): Search iterable collections
|
|
21
21
|
|
|
@@ -110,9 +110,9 @@ handleChange({ target: { value 3 } }) // will be ignored
|
|
|
110
110
|
|
|
111
111
|
<div id="fallback-if-fails"></div>
|
|
112
112
|
|
|
113
|
-
### `fallbackIfFails(
|
|
113
|
+
### `fallbackIfFails(target, args, fallback)`: Gracefully invoke functions or promises with a fallback
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
The `fallbackIfFails` function can wrap a standard function, a promise-returning function, or a promise directly. It automatically handles both synchronous execution and asynchronous resolution, providing a fallback value if the function throws an error or the promise is rejected.
|
|
116
116
|
|
|
117
117
|
#### Sync operations:
|
|
118
118
|
|
|
@@ -149,6 +149,13 @@ fallbackIfFails(
|
|
|
149
149
|
{ products: [] }, // Fallback value to be returned when function throws an error.
|
|
150
150
|
).then(console.log)
|
|
151
151
|
// Prints the result when request is successful or fallback value when request fails
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
// use a promise
|
|
155
|
+
fallbackIfFails(
|
|
156
|
+
Promise.reject('error'),
|
|
157
|
+
[], //
|
|
158
|
+
)
|
|
152
159
|
```
|
|
153
160
|
|
|
154
161
|
<div id="obj-copy"></div>
|
|
@@ -222,7 +229,7 @@ search(data, { query: { age: 28, name: 've' } })
|
|
|
222
229
|
// [4, { age: 28, name: 'Dave' }],
|
|
223
230
|
// ])
|
|
224
231
|
|
|
225
|
-
//
|
|
232
|
+
// Search across all properties
|
|
226
233
|
search(data, { query: 'li' })
|
|
227
234
|
search(data, { query: /li/i }) // Using regular expression
|
|
228
235
|
// Result:
|
|
@@ -249,13 +256,13 @@ search(data, {
|
|
|
249
256
|
asMap: false, // Result type: true => Map (default, keys preserved), false => Array
|
|
250
257
|
ignoreCase: false, // For text case-sensitivity
|
|
251
258
|
limit: 10, // Number of items returned. Default: no limit
|
|
252
|
-
matchExact: true, // true:
|
|
259
|
+
matchExact: true, // true: match exact value. false (default): partial matching
|
|
253
260
|
matchAll: true, // if true, item will be matched only when all of the query properties match
|
|
254
261
|
query: {
|
|
255
262
|
age: /(2[5-9])|(3[0-5])/, // match ages 25-35
|
|
256
263
|
name: /ali|ob|ve/i,
|
|
257
264
|
},
|
|
258
|
-
// transform the property values (or item itself when in
|
|
265
|
+
// transform the property values (or item itself when searching all properties in global search mode using `query: string | RegExp`)
|
|
259
266
|
transform: (item, value, property) => {
|
|
260
267
|
// exclude items by returning undefined or emptry string
|
|
261
268
|
if (item.age < 18) return ''
|
package/dist/index.d.ts
CHANGED
|
@@ -24,15 +24,19 @@ type CreateTuple<T, Length extends number, Output extends readonly unknown[] = [
|
|
|
24
24
|
* @template TData The final return type.
|
|
25
25
|
*/
|
|
26
26
|
type Curry<TData, TParams extends unknown[]> = <TArgs extends unknown[]>(...args: TArgs & KeepFirstN<TParams, TArgs['length']>) => DropFirstN<TParams, TArgs['length']> extends [unknown, ...unknown[]] ? Curry<TData, DropFirstN<TParams, TArgs['length']>> : TData;
|
|
27
|
+
type CurriedArgs<TArgs extends unknown[], TArgsIsFinite extends boolean, TFunc extends (...args: any[]) => unknown, TArity extends number> = TArgsIsFinite extends false ? CreateTuple<Parameters<TFunc>[number], TArity> : KeepFirstN<[
|
|
28
|
+
...KeepRequired<TArgs>,
|
|
29
|
+
...KeepOptionals<TArgs, true, undefined>
|
|
30
|
+
], TArity>;
|
|
27
31
|
/**
|
|
28
|
-
* Deferred function
|
|
32
|
+
* Deferred function options
|
|
29
33
|
*/
|
|
30
|
-
|
|
34
|
+
type DeferredOptions<ThisArg = unknown> = {
|
|
31
35
|
leading?: boolean | 'global';
|
|
32
36
|
onError?: (err: unknown) => ValueOrPromise<unknown>;
|
|
33
37
|
thisArg?: ThisArg;
|
|
34
38
|
tid?: TimeoutId;
|
|
35
|
-
}
|
|
39
|
+
};
|
|
36
40
|
/**
|
|
37
41
|
* Drop the first item from an array/tuple and keep the rest
|
|
38
42
|
* ---
|
|
@@ -249,10 +253,6 @@ type ValueOrPromise<T> = T | Promise<T>;
|
|
|
249
253
|
declare function curry<TData, TArgs extends unknown[], TArgsIsFinite extends boolean = IsFiniteTuple<TArgs>, TArity extends TArgs['length'] = TArgsIsFinite extends true ? TupleMaxLength<TArgs> : number>(func: (...args: TArgs) => TData, ...[arity]: TArgsIsFinite extends true ? [arity?: TArity] : [
|
|
250
254
|
arity: TArity
|
|
251
255
|
]): Curry<TData, CurriedArgs<TArgs, TArgsIsFinite, (...args: TArgs) => TData, TArity>>;
|
|
252
|
-
type CurriedArgs<TArgs extends unknown[], TArgsIsFinite extends boolean, TFunc extends (...args: any[]) => unknown, TArity extends number> = TArgsIsFinite extends false ? CreateTuple<Parameters<TFunc>[number], TArity> : KeepFirstN<[
|
|
253
|
-
...KeepRequired<TArgs>,
|
|
254
|
-
...KeepOptionals<TArgs, true, undefined>
|
|
255
|
-
], TArity>;
|
|
256
256
|
|
|
257
257
|
/**
|
|
258
258
|
* @function deferred AKA debounce
|
|
@@ -966,7 +966,7 @@ type SortOptions = {
|
|
|
966
966
|
undefinedFirst?: boolean;
|
|
967
967
|
};
|
|
968
968
|
/** Search criteria for searcheing iterables */
|
|
969
|
-
type SearchOptions<K, V, AsMap extends boolean = false> = {
|
|
969
|
+
type SearchOptions<K, V, MatchExact extends boolean = false, AsMap extends boolean = false> = {
|
|
970
970
|
/** Whethere to return the result as a map (`true`) or array (`false`). Default: `true` */
|
|
971
971
|
asMap?: AsMap;
|
|
972
972
|
/** case-insensitive search for strings. Default: `false` */
|
|
@@ -974,21 +974,33 @@ type SearchOptions<K, V, AsMap extends boolean = false> = {
|
|
|
974
974
|
/** limit number of results. Default: `Infinity` */
|
|
975
975
|
limit?: number;
|
|
976
976
|
/** partial match for values. Default: `false` */
|
|
977
|
-
matchExact?:
|
|
977
|
+
matchExact?: MatchExact;
|
|
978
978
|
/** match all supplied key-value pairs. Default: `false` */
|
|
979
979
|
matchAll?: boolean;
|
|
980
980
|
/** key-value pairs */
|
|
981
|
-
query: Record<
|
|
981
|
+
query: Record<PropertyKey, unknown> | RegExp | string;
|
|
982
|
+
/** If `true`, the results are sorted by relevance (match index). Default: `false` */
|
|
983
|
+
ranked?: boolean;
|
|
982
984
|
/** Map to store results in. Default: `new Map()` */
|
|
983
985
|
result?: Map<K, V>;
|
|
984
|
-
/**
|
|
985
|
-
|
|
986
|
+
/**
|
|
987
|
+
* Boolean or Callback to prepare item or individual property for search by converting to string.
|
|
988
|
+
*
|
|
989
|
+
* - `true`: value will be stringified
|
|
990
|
+
* - `false`: value will not be stringified when `matchExact = true`
|
|
991
|
+
* - `function`: transformed value will be used to search
|
|
992
|
+
*
|
|
993
|
+
* Returning "empty" (`undefined | null | [] | '' | ...`) value will ignore the item/property.
|
|
994
|
+
*
|
|
995
|
+
* Default: `true`
|
|
996
|
+
*/
|
|
997
|
+
transform?: boolean | ((
|
|
986
998
|
/** List item */
|
|
987
999
|
item: V,
|
|
988
|
-
/** Item property value or `undefined` for
|
|
1000
|
+
/** Item property value or `undefined` for global search across all properties. */
|
|
989
1001
|
value?: V[keyof V],
|
|
990
|
-
/** Item property key provided by query or `undefined` for
|
|
991
|
-
key?: keyof V) => string | undefined;
|
|
1002
|
+
/** Item property key provided by query or `undefined` for global search across all properties. */
|
|
1003
|
+
key?: keyof V) => MatchExact extends true ? unknown : string | undefined);
|
|
992
1004
|
};
|
|
993
1005
|
|
|
994
1006
|
/**
|
|
@@ -1085,8 +1097,8 @@ declare const getValues: <K, V>(data: IterableList<K, V>) => V[];
|
|
|
1085
1097
|
declare const reverse: <K, V, T extends IterableList<K, V>>(data: T, reverse?: boolean, newInstance?: boolean) => V[] | [K, V][] | Map<K, V> | Set<V> | (T & Record<"clear", unknown>);
|
|
1086
1098
|
|
|
1087
1099
|
/**
|
|
1088
|
-
* A versatile utility for searching through an iterable list (
|
|
1089
|
-
* It supports both a
|
|
1100
|
+
* A versatile utility for searching through an iterable list (Array, Map, or Set) of objects.
|
|
1101
|
+
* It supports both a global search (using a string or RegExp) across all properties of an item, and a
|
|
1090
1102
|
* detailed, field-specific search using a query object.
|
|
1091
1103
|
*
|
|
1092
1104
|
* @param data The list of objects to search within. Compatible types include:
|
|
@@ -1097,6 +1109,8 @@ declare const reverse: <K, V, T extends IterableList<K, V>>(data: T, reverse?: b
|
|
|
1097
1109
|
* - `HTMLCollection` (in DOM environments): should accompany `options.transform()`
|
|
1098
1110
|
* @param options The search criteria.
|
|
1099
1111
|
* @param options.query The search query. Can be a string to search all fields, or an object for field-specific
|
|
1112
|
+
* @param options.ranked (optional) If `true`, the results are sorted by relevance (match index). Default: `false`.
|
|
1113
|
+
*
|
|
1100
1114
|
* searches (e.g., `{ name: 'John', city: 'New York' }`).
|
|
1101
1115
|
* @param options.asMap (optional) If `true`, returns a `Map`. If `false`, returns an `Array`. Default: `true`.
|
|
1102
1116
|
* @param options.ignoreCase (optional) If `true`, performs a case-insensitive search for strings. Default: `true`.
|
|
@@ -1131,12 +1145,16 @@ declare const reverse: <K, V, T extends IterableList<K, V>>(data: T, reverse?: b
|
|
|
1131
1145
|
* ```
|
|
1132
1146
|
*/
|
|
1133
1147
|
declare const search: {
|
|
1134
|
-
<K, V, AsMap extends boolean = true, Result = AsMap extends true ? Map<K, V> : V[]>(data: IterableList<K, V>, options: SearchOptions<K, V, AsMap>): Result;
|
|
1135
|
-
defaultOptions: Pick<Required<SearchOptions<unknown, unknown, true>>, "matchAll" | "limit" | "asMap" | "ignoreCase" | "
|
|
1148
|
+
<K, V, MatchExact extends boolean = false, AsMap extends boolean = true, Result = AsMap extends true ? Map<K, V> : V[]>(data: IterableList<K, V>, options: SearchOptions<K, V, MatchExact, AsMap>): Result;
|
|
1149
|
+
defaultOptions: Pick<Required<SearchOptions<unknown, unknown, false, true>>, "matchAll" | "limit" | "asMap" | "ignoreCase" | "ranked" | "transform">;
|
|
1136
1150
|
};
|
|
1137
|
-
/**
|
|
1138
|
-
|
|
1139
|
-
|
|
1151
|
+
/**
|
|
1152
|
+
* Utility for use with {@link search} function
|
|
1153
|
+
*
|
|
1154
|
+
* @returns match index (`-1` means didn't match)
|
|
1155
|
+
*/
|
|
1156
|
+
declare function matchObjOrProp<K, V>(// extends Record<string, unknown>
|
|
1157
|
+
{ query, ignoreCase, matchExact, transform, }: Pick<SearchOptions<K, V, boolean>, 'transform' | 'query' | 'ignoreCase' | 'matchExact'>, item: V, propertyName?: string): number;
|
|
1140
1158
|
|
|
1141
1159
|
type SliceMapTransform<Data, Value, Key> = (item: Value, key: Key, data: Data) => Value;
|
|
1142
1160
|
type SliceMapOptions<Data, Value, Key, AsMap extends boolean = false> = {
|
|
@@ -1356,4 +1374,4 @@ declare const HASH_REGEX: RegExp;
|
|
|
1356
1374
|
*/
|
|
1357
1375
|
declare const strToArr: (value: unknown, seperator?: string) => string[];
|
|
1358
1376
|
|
|
1359
|
-
export { type ArrayComparator, type AsyncFn, type CreateTuple, type CurriedArgs, type Curry, type DeferredOptions, type DropFirst, type DropFirstN, type DropLast, EMAIL_REGEX, type EntryComparator, type FindOptions, HASH_REGEX, HEX_REGEX, type IfPromiseAddValue, type IsFiniteTuple, type IsOptional, type IterableList, type KeepFirst, type KeepFirstN, type KeepOptionals, type KeepRequired, type MakeOptional, type MinLength, type NegativeNumber, type OptionalIf, type PositiveNumber, type PositiveNumberWithZero, type ReadOnlyAllowAddFn, ReadOnlyArrayHelper, type ReadOnlyConfig, type SearchOptions, type Slice, type SliceMapOptions, type SliceMapTransform, type SortOptions, type ThrottleOptions, type TimeoutId, type TupleMaxLength, type TupleWithAlt, type ValueOrFunc, type ValueOrPromise, arrReadOnly, arrReverse, arrToMap, arrUnique, asAny, clearClutter, copyToClipboard, curry, debounce, deferred, fallbackIfFails, filter, find, forceCast, getEntries, getKeys, getSize, getUrlParam, getValues, is, isArr, isArr2D, isArrLike, isArrLikeSafe, isArrObj, isArrUnique, isAsyncFn, isBool, isDate, isDateValid, isDefined, isEmpty, isEmptySafe, isEnvBrowser, isEnvNode, isEnvTouchable, isError, isFn, isInteger, isMap, isMapObj, isNumber, isObj, isPositiveInteger, isPositiveNumber, isPromise, isRegExp, isSet, isStr, isSymbol, isUint8Arr, isUrl, isUrlValid, mapJoin,
|
|
1377
|
+
export { type ArrayComparator, type AsyncFn, type CreateTuple, type CurriedArgs, type Curry, type DeferredOptions, type DropFirst, type DropFirstN, type DropLast, EMAIL_REGEX, type EntryComparator, type FindOptions, HASH_REGEX, HEX_REGEX, type IfPromiseAddValue, type IsFiniteTuple, type IsOptional, type IterableList, type KeepFirst, type KeepFirstN, type KeepOptionals, type KeepRequired, type MakeOptional, type MinLength, type NegativeNumber, type OptionalIf, type PositiveNumber, type PositiveNumberWithZero, type ReadOnlyAllowAddFn, ReadOnlyArrayHelper, type ReadOnlyConfig, type SearchOptions, type Slice, type SliceMapOptions, type SliceMapTransform, type SortOptions, type ThrottleOptions, type TimeoutId, type TupleMaxLength, type TupleWithAlt, type ValueOrFunc, type ValueOrPromise, arrReadOnly, arrReverse, arrToMap, arrUnique, asAny, clearClutter, copyToClipboard, curry, debounce, deferred, fallbackIfFails, filter, find, forceCast, getEntries, getKeys, getSize, getUrlParam, getValues, is, isArr, isArr2D, isArrLike, isArrLikeSafe, isArrObj, isArrUnique, isAsyncFn, isBool, isDate, isDateValid, isDefined, isEmpty, isEmptySafe, isEnvBrowser, isEnvNode, isEnvTouchable, isError, isFn, isInteger, isMap, isMapObj, isNumber, isObj, isPositiveInteger, isPositiveNumber, isPromise, isRegExp, isSet, isStr, isSymbol, isUint8Arr, isUrl, isUrlValid, mapJoin, matchObjOrProp, noop, noopAsync, objClean, objCopy, objCreate, objHasKeys, objKeys, objReadOnly, objSetProp, objSetPropUndefined, objSort, objWithoutKeys, randomInt, reverse, search, sliceMap, sort, strToArr, throttled, toDatetimeLocal };
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
// src/forceCast.ts
|
|
2
|
-
var asAny = (x) => x;
|
|
3
|
-
var forceCast = (x) => x;
|
|
4
|
-
var forceCast_default = forceCast;
|
|
5
|
-
|
|
6
1
|
// src/curry.ts
|
|
7
2
|
function curry(func, ...[arity = func.length]) {
|
|
8
3
|
const curriedFn = (...args) => {
|
|
9
4
|
const _args = args;
|
|
10
|
-
if (_args.length >= arity) return func(...
|
|
11
|
-
return (...nextArgs) =>
|
|
5
|
+
if (_args.length >= arity) return func(...args);
|
|
6
|
+
return (...nextArgs) => curriedFn(
|
|
12
7
|
..._args,
|
|
13
8
|
...nextArgs
|
|
14
9
|
);
|
|
@@ -248,6 +243,10 @@ function debounce(...args) {
|
|
|
248
243
|
return deferred_default(...args);
|
|
249
244
|
}
|
|
250
245
|
|
|
246
|
+
// src/forceCast.ts
|
|
247
|
+
var asAny = (x) => x;
|
|
248
|
+
var forceCast = (x) => x;
|
|
249
|
+
|
|
251
250
|
// src/throttled.ts
|
|
252
251
|
var throttled = (callback, delay = 50, config = {}) => {
|
|
253
252
|
const { defaults: d } = throttled;
|
|
@@ -346,7 +345,7 @@ var objCopy = (input, output, ignoreKeys, override = false, recursive = true) =>
|
|
|
346
345
|
const value = input[key];
|
|
347
346
|
const skip = _output.hasOwnProperty(key) && (override === "empty" ? !isEmpty(_output[key]) : isFn(override) ? !override(key, _output[key], value) : true);
|
|
348
347
|
if (skip) continue;
|
|
349
|
-
const isPrimitive = [void 0, null, Infinity, NaN].includes(
|
|
348
|
+
const isPrimitive = [void 0, null, Infinity, NaN].includes(value) || !isObj(value, false);
|
|
350
349
|
if (isPrimitive) {
|
|
351
350
|
_output[key] = value;
|
|
352
351
|
continue;
|
|
@@ -356,26 +355,28 @@ var objCopy = (input, output, ignoreKeys, override = false, recursive = true) =>
|
|
|
356
355
|
case Array.prototype:
|
|
357
356
|
return clone(value, "[]");
|
|
358
357
|
case ArrayBuffer.prototype:
|
|
359
|
-
return
|
|
358
|
+
return value.slice(0);
|
|
360
359
|
case Date.prototype:
|
|
361
|
-
return new Date(
|
|
360
|
+
return new Date(value.getTime());
|
|
362
361
|
case Map.prototype:
|
|
363
362
|
return new Map(
|
|
364
363
|
clone(
|
|
365
|
-
Array.from(
|
|
364
|
+
Array.from(
|
|
365
|
+
value
|
|
366
|
+
),
|
|
366
367
|
"[]"
|
|
367
368
|
)
|
|
368
369
|
);
|
|
369
370
|
case RegExp.prototype:
|
|
370
|
-
return new RegExp(
|
|
371
|
+
return new RegExp(value);
|
|
371
372
|
case Set.prototype:
|
|
372
373
|
return new Set(
|
|
373
|
-
clone(Array.from(
|
|
374
|
+
clone(Array.from(value))
|
|
374
375
|
);
|
|
375
376
|
case Uint8Array.prototype:
|
|
376
|
-
return new Uint8Array([...
|
|
377
|
+
return new Uint8Array([...value]);
|
|
377
378
|
case URL.prototype:
|
|
378
|
-
return new URL(
|
|
379
|
+
return new URL(value);
|
|
379
380
|
default:
|
|
380
381
|
break;
|
|
381
382
|
}
|
|
@@ -579,11 +580,23 @@ var search = (data, options) => {
|
|
|
579
580
|
const result = isMap(options == null ? void 0 : options.result) ? options.result : /* @__PURE__ */ new Map();
|
|
580
581
|
const asMap = (_a = options == null ? void 0 : options.asMap) != null ? _a : search.defaultOptions.asMap;
|
|
581
582
|
if (ignore) return asMap ? result : getValues_default(result);
|
|
582
|
-
options = objCopy(
|
|
583
|
-
|
|
583
|
+
options = objCopy(
|
|
584
|
+
search.defaultOptions,
|
|
585
|
+
options,
|
|
586
|
+
[],
|
|
587
|
+
"empty"
|
|
588
|
+
// override `option` property with default value when "empty" (undefined, null, '',....)
|
|
589
|
+
);
|
|
590
|
+
const {
|
|
591
|
+
ignoreCase,
|
|
592
|
+
limit = Infinity,
|
|
593
|
+
matchAll,
|
|
594
|
+
matchExact,
|
|
595
|
+
ranked
|
|
596
|
+
} = options;
|
|
584
597
|
let { query } = options;
|
|
585
598
|
const qIsStr = isStr(query);
|
|
586
|
-
const qIsRegExp =
|
|
599
|
+
const qIsRegExp = isRegExp(query);
|
|
587
600
|
const qKeys = fallbackIfFails_default(Object.keys, [query], []);
|
|
588
601
|
if (ignoreCase && !matchExact && !qIsRegExp) {
|
|
589
602
|
query = qIsStr ? query.toLowerCase() : objCreate(
|
|
@@ -594,15 +607,45 @@ var search = (data, options) => {
|
|
|
594
607
|
);
|
|
595
608
|
}
|
|
596
609
|
options.query = query;
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
610
|
+
if (!ranked) {
|
|
611
|
+
for (const [dataKey, dataValue] of data.entries()) {
|
|
612
|
+
if (result.size >= limit) break;
|
|
613
|
+
const matched = qIsStr || qIsRegExp ? (
|
|
614
|
+
// global search across all properties
|
|
615
|
+
matchObjOrProp(options, dataValue, void 0) >= 0
|
|
616
|
+
) : (
|
|
617
|
+
// field-specific search
|
|
618
|
+
qKeys[matchAll ? "every" : "some"](
|
|
619
|
+
(key) => matchObjOrProp(options, dataValue, key) >= 0
|
|
620
|
+
)
|
|
621
|
+
);
|
|
622
|
+
if (!matched) continue;
|
|
623
|
+
result.set(dataKey, dataValue);
|
|
624
|
+
}
|
|
625
|
+
} else {
|
|
626
|
+
const preRankedResults = [];
|
|
627
|
+
for (const [dataKey, dataValue] of data.entries()) {
|
|
628
|
+
let matchIndex = -1;
|
|
629
|
+
if (qIsStr || qIsRegExp) {
|
|
630
|
+
matchIndex = matchObjOrProp(options, dataValue, void 0);
|
|
631
|
+
matchIndex >= 0 && preRankedResults.push([matchIndex, dataKey, dataValue]);
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
const indexes = [];
|
|
635
|
+
const match = qKeys[matchAll ? "every" : "some"](
|
|
636
|
+
// field-specific search
|
|
637
|
+
(key) => {
|
|
638
|
+
const index = matchObjOrProp(options, dataValue, key);
|
|
639
|
+
indexes.push(index);
|
|
640
|
+
return index >= 0;
|
|
641
|
+
}
|
|
642
|
+
);
|
|
643
|
+
if (!match) continue;
|
|
644
|
+
matchIndex = // eslint-disable-next-line @typescript-eslint/prefer-find
|
|
645
|
+
indexes.sort((a, b) => a - b).filter((n) => n !== -1)[0];
|
|
646
|
+
matchIndex >= 0 && preRankedResults.push([matchIndex, dataKey, dataValue]);
|
|
647
|
+
}
|
|
648
|
+
preRankedResults.sort((a, b) => a[0] - b[0]).slice(0, limit).forEach(([_, key, value]) => result.set(key, value));
|
|
606
649
|
}
|
|
607
650
|
return asMap ? result : getValues_default(result);
|
|
608
651
|
};
|
|
@@ -611,36 +654,37 @@ search.defaultOptions = {
|
|
|
611
654
|
ignoreCase: true,
|
|
612
655
|
limit: Infinity,
|
|
613
656
|
matchAll: false,
|
|
614
|
-
|
|
657
|
+
ranked: false,
|
|
658
|
+
transform: true
|
|
615
659
|
};
|
|
616
|
-
function
|
|
660
|
+
function matchObjOrProp({
|
|
617
661
|
query,
|
|
618
662
|
ignoreCase,
|
|
619
663
|
matchExact,
|
|
620
|
-
transform
|
|
664
|
+
transform = true
|
|
621
665
|
}, item, propertyName) {
|
|
622
|
-
|
|
623
|
-
const
|
|
624
|
-
const
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
)
|
|
633
|
-
|
|
634
|
-
) : (_a = propVal == null ? void 0 : propVal.toString) == null ? void 0 : _a.call(propVal);
|
|
635
|
-
},
|
|
666
|
+
var _a, _b;
|
|
667
|
+
const global = isStr(query) || isRegExp(query) || propertyName === void 0;
|
|
668
|
+
const keyword = global ? query : query[propertyName];
|
|
669
|
+
const propVal = global || !isObj(item) ? item : item[propertyName];
|
|
670
|
+
const value = fallbackIfFails_default(
|
|
671
|
+
() => isFn(transform) ? transform(
|
|
672
|
+
item,
|
|
673
|
+
global ? void 0 : propVal,
|
|
674
|
+
propertyName
|
|
675
|
+
) : matchExact && transform === false ? propVal : isObj(propVal, false) ? JSON.stringify(
|
|
676
|
+
isArrLike(propVal) ? [...propVal.values()] : Object.values(propVal)
|
|
677
|
+
) : String(propVal != null ? propVal : ""),
|
|
636
678
|
[],
|
|
637
679
|
""
|
|
638
680
|
);
|
|
639
|
-
if (
|
|
640
|
-
if (
|
|
641
|
-
|
|
642
|
-
if (
|
|
643
|
-
return
|
|
681
|
+
if (value === keyword) return 0;
|
|
682
|
+
if (matchExact) return -1;
|
|
683
|
+
let valueStr = String(value);
|
|
684
|
+
if (!valueStr.trim()) return -1;
|
|
685
|
+
if (isRegExp(keyword)) return (_b = (_a = valueStr.match(keyword)) == null ? void 0 : _a.index) != null ? _b : -1;
|
|
686
|
+
if (ignoreCase) valueStr = valueStr.toLowerCase();
|
|
687
|
+
return valueStr.indexOf(String(keyword));
|
|
644
688
|
}
|
|
645
689
|
var search_default = search;
|
|
646
690
|
|
|
@@ -924,7 +968,7 @@ export {
|
|
|
924
968
|
isUrl,
|
|
925
969
|
isUrlValid,
|
|
926
970
|
mapJoin,
|
|
927
|
-
|
|
971
|
+
matchObjOrProp,
|
|
928
972
|
noop,
|
|
929
973
|
noopAsync,
|
|
930
974
|
objClean,
|
package/package.json
CHANGED