@oscarpalmer/atoms 0.176.0 → 0.177.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/atoms",
3
- "version": "0.176.0",
3
+ "version": "0.177.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"
@@ -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
@@ -15,6 +15,7 @@ export * from './partition';
15
15
  export * from './position';
16
16
  export * from './push';
17
17
  export * from './select';
18
+ export * from './single';
18
19
  export * from './slice';
19
20
  export * from './splice';
20
21
  export * from './to-set';
@@ -0,0 +1,109 @@
1
+ import {findAbsoluteValueOrDefault} from '../internal/array/find';
2
+ import type {PlainObject} from '../models';
3
+
4
+ // #region Functions
5
+
6
+ /**
7
+ * Get the last items 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 Last item that matches the value, or `undefined` if no match is found
12
+ */
13
+ export function last<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 Last item that matches the value, or `undefined` if no match is found
25
+ */
26
+ export function last<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 last item matching the filter
34
+ * @param array Array to search in
35
+ * @param filter Filter callback to match items
36
+ * @returns Last item that matches the filter, or `undefined` if no match is found
37
+ */
38
+ export function last<Item>(
39
+ array: Item[],
40
+ filter: (item: Item, index: number, array: Item[]) => boolean,
41
+ ): Item | undefined;
42
+
43
+ /**
44
+ * Get the last item from an array
45
+ * @param array Array to get from
46
+ * @return Last item from the array, or `undefined` if the array is empty
47
+ */
48
+ export function last<Item>(array: Item[]): Item | undefined;
49
+
50
+ export function last(array: unknown[], ...parameters: unknown[]): unknown {
51
+ return findAbsoluteValueOrDefault(array, parameters, undefined, false, true);
52
+ }
53
+
54
+ last.default = lastOrDefault;
55
+
56
+ /**
57
+ * Get the last 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 Last item that matches the value, or the default value if no match is found
63
+ */
64
+ function lastOrDefault<
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 last 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 Last item that matches the value, or the default value if no match is found
76
+ */
77
+ function lastOrDefault<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 last 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 Last item that matches the filter, or the default value if no match is found
90
+ */
91
+ function lastOrDefault<Item>(
92
+ array: Item[],
93
+ defaultValue: Item,
94
+ filter: (item: Item, index: number, array: Item[]) => boolean,
95
+ ): Item;
96
+
97
+ /**
98
+ * Get the last 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 Last item from the array, or the default value if the array is empty
102
+ */
103
+ function lastOrDefault<Item>(array: Item[], defaultValue: Item): Item;
104
+
105
+ function lastOrDefault(array: unknown[], defaultValue: unknown, ...parameters: unknown[]): unknown {
106
+ return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, true);
107
+ }
108
+
109
+ // #endregion
@@ -20,6 +20,25 @@ export function select<
20
20
  mapCallback: MapCallback,
21
21
  ): Array<ReturnType<MapCallback>>;
22
22
 
