@oscarpalmer/atoms 0.160.0 → 0.162.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.
Files changed (80) hide show
  1. package/dist/array/index.js +5 -3
  2. package/dist/array/insert.js +2 -2
  3. package/dist/array/move.js +64 -0
  4. package/dist/array/push.js +2 -2
  5. package/dist/array/sort.js +4 -2
  6. package/dist/array/splice.js +2 -2
  7. package/dist/array/swap.js +59 -0
  8. package/dist/atoms.full.js +280 -121
  9. package/dist/beacon.js +5 -3
  10. package/dist/color/instance.js +1 -0
  11. package/dist/color/misc/index.js +3 -2
  12. package/dist/function/memoize.js +2 -1
  13. package/dist/index.js +5 -3
  14. package/dist/internal/array/find.js +3 -3
  15. package/dist/{array → internal/array}/index-of.js +2 -2
  16. package/dist/internal/array/insert.js +4 -1
  17. package/dist/internal/array/overlap.js +21 -0
  18. package/dist/internal/math/aggregate.js +5 -1
  19. package/dist/internal/number.js +1 -1
  20. package/dist/internal/result.js +3 -1
  21. package/dist/internal/value/compare.js +3 -2
  22. package/dist/internal/value/equal.js +4 -2
  23. package/dist/math.js +2 -2
  24. package/dist/promise/delay.js +2 -2
  25. package/dist/promise/index.js +3 -3
  26. package/dist/promise/misc.js +2 -2
  27. package/dist/promise/models.js +2 -2
  28. package/dist/promise/timed.js +3 -3
  29. package/dist/query.js +2 -1
  30. package/dist/queue.js +2 -2
  31. package/dist/string/case.js +20 -12
  32. package/dist/string/index.js +2 -1
  33. package/dist/string/match.js +6 -3
  34. package/dist/value/clone.js +2 -1
  35. package/dist/value/diff.js +7 -4
  36. package/package.json +4 -4
  37. package/src/array/index.ts +4 -2
  38. package/src/array/insert.ts +2 -2
  39. package/src/array/move.ts +228 -0
  40. package/src/array/push.ts +2 -2
  41. package/src/array/sort.ts +18 -6
  42. package/src/array/splice.ts +2 -2
  43. package/src/array/swap.ts +211 -0
  44. package/src/beacon.ts +7 -3
  45. package/src/color/instance.ts +3 -2
  46. package/src/color/misc/index.ts +5 -3
  47. package/src/function/memoize.ts +3 -1
  48. package/src/internal/array/find.ts +3 -3
  49. package/src/{array → internal/array}/index-of.ts +2 -2
  50. package/src/internal/array/insert.ts +13 -3
  51. package/src/internal/array/overlap.ts +35 -0
  52. package/src/internal/math/aggregate.ts +12 -2
  53. package/src/internal/number.ts +2 -2
  54. package/src/internal/result.ts +9 -1
  55. package/src/internal/value/compare.ts +8 -2
  56. package/src/internal/value/equal.ts +6 -2
  57. package/src/internal/value/set.ts +1 -6
  58. package/src/math.ts +11 -4
  59. package/src/promise/delay.ts +2 -2
  60. package/src/promise/helpers.ts +2 -6
  61. package/src/promise/index.ts +3 -3
  62. package/src/promise/misc.ts +2 -2
  63. package/src/promise/models.ts +2 -2
  64. package/src/promise/timed.ts +3 -3
  65. package/src/query.ts +3 -1
  66. package/src/queue.ts +3 -3
  67. package/src/string/case.ts +28 -12
  68. package/src/string/index.ts +7 -1
  69. package/src/string/match.ts +9 -3
  70. package/src/value/clone.ts +7 -1
  71. package/src/value/diff.ts +14 -4
  72. package/types/array/index.d.ts +4 -2
  73. package/types/array/move.d.ts +86 -0
  74. package/types/array/sort.d.ts +5 -2
  75. package/types/array/swap.d.ts +77 -0
  76. package/types/{array → internal/array}/index-of.d.ts +1 -1
  77. package/types/internal/array/insert.d.ts +3 -0
  78. package/types/internal/array/overlap.d.ts +11 -0
  79. package/types/internal/math/aggregate.d.ts +6 -1
  80. package/types/promise/models.d.ts +1 -1
