es-toolkit 0.0.1 → 1.0.0-dev.24

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 (236) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +31 -0
  3. package/dist/array/chunk.d.ts +21 -5
  4. package/dist/array/difference.d.ts +20 -4
  5. package/dist/array/differenceBy.d.ts +23 -5
  6. package/dist/array/differenceWith.d.ts +19 -5
  7. package/dist/array/drop.d.ts +13 -4
  8. package/dist/array/dropRight.d.ts +13 -4
  9. package/dist/array/dropRightWhile.d.ts +15 -4
  10. package/dist/array/dropWhile.d.ts +15 -4
  11. package/dist/array/groupBy.d.ts +31 -1
  12. package/dist/array/groupBy.spec.d.ts +1 -0
  13. package/dist/array/index.js +437 -224
  14. package/dist/array/intersection.d.ts +13 -6
  15. package/dist/array/intersectionBy.d.ts +17 -8
  16. package/dist/array/intersectionWith.d.ts +18 -8
  17. package/dist/array/partition.d.ts +19 -5
  18. package/dist/array/sample.d.ts +10 -3
  19. package/dist/array/shuffle.d.ts +11 -4
  20. package/dist/array/takeRight.d.ts +0 -2
  21. package/dist/array/union.d.ts +13 -6
  22. package/dist/array/unionBy.d.ts +0 -3
  23. package/dist/array/unionWith.d.ts +15 -9
  24. package/dist/array/uniq.d.ts +9 -8
  25. package/dist/array/xor.d.ts +0 -2
  26. package/dist/array/zip.d.ts +18 -6
  27. package/dist/array/zipWith.d.ts +26 -3
  28. package/dist/function/debounce.d.ts +4 -4
  29. package/dist/function/index.js +4 -5
  30. package/dist/function/once.d.ts +0 -1
  31. package/dist/index.js +651 -248
  32. package/dist/math/clamp.d.ts +16 -8
  33. package/dist/math/index.js +114 -29
  34. package/dist/math/round.d.ts +11 -8
  35. package/dist/math/sum.d.ts +10 -9
  36. package/dist/object/index.js +122 -12
  37. package/dist/object/omit.d.ts +13 -4
  38. package/dist/object/omitBy.d.ts +19 -1
  39. package/dist/object/omitBy.spec.d.ts +1 -0
  40. package/dist/object/pick.d.ts +13 -4
  41. package/dist/object/pickBy.d.ts +19 -1
  42. package/dist/object/pickBy.spec.d.ts +1 -0
  43. package/dist/predicate/index.js +58 -10
  44. package/dist/predicate/isNil.d.ts +17 -3
  45. package/dist/predicate/isNotNil.d.ts +7 -3
  46. package/dist/predicate/isNull.d.ts +17 -2
  47. package/dist/predicate/isUndefined.d.ts +17 -2
  48. package/dist/promise/delay.d.ts +17 -4
  49. package/dist/promise/index.js +16 -3
  50. package/esm/array/_virtual/_rollupPluginBabelHelpers.mjs +1 -37
  51. package/esm/array/chunk.mjs +28 -25
  52. package/esm/array/difference.mjs +20 -4
  53. package/esm/array/differenceBy.mjs +23 -5
  54. package/esm/array/differenceWith.mjs +19 -5
  55. package/esm/array/drop.mjs +13 -4
  56. package/esm/array/dropRight.mjs +13 -4
  57. package/esm/array/dropRightWhile.mjs +15 -4
  58. package/esm/array/dropWhile.mjs +15 -4
  59. package/esm/array/groupBy.mjs +51 -2
  60. package/esm/array/intersection.mjs +13 -6
  61. package/esm/array/intersectionBy.mjs +17 -8
  62. package/esm/array/intersectionWith.mjs +18 -8
  63. package/esm/array/partition.mjs +19 -5
  64. package/esm/array/sample.mjs +10 -3
  65. package/esm/array/shuffle.mjs +12 -5
  66. package/esm/array/takeRight.mjs +0 -2
  67. package/esm/array/union.mjs +14 -10
  68. package/esm/array/unionBy.mjs +0 -3
  69. package/esm/array/unionWith.mjs +16 -11
  70. package/esm/array/uniq.mjs +28 -9
  71. package/esm/array/xor.mjs +0 -2
  72. package/esm/array/zip.mjs +18 -6
  73. package/esm/array/zipWith.mjs +26 -3
  74. package/esm/function/debounce.mjs +4 -4
  75. package/esm/function/once.mjs +0 -1
  76. package/esm/math/_virtual/_rollupPluginBabelHelpers.mjs +66 -0
  77. package/esm/math/clamp.mjs +15 -7
  78. package/esm/math/round.mjs +11 -8
  79. package/esm/math/sum.mjs +25 -14
  80. package/esm/object/_virtual/_rollupPluginBabelHelpers.mjs +37 -1
  81. package/esm/object/omit.mjs +13 -4
  82. package/esm/object/omitBy.mjs +32 -2
  83. package/esm/object/pick.mjs +13 -4
  84. package/esm/object/pickBy.mjs +32 -2
  85. package/esm/predicate/isNil.mjs +17 -3
  86. package/esm/predicate/isNotNil.mjs +7 -3
  87. package/esm/predicate/isNull.mjs +17 -2
  88. package/esm/predicate/isUndefined.mjs +17 -2
  89. package/esm/promise/delay.mjs +16 -3
  90. package/package.json +17 -3
  91. package/.vscode/extensions.json +0 -7
  92. package/.vscode/settings.json +0 -10
  93. package/.yarn/sdks/eslint/bin/eslint.js +0 -20
  94. package/.yarn/sdks/eslint/lib/api.js +0 -20
  95. package/.yarn/sdks/eslint/lib/unsupported-api.js +0 -20
  96. package/.yarn/sdks/eslint/package.json +0 -14
  97. package/.yarn/sdks/integrations.yml +0 -5
  98. package/.yarn/sdks/prettier/bin/prettier.cjs +0 -20
  99. package/.yarn/sdks/prettier/index.cjs +0 -20
  100. package/.yarn/sdks/prettier/package.json +0 -7
  101. package/.yarn/sdks/typescript/bin/tsc +0 -20
  102. package/.yarn/sdks/typescript/bin/tsserver +0 -20
  103. package/.yarn/sdks/typescript/lib/tsc.js +0 -20
  104. package/.yarn/sdks/typescript/lib/tsserver.js +0 -225
  105. package/.yarn/sdks/typescript/lib/tsserverlibrary.js +0 -225
  106. package/.yarn/sdks/typescript/lib/typescript.js +0 -20
  107. package/.yarn/sdks/typescript/package.json +0 -10
  108. package/babel.config.js +0 -6
  109. package/coverage/.tmp/coverage-0.json +0 -1
  110. package/coverage/.tmp/coverage-1.json +0 -1
  111. package/coverage/.tmp/coverage-10.json +0 -1
  112. package/coverage/.tmp/coverage-11.json +0 -1
  113. package/coverage/.tmp/coverage-12.json +0 -1
  114. package/coverage/.tmp/coverage-13.json +0 -1
  115. package/coverage/.tmp/coverage-14.json +0 -1
  116. package/coverage/.tmp/coverage-15.json +0 -1
  117. package/coverage/.tmp/coverage-16.json +0 -1
  118. package/coverage/.tmp/coverage-17.json +0 -1
  119. package/coverage/.tmp/coverage-18.json +0 -1
  120. package/coverage/.tmp/coverage-19.json +0 -1
  121. package/coverage/.tmp/coverage-2.json +0 -1
  122. package/coverage/.tmp/coverage-20.json +0 -1
  123. package/coverage/.tmp/coverage-21.json +0 -1
  124. package/coverage/.tmp/coverage-22.json +0 -1
  125. package/coverage/.tmp/coverage-23.json +0 -1
  126. package/coverage/.tmp/coverage-24.json +0 -1
  127. package/coverage/.tmp/coverage-25.json +0 -1
  128. package/coverage/.tmp/coverage-26.json +0 -1
  129. package/coverage/.tmp/coverage-27.json +0 -1
  130. package/coverage/.tmp/coverage-28.json +0 -1
  131. package/coverage/.tmp/coverage-29.json +0 -1
  132. package/coverage/.tmp/coverage-3.json +0 -1
  133. package/coverage/.tmp/coverage-30.json +0 -1
  134. package/coverage/.tmp/coverage-4.json +0 -1
  135. package/coverage/.tmp/coverage-5.json +0 -1
  136. package/coverage/.tmp/coverage-6.json +0 -1
  137. package/coverage/.tmp/coverage-7.json +0 -1
  138. package/coverage/.tmp/coverage-8.json +0 -1
  139. package/coverage/.tmp/coverage-9.json +0 -1
  140. package/rollup.config.js +0 -5
  141. package/src/array/chunk.spec.ts +0 -27
  142. package/src/array/chunk.ts +0 -24
  143. package/src/array/difference.spec.ts +0 -10
  144. package/src/array/difference.ts +0 -11
  145. package/src/array/differenceBy.spec.ts +0 -9
  146. package/src/array/differenceBy.ts +0 -14
  147. package/src/array/differenceWith.spec.ts +0 -8
  148. package/src/array/differenceWith.ts +0 -14
  149. package/src/array/drop.spec.ts +0 -9
  150. package/src/array/drop.ts +0 -9
  151. package/src/array/dropRight.spec.ts +0 -9
  152. package/src/array/dropRight.ts +0 -9
  153. package/src/array/dropRightWhile.spec.ts +0 -16
  154. package/src/array/dropRightWhile.ts +0 -13
  155. package/src/array/dropWhile.spec.ts +0 -14
  156. package/src/array/dropWhile.ts +0 -10
  157. package/src/array/groupBy.ts +0 -3
  158. package/src/array/index.ts +0 -30
  159. package/src/array/intersection.spec.ts +0 -11
  160. package/src/array/intersection.ts +0 -15
  161. package/src/array/intersectionBy.spec.ts +0 -9
  162. package/src/array/intersectionBy.ts +0 -18
  163. package/src/array/intersectionWith.spec.ts +0 -9
  164. package/src/array/intersectionWith.ts +0 -18
  165. package/src/array/partition.spec.ts +0 -23
  166. package/src/array/partition.ts +0 -21
  167. package/src/array/sample.spec.ts +0 -11
  168. package/src/array/sample.ts +0 -10
  169. package/src/array/shuffle.spec.ts +0 -11
  170. package/src/array/shuffle.ts +0 -18
  171. package/src/array/take.spec.ts +0 -25
  172. package/src/array/take.ts +0 -25
  173. package/src/array/takeRight.spec.ts +0 -25
  174. package/src/array/takeRight.ts +0 -29
  175. package/src/array/takeRightWhile.spec.ts +0 -16
  176. package/src/array/takeRightWhile.ts +0 -26
  177. package/src/array/takeWhile.spec.ts +0 -34
  178. package/src/array/takeWhile.ts +0 -31
  179. package/src/array/union.spec.ts +0 -10
  180. package/src/array/union.ts +0 -14
  181. package/src/array/unionBy.spec.ts +0 -12
  182. package/src/array/unionBy.ts +0 -35
  183. package/src/array/unionWith.spec.ts +0 -23
  184. package/src/array/unionWith.ts +0 -18
  185. package/src/array/uniq.spec.ts +0 -8
  186. package/src/array/uniq.ts +0 -16
  187. package/src/array/uniqBy.spec.ts +0 -8
  188. package/src/array/uniqBy.ts +0 -17
  189. package/src/array/uniqWith.spec.ts +0 -16
  190. package/src/array/uniqWith.ts +0 -29
  191. package/src/array/xor.spec.ts +0 -13
  192. package/src/array/xor.ts +0 -25
  193. package/src/array/xorBy.spec.ts +0 -16
  194. package/src/array/xorBy.ts +0 -29
  195. package/src/array/xorWith.spec.ts +0 -16
  196. package/src/array/xorWith.ts +0 -28
  197. package/src/array/zip.spec.ts +0 -14
  198. package/src/array/zip.ts +0 -34
  199. package/src/array/zipWith.spec.ts +0 -13
  200. package/src/array/zipWith.ts +0 -23
  201. package/src/function/debounce.spec.ts +0 -76
  202. package/src/function/debounce.ts +0 -42
  203. package/src/function/index.ts +0 -3
  204. package/src/function/once.spec.ts +0 -31
  205. package/src/function/once.ts +0 -34
  206. package/src/function/throttle.spec.ts +0 -32
  207. package/src/function/throttle.ts +0 -39
  208. package/src/index.ts +0 -6
  209. package/src/math/clamp.spec.ts +0 -18
  210. package/src/math/clamp.ts +0 -17
  211. package/src/math/index.ts +0 -3
  212. package/src/math/round.spec.ts +0 -53
  213. package/src/math/round.ts +0 -16
  214. package/src/math/sum.spec.ts +0 -29
  215. package/src/math/sum.ts +0 -17
  216. package/src/object/index.ts +0 -4
  217. package/src/object/omit.spec.ts +0 -10
  218. package/src/object/omit.ts +0 -15
  219. package/src/object/omitBy.ts +0 -3
  220. package/src/object/pick.spec.ts +0 -10
  221. package/src/object/pick.ts +0 -15
  222. package/src/object/pickBy.ts +0 -3
  223. package/src/predicate/index.ts +0 -4
  224. package/src/predicate/isNil.spec.ts +0 -23
  225. package/src/predicate/isNil.ts +0 -8
  226. package/src/predicate/isNotNil.spec.ts +0 -29
  227. package/src/predicate/isNotNil.ts +0 -14
  228. package/src/predicate/isNull.spec.ts +0 -23
  229. package/src/predicate/isNull.ts +0 -8
  230. package/src/predicate/isUndefined.spec.ts +0 -23
  231. package/src/predicate/isUndefined.ts +0 -8
  232. package/src/promise/delay.spec.ts +0 -13
  233. package/src/promise/delay.ts +0 -10
  234. package/src/promise/index.ts +0 -1
  235. package/tsconfig.json +0 -12
  236. package/vitest.config.mts +0 -13
