@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.
@@ -2,7 +2,7 @@ import { FIND_VALUE_INDEX, findValue } from "../internal/array/find.mjs";
2
2
  //#region src/array/exists.ts
3
3
  function exists(array, ...parameters) {
4
4
  if (parameters.length === 1 && typeof parameters[0] !== "function") return Array.isArray(array) ? array.includes(parameters[0]) : false;
5
- return findValue(FIND_VALUE_INDEX, array, parameters) > -1;
5
+ return findValue(FIND_VALUE_INDEX, array, parameters, false) > -1;
6
6
  }
7
7
  //#endregion
8
8
  export { exists };
@@ -34,9 +34,35 @@ declare function filter<Item>(array: Item[], item: Item): Item[];
34
34
  declare namespace filter {
35
35
  var remove: typeof removeFiltered;
36
36
  }
37
+ /**
38
+ * Get a filtered array of items that do not match the filter
39
+ * @param array Array to search in
40
+ * @param callback Callback to get an item's value for matching
41
+ * @param value Value to match against
42
+ * @returns Filtered array of items that do not match the filter
43
+ */
37
44
  declare function removeFiltered<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): unknown[];
45
+ /**
46
+ * Get a filtered array of items that do not match the filter
47
+ * @param array Array to search in
48
+ * @param key Key to get an item's value for matching
49
+ * @param value Value to match against
50
+ * @returns Filtered array of items that do not match the filter
51
+ */
38
52
  declare function removeFiltered<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): unknown[];
53
+ /**
54
+ * Get a filtered array of items that do not match the filter
55
+ * @param array Array to search in
56
+ * @param filter Filter callback to match items
57
+ * @returns Filtered array of items that do not match the filter
58
+ */
39
59
  declare function removeFiltered<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): unknown[];
60
+ /**
61
+ * Get a filtered array of items that do not match the given item
62
+ * @param array Array to search in
63
+ * @param item Item to match against
64
+ * @returns Filtered array of items that do not match the given item
65
+ */
40
66
  declare function removeFiltered<Item>(array: Item[], item: Item): unknown[];
41
67
  //#endregion
42
68
  export { filter };
@@ -2,7 +2,7 @@ import { PlainObject } from "../models.mjs";
2
2
 
3
3
  //#region src/array/find.d.ts