23
+ /**
24
+ * Get a filtered and mapped array of items
25
+ * @param array Array to search in
26
+ * @param filterCallback Callback to get an item's value for matching
27
+ * @param filterValue Value to match against
28
+ * @param mapKey Key to get an item's value for mapping
29
+ * @returns Filtered and mapped array of items
30
+ */
31
+ export function select<
32
+ Item extends PlainObject,
33
+ FilterCallback extends (item: Item, index: number, array: Item[]) => unknown,
34
+ MapKey extends keyof Item,
35
+ >(
36
+ array: Item[],
37
+ filterCallback: FilterCallback,
38
+ filterValue: ReturnType<FilterCallback>,
39
+ mapKey: MapKey,
40
+ ): Array<Item[MapKey]>;
41
+
23
42
  /**
24
43
  * Get a filtered and mapped array of items
25
44
  * @param array Array to search in
@@ -39,6 +58,25 @@ export function select<
39
58
  mapCallback: MapCallback,
40
59
  ): Array<ReturnType<MapCallback>>;
41
60
 
61
+ /**
62
+ * Get a filtered and mapped array of items
63
+ * @param array Array to search in
64
+ * @param filterKey Key to get an item's value for matching
65
+ * @param filterValue Value to match against
66
+ * @param mapKey Key to get an item's value for mapping
67
+ * @returns Filtered and mapped array of items
68
+ */
69
+ export function select<
70
+ Item extends PlainObject,
71
+ ItemKey extends keyof Item,
72
+ MapKey extends keyof Item,
73
+ >(
74
+ array: Item[],
75
+ filterKey: ItemKey,
76
+ filterValue: Item[ItemKey],
77
+ mapKey: MapKey,
78
+ ): Array<Item[MapKey]>;
79
+
42
80
  /**
43
81
  * Get a filtered and mapped array of items
44
82
  * @param array Array to search in
@@ -57,6 +95,24 @@ export function select<
57
95
  mapCallback: MapCallback,
58
96
  ): Array<ReturnType<MapCallback>>;
59
97
 
98
+ /**
99
+ * Get a filtered and mapped array of items
100
+ * @param array Array to search in
101
+ * @param filterCallback Filter callback to match items
102
+ * @param mapKey Key to get an item's value for mapping
103
+ * @returns Filtered and mapped array of items
104
+ */
105
+ export function select<
106
+ Item extends PlainObject,
107
+ FilterCallback extends (item: Item, index: number, array: Item[]) => unknown,
108
+ MapKey extends keyof Item,
109
+ >(
110
+ array: Item[],
111
+ filterCallback: FilterCallback,
112
+ filterValue: ReturnType<FilterCallback>,
113
+ mapKey: MapKey,
114
+ ): Array<Item[MapKey]>;
115
+
60
116
  /**
61
117
  * Get a filtered and mapped array of items
62
118
  * @param array Array to search in
@@ -73,6 +129,19 @@ export function select<
73
129
  map: MapCallback,
74
130
  ): Array<ReturnType<MapCallback>>;
75
131
 
132
+ /**
133
+ * Get a filtered and mapped array of items
134
+ * @param array Array to search in
135
+ * @param filter Filter callback to match items
136
+ * @param map Key to get an item's value for mapping
137
+ * @returns Filtered and mapped array of items
138
+ */
139
+ export function select<Item extends PlainObject, MapKey extends keyof Item>(
140
+ array: Item[],
141
+ filter: (item: Item, index: number, array: Item[]) => boolean,
142
+ map: MapKey,
143
+ ): Array<Item[MapKey]>;
144
+
76
145
  /**
77
146
  * Get a filtered and mapped array of items
78
147
  * @param array Array to search in
@@ -85,8 +154,23 @@ export function select<
85
154
  MapCallback extends (item: Item, index: number, array: Item[]) => unknown,
86
155
  >(array: Item[], filter: Item, map: MapCallback): Array<ReturnType<MapCallback>>;
87
156
 
157
+ /**
158
+ * Get a filtered and mapped array of items
159
+ * @param array Array to search in
160
+ * @param item Item to match against
161
+ * @param map Key to get an item's value for mapping
162
+ * @returns Filtered and mapped array of items
163
+ */
164
+ export function select<Item extends PlainObject, MapKey extends keyof Item>(
165
+ array: Item[],
166
+ filter: Item,
167
+ map: MapKey,
168
+ ): Array<Item[MapKey]>;
169
+
88
170
  export function select(array: unknown[], ...parameters: unknown[]): unknown[] {
89
171
  const mapper = parameters.pop();
90
172
 
91
173
  return findValues(FIND_VALUES_ALL, array, parameters, mapper).matched;
92
174
  }
175
+
176
+ // #endregion
@@ -0,0 +1,64 @@
1
+ import {FIND_VALUES_ALL, findValues} from '../internal/array/find';
2
+ import type {PlainObject} from '../models';
3
+
4
+ // #region Functions
5
+
6
+ /**
7
+ * Get the _only_ item matching the given value
8
+ *
9
+ * Throws an error if multiple items match the value
10
+ * @param array Array to search in
11
+ * @param callback Callback to get an item's value for matching
12
+ * @param value Value to match against
13
+ * @returns Only item that matches the value, or `undefined` if no match is found
14
+ */
15
+ export function single<
16
+ Item,
17
+ Callback extends (item: Item, index: number, array: Item[]) => unknown,
18
+ >(array: Item[], callback: Callback, value: ReturnType<Callback>): Item | undefined;
19
+
20
+ /**
21
+ * Get the _only_ item matching the given value by key
22
+ *
23
+ * Throws an error if multiple items match the value
24
+ * @param array Array to search in
25
+ * @param key Key to get an item's value for matching
26
+ * @param value Value to match against
27
+ * @returns Only item that matches the value, or `undefined` if no match is found
28
+ */
29
+ export function single<Item extends PlainObject, ItemKey extends keyof Item>(
30
+ array: Item[],
31
+ key: ItemKey,
32
+ value: Item[ItemKey],
33
+ ): Item | undefined;
34
+
35
+ /**
36
+ * Get the _only_ item matching the filter
37
+ *
38
+ * Throws an error if multiple items match the filter
39
+ * @param array Array to search in
40
+ * @param filter Filter callback to match items
41
+ * @returns Only item that matches the filter, or `undefined` if no match is found
42
+ */
43
+ export function single<Item>(
44
+ array: Item[],
45
+ filter: (item: Item, index: number, array: Item[]) => boolean,
46
+ ): Item | undefined;
47
+
48
+ export function single(array: unknown[], ...parameters: unknown[]): unknown {
49
+ const {matched} = findValues(FIND_VALUES_ALL, array, parameters);
50
+
51
+ if (matched.length > 1) {
52
+ throw new Error(MESSAGE);
53
+ }
54
+
55
+ return matched[0];
56
+ }
57
+
58
+ // #endregion
59
+
60
+ // #region Variables
61
+
62
+ const MESSAGE = 'Multiple items were found';
63
+
64
+ // #endregion
package/src/index.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export * from './array/filter';
2
+ export * from './array/first';
2
3
  export * from './array/group-by';
3
4
  export * from './array/index';
5
+ export * from './array/last';
4
6
  export * from './array/move';