@@ -1,34 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { takeWhile } from './takeWhile'; // adjust the import path as necessary
3
-
4
- describe('takeWhile', () => {
5
- it('should return elements while the predicate is true', () => {
6
- const arr = [1, 2, 3, 4, 5];
7
- const result = takeWhile(arr, x => x < 4);
8
- expect(result).toEqual([1, 2, 3]);
9
- });
10
-
11
- it('should return an empty array if the first element does not satisfy the predicate', () => {
12
- const arr = [1, 2, 3, 4, 5];
13
- const result = takeWhile(arr, x => x > 4);
14
- expect(result).toEqual([]);
15
- });
16
-
17
- it('should return all elements if all satisfy the predicate', () => {
18
- const arr = [1, 2, 3];
19
- const result = takeWhile(arr, x => x < 4);
20
- expect(result).toEqual([1, 2, 3]);
21
- });
22
-
23
- it('should return an empty array if the input array is empty', () => {
24
- const arr: number[] = [];
25
- const result = takeWhile(arr, x => x < 4);
26
- expect(result).toEqual([]);
27
- });
28
-
29
- it('should handle complex predicate functions', () => {
30
- const arr = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
31
- const result = takeWhile(arr, item => item.id < 3);
32
- expect(result).toEqual([{ id: 1 }, { id: 2 }]);
33
- });
34
- });
@@ -1,31 +0,0 @@
1
- /**
2
- * Returns a new array containing the leading elements of the provided array
3
- * that satisfy the provided predicate function. It stops taking elements as soon
4
- * as an element does not satisfy the predicate.
5
- *
6
- * @template T - The type of elements in the array.
7
- * @param {T[]} arr - The array to process.
8
- * @param {(element: T) => boolean} shouldContinueTaking - The predicate function that is called with each element. Elements are included in the result as long as this function returns true.
9
- * @returns {T[]} A new array containing the leading elements that satisfy the predicate.
10
- *
11
- * @example
12
- * // Returns [1, 2]
13
- * takeWhile([1, 2, 3, 4], x => x < 3);
14
- *
15
- * @example
16
- * // Returns []
17
- * takeWhile([1, 2, 3, 4], x => x > 3);
18
- */
19
- export function takeWhile<T>(arr: T[], shouldContinueTaking: (element: T) => boolean): T[] {
20
- const result: T[] = [];
21
-
22
- for (const item of arr) {
23
- if (!shouldContinueTaking(item)) {
24
- break;
25
- }
26
-
27
- result.push(item);
28
- }
29
-
30
- return result;
31
- }
@@ -1,10 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { union } from "./union";
3
-
4
- describe("union", () => {
5
- it("uniq function should return the union of two arrays", () => {
6
- expect(union([2, 1], [2])).toEqual([2, 1]);
7
- expect(union([2, 1], [2], [2, 3])).toEqual([2, 1, 3]);
8
- expect(union([2, 1], [2], [1, [5]])).toEqual([2, 1, [5]]);
9
- });
10
- });
@@ -1,14 +0,0 @@
1
- import { uniq } from "./uniq";
2
-
3
- /**
4
- * `union` function returns the union of the arrays passed as arguments.
5
- *
6
- * @example
7
- * ```ts
8
- * union([2], [1, 2]);
9
- * // [2, 1]
10
- * ```
11
- */
12
- export function union<T>(...args: T[][]): T[] {
13
- return uniq(args.flat());
14
- }
@@ -1,12 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { unionBy } from "./unionBy";
3
-
4
- describe("unionBy", () => {
5
- it("should work with a `converter`", () => {
6
- expect(unionBy([2.1], [1.2, 2.3], Math.floor)).toEqual([2.1, 1.2]);
7
- expect(unionBy([{ x: 1 }], [{ x: 2 }, { x: 1 }], ({ x }) => x)).toEqual([
8
- { x: 1 },
9
- { x: 2 },
10
- ]);
11
- });
12
- });
@@ -1,35 +0,0 @@
1
- /**
2
- * Creates an array of unique values, in order, from all given arrays using a provided mapping function to determine equality.
3
- *
4
- * @template T - Type of elements in the input arrays.
5
- * @template U - Type of the values returned by the mapping function.
6
- *
7
- * @param {T[]} arr1 - The first array.
8
- * @param {T[]} arr2 - The second array.
9
- * @param {function(item: T): U} mapper - The function to map array elements to comparison values.
10
- * @returns {T[]} A new array containing the union of unique elements from `arr1` and `arr2`, based on the values returned by the mapping function.
11
- *
12
- * @example
13
- * // Custom mapping function for numbers (modulo comparison)
14
- * const moduloMapper = (x) => x % 3;
15
- * unionBy([1, 2, 3], [4, 5, 6], moduloMapper);
16
- * // Returns [1, 2, 3]
17
- *
18
- * @example
19
- * // Custom mapping function for objects with an 'id' property
20
- * const idMapper = (obj) => obj.id;
21
- * unionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
22
- * // Returns [{ id: 1 }, { id: 2 }, { id: 3 }]
23
- */
24
- export function unionBy<T, U>(arr1: T[], arr2: T[], mapper: (item: T) => U): T[] {
25
- const map = new Map<U, T>();
26
-
27
- for (const item of [...arr1, ...arr2]) {
28
- const key = mapper(item);
29
- if (!map.has(key)) {
30
- map.set(key, item);
31
- }
32
- }
33
-
34
- return [...map.values()];
35
- }
@@ -1,23 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { unionWith } from "./unionWith";
3
-
4
- describe("unionWith", () => {
5
- it("should work with a `comparator`", () => {
6
- expect(
7
- unionWith(
8
- [
9
- { x: 1, y: 2 },
10
- { x: 2, y: 1 },
11
- ],
12
- [
13
- { x: 1, y: 1 },
14
- { x: 1, y: 2 },
15
- ],
16
- (a, b) => a.x === b.x
17
- )
18
- ).toEqual([
19
- { x: 1, y: 2 },
20
- { x: 2, y: 1 },
21
- ]);
22
- });
23
- });
@@ -1,18 +0,0 @@
1
- import { uniqWith } from "./uniqWith";
2
-
3
- /**
4
- * `unionWith` function returns the union of multiple arrays passed as arguments. During this process, it removes any duplicated elements.
5
- *
6
- * @example
7
- * ```ts
8
- * unionWith(
9
- * [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }],
10
- * [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }],
11
- * (x, y) => x === y
12
- * );
13
- * // [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
14
- * ```
15
- */
16
- export function unionWith<T>(arr1: T[], arr2: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[] {
17
- return uniqWith([...arr1, ...arr2], areItemsEqual);
18
- }
@@ -1,8 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { uniq } from "./uniq";
3
-
4
- describe("uniq", () => {
5
- it("uniq function creates unique elements from the array passed as an argument.", () => {
6
- expect(uniq([11, 2, 3, 44, 11, 2, 3])).toEqual([11, 2, 3, 44]);
7
- });
8
- });
package/src/array/uniq.ts DELETED
@@ -1,16 +0,0 @@
1
- /**
2
- * @name uniq
3
- * @description
4
- * `uniq` removes duplicated elements from the array given as an argument.
5
- *
6
- * It filters out elements with the same value, so duplicates of data types like Objects are not checked.
7
- *
8
- * @example
9
- * ```ts
10
- * uniq([1, 1, 2, 3, 5, 5, 7, 'foo', 'bar', 'bar']);
11
- * // [1, 2, 3, 5, 7, 'foo', 'bar']
12
- * ```
13
- */
14
- export function uniq<T>(arr: T[]): T[] {
15
- return Array.from(new Set(arr));
16
- }
@@ -1,8 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { uniqBy } from "./uniqBy";
3
-
4
- describe("uniqBy", () => {
5
- it("should work with a `converter`", () => {
6
- expect(uniqBy([2.1, 1.2, 2.3], Math.floor)).toEqual([2.1, 1.2]);
7
- });
8
- });
@@ -1,17 +0,0 @@
1
- import { uniq } from "./uniq";
2
- import { uniqWith } from "./uniqWith";
3
-
4
- /**
5
- * The `uniqBy` function takes an array as its first argument and a 'converter' function as the second. It maps the array elements using the converter function, then removes any duplicates.
6
- *
7
- * It filters out elements with the same value, meaning it does not check for duplicates in data types like Objects.
8
- *
9
- * @example
10
- * ```ts
11
- * uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
12
- * // [1.2, 2.1, 3.3, 5.7, 7.19]
13
- * ```
14
- */
15
- export function uniqBy<T, U>(arr: T[], converter: (item: T) => U): T[] {
16
- return uniqWith(arr, (item1, item2) => converter(item1) === converter(item2));
17
- }
@@ -1,16 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { uniqWith } from "./uniqWith";
3
-
4
- describe("uniqWith", () => {
5
- it("should work with a `comparator`", () => {
6
- expect(
7
- uniqWith(
8
- [
9
- { x: 1, y: 2 },
10
- { x: 1, y: 3 },
11
- ],
12
- (a, b) => a.x === b.x
13
- )
14
- ).toEqual([{ x: 1, y: 2 }]);
15
- });
16
- });
@@ -1,29 +0,0 @@
1
- import { uniq } from "./uniq";
2
-
3
- /**
4
- * The `uniqWith` function takes an array as its first argument and a 'comparator' function as the second.
5
- *
6
- * It evaluates the elements of the array using the comparator function, and if true is returned, it considers those elements as duplicates and removes them.
7
- *
8
- * @example
9
- * ```ts
10
- * uniqWith([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], (a, b) => );
11
- * // [1, 2, 3, 5, 7]
12
- * ```
13
- */
14
- export function uniqWith<T>(
15
- arr: T[],
16
- areItemsEqual: (item1: T, item2: T) => boolean
17
- ): T[] {
18
- const result: T[] = [];
19
-
20
- for (const item of arr) {
21
- const isUniq = result.every((v) => !areItemsEqual(v, item));
22
-
23
- if (isUniq) {
24
- result.push(item);
25
- }
26
- }
27
-
28
- return result;
29
- }
@@ -1,13 +0,0 @@
1
- import { describe, expect, it } from "vitest"
2
- import { xor } from "./xor"
3
-
4
- describe('xor', () => {
5
- it('computes the symmetric difference between two arrays', () => {
6
- expect(xor([1, 2, 3, 4], [3, 4, 5, 6])).toEqual([1, 2, 5, 6]);
7
- expect(xor(['a', 'b'], ['b', 'c'])).toEqual(['a', 'c']);
8
- expect(xor([1, 2, 3], [4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6]);
9
- expect(xor([1, 2, 3], [1, 2, 3])).toEqual([]);
10
- expect(xor([], [1, 2, 3])).toEqual([1, 2, 3]);
11
- expect(xor([1, 2, 3], [])).toEqual([1, 2, 3]);
12
- })
13
- })
package/src/array/xor.ts DELETED
@@ -1,25 +0,0 @@
1
- import { difference } from "./difference";
2
- import { intersection } from "./intersection";
3
- import { union } from "./union";
4
-
5
- /**
6
- * Computes the symmetric difference between two arrays. The symmetric difference is the set of elements
7
- * which are in either of the arrays, but not in their intersection.
8
- *
9
- * @template T - Type of elements in the input arrays.
10
- *
11
- * @param {T[]} arr1 - The first array.
12
- * @param {T[]} arr2 - The second array.
13
- * @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both.
14
- *
15
- * @example
16
- * // Returns [1, 2, 5, 6]
17
- * xor([1, 2, 3, 4], [3, 4, 5, 6]);
18
- *
19
- * @example
20
- * // Returns ['a', 'c']
21
- * xor(['a', 'b'], ['b', 'c']);
22
- */
23
- export function xor<T>(arr1: T[], arr2: T[]): T[] {
24
- return difference(union(arr1, arr2), intersection(arr1, arr2));
25
- }
@@ -1,16 +0,0 @@
1
- import { describe, expect, it } from "vitest"
2
- import { xorBy } from "./xorBy"
3
-
4
- describe('xorBy', () => {
5
- it('computes the symmetric difference between two arrays using a custom mapping function', () => {
6
- const identity = (x: number) => x;
7
- const idMapper = (obj: { id: number }) => obj.id;
8
-
9
- expect(xorBy([1, 2, 3, 4], [3, 4, 5, 6], identity)).toEqual([1, 2, 5, 6]);
10
- expect(xorBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper)).toEqual([{ id: 1 }, { id: 3 }]);
11
- expect(xorBy([1, 2, 3], [4, 5, 6], identity)).toEqual([1, 2, 3, 4, 5, 6]);
12
- expect(xorBy([1, 2, 3], [1, 2, 3], identity)).toEqual([]);
13
- expect(xorBy([], [1, 2, 3], identity)).toEqual([1, 2, 3]);
14
- expect(xorBy([1, 2, 3], [], identity)).toEqual([1, 2, 3]);
15
- });
16
- });
@@ -1,29 +0,0 @@
1
- import { differenceBy } from "./differenceBy";
2
- import { intersectionBy } from "./intersectionBy";
3
- import { unionBy } from "./unionBy";
4
-
5
- /**
6
- * Computes the symmetric difference between two arrays using a custom mapping function.
7
- * The symmetric difference is the set of elements which are in either of the arrays,
8
- * but not in their intersection, determined by the result of the mapping function.
9
- *
10
- * @template T - Type of elements in the input arrays.
11
- * @template U - Type of the values returned by the mapping function.
12
- *
13
- * @param {T[]} arr1 - The first array.
14
- * @param {T[]} arr2 - The second array.
15
- * @param {function(item: T): U} mapper - The function to map array elements to comparison values.
16
- * @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both, based on the values returned by the mapping function.
17
- *
18
- * @example
19
- * // Custom mapping function for objects with an 'id' property
20
- * const idMapper = obj => obj.id;
21
- * xorBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
22
- * // Returns [{ id: 1 }, { id: 3 }]
23
- */
24
- export function xorBy<T, U>(arr1: T[], arr2: T[], mapper: (item: T) => U): T[] {
25
- const union = unionBy(arr1, arr2, mapper);
26
- const intersection = intersectionBy(arr1, arr2, mapper);
27
-
28
- return differenceBy(union, intersection, mapper);
29
- }
@@ -1,16 +0,0 @@
1
- import { describe, expect, it } from "vitest"
2
- import { xorWith } from "./xorWith"
3
-
4
- describe('xorWith', () => {
5
- it('computes the symmetric difference between two arrays using a custom equality function', () => {
6
- const areNumbersEqual = (a: number, b: number) => a === b;
7
- const areObjectsEqual = (a: { id: number }, b: { id: number }) => a.id === b.id;
8
-
9
- expect(xorWith([1, 2, 3, 4], [3, 4, 5, 6], areNumbersEqual)).toEqual([1, 2, 5, 6]);
10
- expect(xorWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], areObjectsEqual)).toEqual([{ id: 1 }, { id: 3 }]);
11
- expect(xorWith([1, 2, 3], [4, 5, 6], areNumbersEqual)).toEqual([1, 2, 3, 4, 5, 6]);
12
- expect(xorWith([1, 2, 3], [1, 2, 3], areNumbersEqual)).toEqual([]);
13
- expect(xorWith([], [1, 2, 3], areNumbersEqual)).toEqual([1, 2, 3]);
14
- expect(xorWith([1, 2, 3], [], areNumbersEqual)).toEqual([1, 2, 3]);
15
- })
16
- })
@@ -1,28 +0,0 @@
1
- import { differenceWith } from "./differenceWith";
2
- import { intersectionWith } from "./intersectionWith";
3
- import { unionWith } from "./unionWith";
4
-
5
- /**
6
- * Computes the symmetric difference between two arrays using a custom equality function.
7
- * The symmetric difference is the set of elements which are in either of the arrays,
8
- * but not in their intersection.
9
- *
10
- * @template T - Type of elements in the input arrays.
11
- *
12
- * @param {T[]} arr1 - The first array.
13
- * @param {T[]} arr2 - The second array.
14
- * @param {(item1: T, item2: T) => boolean} areElementsEqual - The custom equality function to compare elements.
15
- * @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both, based on the custom equality function.
16
- *
17
- * @example
18
- * // Custom equality function for objects with an 'id' property
19
- * const areObjectsEqual = (a, b) => a.id === b.id;
20
- * xorWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], areObjectsEqual);
21
- * // Returns [{ id: 1 }, { id: 3 }]
22
- */
23
- export function xorWith<T>(arr1: T[], arr2: T[], areElementsEqual: (item1: T, item2: T) => boolean): T[] {
24
- const union = unionWith(arr1, arr2, areElementsEqual);
25
- const intersection = intersectionWith(arr1, arr2, areElementsEqual);
26
-
27
- return differenceWith(union, intersection, areElementsEqual);
28
- }
@@ -1,14 +0,0 @@
1
- import { describe, expect, it } from "vitest"
2
- import { zip } from "./zip"
3
-
4
- describe('zip', () => {
5
- it('zips multiple arrays to create a tuple', () => {
6
- expect(zip([1, 2, 3])).toEqual([[1], [2], [3]]);
7
-
8
- expect(zip([1, 2, 3], ['a', 'b', 'c'])).toEqual([[1, 'a'], [2, 'b'], [3, 'c']]);
9
- expect(zip([1, 2, 3], ['a', 'b'])).toEqual([[1, 'a'], [2, 'b'], [3, undefined]]);
10
-
11
- expect(zip([1, 2, 3], ['a', 'b', 'c'], [true, true, false])).toEqual([[1, 'a', true], [2, 'b', true], [3, 'c', false]]);
12
- expect(zip([1], ['a'], [true], [null])).toEqual([[1, 'a', true, null]]);
13
- })
14
- })
package/src/array/zip.ts DELETED
@@ -1,34 +0,0 @@
1
- import { zipWith } from "./zipWith";
2
-
3
- /**
4
- * Merges multiple arrays element-wise. Returns an array where each element is an array containing
5
- * elements from the input arrays at the corresponding index.
6
- *
7
- * @param {T[]} arr1 - The first array to be zipped.
8
- * @param {T[]} arr2 - The second array to be zipped.
9
- * @param {T[]} arr3 - The third array to be zipped.
10
- * @param {T[]} arr4 - The fourth array to be zipped.
11
- */
12
- export function zip<T>(arr1: T[]): [T][];
13
- export function zip<T, U>(arr1: T[], arr2: U[]): [T, U][];
14
- export function zip<T, U, V>(arr1: T[], arr2: U[], arr3: V[]): [T, U, V][];
15
- export function zip<T, U, V, W>(arr1: T[], arr2: U[], arr3: V[], arr4: W[]): [T, U, V, W][];
16
- export function zip<T>(...arrs: T[][]): T[][] {
17
- const result: T[][] = [];
18
-
19
- const maxIndex = Math.max(...arrs.map(x => x.length));
20
-
21
- for (let i = 0; i < maxIndex; i++) {
22
- const element: T[] = [];
23
-
24
- for (const arr of arrs) {
25
- element.push(arr[i]);
26
- }
27
-
28
- result.push(element);
29
- }
30
-
31
- return result;
32
- }
33
-
34
-
@@ -1,13 +0,0 @@
1
- import { describe, expect, it } from "vitest"
2
- import { zipWith } from "./zipWith"
3
-
4
- describe('zipWith', () => {
5
- it('zips multiple arrays with the given combine function', () => {
6
- expect(zipWith([1, 2, 3], x => x)).toEqual([1, 2, 3]);
7
-
8
- expect(zipWith([1, 2, 3], ['a', 'b', 'c'], (x, y) => `${x}${y}`)).toEqual(['1a', '2b', '3c']);
9
- expect(zipWith([1, 2, 3], ['a', 'b'], (x, y) => `${x}${y}`)).toEqual(['1a', '2b', '3undefined']);
10
-
11
- expect(zipWith([1, 2, 3], ['a', 'b', 'c'], [true, true, false], (x, y, z) => `${x}-${y}-${z}`)).toEqual([`1-a-true`, `2-b-true`, `3-c-false`]);
12
- })
13
- })
@@ -1,23 +0,0 @@
1
- /**
2
- * Combines multiple arrays element-wise using a provided combining function.
3
- * Returns an array where each element is the result of applying the combining function
4
- * to the corresponding elements from the input arrays.
5
- */
6
- export function zipWith<T, R>(arr1: T[], combine: (item: T) => R): R[];
7
- export function zipWith<T, U, R>(arr1: T[], arr2: U[], combine: (item1: T, item2: U) => R): R[];
8
- export function zipWith<T, U, V, R>(arr1: T[], arr2: U[], arr3: V[], combine: (item1: T, item2: U, item3: V) => R): R[];
9
- export function zipWith<T, U, V, W, R>(arr1: T[], arr2: U[], arr3: V[], arr4: W[], combine: (item1: T, item2: U, item3: V, item4: W) => R): R[];
10
- export function zipWith<T, R>(arr1: T[], ...rest: any[]): R[] {
11
- const arrs = [arr1, ...rest.slice(0, -1)];
12
- const combine = rest[rest.length - 1] as (...items: T[]) => R;
13
-
14
- const result: R[] = [];
15
- const maxIndex = Math.max(...arrs.map(arr => arr.length));
16
-
17
- for (let i = 0; i < maxIndex; i++) {
18
- const elements: T[] = arrs.map(arr => arr[i]);
19
- result.push(combine(...elements));
20
- }
21
-
22
- return result;
23
- }
@@ -1,76 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest';
2
- import { debounce } from './debounce'; // adjust the import path as necessary
3
- import { delay } from '../promise';
4
-
5
- describe('debounce', () => {
6
- it('should debounce function calls', async () => {
7
- const func = vi.fn();
8
- const waitMs = 50;
9
- const debouncedFunc = debounce(func, waitMs);
10
-
11
- debouncedFunc();
12
- debouncedFunc();
13
- debouncedFunc();
14
-
15
- await delay(waitMs * 2)
16
-
17
- expect(func).toHaveBeenCalledTimes(1);
18
- });
19
-
20
- it('should delay the function call by the specified wait time', async () => {
21
- const func = vi.fn();
22
- const waitMs = 50;
23
- const debouncedFunc = debounce(func, waitMs);
24
-
25
- debouncedFunc();
26
- await delay(waitMs / 2);
27
- expect(func).not.toHaveBeenCalled();
28
-
29
- await delay(waitMs / 2 + 1);
30
- expect(func).toHaveBeenCalledTimes(1);
31
- });
32
-
33
- it('should reset the wait time if called again before wait time ends', async () => {
34
- const func = vi.fn();
35
- const waitMs = 50;
36
- const debouncedFunc = debounce(func, waitMs);
37
-
38
- debouncedFunc();
39
- await delay(waitMs / 2);
40
- debouncedFunc();
41
- await delay(waitMs / 2);
42
- debouncedFunc();
43
- await delay(waitMs / 2);
44
- debouncedFunc();
45
-
46
- expect(func).not.toHaveBeenCalled();
47
-
48
- await delay(waitMs + 1);
49
- expect(func).toHaveBeenCalledTimes(1);
50
- });
51
-
52
- it('should cancel the debounced function call', async () => {
53
- const func = vi.fn();
54
- const waitMs = 50;
55
- const debouncedFunc = debounce(func, waitMs);
56
-
57
- debouncedFunc();
58
- debouncedFunc.cancel();
59
- await delay(waitMs);
60
-
61
- expect(func).not.toHaveBeenCalled();
62
- });
63
-
64
- it('should work correctly if the debounced function is called after the wait time', async () => {
65
- const func = vi.fn();
66
- const waitMs = 50;
67
- const debouncedFunc = debounce(func, waitMs);
68
-
69
- debouncedFunc();
70
- await delay(waitMs + 1);
71
- debouncedFunc();
72
- await delay(waitMs + 1);
73
-
74
- expect(func).toHaveBeenCalledTimes(2);
75
- });
76
- });
@@ -1,42 +0,0 @@
1
- /**
2
- * Creates a debounced function that delays invoking the provided function until after `waitMs` milliseconds
3
- * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
4
- * method to cancel any pending execution.
5
- *
6
- * @param {() => void} func - The function to debounce.
7
- * @param {number} waitMs - The number of milliseconds to delay.
8
- * @returns {{ (): void; cancel: () => void }} A new debounced function with a `cancel` method.
9
- *
10
- * @example
11
- * const debouncedFunction = debounce(() => {
12
- * console.log('Function executed');
13
- * }, 1000);
14
- *
15
- * // Will log 'Function executed' after 1 second if not called again in that time
16
- * debouncedFunction();
17
- *
18
- * // Will not log anything as the previous call is canceled
19
- * debouncedFunction.cancel();
20
- */
21
- export function debounce(func: () => void, waitMs: number) {
22
- let timeoutId: number | NodeJS.Timeout | null = null;
23
-
24
- const debounced = function () {
25
- if (timeoutId != null) {
26
- clearTimeout(timeoutId);
27
- }
28
-
29
-
30
- timeoutId = setTimeout(() => {
31
- func();
32
- }, waitMs);
33
- };
34
-
35
- debounced.cancel = function () {
36
- if (timeoutId != null) {
37
- clearTimeout(timeoutId);
38
- }
39
- }
40
-
41
- return debounced;
42
- }
@@ -1,3 +0,0 @@
1
- export { debounce } from './debounce';
2
- export { once } from './once';
3
- export { throttle } from './throttle';