moderndash 0.0.5

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 (100) hide show
  1. package/.ctiignore +3 -0
  2. package/package.json +41 -0
  3. package/src/array/chunk.ts +31 -0
  4. package/src/array/difference.ts +21 -0
  5. package/src/array/differenceBy.ts +30 -0
  6. package/src/array/differenceWith.ts +31 -0
  7. package/src/array/dropRightWhile.ts +28 -0
  8. package/src/array/dropWhile.ts +24 -0
  9. package/src/array/index.ts +24 -0
  10. package/src/array/intersection.ts +20 -0
  11. package/src/array/intersectionBy.ts +26 -0
  12. package/src/array/intersectionWith.ts +33 -0
  13. package/src/array/sample.ts +18 -0
  14. package/src/array/sampleSize.ts +31 -0
  15. package/src/array/shuffle.ts +33 -0
  16. package/src/array/takeRightWhile.ts +33 -0
  17. package/src/array/takeWhile.ts +33 -0
  18. package/src/array/union.ts +16 -0
  19. package/src/array/unionBy.ts +26 -0
  20. package/src/array/unionWith.ts +31 -0
  21. package/src/array/uniq.ts +18 -0
  22. package/src/array/uniqBy.ts +25 -0
  23. package/src/array/uniqWith.ts +20 -0
  24. package/src/array/unzip.ts +20 -0
  25. package/src/array/unzipWith.ts +26 -0
  26. package/src/array/zip.ts +17 -0
  27. package/src/array/zipWith.ts +28 -0
  28. package/src/collection/countBy.ts +38 -0
  29. package/src/collection/groupBy.ts +33 -0
  30. package/src/collection/index.ts +3 -0
  31. package/src/collection/sortBy.ts +15 -0
  32. package/src/function/after.ts +23 -0
  33. package/src/function/before.ts +27 -0
  34. package/src/function/debounce.ts +124 -0
  35. package/src/function/index.ts +6 -0
  36. package/src/function/memoize.ts +59 -0
  37. package/src/function/once.ts +19 -0
  38. package/src/function/throttle.ts +11 -0
  39. package/src/helpers/collections.ts +5 -0
  40. package/src/helpers/shortHands.ts +17 -0
  41. package/src/helpers/stringModifiers.ts +18 -0
  42. package/src/helpers/typeofChecks.ts +3 -0
  43. package/src/index.ts +7 -0
  44. package/src/lang/index.ts +4 -0
  45. package/src/lang/isEmpty.ts +23 -0
  46. package/src/lang/isEqual.ts +54 -0
  47. package/src/lang/isEqualWith.ts +5 -0
  48. package/src/lang/isPlainObject.ts +3 -0
  49. package/src/object/index.ts +1 -0
  50. package/src/object/pick.ts +22 -0
  51. package/src/string/camelCase.ts +15 -0
  52. package/src/string/capitalize.ts +3 -0
  53. package/src/string/deburr.ts +5 -0
  54. package/src/string/escape.ts +10 -0
  55. package/src/string/escapeRegExp.ts +3 -0
  56. package/src/string/index.ts +11 -0
  57. package/src/string/kebabCase.ts +10 -0
  58. package/src/string/pascalCase.ts +10 -0
  59. package/src/string/snakeCase.ts +13 -0
  60. package/src/string/startCase.ts +10 -0
  61. package/src/string/stripSpecialChars.ts +6 -0
  62. package/src/string/unescape.ts +10 -0
  63. package/src/types.ts +9 -0
  64. package/test/.eslintrc.cjs +5 -0
  65. package/test/array/chunk.test.ts +19 -0
  66. package/test/array/differenceMethods.test.ts +49 -0
  67. package/test/array/dropMethods.test.ts +30 -0
  68. package/test/array/intersectionMethods.test.ts +36 -0
  69. package/test/array/shuffle.test.ts +17 -0
  70. package/test/array/takeMethods.test.ts +41 -0
  71. package/test/array/unionMethods.test.ts +41 -0
  72. package/test/array/uniqMethods.test.ts +18 -0
  73. package/test/array/unzipMethods.test.ts +13 -0
  74. package/test/array/zipMethods.test.ts +48 -0
  75. package/test/collection/countBy.test.ts +13 -0
  76. package/test/collection/groupBy.test.ts +35 -0
  77. package/test/collection/sample.test.ts +17 -0
  78. package/test/collection/sampleSize.test.ts +33 -0
  79. package/test/collection/sortBy.test.ts +31 -0
  80. package/test/function/after.test.ts +19 -0
  81. package/test/function/before.test.ts +19 -0
  82. package/test/function/debounce.test.ts +47 -0
  83. package/test/function/memoize.test.ts +33 -0
  84. package/test/function/once.test.ts +29 -0
  85. package/test/function/throttle.test.ts +24 -0
  86. package/test/lang/isEqualMethods.test.ts +98 -0
  87. package/test/string/camelCase.test.ts +13 -0
  88. package/test/string/capitalize.test.ts +17 -0
  89. package/test/string/deburr.test.ts +16 -0
  90. package/test/string/escape.test.ts +17 -0
  91. package/test/string/escapeRegExp.test.ts +18 -0
  92. package/test/string/kebabCase.test.ts +18 -0
  93. package/test/string/pascalCase.test.ts +16 -0
  94. package/test/string/snakeCase.test.ts +23 -0
  95. package/test/string/startCase.test.ts +14 -0
  96. package/test/string/stripSpecialChars.test.ts +15 -0
  97. package/test/string/unescape.test.ts +17 -0
  98. package/tsconfig.json +112 -0
  99. package/tsup.config.js +10 -0
  100. package/vitest.config.ts +27 -0