@@ -5,21 +5,23 @@ import { find } from "./find.js";
5
5
  import { flatten } from "./flatten.js";
6
6
  import { range, times } from "./from.js";
7
7
  import { getArray } from "./get.js";
8
- import { indexOf } from "./index-of.js";
9
8
  import { chunk } from "../internal/array/chunk.js";
10
9
  import { compact } from "../internal/array/compact.js";
10
+ import { indexOf } from "../internal/array/index-of.js";
11
11
  import { shuffle } from "../internal/array/shuffle.js";
12
12
  import { insert } from "./insert.js";
13
13
  import { intersection } from "./intersection.js";
14
- import { partition } from "./partition.js";
15
14
  import { endsWithArray, getArrayPosition, includesArray, indexOfArray, startsWithArray } from "./position.js";
15
+ import { move } from "./move.js";
16
+ import { partition } from "./partition.js";
16
17
  import { push } from "./push.js";
17
18
  import { select } from "./select.js";
18
19
  import { drop, slice, take } from "./slice.js";
19
20
  import { sort } from "./sort.js";
20
21
  import { splice } from "./splice.js";
22
+ import { swap } from "./swap.js";
21
23
  import { toSet } from "./to-set.js";
22
24
  import { toggle } from "./toggle.js";
23
25
  import { union } from "./union.js";
24
26
  import { update } from "./update.js";
25
- export { chunk, compact, difference, drop, endsWithArray, exists, filter, find, flatten, getArray, getArrayPosition, includesArray, indexOf, indexOfArray, insert, intersection, partition, push, range, select, shuffle, slice, sort, splice, startsWithArray, take, times, toSet, toggle, union, update };
27
+ export { chunk, compact, difference, drop, endsWithArray, exists, filter, find, flatten, getArray, getArrayPosition, includesArray, indexOf, indexOfArray, insert, intersection, move, partition, push, range, select, shuffle, slice, sort, splice, startsWithArray, swap, take, times, toSet, toggle, union, update };
@@ -1,7 +1,7 @@
1
- import { insertValues } from "../internal/array/insert.js";
1
+ import { INSERT_TYPE_INSERT, insertValues } from "../internal/array/insert.js";
2
2
  //#region src/array/insert.ts
3
3
  function insert(array, indexOrItems, items) {
4
- return insertValues("insert", array, items == null ? indexOrItems : items, typeof indexOrItems === "number" ? indexOrItems : array?.length, 0);
4
+ return insertValues(INSERT_TYPE_INSERT, array, items == null ? indexOrItems : items, typeof indexOrItems === "number" ? indexOrItems : array?.length, 0);
5
5
  }
6
6
  //#endregion
7
7
  export { insert };
