@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.
@@ -33,6 +33,79 @@ function isKey(value) {
33
33
  return typeof value === "number" || typeof value === "string";
34
34
  }
35
35
  /**
36
+ * Is the value not an array or a plain object?
37
+ * @param value Value to check
38
+ * @returns `true` if the value is not an array or a plain object, otherwise `false`
39
+ */
40
+ function isNonArrayOrPlainObject(value) {
41
+ return !isArrayOrPlainObject(value);
42
+ }
43
+ /**
44
+ * Is the value not a constructor function?
45
+ * @param value Value to check
46
+ * @returns `true` if the value is not a constructor function, otherwise `false`
47
+ */
48
+ function isNonConstructor(value) {
49
+ return !isConstructor(value);
50
+ }
51
+ /**
52
+ * Is the value not an instance of the constructor?
53
+ * @param constructor Class constructor
54
+ * @param value Value to check
55
+ * @returns `true` if the value is not an instance of the constructor, otherwise `false`
56
+ */
57
+ function isNonInstanceOf(constructor, value) {
58
+ return !isInstanceOf(constructor, value);
59
+ }
60
+ /**
61
+ * Is the value not a key?
62
+ * @param value Value to check
63
+ * @returns `true` if the value is not a `Key` _(`number` or `string`)_, otherwise `false`
64
+ */
65
+ function isNonKey(value) {
66
+ return !isKey(value);
67
+ }
68
+ /**
69
+ * Is the value not a number?
70
+ * @param value Value to check
71
+ * @returns `true` if the value is not a `number`, otherwise `false`
72
+ */
73
+ function isNonNumber(value) {
74
+ return !isNumber(value);
75
+ }
76
+ /**
77
+ * Is the value not a plain object?
78
+ * @param value Value to check
79
+ * @returns `true` if the value is not a plain object, otherwise `false`
80
+ */
81
+ function isNonPlainObject(value) {
82
+ return !isPlainObject(value);
83
+ }
84
+ /**
85
+ * Is the value not a primitive value?
86
+ * @param value Value to check
87
+ * @returns `true` if the value is not a primitive value, otherwise `false`
88
+ */
89
+ function isNonPrimitive(value) {
90
+ return !isPrimitive(value);
91
+ }
92
+ /**
93
+ * Is the value not a template strings array?
94
+ * @param value Value to check
95
+ * @returns `true` if the value is not a `TemplateStringsArray`, otherwise `false`
96
+ */
97
+ function isNonTemplateStringsArray(value) {
98
+ return !isTemplateStringsArray(value);
99
+ }
100
+ /**
101
+ * Is the value not a typed array?
102
+ * @param value Value to check
103
+ * @returns `true` if the value is not a typed array, otherwise `false`
104
+ */
105
+ function isNonTypedArray(value) {
106
+ return !isTypedArray(value);
107
+ }
108
+ /**
36
109
  * Is the value a number?
37
110
  * @param value Value to check
38
111
  * @returns `true` if the value is a `number`, otherwise `false`
@@ -57,7 +130,17 @@ function isPlainObject(value) {
57
130
  * @returns `true` if the value matches, otherwise `false`
58
131
  */
59
132
  function isPrimitive(value) {
60
- return value == null || EXPRESSION_PRIMITIVE.test(typeof value);
133
+ if (value == null) return true;
134
+ const type = typeof value;
135
+ return type === "bigint" || type === "boolean" || type === "number" || type === "string" || type === "symbol";
136
+ }
137
+ /**
138
+ * Is the value a template strings array?
139
+ * @param value Value to check
140
+ * @returns `true` if the value is a `TemplateStringsArray`, otherwise `false`
141
+ */
142
+ function isTemplateStringsArray(value) {
143
+ return Array.isArray(value) && Array.isArray(value.raw);
61
144
  }
62
145
  /**
63
146
  * Is the value a typed array?
@@ -65,7 +148,7 @@ function isPrimitive(value) {
65
148
  * @returns `true` if the value is a typed array, otherwise `false`
66
149
  */
