@oscarpalmer/atoms 0.176.0 → 0.178.0

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/dist/index.mjs CHANGED
@@ -16,26 +16,34 @@ function getArrayCallbacks(bool, key, value) {
16
16
  }
17
17
  //#endregion
18
18
  //#region src/internal/array/find.ts
19
- function findValue(type, array, parameters) {
19
+ function findValue(type, array, parameters, reversed) {
20
20
  const findIndex = type === FIND_VALUE_INDEX;
21
21
  if (!Array.isArray(array) || array.length === 0) return findIndex ? -1 : void 0;
22
- const { bool, key, value } = getParameters(parameters);
22
+ const { bool, key, value } = getFindParameters(parameters);
23
23
  const callbacks = getArrayCallbacks(bool, key);
24
24
  if (callbacks?.bool == null && callbacks?.keyed == null) return findIndex ? array.indexOf(value) : array.find((item) => Object.is(item, value));
25
25
  if (callbacks.bool != null) {
26
26
  const index = array.findIndex(callbacks.bool);
27
27
  return findIndex ? index : array[index];
28
28
  }
29
- return findValueInArray(array, callbacks.keyed, value, findIndex);
29
+ return findValueInArray(array, callbacks.keyed, value, findIndex, reversed);
30
30
  }
31
- function findValueInArray(array, callback, value, findIndex) {
31
+ function findValueInArray(array, callback, value, findIndex, reversed) {
32
32
  const { length } = array;
33
33
  for (let index = 0; index < length; index += 1) {
34
- const item = array[index];
34
+ const item = reversed ? array.at(-(index + 1)) : array[index];
35
35
  if (Object.is(callback?.(item, index, array), value)) return findIndex ? index : item;
36
36
  }
37
37
  return findIndex ? -1 : void 0;
38
38
  }
39
+ function findAbsoluteValueOrDefault(array, parameters, defaultValue, useDefaultValue, reversed) {
40
+ if (parameters.length === 0) {
41
+ if (Array.isArray(array) && array.length > 0) return reversed ? array.at(-1) : array[0];
42
+ return useDefaultValue ? defaultValue : void 0;
43
+ }
44
+ const index = findValue(FIND_VALUE_INDEX, array, parameters, reversed);
45
+ return index > -1 ? array[index] : useDefaultValue ? defaultValue : void 0;
46
+ }
39
47
  function findValues(type, array, parameters, mapper) {
40
48
  const result = {
41
49
  matched: [],
@@ -43,13 +51,13 @@ function findValues(type, array, parameters, mapper) {
43
51
  };
44
52
  if (!Array.isArray(array) || array.length === 0) return result;
45
53
  const { length } = array;
46
- const { bool, key, value } = getParameters(parameters);
54
+ const { bool, key, value } = getFindParameters(parameters);
47
55
  const callbacks = getArrayCallbacks(bool, key);
48
56
  if (type === "unique" && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD) {
49
57
  result.matched = [...new Set(array)];
50
58
  return result;
51
59
  }
52
- const mapCallback = typeof mapper === "function" ? mapper : void 0;
60
+ const mapCallback = getArrayCallback(mapper);
53
61
  if (callbacks?.bool != null || type === "all" && key == null) {
54
62
  const callback = callbacks?.bool ?? ((item) => Object.is(item, value));
55
63
  for (let index = 0; index < length; index += 1) {
@@ -70,7 +78,7 @@ function findValues(type, array, parameters, mapper) {
70
78
  }
71
79
  return result;
72
80
  }
73
- function getParameters(original) {
81
+ function getFindParameters(original) {
74
82
  const { length } = original;
75
83
  return {
76
84
  bool: length === 1 && typeof original[0] === "function" ? original[0] : void 0,
@@ -79,7 +87,7 @@ function getParameters(original) {
79
87
  };
80
88
  }
81
89
  const FIND_VALUE_INDEX = "index";
82
- const FIND_VALUE_VALUE = "value";
90
+ const FIND_VALUE_ITEM = "item";
83
91
  const FIND_VALUES_UNIQUE = "unique";
84
92
  const UNIQUE_THRESHOLD = 100;
85
93
  //#endregion
@@ -92,6 +100,15 @@ function removeFiltered(array, ...parameters) {
92
100
  return findValues("all", array, parameters).notMatched;
93
101
  }
94
102
  //#endregion
103
+ //#region src/array/first.ts
104
+ function first(array, ...parameters) {
105
+ return findAbsoluteValueOrDefault(array, parameters, void 0, false, false);
106
+ }
107
+ first.default = firstOrDefault;
108
+ function firstOrDefault(array, defaultValue, ...parameters) {
109
+ return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, false);
110
+ }
111
+ //#endregion
95
112
  //#region src/internal/array/group.ts
96
113
  function groupValues(array, key, value, arrays) {
97
114
  if (!Array.isArray(array) || array.length === 0) return {};
@@ -196,6 +213,71 @@ function isKey(value) {
196
213
  return typeof value === "number" || typeof value === "string";
197
214
  }
198
215
  /**
216
+ * Is the value not an array or a plain object?
217
+ * @param value Value to check
218
+ * @returns `true` if the value is not an array or a plain object, otherwise `false`
219
+ */
220
+ function isNonArrayOrPlainObject(value) {
221
+ return !isArrayOrPlainObject(value);
222
+ }
223
+ /**
224
+ * Is the value not a constructor function?
225
+ * @param value Value to check
226
+ * @returns `true` if the value is not a constructor function, otherwise `false`
227
+ */
228
+ function isNonConstructor(value) {
229
+ return !isConstructor(value);
230
+ }
231
+ /**
232
+ * Is the value not an instance of the constructor?
233
+ * @param constructor Class constructor
234
+ * @param value Value to check
235
+ * @returns `true` if the value is not an instance of the constructor, otherwise `false`
236
+ */
237
+ function isNonInstanceOf(constructor, value) {
238
+ return !isInstanceOf(constructor, value);
239
+ }
240
+ /**
241
+ * Is the value not a key?
242
+ * @param value Value to check
243
+ * @returns `true` if the value is not a `Key` _(`number` or `string`)_, otherwise `false`
244
+ */
245
+ function isNonKey(value) {
246
+ return !isKey(value);
247
+ }
248
+ /**
249
+ * Is the value not a number?
250
+ * @param value Value to check
251
+ * @returns `true` if the value is not a `number`, otherwise `false`
252
+ */
253
+ function isNonNumber(value) {
254
+ return !isNumber(value);
255
+ }
256
+ /**
257
+ * Is the value not a plain object?
258
+ * @param value Value to check
259
+ * @returns `true` if the value is not a plain object, otherwise `false`
260
+ */
261
+ function isNonPlainObject(value) {
262
+ return !isPlainObject(value);
263
+ }
264
+ /**
265
+ * Is the value not a primitive value?
266
+ * @param value Value to check
267
+ * @returns `true` if the value is not a primitive value, otherwise `false`
268
+ */
269
+ function isNonPrimitive(value) {
270
+ return !isPrimitive(value);
271
+ }
272
+ /**
273
+ * Is the value not a typed array?
274
+ * @param value Value to check
275
+ * @returns `true` if the value is not a typed array, otherwise `false`
276
+ */
277
+ function isNonTypedArray(value) {
278
+ return !isTypedArray(value);
279
+ }
280
+ /**
199
281
  * Is the value a number?
200
282
  * @param value Value to check
201
283
  * @returns `true` if the value is a `number`, otherwise `false`
@@ -220,7 +302,17 @@ function isPlainObject(value) {
220
302
  * @returns `true` if the value matches, otherwise `false`
221
303
  */
222
304
  function isPrimitive(value) {
223
- return value == null || EXPRESSION_PRIMITIVE.test(typeof value);
305
+ if (value == null) return true;
306
+ const type = typeof value;
307
+ return type === "bigint" || type === "boolean" || type === "number" || type === "string" || type === "symbol";
308
+ }
309
+ /**
310
+ * Is the value a template strings array?
311
+ * @param value Value to check
312
+ * @returns `true` if the value is a `TemplateStringsArray`, otherwise `false`
313
+ */
314
+ function isTemplateStringsArray(value) {
315
+ return Array.isArray(value) && Array.isArray(value.raw);
224
316
  }
225
317
  /**
226
318
  * Is the value a typed array?
@@ -228,7 +320,7 @@ function isPrimitive(value) {
228
320
  * @returns `true` if the value is a typed array, otherwise `false`
229
321
  */
230
322
  function isTypedArray(value) {
231
- TYPED_ARRAYS ??= new Set([
323
+ isTypedArray.types ??= new Set([
232
324
  Int8Array,
233
325
  Uint8Array,
234
326
  Uint8ClampedArray,
@@ -241,10 +333,8 @@ function isTypedArray(value) {
241
333
  BigInt64Array,
242
334
  BigUint64Array
243
335
  ]);
244
- return TYPED_ARRAYS.has(value?.constructor);
336
+ return isTypedArray.types.has(value?.constructor);
245
337
  }
246
- const EXPRESSION_PRIMITIVE = /^(bigint|boolean|number|string|symbol)$/;
247
- let TYPED_ARRAYS;
248
338
  //#endregion
249
339
  //#region src/internal/random.ts
250
340
  function _getRandomFloat(inclusive, minimum, maximum) {
@@ -329,12 +419,12 @@ function difference(first, second, key) {
329
419
  //#region src/array/exists.ts
330
420
  function exists(array, ...parameters) {
331
421
  if (parameters.length === 1 && typeof parameters[0] !== "function") return Array.isArray(array) ? array.includes(parameters[0]) : false;
332
- return findValue(FIND_VALUE_INDEX, array, parameters) > -1;
422
+ return findValue(FIND_VALUE_INDEX, array, parameters, false) > -1;
333
423
  }
334
424
  //#endregion
335
425
  //#region src/array/find.ts
336
426
  function find(array, ...parameters) {
337
- return findValue(FIND_VALUE_VALUE, array, parameters);
427
+ return findValue(FIND_VALUE_ITEM, array, parameters, false);
338
428
  }
339
429
  //#endregion
340
430
  //#region src/array/flatten.ts
@@ -491,11 +581,34 @@ function push(array, pushed) {
491
581
  return insertValues(INSERT_TYPE_PUSH, array, pushed, array.length, 0);
492
582
  }
493
583
  //#endregion
584
+ //#region src/array/reverse.ts
585
+ function reverse(array) {
586
+ if (!Array.isArray(array)) return [];
587
+ const { length } = array;
588
+ if (length < 2) return array;
589
+ const half = Math.floor(length / 2);
590
+ for (let firstIndex = 0; firstIndex < half; firstIndex += 1) {
591
+ const temporaryItem = array[firstIndex];
592
+ const secondIndex = length - 1 - firstIndex;
593
+ array[firstIndex] = array[secondIndex];
594
+ array[secondIndex] = temporaryItem;
595
+ }
596
+ return array;
597
+ }
598
+ //#endregion
494
599
  //#region src/array/select.ts
495
600
  function select(array, ...parameters) {
496
601
  return findValues("all", array, parameters, parameters.pop()).matched;
497
602
  }
498
603
  //#endregion
604
+ //#region src/array/single.ts
605
+ function single(array, ...parameters) {
606
+ const { matched } = findValues("all", array, parameters);
607
+ if (matched.length > 1) throw new Error(MESSAGE);
608
+ return matched[0];
609
+ }
610
+ const MESSAGE = "Multiple items were found";
611
+ //#endregion
499
612
  //#region src/array/slice.ts
500
613
  function drop(array, first, second) {
501
614
  return extract(EXTRACT_DROP, array, first, second);
@@ -606,6 +719,15 @@ function update(array, values, key) {
606
719
  return updateInArray(array, values, key, true);
607
720
  }
608
721
  //#endregion
722
+ //#region src/array/last.ts
723
+ function last(array, ...parameters) {
724
+ return findAbsoluteValueOrDefault(array, parameters, void 0, false, true);
725
+ }
726
+ last.default = lastOrDefault;
727
+ function lastOrDefault(array, defaultValue, ...parameters) {
728
+ return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, true);
729
+ }
730
+ //#endregion
609
731
  //#region src/internal/array/overlap.ts
610
732
  function arraysOverlap(first, second) {
611
733
  const firstArray = first.index < second.index ? first.array : second.array;
@@ -2195,6 +2317,30 @@ let memoizedCapitalize;
2195
2317
  let memoizedTitleCase;
2196
2318
  //#endregion
2197
2319
  //#region src/string/index.ts
2320
+ function dedent(value, ...values) {
2321
+ let actual;
2322
+ if (isTemplateStringsArray(value)) actual = interpolate(value, values);
2323
+ else actual = value;
2324
+ if (typeof actual !== "string") return "";
2325
+ const lines = actual.split("\n");
2326
+ const { length } = lines;
2327
+ if (length === 1) return actual.trim();
2328
+ const lastIndex = length - 1;
2329
+ const lengths = [];
2330
+ for (let index = 0; index < length; index += 1) {
2331
+ const [, indentation] = /^(\s+)/.exec(lines[index]) ?? [];
2332
+ if (indentation != null) lengths.push(indentation.length);
2333
+ }
2334
+ if (lengths.length === 0) return actual.trim();
2335
+ const minimum = Math.min(...lengths);
2336
+ const pattern = new RegExp(`^\\s{0,${minimum}}`);
2337
+ let result = "";
2338
+ for (let index = 0; index < length; index += 1) {
2339
+ const line = lines[index];
2340
+ result += line.replace(pattern, "") + (index === lastIndex ? "" : "\n");
2341
+ }
2342
+ return result.trim();
2343
+ }
2198
2344
  /**
2199
2345
  * Get a new UUID-string _(version 4)_
2200
2346
  * @returns UUID string
@@ -2213,6 +2359,12 @@ function getUuid() {
2213
2359
  hex.substring(20, 32)
2214
2360
  ].join("-");
2215
2361
  }
2362
+ function interpolate(strings, values) {
2363
+ const { length } = strings;
2364
+ let interpolated = "";
2365
+ for (let index = 0; index < length; index += 1) interpolated += `${strings[index]}${getString(values[index])}`;
2366
+ return interpolated;
2367
+ }
2216
2368
  /**
2217
2369
  * Parse a JSON string into its proper value _(or `undefined` if it fails)_
2218
2370
  * @param value JSON string to parse
@@ -3600,6 +3752,14 @@ function isEmpty(value) {
3600
3752
  return true;
3601
3753
  }
3602
3754
  /**
3755
+ * Is the value not empty, or holding non-empty values?
3756
+ * @param value Value to check
3757
+ * @returns `true` if the value is not considered empty, otherwise `false`
3758
+ */
3759
+ function isNonEmpty(value) {
3760
+ return !isEmpty(value);
3761
+ }
3762
+ /**
3603
3763
  * Is the value not `undefined` or `null`?
3604
3764
  * @param value Value to check
3605
3765
  * @returns `true` if the value is not `undefined` or `null`, otherwise `false`
@@ -3608,6 +3768,38 @@ function isNonNullable(value) {
3608
3768
  return value != null;
3609
3769
  }
3610
3770
  /**
3771
+ * Is the value not `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
3772
+ * @param value Value to check
3773
+ * @returns `true` if the value is not `undefined`, `null`, or matches an empty string, otherwise `false`
3774
+ */
3775
+ function isNonNullableOrEmpty(value) {
3776
+ return value != null && getString(value) !== "";
3777
+ }
3778
+ /**
3779
+ * Is the value not `undefined`, `null`, or stringified as a whitespace-only string?
3780
+ * @param value Value to check
3781
+ * @returns `true` if the value is not `undefined`, `null`, or matches a whitespace-only string, otherwise `false`
3782
+ */
3783
+ function isNonNullableOrWhitespace(value) {
3784
+ return value != null && !EXPRESSION_WHITESPACE.test(getString(value));
3785
+ }
3786
+ /**
3787
+ * Is the value not a number or a number-like string?
3788
+ * @param value Value to check
3789
+ * @returns `true` if the value is not a number or a number-like string, otherwise `false`
3790
+ */
3791
+ function isNonNumerical(value) {
3792
+ return !isNumerical(value);
3793
+ }
3794
+ /**
3795
+ * Is the value not an object _(or function)_?
3796
+ * @param value Value to check
3797
+ * @returns `true` if the value is not an object, otherwise `false`
3798
+ */
3799
+ function isNonObject(value) {
3800
+ return !isObject(value);
3801
+ }
3802
+ /**
3611
3803
  * Is the value `undefined` or `null`?
3612
3804
  * @param value Value to check
3613
3805
  * @returns `true` if the value is `undefined` or `null`, otherwise `false`
@@ -3616,17 +3808,17 @@ function isNullable(value) {
3616
3808
  return value == null;
3617
3809
  }
3618
3810
  /**
3619
- * Is the value `undefined`, `null`, or an empty _(no whitespace)_ string?
3811
+ * Is the value `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
3620
3812
  * @param value Value to check
3621
- * @returns `true` if the value is nullable or an empty string, otherwise `false`
3813
+ * @returns `true` if the value is nullable or matches an empty string, otherwise `false`
3622
3814
  */
3623
3815
  function isNullableOrEmpty(value) {
3624
3816
  return value == null || getString(value) === "";
3625
3817
  }
3626
3818
  /**
3627
- * Is the value `undefined`, `null`, or a whitespace-only string?
3819
+ * Is the value `undefined`, `null`, or stringified as a whitespace-only string?
3628
3820
  * @param value Value to check
3629
- * @returns `true` if the value is nullable or a whitespace-only string, otherwise `false`
3821
+ * @returns `true` if the value is nullable or matches a whitespace-only string, otherwise `false`
3630
3822
  */
3631
3823
  function isNullableOrWhitespace(value) {
3632
3824
  return value == null || EXPRESSION_WHITESPACE.test(getString(value));
@@ -3634,7 +3826,7 @@ function isNullableOrWhitespace(value) {
3634
3826
  /**
3635
3827
  * Is the value a number or a number-like string?
3636
3828
  * @param value Value to check
3637
- * @returns `true` if the value is a number or a parseable string, otherwise `false`
3829
+ * @returns `true` if the value is a number or a number-like string, otherwise `false`
3638
3830
  */
3639
3831
  function isNumerical(value) {
3640
3832
  return isNumber(value) || typeof value === "string" && value.trim().length > 0 && !Number.isNaN(+value);
@@ -4608,4 +4800,4 @@ var SizedSet = class extends Set {
4608
4800
  }
4609
4801
  };
4610
4802
  //#endregion
4611
- export { CancelablePromise, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError, QueueError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, assert, attempt, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, difference, drop, endsWith, endsWithArray, equal, error, exists, filter, find, flatten, floor, flow, fromQuery, toPromise as fromResult, toPromise, getArray, getArrayPosition, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getTimedPromise, getUuid, getValue, groupBy, handleResult, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, matchResult, max, median, memoize, merge, min, move, noop, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, settlePromise, shuffle, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toQuery, toRecord, toResult, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
4803
+ export { CancelablePromise, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError, QueueError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, assert, attempt, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, dedent, delay, diff, difference, drop, endsWith, endsWithArray, equal, error, exists, filter, find, first, flatten, floor, flow, fromQuery, toPromise as fromResult, toPromise, getArray, getArrayPosition, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getTimedPromise, getUuid, getValue, groupBy, handleResult, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, last, logger, lowerCase, matchResult, max, median, memoize, merge, min, move, noop, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, settlePromise, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toQuery, toRecord, toResult, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
@@ -1,15 +1,22 @@
1
1
  //#region src/internal/array/find.d.ts
2
- type FindValueType = 'index' | 'value';
2
+ type FindValueType = 'index' | 'item';
3
3
  type FindValuesResult = {
4
4
  matched: unknown[];
5
5
  notMatched: unknown[];
6
6
  };
7
7
  type FindValuesType = 'all' | 'unique';
8
- declare function findValue(type: FindValueType, array: unknown[], parameters: unknown[]): unknown;
8
+ type Parameters = {
9
+ bool?: unknown;
10
+ key?: unknown;
11
+ value?: unknown;
12
+ };
13
+ declare function findValue(type: FindValueType, array: unknown[], parameters: unknown[], reversed: boolean): unknown;
14
+ declare function findAbsoluteValueOrDefault(array: unknown[], parameters: unknown[], defaultValue: unknown, useDefaultValue: boolean, reversed: boolean): unknown;
9
15
  declare function findValues(type: FindValuesType, array: unknown[], parameters: unknown[], mapper?: unknown): FindValuesResult;
16
+ declare function getFindParameters(original: unknown[]): Parameters;
10
17
  declare const FIND_VALUE_INDEX: FindValueType;
11
- declare const FIND_VALUE_VALUE: FindValueType;
18
+ declare const FIND_VALUE_ITEM: FindValueType;
12
19
  declare const FIND_VALUES_ALL: FindValuesType;
13
20
  declare const FIND_VALUES_UNIQUE: FindValuesType;
14
21
  //#endregion
15
- export { FIND_VALUES_ALL, FIND_VALUES_UNIQUE, FIND_VALUE_INDEX, FIND_VALUE_VALUE, findValue, findValues };
22
+ export { FIND_VALUES_ALL, FIND_VALUES_UNIQUE, FIND_VALUE_INDEX, FIND_VALUE_ITEM, findAbsoluteValueOrDefault, findValue, findValues, getFindParameters };
@@ -1,25 +1,33 @@
1
- import { getArrayCallbacks } from "./callbacks.mjs";
1
+ import { getArrayCallback, getArrayCallbacks } from "./callbacks.mjs";
2
2
  //#region src/internal/array/find.ts
3
- function findValue(type, array, parameters) {
3
+ function findValue(type, array, parameters, reversed) {
4
4
  const findIndex = type === FIND_VALUE_INDEX;
5
5
  if (!Array.isArray(array) || array.length === 0) return findIndex ? -1 : void 0;
6
- const { bool, key, value } = getParameters(parameters);
6
+ const { bool, key, value } = getFindParameters(parameters);
7
7
  const callbacks = getArrayCallbacks(bool, key);
8
8
  if (callbacks?.bool == null && callbacks?.keyed == null) return findIndex ? array.indexOf(value) : array.find((item) => Object.is(item, value));
9
9
  if (callbacks.bool != null) {
10
10
  const index = array.findIndex(callbacks.bool);
11
11
  return findIndex ? index : array[index];
12
12
  }
13
- return findValueInArray(array, callbacks.keyed, value, findIndex);
13
+ return findValueInArray(array, callbacks.keyed, value, findIndex, reversed);
14
14
  }
15
- function findValueInArray(array, callback, value, findIndex) {
15
+ function findValueInArray(array, callback, value, findIndex, reversed) {
16
16
  const { length } = array;
17
17
  for (let index = 0; index < length; index += 1) {
18
- const item = array[index];
18
+ const item = reversed ? array.at(-(index + 1)) : array[index];
19
19
  if (Object.is(callback?.(item, index, array), value)) return findIndex ? index : item;
20
20
  }
21
21
  return findIndex ? -1 : void 0;
22
22
  }
23
+ function findAbsoluteValueOrDefault(array, parameters, defaultValue, useDefaultValue, reversed) {
24
+ if (parameters.length === 0) {
25
+ if (Array.isArray(array) && array.length > 0) return reversed ? array.at(-1) : array[0];
26
+ return useDefaultValue ? defaultValue : void 0;
27
+ }
28
+ const index = findValue(FIND_VALUE_INDEX, array, parameters, reversed);
29
+ return index > -1 ? array[index] : useDefaultValue ? defaultValue : void 0;
30
+ }
23
31
  function findValues(type, array, parameters, mapper) {
24
32
  const result = {
25
33
  matched: [],
@@ -27,13 +35,13 @@ function findValues(type, array, parameters, mapper) {
27
35
  };
28
36
  if (!Array.isArray(array) || array.length === 0) return result;
29
37
  const { length } = array;
30
- const { bool, key, value } = getParameters(parameters);
38
+ const { bool, key, value } = getFindParameters(parameters);
31
39
  const callbacks = getArrayCallbacks(bool, key);
32
40
  if (type === "unique" && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD) {
33
41
  result.matched = [...new Set(array)];
34
42
  return result;
35
43
  }
36
- const mapCallback = typeof mapper === "function" ? mapper : void 0;
44
+ const mapCallback = getArrayCallback(mapper);
37
45
  if (callbacks?.bool != null || type === "all" && key == null) {
38
46
  const callback = callbacks?.bool ?? ((item) => Object.is(item, value));
39
47
  for (let index = 0; index < length; index += 1) {
@@ -54,7 +62,7 @@ function findValues(type, array, parameters, mapper) {
54
62
  }
55
63
  return result;
56
64
  }
57
- function getParameters(original) {
65
+ function getFindParameters(original) {
58
66
  const { length } = original;
59
67
  return {
60
68
  bool: length === 1 && typeof original[0] === "function" ? original[0] : void 0,
@@ -63,9 +71,9 @@ function getParameters(original) {
63
71
  };
64
72
  }
65
73
  const FIND_VALUE_INDEX = "index";
66
- const FIND_VALUE_VALUE = "value";
74
+ const FIND_VALUE_ITEM = "item";
67
75
  const FIND_VALUES_ALL = "all";
68
76
  const FIND_VALUES_UNIQUE = "unique";
69
77
  const UNIQUE_THRESHOLD = 100;
70
78
  //#endregion
71
- export { FIND_VALUES_ALL, FIND_VALUES_UNIQUE, FIND_VALUE_INDEX, FIND_VALUE_VALUE, findValue, findValues };
79
+ export { FIND_VALUES_ALL, FIND_VALUES_UNIQUE, FIND_VALUE_INDEX, FIND_VALUE_ITEM, findAbsoluteValueOrDefault, findValue, findValues, getFindParameters };
@@ -26,6 +26,61 @@ declare function isInstanceOf<Instance>(constructor: Constructor<Instance>, valu
26
26
  * @returns `true` if the value is a `Key` _(`number` or `string`)_, otherwise `false`
27
27
  */
28
28
  declare function isKey(value: unknown): value is Key;
29
+ /**
30
+ * Is the value not an array or a plain object?
31
+ * @param value Value to check
32
+ * @returns `true` if the value is not an array or a plain object, otherwise `false`
33
+ */
34
+ declare function isNonArrayOrPlainObject<Value>(value: Value): value is Exclude<Value, ArrayOrPlainObject>;
35
+ /**
36
+ * Is the value not a constructor function?
37
+ * @param value Value to check
38
+ * @returns `true` if the value is not a constructor function, otherwise `false`
39
+ */
40
+ declare function isNonConstructor<Value>(value: Value): value is Exclude<Value, Constructor>;
41
+ /**
42
+ * Is the value not an instance of the constructor?
43
+ * @param constructor Class constructor
44
+ * @param value Value to check
45
+ * @returns `true` if the value is not an instance of the constructor, otherwise `false`
46
+ */
47
+ declare function isNonInstanceOf<Instance, Value>(constructor: Constructor<Instance>, value: Value): value is Exclude<Value, Instance>;
48
+ /**
49
+ * Is the value not a key?
50
+ * @param value Value to check
51
+ * @returns `true` if the value is not a `Key` _(`number` or `string`)_, otherwise `false`
52
+ */
53
+ declare function isNonKey<Value>(value: Value): value is Exclude<Value, Key>;
54
+ /**
55
+ * Is the value not a number?
56
+ * @param value Value to check
57
+ * @returns `true` if the value is not a `number`, otherwise `false`
58
+ */
59
+ declare function isNonNumber<Value>(value: Value): value is Exclude<Value, number>;
60
+ /**
61
+ * Is the value not a plain object?
62
+ * @param value Value to check
63
+ * @returns `true` if the value is not a plain object, otherwise `false`
64
+ */
65
+ declare function isNonPlainObject<Value>(value: Value): value is Exclude<Value, PlainObject>;
66
+ /**
67
+ * Is the value not a primitive value?
68
+ * @param value Value to check
69
+ * @returns `true` if the value is not a primitive value, otherwise `false`
70
+ */
71
+ declare function isNonPrimitive<Value>(value: Value): value is Exclude<Value, Primitive>;
72
+ /**
73
+ * Is the value not a template strings array?
74
+ * @param value Value to check
75
+ * @returns `true` if the value is not a `TemplateStringsArray`, otherwise `false`
76
+ */
77
+ declare function isNonTemplateStringsArray<Value>(value: Value): value is Exclude<Value, TemplateStringsArray>;
78
+ /**
79
+ * Is the value not a typed array?
80
+ * @param value Value to check
81
+ * @returns `true` if the value is not a typed array, otherwise `false`
82
+ */
83
+ declare function isNonTypedArray<Value>(value: Value): value is Exclude<Value, TypedArray>;
29
84
  /**
30
85
  * Is the value a number?
31
86
  * @param value Value to check
@@ -44,6 +99,12 @@ declare function isPlainObject(value: unknown): value is PlainObject;
44
99
  * @returns `true` if the value matches, otherwise `false`
45
100
  */
46
101
  declare function isPrimitive(value: unknown): value is Primitive;
102
+ /**
103
+ * Is the value a template strings array?
104
+ * @param value Value to check
105
+ * @returns `true` if the value is a `TemplateStringsArray`, otherwise `false`
106
+ */
107
+ declare function isTemplateStringsArray(value: unknown): value is TemplateStringsArray;
47
108
  /**
48
109
  * Is the value a typed array?
49
110
  * @param value Value to check
@@ -51,4 +112,4 @@ declare function isPrimitive(value: unknown): value is Primitive;
51
112
  */
52
113
  declare function isTypedArray(value: unknown): value is TypedArray;
53
114
  //#endregion
54
- export { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNumber, isPlainObject, isPrimitive, isTypedArray };
115
+ export { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonInstanceOf, isNonKey, isNonNumber, isNonPlainObject, isNonPrimitive, isNonTemplateStringsArray, isNonTypedArray, isNumber, isPlainObject, isPrimitive, isTemplateStringsArray, isTypedArray };