@@ -0,0 +1,64 @@
1
+ import { arraysOverlap } from "../internal/array/overlap.js";
2
+ import { indexOfArray } from "./position.js";
3
+ //#region src/array/move.ts
4
+ function move(array, from, to, key) {
5
+ if (!Array.isArray(array)) return [];
6
+ const firstArray = Array.isArray(from) ? from : [from];
7
+ const secondArray = Array.isArray(to) ? to : [to];
8
+ if (firstArray.length === 0 || secondArray.length === 0) return array;
9
+ const firstPosition = indexOfArray(array, firstArray, key);
10
+ const secondPosition = indexOfArray(array, secondArray, key);
11
+ if (firstPosition === -1 || secondPosition === -1 || firstPosition === secondPosition) return array;
12
+ const { overlap } = arraysOverlap({
13
+ array: firstArray,
14
+ index: firstPosition
15
+ }, {
16
+ array: secondArray,
17
+ index: secondPosition
18
+ });
19
+ if (overlap) return array;
20
+ array.splice(firstPosition, firstArray.length);
21
+ const next = secondPosition < firstPosition ? secondPosition : secondPosition + secondArray.length - firstArray.length;
22
+ if (next >= array.length) array.push(...firstArray);
23
+ else array.splice(next, 0, ...firstArray);
24
+ return array;
25
+ }
26
+ move.indices = moveIndices;
27
+ move.toIndex = moveToIndex;
28
+ /**
29
+ * Move an item from one index to another within an array
30
+ *
31
+ * If the from index is out of bounds, the array will be returned unchanged
32
+ * @param array Array to move within
33
+ * @param from Index to move from
34
+ * @param to Index to move to
35
+ * @returns Original array with item moved _(or unchanged if unable to move)_
36
+ */
37
+ function moveIndices(array, from, to) {
38
+ if (!Array.isArray(array)) return [];
39
+ const { length } = array;
40
+ if (length === 0 || typeof from !== "number" || typeof to !== "number") return array;
41
+ const fromIndex = from < 0 ? length + from : from;
42
+ const toIndex = to < 0 ? length + to : to;
43
+ if (fromIndex === toIndex || fromIndex >= length || toIndex >= length) return array;
44
+ const spliced = array.splice(fromIndex, 1);
45
+ if (toIndex >= array.length) array.push(...spliced);
46
+ else array.splice(toIndex, 0, ...spliced);
47
+ return array;
48
+ }
49
+ function moveToIndex(array, value, index, key) {
50
+ if (!Array.isArray(array)) return [];
51
+ const { length } = array;
52
+ if (length === 0 || typeof index !== "number") return array;
53
+ const next = index < 0 ? length + index : index;
54
+ if (next >= length) return array;
55
+ const values = Array.isArray(value) ? value : [value];
56
+ const position = indexOfArray(array, values, key);
57
+ if (position === -1 || position === next) return array;
58
+ array.splice(position, values.length);
59
+ if (next >= array.length) array.push(...values);
60
+ else array.splice(next, 0, ...values);
61
+ return array;
62
+ }
63
+ //#endregion
64
+ export { move };
@@ -1,4 +1,4 @@
1
- import { insertValues } from "../internal/array/insert.js";
1
+ import { INSERT_TYPE_PUSH, insertValues } from "../internal/array/insert.js";
2
2
  //#region src/array/push.ts
3
3
  /**
4
4
  * Push items into an array _(at the end)_
@@ -7,7 +7,7 @@ import { insertValues } from "../internal/array/insert.js";
7
7
  * @returns New length of the array
8
8
  */
9
9
  function push(array, pushed) {
10
- return insertValues("push", array, pushed, array.length, 0);
10
+ return insertValues(INSERT_TYPE_PUSH, array, pushed, array.length, 0);
11
11
  }
12
12
  //#endregion
13
13
  export { push };
@@ -33,7 +33,7 @@ function getSorter(value, modifier) {
33
33
  function sort(array, first, second) {
34
34
  if (!Array.isArray(array)) return [];
35
35
  if (array.length < 2) return array;
36
- const modifier = (first === true || second === true ? "desc" : "asc") === "asc" ? 1 : -1;
36
+ const modifier = (first === true || second === true ? "descending" : "ascending") === "ascending" ? 1 : -1;
37
37
  const sorters = (Array.isArray(first) ? first : [first]).map((item) => getSorter(item, modifier)).filter((sorter) => sorter != null).filter((current, index, filtered) => filtered.findIndex((next) => next.identifier === current.identifier) === index);
38
38
  const { length } = sorters;
39
39
  if (length === 0) return array.sort((firstItem, secondItem) => compare(firstItem, secondItem) * modifier);
@@ -58,5 +58,7 @@ function sort(array, first, second) {
58
58
  return 0;
59
59
  });
60
60
  }
61
+ var SORT_DIRECTION_ASCENDING = "ascending";
62
+ var SORT_DIRECTION_DESCENDING = "descending";
61
63
  //#endregion