package/.ctiignore ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "src/helpers": "*",
3
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "moderndash",
3
+ "version": "0.0.5",
4
+ "type": "module",
5
+ "description": "A lodash inspired utility framework for ESM/Typescript/ES2020",
6
+ "scripts": {
7
+ "build": "ctix create ----startAt src --overwrite --noBackup & tsup",
8
+ "publish": "npm run build & npm publish",
9
+ "test": "vitest",
10
+ "test-ui": "vitest --ui"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/Maggi64/moderndash.git"
15
+ },
16
+ "keywords": [
17
+ "lodash",
18
+ "utility",
19
+ "helper",
20
+ "underscore",
21
+ "esm"
22
+ ],
23
+ "author": "Maggi64",
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/Maggi64/moderndash/issues"
27
+ },
28
+ "browserslist": [
29
+ ">2% and not dead"
30
+ ],
31
+ "types": "./dist/index.d.ts",
32
+ "main": "./dist/index.js",
33
+ "homepage": "https://github.com/Maggi64/moderndash#readme",
34
+ "devDependencies": {
35
+ "@vitest/coverage-c8": "0.27.1",
36
+ "@vitest/ui": "0.27.1",
37
+ "vitest": "0.27.1",
38
+ "tsup": "6.5.0",
39
+ "ctix": "1.8.1"
40
+ }
41
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
3
+ *
4
+ * @category Array
5
+ * @returns Returns the new array of chunks.
6
+ * @param chunkSize - The array to process.
7
+ * @param array - The length of each chunk
8
+ * @example
9
+ * chunk(2, ['a', 'b', 'c', 'd'])
10
+ * // => [['a', 'b'], ['c', 'd']]
11
+ *
12
+ * chunk(3, ['a', 'b', 'c', 'd'])
13
+ * // => [['a', 'b', 'c'], ['d']]
14
+ */
15
+
16
+ export function chunk<TInput>(chunkSize: number, array: TInput[]): TInput[][] {
17
+ const sizeInteger = Math.trunc(chunkSize);
18
+ if (array.length === 0 || sizeInteger < 1) {
19
+ return [];
20
+ }
21
+
22
+ const chunkedArray = [];
23
+ let i = 0;
24
+
25
+ while (i < array.length) {
26
+ chunkedArray.push(array.slice(i, i + sizeInteger));
27
+ i += sizeInteger;
28
+ }
29
+
30
+ return chunkedArray;
31
+ }
@@ -0,0 +1,21 @@
1
+ import type { MinimumTwoArrays } from '../types';
2
+
3
+ import { differenceWith } from '@array/differenceWith';
4
+ import { isEqual } from '@lang/isEqual';
5
+
6
+ /**
7
+ * Creates an array of `array` values not included in the other given arrays. The order and references of result values are determined by the first array.
8
+ *
9
+ * **Note:** Unlike `pullAll`, this method returns a new array.
10
+ *
11
+ * @category Array
12
+ * @param arrays - First array is inspected, others are excluded.
13
+ * @returns Returns the new array of filtered values.
14
+ * @example
15
+ * difference([2, 1], [2, 3])
16
+ * // =\> [1]
17
+ */
18
+
19
+ export function difference<T>(...arrays: MinimumTwoArrays<T>): T[] {
20
+ return differenceWith(isEqual, ...arrays);
21
+ }
@@ -0,0 +1,30 @@
1
+ import type { IterateeFunction, MinimumTwoArrays, PropertyShorthand } from '../types';
2
+
3
+ import { differenceWith } from '@array/differenceWith';
4
+ import { getIterateFunction } from '@helpers/shortHands';
5
+ import { isEqualWith } from '@lang/isEqualWith';
6
+
7
+ /**
8
+ * This method is like `difference` except that it accepts `iteratee` which
9
+ * is invoked for each element of `array` and `values` to generate the criterion
10
+ * by which they're compared. The order and references of result values are
11
+ * determined by the first array.
12
+ *
13
+ * **Note:** Unlike `pullAllBy`, this method returns a new array.
14
+ *
15
+ * @category Array
16
+ * @param iteratee - The iteratee invoked per element. Or property shorthand.
17
+ * @param arrays - First array to inspect. Others are excluded.
18
+
19
+ * @returns Returns the new array of filtered values.
20
+ * @example
21
+ * differenceBy(Math.floor, [2.1, 1.2], [2.3, 3.4])
22
+ * // => [1.2]
23
+ * differenceBy('x', [{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }])
24
+ * // => [{ 'x': 2 }]
25
+ */
26
+
27
+ export function differenceBy<T>(iteratee: IterateeFunction<T> | PropertyShorthand<T>, ...arrays: MinimumTwoArrays<T>): T[] {
28
+ const iterateeFunction = getIterateFunction(iteratee);
29
+ return differenceWith((a, b) => isEqualWith(iterateeFunction, a, b), ...arrays);
30
+ }
@@ -0,0 +1,31 @@
1
+ import type { MinimumTwoArrays } from '../types';
2
+
3
+ /**
4
+ * This method is like `difference` except that it accepts `comparator`
5
+ * which is invoked to compare elements of `array` to `values`. The order and
6
+ * references of result values are determined by the first array. The comparator
7
+ * is invoked with two arguments: (arrVal, othVal).
8
+ *
9
+ * **Note:** Unlike `pullAllWith`, this method returns a new array.
10
+ *
11
+ * @category Array
12
+ * @param comparator - The comparator invoked per element.
13
+ * @param arrays - First array to inspect. Others are excluded.
14
+ * @returns Returns the new array of filtered values.
15
+ * @example
16
+ * differenceWith(isEqual, [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }], [{ 'x': 1, 'y': 2 }])
17
+ * // => [{ 'x': 2, 'y': 1 }]
18
+ */
19
+
20
+ export function differenceWith<T>(comparator: (a: T, b: T) => boolean, ...arrays: MinimumTwoArrays<T>): T[] {
21
+ const difference: T[] = [];
22
+ const [firstArray, ...restArrays] = arrays;
23
+
24
+ firstArray.forEach(element => {
25
+ if (!restArrays.some(array => array.some(item => comparator(item, element)))) {
26
+ difference.push(element);
27
+ }
28
+ });
29
+
30
+ return difference;
31
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Creates a slice of `array` excluding elements dropped from the end.
3
+ * Elements are dropped until `predicate` returns falsey. The predicate is
4
+ * invoked with three arguments: (value, index, array).
5
+ *
6
+ * @category Array
7
+ * @param predicate - The function invoked per iteration.
8
+ * @param array - The array to query.
9
+ * @returns Returns the slice of `array`.
10
+ * @example
11
+ * const users = [
12
+ * { 'user': 'barney', 'active': false },
13
+ * { 'user': 'fred', 'active': true },
14
+ * { 'user': 'pebbles', 'active': true }
15
+ * ]
16
+ *
17
+ * dropRightWhile(({ active }) => active, users)
18
+ * // => objects for ['barney']
19
+ */
20
+
21
+
22
+ export function dropRightWhile<T>(predicate: (value: T) => boolean, array: T[]) {
23
+ let i = array.length;
24
+ while (i > 0 && predicate(array[i - 1])) {
25
+ i--;
26
+ }
27
+ return array.slice(0, i);
28
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Creates a slice of `array` excluding elements dropped from the beginning.
3
+ * Elements are dropped until `predicate` returns falsey. The predicate is
4
+ * invoked with three arguments: (value, index, array).
5
+ *
6
+ * @category Array
7
+ * @param predicate - The function invoked per iteration.
8
+ * @param array - The array to query.
9
+ * @returns Returns the slice of `array`.
10
+ * @example
11
+ * const users = [
12
+ * { 'user': 'barney', 'active': true },
13
+ * { 'user': 'fred', 'active': true },
14
+ * { 'user': 'pebbles', 'active': false }
15
+ * ]
16
+ *
17
+ * dropWhile(({ active }) => active, users)
18
+ * // => objects for ['pebbles']
19
+ */
20
+
21
+ export function dropWhile<T>(predicate: (value: T) => boolean, array: T[]): T[] {
22
+ const index = array.findIndex(x => !predicate(x));
23
+ return array.slice(index === -1 ? array.length : index);
24
+ }
@@ -0,0 +1,24 @@
1
+ export * from './chunk';
2
+ export * from './difference';
3
+ export * from './differenceBy';
4
+ export * from './differenceWith';
5
+ export * from './dropRightWhile';
6
+ export * from './dropWhile';
7
+ export * from './intersection';
8
+ export * from './intersectionBy';
9
+ export * from './intersectionWith';
10
+ export * from './sample';
11
+ export * from './sampleSize';
12
+ export * from './shuffle';
13
+ export * from './takeRightWhile';
14
+ export * from './takeWhile';
15
+ export * from './union';
16
+ export * from './unionBy';
17
+ export * from './unionWith';
18
+ export * from './uniq';
19
+ export * from './uniqBy';
20
+ export * from './uniqWith';
21
+ export * from './unzip';
22
+ export * from './unzipWith';
23
+ export * from './zip';
24
+ export * from './zipWith';
@@ -0,0 +1,20 @@
1
+ import type { MinimumTwoArrays } from '../types';
2
+
3
+ import { intersectionWith } from '@array/intersectionWith';
4
+ import { isEqual } from '@lang/isEqual';
5
+
6
+ /**
7
+ * Creates an array of unique values that are included in all given arrays.
8
+ * The order and references of result values are determined by the first array.
9
+ *
10
+ * @category Array
11
+ * @param arrays - The arrays to inspect.
12
+ * @returns Returns the new array of intersecting values.
13
+ * @example
14
+ * intersection([2, 1], [2, 3])
15
+ * // => [2]
16
+ */
17
+
18
+ export function intersection<TInput>(...arrays: MinimumTwoArrays<TInput>): TInput[] {
19
+ return intersectionWith(isEqual, ...arrays);
20
+ }
@@ -0,0 +1,26 @@
1
+ import type { IterateeFunction, MinimumTwoArrays, PropertyShorthand } from '../types';
2
+
3
+ import { intersectionWith } from '@array/intersectionWith';
4
+ import { getIterateFunction } from '@helpers/shortHands';
5
+ import { isEqualWith } from '@lang/isEqualWith';
6
+
7
+ /**
8
+ * This method is like `intersection` except that it accepts `iteratee`
9
+ * which is invoked for each element of each `arrays` to generate the criterion
10
+ * by which they're compared. The order and references of result values are
11
+ * determined by the first array. The iteratee is invoked with one argument:
12
+ * (value).
13
+ *
14
+ * @category Array
15
+ * @param iteratee - The iteratee invoked per element. Or property shorthand.
16
+ * @param arrays - The arrays to inspect.
17
+ * @returns Returns the new array of intersecting values.
18
+ * @example
19
+ * intersectionBy(Math.floor, [2.1, 1.2], [2.3, 3.4])
20
+ * // => [2.1]
21
+ */
22
+
23
+ export function intersectionBy<TInput>(iteratee: IterateeFunction<TInput> | PropertyShorthand<TInput>, ...arrays: MinimumTwoArrays<TInput>): TInput[] {
24
+ const iterateeFunction = getIterateFunction(iteratee);
25
+ return intersectionWith((a, b) => isEqualWith(iterateeFunction, a, b), ...arrays);
26
+ }
@@ -0,0 +1,33 @@
1
+ import type { MinimumTwoArrays } from '../types';
2
+
3
+ /**
4
+ * This method is like `intersection` except that it accepts `comparator`
5
+ * which is invoked to compare elements of `arrays`. The order and references
6
+ * of result values are determined by the first array. The comparator is
7
+ * invoked with two arguments: (arrVal, othVal).
8
+ *
9
+ * @category Array
10
+ * @param comparator - The comparator invoked per element.
11
+ * @param arrays - The arrays to inspect.
12
+ * @returns Returns the new array of intersecting values.
13
+ * @example
14
+ * const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
15
+ * const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]
16
+ *
17
+ * intersectionWith(isEqual, objects, others)
18
+ * // => [{ 'x': 1, 'y': 2 }]
19
+ */
20
+
21
+ export function intersectionWith<T>(comparator: (a: T, b: T) => boolean, ...arrays: MinimumTwoArrays<T>): T[] {
22
+ const intersection: T[] = [];
23
+
24
+ const [firstArray, ...restArrays] = arrays;
25
+
26
+ firstArray.forEach(element => {
27
+ if (restArrays.every(array => array.some(item => comparator(item, element)))) {
28
+ intersection.push(element);
29
+ }
30
+ });
31
+
32
+ return intersection;
33
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Gets a random element from `array`.
3
+ *
4
+ * @category Array
5
+ * @param array - The array to sample.
6
+ * @returns Returns the random element.
7
+ * @example
8
+ * sample([1, 2, 3, 4])
9
+ * // => 2
10
+ */
11
+
12
+ export function sample<T>(array: T[]): T | undefined {
13
+ if (array.length === 0) {
14
+ return undefined;
15
+ }
16
+ const randomIndex = Math.floor(Math.random() * array.length);
17
+ return array[randomIndex];
18
+ }
@@ -0,0 +1,31 @@
1
+ import { sample } from '@array/sample';
2
+
3
+ /**
4
+ * Gets `n` random elements at unique keys from `array` up to the
5
+ * size of `array`.
6
+ *
7
+ * @category Array
8
+ * @param size The number of elements to sample.
9
+ * @param array The array to sample.
10
+ * @returns Returns the random elements.
11
+ * @example
12
+ * sampleSize([1, 2, 3], 2)
13
+ * // => [3, 1]
14
+ *
15
+ * sampleSize([1, 2, 3], 4)
16
+ * // => [2, 3, 1]
17
+ */
18
+
19
+ export function sampleSize<TInput>(size: number, array: TInput[]): TInput[] {
20
+ const sampleArray: TInput[] = [];
21
+
22
+ if (array.length === 0 || size <= 0) {
23
+ return sampleArray;
24
+ }
25
+
26
+ for (let i = 0; i < size; i++) {
27
+ sampleArray.push(sample(array) as TInput);
28
+ }
29
+
30
+ return sampleArray;
31
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Creates an array of shuffled values, using a version of the
3
+ * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
4
+ *
5
+ * @since 0.1.0
6
+ * @category Array
7
+ * @param array - The array or object to shuffle.
8
+ * @returns {Array} Returns the new shuffled array.
9
+ * @example
10
+ * shuffle([1, 2, 3, 4])
11
+ * // => [4, 1, 3, 2]
12
+ */
13
+
14
+ export function shuffle<TInput>(array: TInput[]): TInput[] {
15
+ const shuffledArray = [...array];
16
+ let currentIndex = shuffledArray.length;
17
+ let temporaryValue: TInput;
18
+ let randomIndex: number;
19
+
20
+ // While there remain elements to shuffle...
21
+ while (0 !== currentIndex) {
22
+ // Pick a remaining element...
23
+ randomIndex = Math.floor(Math.random() * currentIndex);
24
+ currentIndex -= 1;
25
+
26
+ // And swap it with the current element.
27
+ temporaryValue = shuffledArray[currentIndex];
28
+ shuffledArray[currentIndex] = shuffledArray[randomIndex];
29
+ shuffledArray[randomIndex] = temporaryValue;
30
+ }
31
+
32
+ return shuffledArray;
33
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Creates a slice of `array` with elements taken from the end. Elements are
3
+ * taken until `predicate` returns falsey. The predicate is invoked with
4
+ * three arguments: (value, index, array).
5
+ *
6
+ * @category Array
7
+ * @param predicate - The function invoked per iteration.
8
+ * @param array - The array to query.
9
+ * @returns Returns the slice of `array`.
10
+ * @example
11
+ * const users = [
12
+ * { 'user': 'barney', 'active': false },
13
+ * { 'user': 'fred', 'active': true },
14
+ * { 'user': 'pebbles', 'active': true }
15
+ * ]
16
+ *
17
+ * takeRightWhile(({ active }) => active, users)
18
+ * // => objects for ['fred', 'pebbles']
19
+ */
20
+
21
+ export function takeRightWhile<T>(predicate: (elem: T) => boolean, array: T[]): T[] {
22
+ const result: T[] = [];
23
+
24
+ for (let i = array.length - 1; i >= 0; i--) {
25
+ if (predicate(array[i])) {
26
+ result.unshift(array[i]);
27
+ } else {
28
+ break;
29
+ }
30
+ }
31
+
32
+ return result;
33
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Creates a slice of `array` with elements taken from the beginning. Elements
3
+ * are taken until `predicate` returns falsey. The predicate is invoked with
4
+ * three arguments: (value, index, array).
5
+ *
6
+ * @category Array
7
+ * @param predicate The function invoked per iteration.
8
+ * @param array The array to query.
9
+ * @returns Returns the slice of `array`.
10
+ * @example
11
+ * const users = [
12
+ * { 'user': 'barney', 'active': true },
13
+ * { 'user': 'fred', 'active': true },
14
+ * { 'user': 'pebbles', 'active': false }
15
+ * ]
16
+ *
17
+ * takeWhile(({ active }) => active, users)
18
+ * // => objects for ['barney', 'fred']
19
+ */
20
+
21
+ export function takeWhile<T>(predicate: (elem: T) => boolean, array: T[]): T[] {
22
+ const result: T[] = [];
23
+
24
+ for (const element of array) {
25
+ if (predicate(element)) {
26
+ result.push(element);
27
+ } else {
28
+ break;
29
+ }
30
+ }
31
+
32
+ return result;
33
+ }
@@ -0,0 +1,16 @@
1
+ import type { MinimumTwoArrays } from '../types';
2
+
3
+ /**
4
+ * Creates an array of unique values, in order, from all given arrays using for equality comparisons.
5
+ *
6
+ * @category Array
7
+ * @param arrays - The arrays to inspect.
8
+ * @returns Returns the new array of combined values.
9
+ * @example
10
+ * union([2, 3], [1, 2])
11
+ * // => [2, 3, 1]
12
+ */
13
+
14
+ export function union<TInput>(...arrays: MinimumTwoArrays<TInput>): TInput[] {
15
+ return [...new Set(arrays.flat())];
16
+ }
@@ -0,0 +1,26 @@
1
+ import type { IterateeFunction, MinimumTwoArrays, PropertyShorthand } from '../types';
2
+
3
+ import { unionWith } from '@array/unionWith';
4
+ import { getIterateFunction } from '@helpers/shortHands';
5
+ import { isEqualWith } from '@lang/isEqualWith';
6
+
7
+ /**
8
+ * This method is like `union` except that it accepts `iteratee` which is
9
+ * invoked for each element of each `arrays` to generate the criterion by
10
+ * which uniqueness is computed. Result values are chosen from the first
11
+ * array in which the value occurs. The iteratee is invoked with one argument:
12
+ * (value).
13
+ *
14
+ * @category Array
15
+ * @param arrays - The arrays to inspect.
16
+ * @param iteratee - The iteratee invoked per element. Or property shorthand.
17
+ * @returns Returns the new array of combined values.
18
+ * @example
19
+ * unionBy(Math.floor, [2.1], [1.2, 2.3])
20
+ * // => [2.1, 1.2]
21
+ */
22
+
23
+ export function unionBy<T>(iteratee: IterateeFunction<T> | PropertyShorthand<T>, ...arrays: MinimumTwoArrays<T>): T[] {
24
+ const iterateeFunction = getIterateFunction(iteratee);
25
+ return unionWith((a, b) => isEqualWith(iterateeFunction, a, b), ...arrays);
26
+ }
@@ -0,0 +1,31 @@
1
+ import type { MinimumTwoArrays } from '../types';
2
+
3
+ /**
4
+ * This method is like `union` except that it accepts `comparator` which
5
+ * is invoked to compare elements of `arrays`. Result values are chosen from
6
+ * the first array in which the value occurs. The comparator is invoked
7
+ * with two arguments: (arrVal, othVal).
8
+ *
9
+ * @category Array
10
+ * @param comparator - The comparator invoked per element.
11
+ * @param arrays - The arrays to inspect.
12
+ * @returns Returns the new array of combined values.
13
+ * @example
14
+ * const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
15
+ * const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]
16
+ *
17
+ * unionWith(isEqual, objects, others)
18
+ * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
19
+ */
20
+
21
+ export function unionWith<TInput>(comparator: (a: TInput, b: TInput) => boolean, ...arrays: MinimumTwoArrays<TInput>): TInput[] {
22
+ return arrays.reduce((acc, current) => {
23
+ for (const item of current) {
24
+ if (acc.some(x => comparator(x, item))) {
25
+ continue;
26
+ }
27
+ acc.push(item);
28
+ }
29
+ return acc;
30
+ }, []);
31
+ }
@@ -0,0 +1,18 @@
1
+ import { uniqWith } from '@array/uniqWith';
2
+ import { isEqual } from '@lang/isEqual';
3
+
4
+ /**
5
+ * Creates a duplicate-free version of an array, in which only the first occurrence of each element is kept.
6
+ * The order of result values is determined by the order they occur in the array.
7
+ *
8
+ * @category Array
9
+ * @param array - The array to inspect.
10
+ * @returns Returns the new duplicate free array.
11
+ * @example
12
+ * uniq([2, 1, 2])
13
+ * // => [2, 1]
14
+ */
15
+
16
+ export function uniq<TInput>(array: TInput[]): TInput[] {
17
+ return uniqWith(isEqual, array);
18
+ }
@@ -0,0 +1,25 @@
1
+ import type { IterateeFunction, PropertyShorthand } from '../types';
2
+
3
+ import { uniqWith } from '@array/uniqWith';
4
+ import { getIterateFunction } from '@helpers/shortHands';
5
+
6
+ /**
7
+ * This method is like `uniq` except that it accepts `iteratee` which is
8
+ * invoked for each element in `array` to generate the criterion by which
9
+ * uniqueness is computed. The order of result values is determined by the
10
+ * order they occur in the array.
11
+ *
12
+ * @category Array
13
+ * @param array - The array to inspect.
14
+ * @param iteratee - The iteratee invoked per element. Or property shorthand.
15
+ * @returns Returns the new duplicate free array.
16
+ * @example
17
+ * uniqBy(Math.floor, [2.1, 1.2, 2.3])
18
+ * // => [2.1, 1.2]
19
+ */
20
+
21
+ export function uniqBy<T>(iteratee: IterateeFunction<T> | PropertyShorthand<T>, array: T[]): T[] {
22
+ const iterateeFunction = getIterateFunction(iteratee);
23
+
24
+ return uniqWith((a, b) => iterateeFunction(a) === iterateeFunction(b), array);
25
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * This method is like `uniq` except that it accepts `comparator` which is invoked to compare elements of `array`.
3
+ * The order of result values is determined by the order they occur in the array.
4
+ *
5
+ * @category Array
6
+ * @param array - The array to inspect.
7
+ * @param comparator - The comparator invoked per element.
8
+ * @returns {Array} Returns the new duplicate free array.
9
+ * @example
10
+ * const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]
11
+ *
12
+ * uniqWith(isEqual, objects)
13
+ * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
14
+ */
15
+
16
+ export function uniqWith<TInput>(comparator: (a: TInput, b: TInput) => boolean, array: TInput[]): TInput[] {
17
+ return array.filter((value, index, self) => {
18
+ return self.findIndex(otherValue => comparator(value, otherValue)) === index;
19
+ });
20
+ }
@@ -0,0 +1,20 @@
1
+ import { unzipWith } from '@array/unzipWith';
2
+
3
+ /**
4
+ * This method is like `zip` except that it accepts an array of grouped
5
+ * elements and creates an array regrouping the elements to their pre-zip configuration.
6
+ *
7
+ * @category Array
8
+ * @param array - The array of grouped elements to process.
9
+ * @returns Returns the new array of regrouped elements.
10
+ * @example
11
+ * const zipped = zip(['a', 'b'], [1, 2], [true, false])
12
+ * // => [['a', 1, true], ['b', 2, false]]
13
+ *
14
+ * unzip(zipped)
15
+ * // => [['a', 'b'], [1, 2], [true, false]]
16
+ */
17
+
18
+ export function unzip<TInput extends unknown[]>(array: TInput[]): TInput[] {
19
+ return unzipWith((...t) => t, array);
20
+ }