4
4
  /**
5
- * Get the first items matching the given value
5
+ * Get the first item matching the given value
6
6
  * @param array Array to search in
7
7
  * @param callback Callback to get an item's value for matching
8
8
  * @param value Value to match against
@@ -1,7 +1,7 @@
1
- import { FIND_VALUE_VALUE, findValue } from "../internal/array/find.mjs";
1
+ import { FIND_VALUE_ITEM, findValue } from "../internal/array/find.mjs";
2
2
  //#region src/array/find.ts
3
3
  function find(array, ...parameters) {
4
- return findValue(FIND_VALUE_VALUE, array, parameters);
4
+ return findValue(FIND_VALUE_ITEM, array, parameters, false);
5
5
  }
6
6
  //#endregion
7
7
  export { find };
@@ -0,0 +1,71 @@
1
+ import { PlainObject } from "../models.mjs";
2
+
3
+ //#region src/array/first.d.ts
4
+ /**
5
+ * Get the first item matching the given value
6
+ * @param array Array to search in
7
+ * @param callback Callback to get an item's value for matching
8
+ * @param value Value to match against
9
+ * @returns First item that matches the value, or `undefined` if no match is found
10
+ */
11
+ declare function first<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item | undefined;
12
+ /**
13
+ * Get the first item matching the given value by key
14
+ * @param array Array to search in
15
+ * @param key Key to get an item's value for matching
16
+ * @param value Value to match against
17
+ * @returns First item that matches the value, or `undefined` if no match is found
18
+ */
19
+ declare function first<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item | undefined;
20
+ /**
21
+ * Get the first item matching the filter
22
+ * @param array Array to search in
23
+ * @param filter Filter callback to match items
24
+ * @returns First item that matches the filter, or `undefined` if no match is found
25
+ */
26
+ declare function first<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): Item | undefined;
27
+ /**
28
+ * Get the first item from an array
29
+ * @param array Array to get from
30
+ * @return First item from the array, or `undefined` if the array is empty
31
+ */
32
+ declare function first<Item>(array: Item[]): Item | undefined;
33
+ declare namespace first {
34
+ var _a: typeof firstOrDefault;
35
+ export { _a as default };
36
+ }
37
+ /**
38
+ * Get the first item matching the given value
39
+ * @param array Array to search in
40
+ * @param defaultValue Default value to return if no match is found
41
+ * @param callback Callback to get an item's value for matching
42
+ * @param value Value to match against
43
+ * @returns First item that matches the value, or the default value if no match is found
44
+ */
45
+ declare function firstOrDefault<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], defaultValue: Item, callback: Callback, value: ReturnType<Callback>): Item;
46
+ /**
47
+ * Get the first item matching the given value by key
48
+ * @param array Array to search in
49
+ * @param defaultValue Default value to return if no match is found
50
+ * @param key Key to get an item's value for matching
51
+ * @param value Value to match against
52
+ * @returns First item that matches the value, or the default value if no match is found
53
+ */
54
+ declare function firstOrDefault<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], defaultValue: Item, key: ItemKey, value: Item[ItemKey]): Item;
55
+ /**
56
+ * Get the first item matching the filter
57
+ * @param array Array to search in
58
+ * @param defaultValue Default value to return if no match is found
59
+ * @param filter Filter callback to match items
60
+ * @returns First item that matches the filter, or the default value if no match is found
61
+ */
62
+ declare function firstOrDefault<Item>(array: Item[], defaultValue: Item, filter: (item: Item, index: number, array: Item[]) => boolean): Item;
63
+ /**
64
+ * Get the first item from an array
65
+ * @param array Array to get from
66
+ * @param defaultValue Default value to return if the array is empty
67
+ * @return First item from the array, or the default value if the array is empty
68
+ */
69
+ declare function firstOrDefault<Item>(array: Item[], defaultValue: Item): Item;
70
+ //#endregion
71
+ export { first };
@@ -0,0 +1,11 @@
1
+ import { findAbsoluteValueOrDefault } from "../internal/array/find.mjs";
2
+ //#region src/array/first.ts
3
+ function first(array, ...parameters) {
4
+ return findAbsoluteValueOrDefault(array, parameters, void 0, false, false);
5
+ }
6
+ first.default = firstOrDefault;
7
+ function firstOrDefault(array, defaultValue, ...parameters) {
8
+ return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, false);
9
+ }
10
+ //#endregion
11
+ export { first };
@@ -13,7 +13,9 @@ import { intersection } from "./intersection.mjs";
13
13
  import { partition } from "./partition.mjs";
14
14
  import { ArrayPosition, endsWithArray, getArrayPosition, includesArray, indexOfArray, startsWithArray } from "./position.mjs";
15
15
  import { push } from "./push.mjs";
16
+ import { reverse } from "./reverse.mjs";
16
17
  import { select } from "./select.mjs";
18
+ import { single } from "./single.mjs";
17
19
  import { drop, slice, take } from "./slice.mjs";
18
20
  import { splice } from "./splice.mjs";
19
21
  import { toSet } from "./to-set.mjs";
@@ -21,4 +23,4 @@ import { toggle } from "./toggle.mjs";
21
23
  import { union } from "./union.mjs";
22
24
  import { unique } from "./unique.mjs";
23
25
  import { update } from "./update.mjs";