5
7
  export * from './array/sort';
6
8
  export * from './array/swap';
@@ -1,8 +1,8 @@
1
- import {getArrayCallbacks} from './callbacks';
1
+ import {getArrayCallback, getArrayCallbacks} from './callbacks';
2
2
 
3
3
  // #region Types
4
4
 
5
- type FindValueType = 'index' | 'value';
5
+ type FindValueType = 'index' | 'item';
6
6
 
7
7
  type FindValuesResult = {
8
8
  matched: unknown[];
@@ -21,14 +21,19 @@ type Parameters = {
21
21
 
22
22
  // #region Functions
23
23
 
24
- export function findValue(type: FindValueType, array: unknown[], parameters: unknown[]): unknown {
24
+ export function findValue(
25
+ type: FindValueType,
26
+ array: unknown[],
27
+ parameters: unknown[],
28
+ reversed: boolean,
29
+ ): unknown {
25
30
  const findIndex = type === FIND_VALUE_INDEX;
26
31
 
27
32
  if (!Array.isArray(array) || array.length === 0) {
28
33
  return findIndex ? -1 : undefined;
29
34
  }
30
35
 
31
- const {bool, key, value} = getParameters(parameters);
36
+ const {bool, key, value} = getFindParameters(parameters);
32
37
 
33
38
  const callbacks = getArrayCallbacks(bool, key);
34
39
 
@@ -42,7 +47,7 @@ export function findValue(type: FindValueType, array: unknown[], parameters: unk
42
47
  return findIndex ? index : array[index];
43
48
  }
44
49
 
45
- return findValueInArray(array, callbacks.keyed, value, findIndex);
50
+ return findValueInArray(array, callbacks.keyed, value, findIndex, reversed);
46
51
  }
47
52
 
48
53
  function findValueInArray(
@@ -50,11 +55,12 @@ function findValueInArray(
50
55
  callback: ((item: unknown, index: number, array: unknown[]) => boolean) | undefined,
51
56
  value: unknown,
52
57
  findIndex: boolean,
58
+ reversed: boolean,
53
59
  ): unknown {
54
60
  const {length} = array;
55
61
 
56
62
  for (let index = 0; index < length; index += 1) {
57
- const item = array[index];
63
+ const item = reversed ? array.at(-(index + 1)) : array[index];
58
64
 
59
65
  if (Object.is(callback?.(item, index, array), value)) {
60
66
  return findIndex ? index : item;
@@ -64,6 +70,26 @@ function findValueInArray(
64
70
  return findIndex ? -1 : undefined;
65
71
  }
66
72
 
73
+ export function findAbsoluteValueOrDefault(
74
+ array: unknown[],
75
+ parameters: unknown[],
76
+ defaultValue: unknown,
77
+ useDefaultValue: boolean,
78
+ reversed: boolean,
79
+ ): unknown {
80
+ if (parameters.length === 0) {
81
+ if (Array.isArray(array) && array.length > 0) {
82
+ return reversed ? array.at(-1) : array[0];
83
+ }
84
+
85
+ return useDefaultValue ? defaultValue : undefined;
86
+ }
87
+
88
+ const index = findValue(FIND_VALUE_INDEX, array, parameters, reversed) as number;
89
+
90
+ return index > -1 ? array[index] : useDefaultValue ? defaultValue : undefined;
91
+ }
92
+
67
93
  export function findValues(
68
94
  type: FindValuesType,
69
95
  array: unknown[],
@@ -80,7 +106,7 @@ export function findValues(
80
106
  }
81
107
 
82
108
  const {length} = array;
83
- const {bool, key, value} = getParameters(parameters);
109
+ const {bool, key, value} = getFindParameters(parameters);
84
110
  const callbacks = getArrayCallbacks(bool, key);
85
111
 
86
112
  if (type === FIND_VALUES_UNIQUE && callbacks?.keyed == null && length >= UNIQUE_THRESHOLD) {
@@ -89,7 +115,7 @@ export function findValues(
89
115
  return result;
90
116
  }
91
117
 
92
- const mapCallback = typeof mapper === 'function' ? mapper : undefined;
118
+ const mapCallback = getArrayCallback(mapper);
93
119
 
94
120
  if (callbacks?.bool != null || (type === FIND_VALUES_ALL && key == null)) {
95
121
  const callback = callbacks?.bool ?? (item => Object.is(item, value));
@@ -127,7 +153,7 @@ export function findValues(
127
153
  return result;
128
154
  }
129
155
 
130
- function getParameters(original: unknown[]): Parameters {
156
+ export function getFindParameters(original: unknown[]): Parameters {
131
157
  const {length} = original;
132
158
 
133
159
  return {
@@ -143,7 +169,7 @@ function getParameters(original: unknown[]): Parameters {
143
169
 
144
170
  export const FIND_VALUE_INDEX: FindValueType = 'index';
145
171
 
146
- export const FIND_VALUE_VALUE: FindValueType = 'value';
172
+ export const FIND_VALUE_ITEM: FindValueType = 'item';
147
173
 
148
174
  export const FIND_VALUES_ALL: FindValuesType = 'all';
149
175