67
150
  function isTypedArray(value) {
68
- TYPED_ARRAYS ??= new Set([
151
+ isTypedArray.types ??= new Set([
69
152
  Int8Array,
70
153
  Uint8Array,
71
154
  Uint8ClampedArray,
@@ -78,9 +161,7 @@ function isTypedArray(value) {
78
161
  BigInt64Array,
79
162
  BigUint64Array
80
163
  ]);
81
- return TYPED_ARRAYS.has(value?.constructor);
164
+ return isTypedArray.types.has(value?.constructor);
82
165
  }
83
- const EXPRESSION_PRIMITIVE = /^(bigint|boolean|number|string|symbol)$/;
84
- let TYPED_ARRAYS;
85
166
  //#endregion
86
- export { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNumber, isPlainObject, isPrimitive, isTypedArray };
167
+ export { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonInstanceOf, isNonKey, isNonNumber, isNonPlainObject, isNonPrimitive, isNonTemplateStringsArray, isNonTypedArray, isNumber, isPlainObject, isPrimitive, isTemplateStringsArray, isTypedArray };
package/dist/is.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNumber, isPlainObject, isPrimitive, isTypedArray } from "./internal/is.mjs";
1
+ import { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonInstanceOf, isNonKey, isNonNumber, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNumber, isPlainObject, isPrimitive, isTypedArray } from "./internal/is.mjs";
2
2
 
3
3
  //#region src/is.d.ts
4
4
  /**
@@ -7,12 +7,42 @@ import { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNumber, isP
7
7
  * @returns `true` if the value is considered empty, otherwise `false`
8
8
  */
9
9
  declare function isEmpty(value: unknown): boolean;
10
+ /**
11
+ * Is the value not empty, or holding non-empty values?
12
+ * @param value Value to check
13
+ * @returns `true` if the value is not considered empty, otherwise `false`
14
+ */
15
+ declare function isNonEmpty(value: unknown): boolean;
10
16
  /**
11
17
  * Is the value not `undefined` or `null`?
12
18
  * @param value Value to check
13
19
  * @returns `true` if the value is not `undefined` or `null`, otherwise `false`
14
20
  */
15
- declare function isNonNullable(value: unknown): value is Exclude<unknown, undefined | null>;
21
+ declare function isNonNullable<Value>(value: Value): value is Exclude<Value, undefined | null>;
22
+ /**
23
+ * Is the value not `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
24
+ * @param value Value to check
25
+ * @returns `true` if the value is not `undefined`, `null`, or matches an empty string, otherwise `false`
26
+ */
27
+ declare function isNonNullableOrEmpty<Value>(value: Value): value is Exclude<Value, undefined | null | ''>;
28
+ /**
29
+ * Is the value not `undefined`, `null`, or stringified as a whitespace-only string?
30
+ * @param value Value to check
31
+ * @returns `true` if the value is not `undefined`, `null`, or matches a whitespace-only string, otherwise `false`
32
+ */
33
+ declare function isNonNullableOrWhitespace<Value>(value: Value): value is Exclude<Value, undefined | null | ''>;
34
+ /**
35
+ * Is the value not a number or a number-like string?
36
+ * @param value Value to check
37
+ * @returns `true` if the value is not a number or a number-like string, otherwise `false`
38
+ */
39
+ declare function isNonNumerical<Value>(value: Value): value is Exclude<Value, number | `${number}`>;
40
+ /**
41
+ * Is the value not an object _(or function)_?
42
+ * @param value Value to check
43
+ * @returns `true` if the value is not an object, otherwise `false`
44
+ */
45
+ declare function isNonObject<Value>(value: Value): value is Exclude<Value, object>;
16
46
  /**
17
47
  * Is the value `undefined` or `null`?
18
48
  * @param value Value to check
@@ -20,21 +50,21 @@ declare function isNonNullable(value: unknown): value is Exclude<unknown, undefi
20
50
  */
21
51
  declare function isNullable(value: unknown): value is undefined | null;
22
52
  /**
23
- * Is the value `undefined`, `null`, or an empty _(no whitespace)_ string?
53
+ * Is the value `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
24
54
  * @param value Value to check
25
- * @returns `true` if the value is nullable or an empty string, otherwise `false`
55
+ * @returns `true` if the value is nullable or matches an empty string, otherwise `false`
26
56
  */
27
57
  declare function isNullableOrEmpty(value: unknown): value is undefined | null | '';
28
58
  /**
29
- * Is the value `undefined`, `null`, or a whitespace-only string?
59
+ * Is the value `undefined`, `null`, or stringified as a whitespace-only string?
30
60
  * @param value Value to check
31
- * @returns `true` if the value is nullable or a whitespace-only string, otherwise `false`
61
+ * @returns `true` if the value is nullable or matches a whitespace-only string, otherwise `false`
32
62
  */
33
63
  declare function isNullableOrWhitespace(value: unknown): value is undefined | null | '';
34
64
  /**
35
65
  * Is the value a number or a number-like string?
36
66
  * @param value Value to check
37
- * @returns `true` if the value is a number or a parseable string, otherwise `false`
67
+ * @returns `true` if the value is a number or a number-like string, otherwise `false`
38
68
  */
39
69
  declare function isNumerical(value: unknown): value is number | `${number}`;
40
70
  /**
@@ -44,4 +74,4 @@ declare function isNumerical(value: unknown): value is number | `${number}`;
44
74
  */
45
75
  declare function isObject(value: unknown): value is object;
46
76
  //#endregion
47
- export { isArrayOrPlainObject, isConstructor, isEmpty, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
77
+ export { isArrayOrPlainObject, isConstructor, isEmpty, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
package/dist/is.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNumber, isPlainObject, isPrimitive, isTypedArray } from "./internal/is.mjs";
1
+ import { isArrayOrPlainObject, isConstructor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonInstanceOf, isNonKey, isNonNumber, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNumber, isPlainObject, isPrimitive, isTypedArray } from "./internal/is.mjs";
2
2
  import { getArray } from "./array/get.mjs";
3
3
  import { getString } from "./internal/string.mjs";
4
4
  //#region src/is.ts
@@ -16,6 +16,14 @@ function isEmpty(value) {
16
16
  return true;
17
17
  }
18
18
  /**
19
+ * Is the value not empty, or holding non-empty values?
20
+ * @param value Value to check
21
+ * @returns `true` if the value is not considered empty, otherwise `false`
22
+ */
23
+ function isNonEmpty(value) {
24
+ return !isEmpty(value);
25
+ }
26
+ /**
19
27
  * Is the value not `undefined` or `null`?
20
28
  * @param value Value to check
21
29
  * @returns `true` if the value is not `undefined` or `null`, otherwise `false`
@@ -24,6 +32,38 @@ function isNonNullable(value) {
24
32
  return value != null;
25
33
  }
26
34
  /**
35
+ * Is the value not `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
36
+ * @param value Value to check
37
+ * @returns `true` if the value is not `undefined`, `null`, or matches an empty string, otherwise `false`
38
+ */
39
+ function isNonNullableOrEmpty(value) {
40
+ return value != null && getString(value) !== "";
41
+ }
42
+ /**
43
+ * Is the value not `undefined`, `null`, or stringified as a whitespace-only string?
44
+ * @param value Value to check
45
+ * @returns `true` if the value is not `undefined`, `null`, or matches a whitespace-only string, otherwise `false`
46
+ */
47
+ function isNonNullableOrWhitespace(value) {
48
+ return value != null && !EXPRESSION_WHITESPACE.test(getString(value));
49
+ }
50
+ /**
51
+ * Is the value not a number or a number-like string?
52
+ * @param value Value to check
53
+ * @returns `true` if the value is not a number or a number-like string, otherwise `false`
54
+ */
55
+ function isNonNumerical(value) {
56
+ return !isNumerical(value);
57
+ }
58
+ /**
59
+ * Is the value not an object _(or function)_?
60
+ * @param value Value to check
61
+ * @returns `true` if the value is not an object, otherwise `false`
62
+ */
63
+ function isNonObject(value) {
64
+ return !isObject(value);
65
+ }
66
+ /**
27
67
  * Is the value `undefined` or `null`?
28
68
  * @param value Value to check
29
69
  * @returns `true` if the value is `undefined` or `null`, otherwise `false`
@@ -32,17 +72,17 @@ function isNullable(value) {
32
72
  return value == null;
33
73
  }
34
74
  /**
35
- * Is the value `undefined`, `null`, or an empty _(no whitespace)_ string?
75
+ * Is the value `undefined`, `null`, or stringified as an empty _(no whitespace)_ string?
36
76
  * @param value Value to check
37
- * @returns `true` if the value is nullable or an empty string, otherwise `false`
77
+ * @returns `true` if the value is nullable or matches an empty string, otherwise `false`
38
78
  */
39
79
  function isNullableOrEmpty(value) {
40
80
  return value == null || getString(value) === "";
41
81
  }
42
82
  /**
43
- * Is the value `undefined`, `null`, or a whitespace-only string?
83
+ * Is the value `undefined`, `null`, or stringified as a whitespace-only string?
44
84
  * @param value Value to check
45
- * @returns `true` if the value is nullable or a whitespace-only string, otherwise `false`
85
+ * @returns `true` if the value is nullable or matches a whitespace-only string, otherwise `false`
46
86
  */
47
87
  function isNullableOrWhitespace(value) {
48
88
  return value == null || EXPRESSION_WHITESPACE.test(getString(value));
@@ -50,7 +90,7 @@ function isNullableOrWhitespace(value) {
50
90
  /**
51
91
  * Is the value a number or a number-like string?
52
92
  * @param value Value to check
53
- * @returns `true` if the value is a number or a parseable string, otherwise `false`
93
+ * @returns `true` if the value is a number or a number-like string, otherwise `false`
54
94
  */
55
95
  function isNumerical(value) {
56
96
  return isNumber(value) || typeof value === "string" && value.trim().length > 0 && !Number.isNaN(+value);
@@ -65,4 +105,4 @@ function isObject(value) {
65
105
  }
66
106
  const EXPRESSION_WHITESPACE = /^\s*$/;
67
107
  //#endregion
68
- export { isArrayOrPlainObject, isConstructor, isEmpty, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
108
+ export { isArrayOrPlainObject, isConstructor, isEmpty, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isPlainObject, isPrimitive, isTypedArray };
@@ -1,6 +1,8 @@
1
1
  import { getString, join, words } from "../internal/string.mjs";
2
2
 
3
3
  //#region src/string/index.d.ts
4
+ declare function dedent(strings: TemplateStringsArray, ...values: unknown[]): string;
5
+ declare function dedent(value: string): string;
4
6
  /**
5
7
  * Get a new UUID-string _(version 4)_
6
8
  * @returns UUID string
@@ -28,4 +30,4 @@ declare function trim(value: string): string;
28
30
  */
29
31
  declare function truncate(value: string, length: number, suffix?: string): string;
30
32
  //#endregion
31
- export { getString, getUuid, join, parse, trim, truncate, words };
33
+ export { dedent, getString, getUuid, join, parse, trim, truncate, words };
@@ -1,5 +1,30 @@
1
+ import { isTemplateStringsArray } from "../internal/is.mjs";
1
2
  import { getString, join, words } from "../internal/string.mjs";
2
3
  //#region src/string/index.ts
4
+ function dedent(value, ...values) {
5
+ let actual;
6
+ if (isTemplateStringsArray(value)) actual = interpolate(value, values);
7
+ else actual = value;
8
+ if (typeof actual !== "string") return "";
9
+ const lines = actual.split("\n");
10
+ const { length } = lines;
11
+ if (length === 1) return actual.trim();
12
+ const lastIndex = length - 1;
13
+ const lengths = [];
14
+ for (let index = 0; index < length; index += 1) {
15
+ const [, indentation] = /^(\s+)/.exec(lines[index]) ?? [];
16
+ if (indentation != null) lengths.push(indentation.length);
17
+ }
18
+ if (lengths.length === 0) return actual.trim();
19
+ const minimum = Math.min(...lengths);
20
+ const pattern = new RegExp(`^\\s{0,${minimum}}`);
21
+ let result = "";
22
+ for (let index = 0; index < length; index += 1) {
23
+ const line = lines[index];
24
+ result += line.replace(pattern, "") + (index === lastIndex ? "" : "\n");
25
+ }
26
+ return result.trim();
27
+ }
3
28
  /**
4
29
  * Get a new UUID-string _(version 4)_
5
30
  * @returns UUID string
@@ -18,6 +43,12 @@ function getUuid() {
18
43
  hex.substring(20, 32)
19
44
  ].join("-");
20
45
  }
46
+ function interpolate(strings, values) {
47
+ const { length } = strings;
48
+ let interpolated = "";
49
+ for (let index = 0; index < length; index += 1) interpolated += `${strings[index]}${getString(values[index])}`;
50
+ return interpolated;
51
+ }
21
52
  /**
22
53
  * Parse a JSON string into its proper value _(or `undefined` if it fails)_
23
54
  * @param value JSON string to parse
@@ -57,4 +88,4 @@ function truncate(value, length, suffix) {
57
88
  }
58
89
  const ZERO = "0";
59
90
  //#endregion
60
- export { getString, getUuid, join, parse, trim, truncate, words };
91
+ export { dedent, getString, getUuid, join, parse, trim, truncate, words };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/atoms",
3
- "version": "0.176.0",
3
+ "version": "0.178.0",
4
4
  "description": "Atomic utilities for making your JavaScript better.",
5
5
  "keywords": [
6
6
  "helper",
@@ -37,10 +37,18 @@
37
37
  "types": "./dist/array/filter.d.mts",
38
38
  "default": "./dist/array/filter.mjs"
39
39
  },
40
+ "./array/first": {
41
+ "types": "./dist/array/first.d.mts",
42
+ "default": "./dist/array/first.mjs"
43
+ },
40
44
  "./array/group-by": {
41
45
  "types": "./dist/array/group-by.d.mts",
42
46
  "default": "./dist/array/group-by.mjs"
43
47
  },
48
+ "./array/last": {
49
+ "types": "./dist/array/last.d.mts",
50
+ "default": "./dist/array/last.mjs"
51
+ },
44
52
  "./array/move": {
45
53
  "types": "./dist/array/move.d.mts",
46
54
  "default": "./dist/array/move.mjs"
@@ -233,7 +241,7 @@
233
241
  "watch": "npx vite build --watch"
234
242
  },
235
243
  "devDependencies": {
236
- "@oxlint/plugins": "^1.59",
244
+ "@oxlint/plugins": "^1.60",
237
245
  "@types/node": "^25.6",
238
246
  "@vitest/coverage-istanbul": "^4.1",
239
247
  "eslint": "^10.2",
@@ -245,4 +253,4 @@
245
253
  "vitest": "npm:@voidzero-dev/vite-plus-test@latest"
246
254
  },
247
255
  "packageManager": "npm@11.11.1"
248
- }
256
+ }
package/plugin/index.js CHANGED
@@ -15,10 +15,11 @@ export const arrayPlugins = [
15
15
  getPlugin('array', 'slice', new Set(['slice'])),
16
16
  getPlugin('array', 'sort', new Set(['sort'])),
17
17
  getPlugin('array', 'splice', new Set(['splice'])),
18
- getPlugin('array', 'times', new Set(['from']), true),
18
+ // getPlugin('array', 'times', new Set(['from']), true),
19
19
  ];
20
20
 
21
21
  export const mathPlugins = [
22
+ getPlugin('math', 'ceil', new Set(['ceil']), true),
22
23
  getPlugin('math', 'floor', new Set(['floor']), true),
23
24
  getPlugin('math', 'max', new Set(['max']), true),
24
25
  getPlugin('math', 'min', new Set(['min']), true),
@@ -52,7 +52,7 @@ export function exists(array: unknown[], ...parameters: unknown[]): boolean {
52
52
  return Array.isArray(array) ? array.includes(parameters[0]) : false;
53
53
  }
54
54
 
55
- return (findValue(FIND_VALUE_INDEX, array, parameters) as number) > -1;
55
+ return (findValue(FIND_VALUE_INDEX, array, parameters, false) as number) > -1;
56
56
  }
57
57
 
58
58
  // #endregion
@@ -53,22 +53,48 @@ export function filter(array: unknown[], ...parameters: unknown[]): unknown[] {
53
53
 
54
54
  filter.remove = removeFiltered;
55
55
 
56
+ /**
57
+ * Get a filtered array of items that do not match the filter
58
+ * @param array Array to search in
59
+ * @param callback Callback to get an item's value for matching
60
+ * @param value Value to match against
61
+ * @returns Filtered array of items that do not match the filter
62
+ */
56
63
  function removeFiltered<
57
64
  Item,
58
65
  Callback extends (item: Item, index: number, array: Item[]) => unknown,
59
66
  >(array: Item[], callback: Callback, value: ReturnType<Callback>): unknown[];
60
67
 
68
+ /**
69
+ * Get a filtered array of items that do not match the filter
70
+ * @param array Array to search in
71
+ * @param key Key to get an item's value for matching
72
+ * @param value Value to match against
73
+ * @returns Filtered array of items that do not match the filter
74
+ */
61
75
  function removeFiltered<Item extends PlainObject, ItemKey extends keyof Item>(
62
76
  array: Item[],
63
77
  key: ItemKey,
64
78
  value: Item[ItemKey],
65
79
  ): unknown[];
66
80
 
81
+ /**
82
+ * Get a filtered array of items that do not match the filter
83
+ * @param array Array to search in
84
+ * @param filter Filter callback to match items
85
+ * @returns Filtered array of items that do not match the filter
86
+ */
67
87
  function removeFiltered<Item>(
68
88
  array: Item[],
69
89
  filter: (item: Item, index: number, array: Item[]) => boolean,
70
90
  ): unknown[];
71
91
 
92
+ /**
93
+ * Get a filtered array of items that do not match the given item
94
+ * @param array Array to search in
95
+ * @param item Item to match against
96
+ * @returns Filtered array of items that do not match the given item
97
+ */
72
98
  function removeFiltered<Item>(array: Item[], item: Item): unknown[];
73
99
 
74
100
  function removeFiltered(array: unknown[], ...parameters: unknown[]): unknown[] {
package/src/array/find.ts CHANGED
@@ -1,10 +1,10 @@
1
- import {FIND_VALUE_VALUE, findValue} from '../internal/array/find';
1
+ import {FIND_VALUE_ITEM, findValue} from '../internal/array/find';
2
2
  import type {PlainObject} from '../models';
3
3
 
4
4
  // #region Functions
5
5
 
6
6
  /**
7
- * Get the first items matching the given value
7
+ * Get the first item matching the given value
8
8
  * @param array Array to search in
9
9
  * @param callback Callback to get an item's value for matching
10
10
  * @param value Value to match against
@@ -48,8 +48,8 @@ export function find<Item>(
48
48
  */
49
49
  export function find<Item>(array: Item[], value: Item): Item | undefined;
50
50
 
51
- export function find<Item>(array: unknown[], ...parameters: unknown[]): Item | undefined {
52
- return findValue(FIND_VALUE_VALUE, array, parameters) as Item | undefined;
51
+ export function find(array: unknown[], ...parameters: unknown[]): unknown {
52
+ return findValue(FIND_VALUE_ITEM, array, parameters, false);
53
53
  }
54
54
 
55
55
  // #endregion
@@ -0,0 +1,113 @@
1
+ import {findAbsoluteValueOrDefault} from '../internal/array/find';
2
+ import type {PlainObject} from '../models';
3
+
4
+ // #region Functions
5
+
6
+ /**
7
+ * Get the first item matching the given value
8
+ * @param array Array to search in
9
+ * @param callback Callback to get an item's value for matching
10
+ * @param value Value to match against
11
+ * @returns First item that matches the value, or `undefined` if no match is found
12
+ */
13
+ export function first<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
14
+ array: Item[],
15
+ callback: Callback,
16
+ value: ReturnType<Callback>,
17
+ ): Item | undefined;
18
+
19
+ /**
20
+ * Get the first item matching the given value by key
21
+ * @param array Array to search in
22
+ * @param key Key to get an item's value for matching
23
+ * @param value Value to match against
24
+ * @returns First item that matches the value, or `undefined` if no match is found
25
+ */
26
+ export function first<Item extends PlainObject, ItemKey extends keyof Item>(
27
+ array: Item[],
28
+ key: ItemKey,
29
+ value: Item[ItemKey],
30
+ ): Item | undefined;
31
+
32
+ /**
33
+ * Get the first item matching the filter
34
+ * @param array Array to search in
35
+ * @param filter Filter callback to match items
36
+ * @returns First item that matches the filter, or `undefined` if no match is found
37
+ */
38
+ export function first<Item>(
39
+ array: Item[],
40
+ filter: (item: Item, index: number, array: Item[]) => boolean,
41
+ ): Item | undefined;
42
+
43
+ /**
44
+ * Get the first item from an array
45
+ * @param array Array to get from
46
+ * @return First item from the array, or `undefined` if the array is empty
47
+ */
48
+ export function first<Item>(array: Item[]): Item | undefined;
49
+
50
+ export function first(array: unknown[], ...parameters: unknown[]): unknown {
51
+ return findAbsoluteValueOrDefault(array, parameters, undefined, false, false);
52
+ }
53
+
54
+ first.default = firstOrDefault;
55
+
56
+ /**
57
+ * Get the first item matching the given value
58
+ * @param array Array to search in
59
+ * @param defaultValue Default value to return if no match is found
60
+ * @param callback Callback to get an item's value for matching
61
+ * @param value Value to match against
62
+ * @returns First item that matches the value, or the default value if no match is found
63
+ */
64
+ function firstOrDefault<
65
+ Item,
66
+ Callback extends (item: Item, index: number, array: Item[]) => unknown,
67
+ >(array: Item[], defaultValue: Item, callback: Callback, value: ReturnType<Callback>): Item;
68
+
69
+ /**
70
+ * Get the first item matching the given value by key
71
+ * @param array Array to search in
72
+ * @param defaultValue Default value to return if no match is found
73
+ * @param key Key to get an item's value for matching
74
+ * @param value Value to match against
75
+ * @returns First item that matches the value, or the default value if no match is found
76
+ */
77
+ function firstOrDefault<Item extends PlainObject, ItemKey extends keyof Item>(
78
+ array: Item[],
79
+ defaultValue: Item,
80
+ key: ItemKey,
81
+ value: Item[ItemKey],
82
+ ): Item;
83
+
84
+ /**
85
+ * Get the first item matching the filter
86
+ * @param array Array to search in
87
+ * @param defaultValue Default value to return if no match is found
88
+ * @param filter Filter callback to match items
89
+ * @returns First item that matches the filter, or the default value if no match is found
90
+ */
91
+ function firstOrDefault<Item>(
92
+ array: Item[],
93
+ defaultValue: Item,
94
+ filter: (item: Item, index: number, array: Item[]) => boolean,
95
+ ): Item;
96
+
97
+ /**
98
+ * Get the first item from an array
99
+ * @param array Array to get from
100
+ * @param defaultValue Default value to return if the array is empty
101
+ * @return First item from the array, or the default value if the array is empty
102
+ */
103
+ function firstOrDefault<Item>(array: Item[], defaultValue: Item): Item;
104
+
105
+ function firstOrDefault(
106
+ array: unknown[],
107
+ defaultValue: unknown,
108
+ ...parameters: unknown[]
109
+ ): unknown {
110
+ return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, false);
111
+ }
112
+
113
+ // #endregion
@@ -14,7 +14,9 @@ export * from './intersection';
14
14
  export * from './partition';
15
15
  export * from './position';
16
16
  export * from './push';
17
+ export * from './reverse';
17
18
  export * from './select';
19
+ export * from './single';
18
20
  export * from './slice';
19
21
  export * from './splice';
20
22
  export * from './to-set';