62
- export { sort };
64
+ export { SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, sort };
@@ -1,7 +1,7 @@
1
- import { insertValues } from "../internal/array/insert.js";
1
+ import { INSERT_TYPE_SPLICE, insertValues } from "../internal/array/insert.js";
2
2
  //#region src/array/splice.ts
3
3
  function splice(array, start, deleteCountOrItems, items) {
4
- return insertValues("splice", array, typeof deleteCountOrItems === "number" ? items : deleteCountOrItems, start, typeof deleteCountOrItems === "number" ? deleteCountOrItems : 0);
4
+ return insertValues(INSERT_TYPE_SPLICE, array, typeof deleteCountOrItems === "number" ? items : deleteCountOrItems, start, typeof deleteCountOrItems === "number" ? deleteCountOrItems : 0);
5
5
  }
6
6
  //#endregion
7
7
  export { splice };
@@ -0,0 +1,59 @@
1
+ import { getArrayCallback } from "../internal/array/callbacks.js";
2
+ import { indexOf } from "../internal/array/index-of.js";
3
+ import { arraysOverlap } from "../internal/array/overlap.js";
4
+ import { indexOfArray } from "./position.js";
5
+ //#region src/array/swap.ts
6
+ function swap(array, first, second, third) {
7
+ if (!Array.isArray(array)) return [];
8
+ if (array.length === 0) return array;
9
+ if (Array.isArray(first) && Array.isArray(second)) return swapArrays(array, first, second, third);
10
+ return swapValues(array, first, second, third);
11
+ }
12
+ swap.indices = swapIndices;
13
+ function swapArrays(array, from, to, key) {
14
+ if (from.length === 0 || to.length === 0) return array;
15
+ if (from.length === 1 && to.length === 1) return swapValues(array, from[0], to[0], key);
16
+ const fromIndex = indexOfArray(array, from, key);
17
+ const toIndex = indexOfArray(array, to, key);
18
+ if (fromIndex === -1 || toIndex === -1) return array;
19
+ const { first, second, overlap } = arraysOverlap({
20
+ array: from,
21
+ index: fromIndex
22
+ }, {
23
+ array: to,
24
+ index: toIndex
25
+ });
26
+ if (overlap) return array;
27
+ array.splice(first.index, first.array.length, ...second.array);
28
+ array.splice(second.index + (second.array.length - first.array.length), second.array.length, ...first.array);
29
+ return array;
30
+ }
31
+ /**
32
+ * Swap two indiced items in an array
33
+ *
34
+ * If either index is out of bounds, the array will be returned unchanged
35
+ * @param array Array of items to swap
36
+ * @param first First index _(can be negative to count from the end)_
37
+ * @param second Second index _(can be negative to count from the end)_
38
+ * @returns Original array with items swapped _(or unchanged if unable to swap)_
39
+ */
40
+ function swapIndices(array, first, second) {
41
+ if (!Array.isArray(array) || array.length === 0) return [];
42
+ if (typeof first !== "number" || typeof second !== "number") return array;
43
+ const firstIndex = first < 0 ? array.length + first : first;
44
+ const secondIndex = second < 0 ? array.length + second : second;
45
+ if (firstIndex === secondIndex || firstIndex >= array.length || secondIndex >= array.length) return array;
46
+ const temp = array[firstIndex];
47
+ array[firstIndex] = array[secondIndex];
48
+ array[secondIndex] = temp;
49
+ return array;
50
+ }
51
+ function swapValues(array, from, to, key) {
52
+ const callback = getArrayCallback(key);
53
+ const first = callback == null ? array.indexOf(from) : indexOf(array, callback, callback(from, void 0, []));
54
+ const second = callback == null ? array.indexOf(to) : indexOf(array, callback, callback(to, void 0, []));
55
+ if (first === second || first === -1 || second === -1) return array;
56
+ return swapIndices(array, first, second);
57
+ }
58
+ //#endregion
59
+ export { swap };