24
- export { ArrayPosition, chunk, compact, difference, drop, endsWithArray, exists, find, flatten, getArray, getArrayPosition, includesArray, indexOf, indexOfArray, insert, intersection, partition, push, range, select, shuffle, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
26
+ export { ArrayPosition, chunk, compact, difference, drop, endsWithArray, exists, find, flatten, getArray, getArrayPosition, includesArray, indexOf, indexOfArray, insert, intersection, partition, push, range, reverse, select, shuffle, single, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
@@ -13,7 +13,9 @@ import { intersection } from "./intersection.mjs";
13
13
  import { partition } from "./partition.mjs";
14
14
  import { endsWithArray, getArrayPosition, includesArray, indexOfArray, startsWithArray } from "./position.mjs";
15
15
  import { push } from "./push.mjs";
16
+ import { reverse } from "./reverse.mjs";
16
17
  import { select } from "./select.mjs";
18
+ import { single } from "./single.mjs";
17
19
  import { drop, slice, take } from "./slice.mjs";
18
20
  import { splice } from "./splice.mjs";
19
21
  import { toSet } from "./to-set.mjs";
@@ -21,4 +23,4 @@ import { toggle } from "./toggle.mjs";
21
23
  import { union } from "./union.mjs";
22
24
  import { unique } from "./unique.mjs";
23
25
  import { update } from "./update.mjs";
24
- export { chunk, compact, difference, drop, endsWithArray, exists, find, flatten, getArray, getArrayPosition, includesArray, indexOf, indexOfArray, insert, intersection, partition, push, range, select, shuffle, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
26
+ export { chunk, compact, difference, drop, endsWithArray, exists, find, flatten, getArray, getArrayPosition, includesArray, indexOf, indexOfArray, insert, intersection, partition, push, range, reverse, select, shuffle, single, slice, splice, startsWithArray, take, times, toSet, toggle, union, unique, update };
@@ -0,0 +1,71 @@
1
+ import { PlainObject } from "../models.mjs";
2
+
3
+ //#region src/array/last.d.ts
4
+ /**
5
+ * Get the last items matching the given value
6
+ * @param array Array to search in
7
+ * @param callback Callback to get an item's value for matching
8
+ * @param value Value to match against
9
+ * @returns Last item that matches the value, or `undefined` if no match is found
10
+ */
11
+ declare function last<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item | undefined;
12
+ /**
13
+ * Get the first item matching the given value by key
14
+ * @param array Array to search in
15
+ * @param key Key to get an item's value for matching
16
+ * @param value Value to match against
17
+ * @returns Last item that matches the value, or `undefined` if no match is found
18
+ */
19
+ declare function last<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item | undefined;
20
+ /**
21
+ * Get the last item matching the filter
22
+ * @param array Array to search in
23
+ * @param filter Filter callback to match items
24
+ * @returns Last item that matches the filter, or `undefined` if no match is found
25
+ */
26
+ declare function last<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): Item | undefined;
27
+ /**
28
+ * Get the last item from an array
29
+ * @param array Array to get from
30
+ * @return Last item from the array, or `undefined` if the array is empty
31
+ */
32
+ declare function last<Item>(array: Item[]): Item | undefined;
33
+ declare namespace last {
34
+ var _a: typeof lastOrDefault;
35
+ export { _a as default };
36
+ }
37
+ /**
38
+ * Get the last item matching the given value
39
+ * @param array Array to search in
40
+ * @param defaultValue Default value to return if no match is found
41
+ * @param callback Callback to get an item's value for matching
42
+ * @param value Value to match against
43
+ * @returns Last item that matches the value, or the default value if no match is found
44
+ */
45
+ declare function lastOrDefault<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], defaultValue: Item, callback: Callback, value: ReturnType<Callback>): Item;
46
+ /**
47
+ * Get the last item matching the given value by key
48
+ * @param array Array to search in
49
+ * @param defaultValue Default value to return if no match is found
50
+ * @param key Key to get an item's value for matching
51
+ * @param value Value to match against
52
+ * @returns Last item that matches the value, or the default value if no match is found
53
+ */
54
+ declare function lastOrDefault<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], defaultValue: Item, key: ItemKey, value: Item[ItemKey]): Item;
55
+ /**
56
+ * Get the last item matching the filter
57
+ * @param array Array to search in
58
+ * @param defaultValue Default value to return if no match is found
59
+ * @param filter Filter callback to match items
60
+ * @returns Last item that matches the filter, or the default value if no match is found
61
+ */
62
+ declare function lastOrDefault<Item>(array: Item[], defaultValue: Item, filter: (item: Item, index: number, array: Item[]) => boolean): Item;
63
+ /**
64
+ * Get the last item from an array
65
+ * @param array Array to get from
66
+ * @param defaultValue Default value to return if the array is empty
67
+ * @return Last item from the array, or the default value if the array is empty
68
+ */
69
+ declare function lastOrDefault<Item>(array: Item[], defaultValue: Item): Item;
70
+ //#endregion
71
+ export { last };
@@ -0,0 +1,11 @@
1
+ import { findAbsoluteValueOrDefault } from "../internal/array/find.mjs";
2
+ //#region src/array/last.ts
3
+ function last(array, ...parameters) {
4
+ return findAbsoluteValueOrDefault(array, parameters, void 0, false, true);
5
+ }
6
+ last.default = lastOrDefault;
7
+ function lastOrDefault(array, defaultValue, ...parameters) {
8
+ return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, true);
9
+ }
10
+ //#endregion
11
+ export { last };
@@ -0,0 +1,4 @@
1
+ //#region src/array/reverse.d.ts
2
+ declare function reverse<Item>(array: Item[]): Item[];
3
+ //#endregion
4
+ export { reverse };
@@ -0,0 +1,16 @@
1
+ //#region src/array/reverse.ts
2
+ function reverse(array) {
3
+ if (!Array.isArray(array)) return [];
4
+ const { length } = array;
5
+ if (length < 2) return array;
6
+ const half = Math.floor(length / 2);
7
+ for (let firstIndex = 0; firstIndex < half; firstIndex += 1) {
8
+ const temporaryItem = array[firstIndex];
9
+ const secondIndex = length - 1 - firstIndex;
10
+ array[firstIndex] = array[secondIndex];
11
+ array[secondIndex] = temporaryItem;
12
+ }
13
+ return array;
14
+ }
15
+ //#endregion
16
+ export { reverse };
@@ -10,6 +10,15 @@ import { PlainObject } from "../models.mjs";
10
10
  * @returns Filtered and mapped array of items
