@superutils/core 1.1.4 → 1.1.5
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 +99 -0
- package/dist/index.d.ts +124 -24
- package/dist/index.js +19 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,6 +18,10 @@ For full API reference check out the [docs page](https://alien45.github.io/super
|
|
|
18
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
|
+
- [Advanced search options](#search-advanced)
|
|
22
|
+
- [`Ranked search`](#search-ranked): sort results by relevance
|
|
23
|
+
- [Combine `search()` with `deferred()`](#search-deferred): simulate a search input with debounce mechanism
|
|
24
|
+
- [`curry()`](#curry): Convert any function into a curried function
|
|
21
25
|
|
|
22
26
|
## Installation
|
|
23
27
|
|
|
@@ -236,6 +240,8 @@ search(data, { query: /li/i }) // Using regular expression
|
|
|
236
240
|
// ])
|
|
237
241
|
```
|
|
238
242
|
|
|
243
|
+
<div id="search-advanced"></div>
|
|
244
|
+
|
|
239
245
|
#### Advanced Search Options:
|
|
240
246
|
|
|
241
247
|
```javascript
|
|
@@ -275,3 +281,96 @@ search(data, {
|
|
|
275
281
|
// { age: 28, name: 'Dave' }
|
|
276
282
|
// ]
|
|
277
283
|
```
|
|
284
|
+
|
|
285
|
+
<div id="search-ranked"></div>
|
|
286
|
+
|
|
287
|
+
#### Search Ranked: sort results by relevance
|
|
288
|
+
|
|
289
|
+
When `ranked` is set to `true`, results are sorted by relevance. In this example, "Alice" is ranked higher than "Charlie" because the match "li" appears earlier in the string.
|
|
290
|
+
|
|
291
|
+
```javascript
|
|
292
|
+
import { search } from '@superutils/core'
|
|
293
|
+
|
|
294
|
+
// Sample colletion
|
|
295
|
+
const data = new Map([
|
|
296
|
+
[2, { age: 25, name: 'Bob' }],
|
|
297
|
+
[3, { age: 35, name: 'Charlie' }],
|
|
298
|
+
[4, { age: 28, name: 'Dave' }],
|
|
299
|
+
[5, { age: 22, name: 'Eve' }],
|
|
300
|
+
[1, { age: 30, name: 'Alice' }],
|
|
301
|
+
])
|
|
302
|
+
const result = search(data, {
|
|
303
|
+
asMap: false, // Result type: true => Map (default, keys preserved), false => Array
|
|
304
|
+
limit: 10, // Number of items returned. Default: no limit
|
|
305
|
+
query: /li/i,
|
|
306
|
+
ranked: true,
|
|
307
|
+
})
|
|
308
|
+
console.log(result)
|
|
309
|
+
// [ { age: 30, name: 'Alice' }, { age: 35, name: 'Charlie' } ]
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
<div id="search-deferred"></div>
|
|
313
|
+
|
|
314
|
+
#### Combine `search()` with `deferred()`: simulate a search input with debounce mechanism
|
|
315
|
+
|
|
316
|
+
```javascript
|
|
317
|
+
import { deferred, search } from '@superutils/core'
|
|
318
|
+
|
|
319
|
+
// sample colletion
|
|
320
|
+
const data = new Map([
|
|
321
|
+
[1, { age: 30, name: 'Alice' }],
|
|
322
|
+
[2, { age: 25, name: 'Bob' }],
|
|
323
|
+
[3, { age: 35, name: 'Charlie' }],
|
|
324
|
+
[4, { age: 28, name: 'Dave' }],
|
|
325
|
+
[5, { age: 22, name: 'Eve' }],
|
|
326
|
+
])
|
|
327
|
+
|
|
328
|
+
const searchDeferred = deferred(
|
|
329
|
+
event => {
|
|
330
|
+
const result = search(data, {
|
|
331
|
+
query: {
|
|
332
|
+
name: new RegExp(event?.target?.value, 'i'),
|
|
333
|
+
},
|
|
334
|
+
})
|
|
335
|
+
// print result to console
|
|
336
|
+
console.log(result)
|
|
337
|
+
},
|
|
338
|
+
300, // debounce duration in milliseconds
|
|
339
|
+
{ leading: false }, // optional defer options
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
// ignored
|
|
343
|
+
searchDeferred({ target: { value: 'l' } })
|
|
344
|
+
// ignored
|
|
345
|
+
setTimeout(() => searchDeferred({ target: { value: 'li' } }), 50)
|
|
346
|
+
// executed: prints `Map(1) { 3 => { age: 35, name: 'Charlie' } }`
|
|
347
|
+
setTimeout(() => searchDeferred({ target: { value: 'lie' } }), 200)
|
|
348
|
+
// executed: prints `Map(1) { 1 => { age: 30, name: 'Alice' } }`
|
|
349
|
+
setTimeout(() => searchDeferred({ target: { value: 'lic' } }), 510)
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
<div id="curry"></div>
|
|
353
|
+
|
|
354
|
+
### `curry(fn, arity)`: Convert any function into a curried function
|
|
355
|
+
|
|
356
|
+
```typescript
|
|
357
|
+
const func = (
|
|
358
|
+
first: string,
|
|
359
|
+
second: number,
|
|
360
|
+
third?: boolean,
|
|
361
|
+
fourth?: string,
|
|
362
|
+
) => `${first}-${second}-${third}-${fourth}`
|
|
363
|
+
// We create a new function from the `func()` function that acts like a type-safe curry function
|
|
364
|
+
// while also being flexible with the number of arguments supplied.
|
|
365
|
+
const curriedFunc = curry(func)
|
|
366
|
+
|
|
367
|
+
// Example 1: usage like a regular curry function and provide one argument at a time.
|
|
368
|
+
// Returns a function expecting args: second, third, fourth
|
|
369
|
+
const fnWith1 = curriedFunc('first')
|
|
370
|
+
// Returns a function expecting args: third, fourth
|
|
371
|
+
const fnWith2 = fnWith1(2)
|
|
372
|
+
// returns a function epecting only fourth arg
|
|
373
|
+
const fnWith3 = fnWith2(false)
|
|
374
|
+
// All args are now provided, the original function is called and result is returned.
|
|
375
|
+
const result = fnWith3('last')
|
|
376
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -315,19 +315,14 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
315
315
|
* Yes, the goal of `fallbackIfFails` is to ignore all runtime errors
|
|
316
316
|
* and ensure there's always a value returned.
|
|
317
317
|
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
318
|
+
* @param target promise or function to execute
|
|
319
|
+
* @param args arguments to be supplied to `func` fuction
|
|
320
|
+
* @param fallback alternative value to be used when target throws error.
|
|
321
321
|
*
|
|
322
|
-
*
|
|
323
|
-
* A fallback of the fallback is out of the scope of
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
* ---
|
|
327
|
-
*
|
|
328
|
-
* @param target promise or function to execute
|
|
329
|
-
* @param args arguments to be supplied to `func` fuction
|
|
330
|
-
* @param fallbackValue alternative value to be used when target throws error.
|
|
322
|
+
* - If `fallback` is a function and it raises exception, it will cause to raise an exception instead of gracefully
|
|
323
|
+
* ignoring it. A fallback of the fallback function is out of the scope of `fallbackIfFails`. However, the fallback
|
|
324
|
+
* function can still be wrapped inside a secondary `fallbackIfFails`. See the "Fallback chaining" example below.
|
|
325
|
+
* - If `target` a promise or async function, `fallback` can be either be a value, a promise or an async function.
|
|
331
326
|
*
|
|
332
327
|
* @returns if func is a promise the return a promise
|
|
333
328
|
*
|
|
@@ -335,7 +330,10 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
335
330
|
* ---
|
|
336
331
|
* @example Async functions
|
|
337
332
|
* Working with async functions or functions that returns a promise
|
|
333
|
+
*
|
|
338
334
|
* ```typescript
|
|
335
|
+
* import { fallbackIfFails } from '@superutils/core'
|
|
336
|
+
*
|
|
339
337
|
* const args = ['some value', true] as const
|
|
340
338
|
* const ensureValue = async (value: string, criteria?: boolean) => {
|
|
341
339
|
* if (criteria !== false && !value.trim()) throw new Error('No value. Should use fallback value')
|
|
@@ -351,7 +349,10 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
351
349
|
*
|
|
352
350
|
* @example Non-async functions
|
|
353
351
|
* Working synchronous function that returns value synchronously
|
|
352
|
+
*
|
|
354
353
|
* ```typescript
|
|
354
|
+
* import { fallbackIfFails } from '@superutils/core'
|
|
355
|
+
*
|
|
355
356
|
* const args = ['some value', true] as const
|
|
356
357
|
* const ensureValue = (value: string, criteria?: boolean) => {
|
|
357
358
|
* if (criteria !== false && !value.trim()) throw new Error('No value. Should use fallback value')
|
|
@@ -367,7 +368,10 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
367
368
|
*
|
|
368
369
|
* @example Hybrid functions
|
|
369
370
|
* Working with function that returns value sync/async circumstantially
|
|
371
|
+
*
|
|
370
372
|
* ```typescript
|
|
373
|
+
* import { fallbackIfFails } from '@superutils/core'
|
|
374
|
+
*
|
|
371
375
|
* const getData = (useCache = true, cacheKey = 'data-cache') => {
|
|
372
376
|
* if (useCache && localStorage[cacheKey]) return localStorage[cacheKey]
|
|
373
377
|
* return fetch('https://my.domain.com/api')
|
|
@@ -382,8 +386,29 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
382
386
|
* // Second call: cache available and will return data synchronously
|
|
383
387
|
* const second = fallbackIfFails(getData, [true], {})
|
|
384
388
|
* ```
|
|
389
|
+
*
|
|
390
|
+
* @example Fallback-chaining: gracefully handle the fallback function
|
|
391
|
+
*
|
|
392
|
+
* ```typescript
|
|
393
|
+
* import { fallbackIfFails } from '@superutils/core'
|
|
394
|
+
*
|
|
395
|
+
* const target = () => {
|
|
396
|
+
* if (new Date().getTime() > 0) throw new Error('I will raise error')
|
|
397
|
+
* }
|
|
398
|
+
* const fallback = () => {
|
|
399
|
+
* throw new Error('I will also raise an exception')
|
|
400
|
+
* }
|
|
401
|
+
* const value = fallbackIfFails(
|
|
402
|
+
* target,
|
|
403
|
+
* [],
|
|
404
|
+
* // this function will only be invoked when
|
|
405
|
+
* () => fallbackIfFails(fallback, [], undefined)
|
|
406
|
+
* )
|
|
407
|
+
*
|
|
408
|
+
* console.log({ value }) // undefined
|
|
409
|
+
* ```
|
|
385
410
|
*/
|
|
386
|
-
declare const fallbackIfFails: <T, TArgs extends unknown[] = unknown[]>(target: T | ((...args: TArgs) => T), args: TArgs | (() => TArgs),
|
|
411
|
+
declare const fallbackIfFails: <T, TArgs extends unknown[] = unknown[]>(target: T | ((...args: TArgs) => T), args: TArgs | (() => TArgs), fallback: IfPromiseAddValue<T> | ((reason: unknown) => IfPromiseAddValue<T>)) => T;
|
|
387
412
|
|
|
388
413
|
/** Check if value is an array */
|
|
389
414
|
declare const isArr: <Item = unknown>(x: unknown) => x is Item[];
|
|
@@ -514,12 +539,16 @@ declare const isMapObj: <K extends PropertyKey, V, T extends Record<K, V>>(x: un
|
|
|
514
539
|
|
|
515
540
|
/** Check if value is an integer */
|
|
516
541
|
declare const isInteger: (x: unknown) => x is number;
|
|
542
|
+
/** Check if value is a valid negative integer */
|
|
543
|
+
declare const isNegativeInteger: (x: unknown) => x is number;
|
|
544
|
+
/** Check if value is a valid negative number */
|
|
545
|
+
declare const isNegativeNumber: (x: unknown) => x is number;
|
|
546
|
+
/** Check if value is a valid finite number */
|
|
547
|
+
declare const isNumber: (x: unknown) => x is number;
|
|
517
548
|
/** Check if value is a positive integer */
|
|
518
549
|
declare const isPositiveInteger: (x: unknown) => x is number;
|
|
519
550
|
/** Check if value is a positive number */
|
|
520
551
|
declare const isPositiveNumber: (x: unknown) => x is number;
|
|
521
|
-
/** Check if value is a valid finite number */
|
|
522
|
-
declare const isNumber: (x: unknown) => x is number;
|
|
523
552
|
|
|
524
553
|
/**
|
|
525
554
|
* Check if value is an object.
|
|
@@ -598,6 +627,15 @@ declare const isRegExp: (x: unknown) => x is RegExp;
|
|
|
598
627
|
declare const isSet: <T = unknown>(x: unknown) => x is Set<T>;
|
|
599
628
|
/** Check if value is string */
|
|
600
629
|
declare const isStr: (x: unknown) => x is string;
|
|
630
|
+
/**
|
|
631
|
+
* Check if value is similar to a RxJS subject with .subscribe & .next functions
|
|
632
|
+
*
|
|
633
|
+
* @param x The value to check
|
|
634
|
+
* @param withValue When `true`, also checks if `value` property exists in `x`
|
|
635
|
+
*
|
|
636
|
+
* @returns `true` if the value is subject-like, `false` otherwise.
|
|
637
|
+
*/
|
|
638
|
+
declare const isSubjectLike: <T>(x: unknown, withValue?: boolean) => boolean;
|
|
601
639
|
/** Check if value is a Symbol */
|
|
602
640
|
declare const isSymbol: (x: unknown) => x is symbol;
|
|
603
641
|
/**
|
|
@@ -625,6 +663,8 @@ declare const is: {
|
|
|
625
663
|
integer: (x: unknown) => x is number;
|
|
626
664
|
map: <TKey = unknown, TValue = unknown>(x: unknown) => x is Map<TKey, TValue>;
|
|
627
665
|
mapObj: <K extends PropertyKey, V, T extends Record<K, V>>(x: unknown, strict?: boolean) => x is Map<K, V>;
|
|
666
|
+
negativeInteger: (x: unknown) => x is number;
|
|
667
|
+
negativeNumber: (x: unknown) => x is number;
|
|
628
668
|
number: (x: unknown) => x is number;
|
|
629
669
|
obj: <T = Record<PropertyKey, unknown>>(x: unknown, strict?: boolean) => x is T;
|
|
630
670
|
positiveInteger: (x: unknown) => x is number;
|
|
@@ -633,6 +673,7 @@ declare const is: {
|
|
|
633
673
|
regExp: (x: unknown) => x is RegExp;
|
|
634
674
|
set: <T = unknown>(x: unknown) => x is Set<T>;
|
|
635
675
|
str: (x: unknown) => x is string;
|
|
676
|
+
subjectLike: <T>(x: unknown, withValue?: boolean) => boolean;
|
|
636
677
|
symbol: (x: unknown) => x is symbol;
|
|
637
678
|
uint8Arr: (arr: unknown) => arr is Uint8Array<ArrayBufferLike>;
|
|
638
679
|
url: (x: unknown) => x is URL;
|
|
@@ -841,8 +882,7 @@ declare const objSetPropUndefined: (...[obj, key, ...args]: Parameters<typeof ob
|
|
|
841
882
|
declare const objSort: <T>(obj: T, recursive?: boolean, _done?: Map<unknown, boolean>) => T;
|
|
842
883
|
|
|
843
884
|
/**
|
|
844
|
-
*
|
|
845
|
-
* @summary constructs a new object excluding specific properties
|
|
885
|
+
* Creates a new object excluding specific properties
|
|
846
886
|
*
|
|
847
887
|
* @param {Object} input
|
|
848
888
|
* @param {Array} keys property names to exclude
|
|
@@ -850,6 +890,27 @@ declare const objSort: <T>(obj: T, recursive?: boolean, _done?: Map<unknown, boo
|
|
|
850
890
|
* Default: a copy of the `input` object
|
|
851
891
|
*
|
|
852
892
|
* @returns {Object}
|
|
893
|
+
*
|
|
894
|
+
* @example Create a new object excluding specific properties
|
|
895
|
+
*
|
|
896
|
+
* ```javascript
|
|
897
|
+
* import { objWithoutKeys } from '@superutils/core'
|
|
898
|
+
*
|
|
899
|
+
* const result = objWithoutKeys({ a: 1, b: '2', c: false }, ['b', 'c'])
|
|
900
|
+
* console.log(result) // { a: 1 }
|
|
901
|
+
* ```
|
|
902
|
+
*
|
|
903
|
+
* @example Copy one object's properties to another while ignoring specific properties
|
|
904
|
+
*
|
|
905
|
+
* ```javascript
|
|
906
|
+
* import { objWithoutKeys } from '@superutils/core'
|
|
907
|
+
*
|
|
908
|
+
* const source = { a: 1, b: '2', c: false }
|
|
909
|
+
* const dest = { d: 4, e: 5 }
|
|
910
|
+
* const result = objWithoutKeys(source, ['b', 'c'], dest)
|
|
911
|
+
* console.log(result) // { d: 4, e: 5, a: 1 }
|
|
912
|
+
* console.log(result === dest) // true
|
|
913
|
+
* ```
|
|
853
914
|
*/
|
|
854
915
|
declare const objWithoutKeys: (input: unknown, keys: string[], output?: Record<PropertyKey, unknown>) => Record<PropertyKey, unknown>;
|
|
855
916
|
|
|
@@ -1182,7 +1243,7 @@ type SliceMapOptions<Data, Value, Key, AsMap extends boolean = false> = {
|
|
|
1182
1243
|
start?: number;
|
|
1183
1244
|
};
|
|
1184
1245
|
/**
|
|
1185
|
-
* Slice an iterable list and map the values into an Array/Map
|
|
1246
|
+
* Slice an iterable list and map the values into an Array/Map.
|
|
1186
1247
|
*
|
|
1187
1248
|
* @param data Array, Map, Set...
|
|
1188
1249
|
* @param options One of the following is required to create a new list:
|
|
@@ -1202,6 +1263,27 @@ type SliceMapOptions<Data, Value, Key, AsMap extends boolean = false> = {
|
|
|
1202
1263
|
* - data: original list
|
|
1203
1264
|
*
|
|
1204
1265
|
* @returns Array/Map
|
|
1266
|
+
*
|
|
1267
|
+
* @example
|
|
1268
|
+
* ```javascript
|
|
1269
|
+
* const data = new Map([
|
|
1270
|
+
* [0, { age: 30, name: 'Alice' }],
|
|
1271
|
+
* [1, { age: 25, name: 'Bob' }],
|
|
1272
|
+
* [2, undefined],
|
|
1273
|
+
* [3, {}],
|
|
1274
|
+
* [4, { age: 35, name: 'Charlie' }],
|
|
1275
|
+
* [5, { age: 28, name: 'Dave' }],
|
|
1276
|
+
* [6, { age: 22, name: 'Eve' }],
|
|
1277
|
+
* ])
|
|
1278
|
+
* const result = sliceMap(data, {
|
|
1279
|
+
* asMap: false, // whether to return the result as a Map
|
|
1280
|
+
* end: 5, // last index (exclusive)
|
|
1281
|
+
* ignoreEmpty: true, // ignore items with no value
|
|
1282
|
+
* start: 1, // first index
|
|
1283
|
+
* })
|
|
1284
|
+
* console.log(result)
|
|
1285
|
+
* // [ { age: 25, name: 'Bob' }, { age: 35, name: 'Charlie' } ]
|
|
1286
|
+
* ```
|
|
1205
1287
|
*/
|
|
1206
1288
|
declare const sliceMap: <Data extends IterableList, Key = Data extends IterableList<infer Key_1, unknown> ? Key_1 : never, Value = Data extends IterableList<unknown, infer Value_1> ? Value_1 : never, AsMap extends boolean = false, Result = AsMap extends false ? Value[] : Map<Key, Value>>(data: Data, options?: SliceMapOptions<Data, Value, Key, AsMap> | SliceMapTransform<Data, Value, Key>) => Result;
|
|
1207
1289
|
|
|
@@ -1317,12 +1399,30 @@ declare const mapJoin: <K, V>(...inputs: (Map<K, V> | [K, V][])[]) => Map<K, V>;
|
|
|
1317
1399
|
*/
|
|
1318
1400
|
declare const randomInt: (min?: number, max?: number) => number;
|
|
1319
1401
|
|
|
1320
|
-
/**
|
|
1402
|
+
/**
|
|
1403
|
+
* Describes a valid finite number type.
|
|
1404
|
+
*/
|
|
1405
|
+
type FiniteNumber<N> = N extends number ? number extends N ? never : N : never;
|
|
1406
|
+
/**
|
|
1407
|
+
* Describes an integer type.
|
|
1408
|
+
*/
|
|
1409
|
+
type Integer<N> = N extends number ? number extends N ? never : `${N}` extends `${string}.${string}` | `.${string}` | `${string}e-${string}` ? never : N : never;
|
|
1410
|
+
/**
|
|
1411
|
+
* Describes a number type with negative integer values.
|
|
1412
|
+
*/
|
|
1413
|
+
type NegativeInteger<N> = N extends number ? `${N}` extends `-${string}` ? `${N}` extends `-${string}.${string}` | `-${string}e-${string}` ? never : N : never : never;
|
|
1414
|
+
/**
|
|
1415
|
+
* Describes a number type with negative values.
|
|
1416
|
+
*/
|
|
1321
1417
|
type NegativeNumber<N> = N extends number ? `${N}` extends `-${string}` ? N : never : never;
|
|
1322
|
-
/**
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
type
|
|
1418
|
+
/**
|
|
1419
|
+
* Describes a number type with positive integer values.
|
|
1420
|
+
*/
|
|
1421
|
+
type PositiveInteger<N> = N extends number ? number extends N ? never : `${N}` extends `-${string}` | '0' | `${string}.${string}` | `${string}e-${string}` ? never : N : never;
|
|
1422
|
+
/**
|
|
1423
|
+
* Describes a number type with positive values.
|
|
1424
|
+
*/
|
|
1425
|
+
type PositiveNumber<N> = N extends number ? number extends N ? never : `${N}` extends `-${string}` | '0' ? never : N : never;
|
|
1326
1426
|
|
|
1327
1427
|
/**
|
|
1328
1428
|
* Clears clutter from strings
|
|
@@ -1386,4 +1486,4 @@ declare const HASH_REGEX: RegExp;
|
|
|
1386
1486
|
*/
|
|
1387
1487
|
declare const strToArr: (value: unknown, seperator?: string) => string[];
|
|
1388
1488
|
|
|
1389
|
-
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
|
|
1489
|
+
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, type FiniteNumber, HASH_REGEX, HEX_REGEX, type IfPromiseAddValue, type Integer, type IsFiniteTuple, type IsOptional, type IterableList, type KeepFirst, type KeepFirstN, type KeepOptionals, type KeepRequired, type MakeOptional, type MinLength, type NegativeInteger, type NegativeNumber, type OptionalIf, type PositiveInteger, type PositiveNumber, 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, clearClutter, copyToClipboard, curry, debounce, deferred, fallbackIfFails, filter, find, 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, isNegativeInteger, isNegativeNumber, isNumber, isObj, isPositiveInteger, isPositiveNumber, isPromise, isRegExp, isSet, isStr, isSubjectLike, 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
|
@@ -48,9 +48,11 @@ var isDate_default = isDate;
|
|
|
48
48
|
|
|
49
49
|
// src/is/isNumber.ts
|
|
50
50
|
var isInteger = (x) => Number.isInteger(x);
|
|
51
|
+
var isNegativeInteger = (x) => Number.isInteger(x) && x < 0;
|
|
52
|
+
var isNegativeNumber = (x) => isNumber(x) && x < 0;
|
|
53
|
+
var isNumber = (x) => typeof x === "number" && !Number.isNaN(x) && Number.isFinite(x);
|
|
51
54
|
var isPositiveInteger = (x) => isInteger(x) && x > 0;
|
|
52
55
|
var isPositiveNumber = (x) => isNumber(x) && x > 0;
|
|
53
|
-
var isNumber = (x) => typeof x == "number" && !Number.isNaN(x) && Number.isFinite(x);
|
|
54
56
|
|
|
55
57
|
// src/is/isEmpty.ts
|
|
56
58
|
var isEmpty = (x, nonNumerable = false, fallback = false) => {
|
|
@@ -149,6 +151,7 @@ var isPromise = (x) => x instanceof Promise;
|
|
|
149
151
|
var isRegExp = (x) => x instanceof RegExp;
|
|
150
152
|
var isSet = (x) => x instanceof Set;
|
|
151
153
|
var isStr = (x) => typeof x === "string";
|
|
154
|
+
var isSubjectLike = (x, withValue = false) => isObj_default(x, false) && isFn_default(x.subscribe) && isFn_default(x.next) && (!withValue || "value" in x);
|
|
152
155
|
var isSymbol = (x) => typeof x === "symbol";
|
|
153
156
|
var is = {
|
|
154
157
|
arr: isArr_default,
|
|
@@ -172,6 +175,8 @@ var is = {
|
|
|
172
175
|
integer: isInteger,
|
|
173
176
|
map: isMap,
|
|
174
177
|
mapObj: isMapObj,
|
|
178
|
+
negativeInteger: isNegativeInteger,
|
|
179
|
+
negativeNumber: isNegativeNumber,
|
|
175
180
|
number: isNumber,
|
|
176
181
|
obj: isObj_default,
|
|
177
182
|
positiveInteger: isPositiveInteger,
|
|
@@ -180,7 +185,7 @@ var is = {
|
|
|
180
185
|
regExp: isRegExp,
|
|
181
186
|
set: isSet,
|
|
182
187
|
str: isStr,
|
|
183
|
-
|
|
188
|
+
subjectLike: isSubjectLike,
|
|
184
189
|
symbol: isSymbol,
|
|
185
190
|
uint8Arr: isUint8Arr,
|
|
186
191
|
url: isUrl_default,
|
|
@@ -188,18 +193,18 @@ var is = {
|
|
|
188
193
|
};
|
|
189
194
|
|
|
190
195
|
// src/fallbackIfFails.ts
|
|
191
|
-
var fallbackIfFails = (target, args,
|
|
192
|
-
let result;
|
|
196
|
+
var fallbackIfFails = (target, args, fallback) => {
|
|
193
197
|
try {
|
|
194
|
-
result = !isFn(target) ? target : target(...isFn(args) ? args() : args);
|
|
198
|
+
const result = !isFn(target) ? target : target(...isFn(args) ? args() : args);
|
|
195
199
|
if (!isPromise(result)) return result;
|
|
196
|
-
return result.catch(
|
|
197
|
-
|
|
198
|
-
|
|
200
|
+
return result.catch(
|
|
201
|
+
(err) => isFn(fallback) ? fallback(err) : fallback
|
|
202
|
+
);
|
|
203
|
+
} catch (err) {
|
|
204
|
+
return isFn(fallback) ? fallback(err) : fallback;
|
|
199
205
|
}
|
|
200
206
|
};
|
|
201
207
|
var fallbackIfFails_default = fallbackIfFails;
|
|
202
|
-
var getAltValCb = (error, fallbackValue) => isFn(fallbackValue) ? fallbackValue(error) : fallbackValue;
|
|
203
208
|
|
|
204
209
|
// src/deferred.ts
|
|
205
210
|
var deferred = (callback, delay = 50, config = {}) => {
|
|
@@ -673,10 +678,11 @@ function matchObjOrProp({
|
|
|
673
678
|
""
|
|
674
679
|
);
|
|
675
680
|
if (value === keyword) return 0;
|
|
676
|
-
if (matchExact) return -1;
|
|
681
|
+
if (matchExact && !isRegExp(keyword)) return -1;
|
|
677
682
|
let valueStr = String(value);
|
|
678
683
|
if (!valueStr.trim()) return -1;
|
|
679
684
|
if (isRegExp(keyword)) return (_b = (_a = valueStr.match(keyword)) == null ? void 0 : _a.index) != null ? _b : -1;
|
|
685
|
+
if (matchExact) return -1;
|
|
680
686
|
if (ignoreCase) valueStr = valueStr.toLowerCase();
|
|
681
687
|
return valueStr.indexOf(String(keyword));
|
|
682
688
|
}
|
|
@@ -947,6 +953,8 @@ export {
|
|
|
947
953
|
isInteger,
|
|
948
954
|
isMap,
|
|
949
955
|
isMapObj,
|
|
956
|
+
isNegativeInteger,
|
|
957
|
+
isNegativeNumber,
|
|
950
958
|
isNumber,
|
|
951
959
|
isObj,
|
|
952
960
|
isPositiveInteger,
|
|
@@ -955,6 +963,7 @@ export {
|
|
|
955
963
|
isRegExp,
|
|
956
964
|
isSet,
|
|
957
965
|
isStr,
|
|
966
|
+
isSubjectLike,
|
|
958
967
|
isSymbol,
|
|
959
968
|
isUint8Arr,
|
|
960
969
|
isUrl,
|
package/package.json
CHANGED