11
11
  */
12
12
  declare function select<Item, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapCallback: MapCallback): Array<ReturnType<MapCallback>>;
13
+ /**
14
+ * Get a filtered and mapped array of items
15
+ * @param array Array to search in
16
+ * @param filterCallback Callback to get an item's value for matching
17
+ * @param filterValue Value to match against
18
+ * @param mapKey Key to get an item's value for mapping
19
+ * @returns Filtered and mapped array of items
20
+ */
21
+ declare function select<Item extends PlainObject, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapKey extends keyof Item>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapKey: MapKey): Array<Item[MapKey]>;
13
22
  /**
14
23
  * Get a filtered and mapped array of items
15
24
  * @param array Array to search in
@@ -19,6 +28,15 @@ declare function select<Item, FilterCallback extends (item: Item, index: number,
19
28
  * @returns Filtered and mapped array of items
20
29
  */
21
30
  declare function select<Item extends PlainObject, ItemKey extends keyof Item, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filterKey: ItemKey, filterValue: Item[ItemKey], mapCallback: MapCallback): Array<ReturnType<MapCallback>>;
31
+ /**
32
+ * Get a filtered and mapped array of items
33
+ * @param array Array to search in
34
+ * @param filterKey Key to get an item's value for matching
35
+ * @param filterValue Value to match against
36
+ * @param mapKey Key to get an item's value for mapping
37
+ * @returns Filtered and mapped array of items
38
+ */
39
+ declare function select<Item extends PlainObject, ItemKey extends keyof Item, MapKey extends keyof Item>(array: Item[], filterKey: ItemKey, filterValue: Item[ItemKey], mapKey: MapKey): Array<Item[MapKey]>;
22
40
  /**
23
41
  * Get a filtered and mapped array of items
24
42
  * @param array Array to search in
@@ -27,6 +45,14 @@ declare function select<Item extends PlainObject, ItemKey extends keyof Item, Ma
27
45
  * @returns Filtered and mapped array of items
28
46
  */
29
47
  declare function select<Item, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapCallback: MapCallback): Array<ReturnType<MapCallback>>;
48
+ /**
49
+ * Get a filtered and mapped array of items
50
+ * @param array Array to search in
51
+ * @param filterCallback Filter callback to match items
52
+ * @param mapKey Key to get an item's value for mapping
53
+ * @returns Filtered and mapped array of items
54
+ */
55
+ declare function select<Item extends PlainObject, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapKey extends keyof Item>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapKey: MapKey): Array<Item[MapKey]>;
30
56
  /**
31
57
  * Get a filtered and mapped array of items
32
58
  * @param array Array to search in
@@ -35,6 +61,14 @@ declare function select<Item, FilterCallback extends (item: Item, index: number,
35
61
  * @returns Filtered and mapped array of items
36
62
  */
37
63
  declare function select<Item, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, map: MapCallback): Array<ReturnType<MapCallback>>;
64
+ /**
65
+ * Get a filtered and mapped array of items
66
+ * @param array Array to search in
67
+ * @param filter Filter callback to match items
68
+ * @param map Key to get an item's value for mapping
69
+ * @returns Filtered and mapped array of items
70
+ */
71
+ declare function select<Item extends PlainObject, MapKey extends keyof Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, map: MapKey): Array<Item[MapKey]>;
38
72
  /**
39
73
  * Get a filtered and mapped array of items
40
74
  * @param array Array to search in
@@ -43,5 +77,13 @@ declare function select<Item, MapCallback extends (item: Item, index: number, ar
43
77
  * @returns Filtered and mapped array of items
44
78
  */
45
79
  declare function select<Item, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filter: Item, map: MapCallback): Array<ReturnType<MapCallback>>;
80
+ /**
81
+ * Get a filtered and mapped array of items
82
+ * @param array Array to search in
83
+ * @param item Item to match against
84
+ * @param map Key to get an item's value for mapping
85
+ * @returns Filtered and mapped array of items
86
+ */
87
+ declare function select<Item extends PlainObject, MapKey extends keyof Item>(array: Item[], filter: Item, map: MapKey): Array<Item[MapKey]>;
46
88
  //#endregion
47
89
  export { select };
@@ -0,0 +1,34 @@
1
+ import { PlainObject } from "../models.mjs";
2
+
3
+ //#region src/array/single.d.ts
4
+ /**
5
+ * Get the _only_ item matching the given value
6
+ *
7
+ * Throws an error if multiple items match the 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 Only item that matches the value, or `undefined` if no match is found
12
+ */
13
+ declare function single<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item | undefined;
14
+ /**
15
+ * Get the _only_ item matching the given value by key
16
+ *
17
+ * Throws an error if multiple items match the value
18
+ * @param array Array to search in
19
+ * @param key Key to get an item's value for matching
20
+ * @param value Value to match against
21
+ * @returns Only item that matches the value, or `undefined` if no match is found
22
+ */
23
+ declare function single<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item | undefined;
24
+ /**
25
+ * Get the _only_ item matching the filter
26
+ *
27
+ * Throws an error if multiple items match the filter
28
+ * @param array Array to search in
29
+ * @param filter Filter callback to match items
30
+ * @returns Only item that matches the filter, or `undefined` if no match is found
31
+ */
32
+ declare function single<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): Item | undefined;
33
+ //#endregion
34
+ export { single };
@@ -0,0 +1,10 @@
1
+ import { findValues } from "../internal/array/find.mjs";
2
+ //#region src/array/single.ts
3
+ function single(array, ...parameters) {
4
+ const { matched } = findValues("all", array, parameters);
5
+ if (matched.length > 1) throw new Error(MESSAGE);
6
+ return matched[0];
7
+ }
8
+ const MESSAGE = "Multiple items were found";
9
+ //#endregion
10
+ export { single };