moderndash 0.0.16 → 0.0.18

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 (66) hide show
  1. package/README.md +16 -75
  2. package/dist/index.cjs +66 -73
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.ts +125 -113
  5. package/dist/index.js +62 -69
  6. package/dist/index.js.map +1 -1
  7. package/package.json +6 -5
  8. package/src/array/chunk.ts +0 -31
  9. package/src/array/difference.ts +0 -21
  10. package/src/array/differenceBy.ts +0 -30
  11. package/src/array/differenceWith.ts +0 -31
  12. package/src/array/dropRightWhile.ts +0 -28
  13. package/src/array/dropWhile.ts +0 -24
  14. package/src/array/index.ts +0 -21
  15. package/src/array/intersection.ts +0 -20
  16. package/src/array/intersectionBy.ts +0 -26
  17. package/src/array/intersectionWith.ts +0 -33
  18. package/src/array/sample.ts +0 -18
  19. package/src/array/sampleSize.ts +0 -31
  20. package/src/array/shuffle.ts +0 -33
  21. package/src/array/takeRightWhile.ts +0 -33
  22. package/src/array/takeWhile.ts +0 -33
  23. package/src/array/uniq.ts +0 -18
  24. package/src/array/uniqBy.ts +0 -25
  25. package/src/array/uniqWith.ts +0 -20
  26. package/src/array/unzip.ts +0 -20
  27. package/src/array/unzipWith.ts +0 -26
  28. package/src/array/zip.ts +0 -17
  29. package/src/array/zipWith.ts +0 -28
  30. package/src/collection/countBy.ts +0 -38
  31. package/src/collection/groupBy.ts +0 -32
  32. package/src/collection/index.ts +0 -3
  33. package/src/collection/sortBy.ts +0 -15
  34. package/src/function/after.ts +0 -29
  35. package/src/function/before.ts +0 -31
  36. package/src/function/debounce.ts +0 -125
  37. package/src/function/index.ts +0 -7
  38. package/src/function/memoize.ts +0 -63
  39. package/src/function/once.ts +0 -22
  40. package/src/function/throttle.ts +0 -13
  41. package/src/function/times.ts +0 -24
  42. package/src/helpers/collections.ts +0 -5
  43. package/src/helpers/shortHands.ts +0 -17
  44. package/src/helpers/stringModifiers.ts +0 -18
  45. package/src/helpers/typeofChecks.ts +0 -3
  46. package/src/index.ts +0 -7
  47. package/src/lang/index.ts +0 -4
  48. package/src/lang/isEmpty.ts +0 -51
  49. package/src/lang/isEqual.ts +0 -80
  50. package/src/lang/isEqualWith.ts +0 -34
  51. package/src/lang/isPlainObject.ts +0 -3
  52. package/src/object/index.ts +0 -1
  53. package/src/object/pick.ts +0 -21
  54. package/src/string/camelCase.ts +0 -30
  55. package/src/string/capitalize.ts +0 -14
  56. package/src/string/deburr.ts +0 -20
  57. package/src/string/escape.ts +0 -21
  58. package/src/string/escapeRegExp.ts +0 -15
  59. package/src/string/index.ts +0 -11
  60. package/src/string/kebabCase.ts +0 -25
  61. package/src/string/pascalCase.ts +0 -26
  62. package/src/string/snakeCase.ts +0 -30
  63. package/src/string/startCase.ts +0 -25
  64. package/src/string/stripSpecialChars.ts +0 -17
  65. package/src/string/unescape.ts +0 -22
  66. package/src/types.ts +0 -15
@@ -1,30 +0,0 @@
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(a, b, iterateeFunction), ...arrays);
30
- }
@@ -1,31 +0,0 @@
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
- }
@@ -1,28 +0,0 @@
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
- }
@@ -1,24 +0,0 @@
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
- }
@@ -1,21 +0,0 @@
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 './uniq';
16
- export * from './uniqBy';
17
- export * from './uniqWith';
18
- export * from './unzip';
19
- export * from './unzipWith';
20
- export * from './zip';
21
- export * from './zipWith';
@@ -1,20 +0,0 @@
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
- }
@@ -1,26 +0,0 @@
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
- * @example
15
- * intersectionBy(Math.floor, [2.1, 1.2], [2.3, 3.4])
16
- * // => [2.1]
17
- * @category Array
18
- * @param iteratee - The iteratee invoked per element. Or property shorthand.
19
- * @param arrays - The arrays to inspect.
20
- * @returns Returns the new array of intersecting values.
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(a, b, iterateeFunction), ...arrays);
26
- }
@@ -1,33 +0,0 @@
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
- * @example
10
- * const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
11
- * const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]
12
- *
13
- * intersectionWith(isEqual, objects, others)
14
- * // => [{ 'x': 1, 'y': 2 }]
15
- * @category Array
16
- * @param comparator - The comparator invoked per element.
17
- * @param arrays - The arrays to inspect.
18
- * @returns Returns the new array of intersecting values.
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
- }
@@ -1,18 +0,0 @@
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
- }
@@ -1,31 +0,0 @@
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
- }
@@ -1,33 +0,0 @@
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 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
- }
@@ -1,33 +0,0 @@
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
- }
@@ -1,33 +0,0 @@
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
- }
package/src/array/uniq.ts DELETED
@@ -1,18 +0,0 @@
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
- }
@@ -1,25 +0,0 @@
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
- }
@@ -1,20 +0,0 @@
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 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
- }
@@ -1,20 +0,0 @@
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
- }
@@ -1,26 +0,0 @@
1
- /**
2
- * This method is like `unzip` except that it accepts `iteratee` to specify
3
- * how regrouped values should be combined. The iteratee is invoked with the
4
- * elements of each group: (...group).
5
- *
6
- * @category Array
7
- * @param iteratee - The function to combine regrouped values.
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([1, 2], [10, 20], [100, 200])
12
- * // => [[1, 10, 100], [2, 20, 200]]
13
- *
14
- * unzipWith(add, zipped)
15
- * // => [3, 30, 300]
16
- */
17
-
18
- export function unzipWith<TInput extends unknown[], TOutput>(iteratee: (...t: TInput) => TOutput, array: TInput[]): TOutput[] {
19
- const result: TOutput[] = [];
20
-
21
- for (const elements of array) {
22
- result.push(iteratee(...elements));
23
- }
24
-
25
- return result;
26
- }
package/src/array/zip.ts DELETED
@@ -1,17 +0,0 @@
1
- import { zipWith } from '@array/zipWith';
2
-
3
- /**
4
- * Creates an array of grouped elements, the first of which contains the first elements of the given arrays,
5
- * the second of which contains the second elements of the given arrays, and so on.
6
- *
7
- * @category Array
8
- * @param arrays - The arrays to process.
9
- * @returns Returns the new array of grouped elements.
10
- * @example
11
- * zip(['a', 'b'], [1, 2], [true, false])
12
- * // => [['a', 1, true], ['b', 2, false]]
13
- */
14
-
15
- export function zip<TInput>(...arrays: TInput[][]): TInput[][] {
16
- return zipWith((...t) => t, ...arrays);
17
- }
@@ -1,28 +0,0 @@
1
- // Types from https://gist.github.com/briancavalier/c4af1538ae9b2e4ab97caf14306625ab
2
- type UnZip<A extends readonly unknown[]> = {
3
- [K in keyof A]: readonly A[K][]
4
- };
5
-
6
- /**
7
- * This method is like `zip` except that it accepts `iteratee` to specify
8
- * how grouped values should be combined. The iteratee is invoked with the
9
- * elements of each group: (...group).
10
- *
11
- * @category Array
12
- * @param combineFunc - The function to combine grouped values.
13
- * @param arrays - The arrays to process.
14
- * @returns Returns the new array of grouped elements.
15
- * @example
16
- * zipWith([1, 2], [10, 20], [100, 200], (a, b, c) => a + b + c)
17
- * // => [111, 222]
18
- */
19
-
20
- export function zipWith<Args extends unknown[], TOutput>(combineFunc: (...args: Args) => TOutput, ...arrays: UnZip<Args>): TOutput[] {
21
- const len = Math.min(...arrays.map(a => a.length));
22
- const zipped: TOutput[] = [];
23
- for (let i = 0; i < len; i++) {
24
- // Typescript needs the Args hint, or it infers any[]
25
- zipped[i] = combineFunc(...arrays.map(a => a[i]) as Args);
26
- }
27
- return zipped;
28
- }
@@ -1,38 +0,0 @@
1
- import type { ArrayOrRecord, RecordKey } from '../types';
2
-
3
- import { getValuesFromCollection } from '@helpers/collections';
4
-
5
- /**
6
- * Creates an object composed of keys generated from the results of running
7
- * each element of `collection` thru `iteratee`. The corresponding value of
8
- * each key is the number of times the key was returned by `iteratee`.
9
- *
10
- * @example
11
- * const users = [
12
- * { 'user': 'barney', 'active': true },
13
- * { 'user': 'betty', 'active': true },
14
- * { 'user': 'fred', 'active': false }
15
- * ]
16
- *
17
- * countBy(users, value => value.active);
18
- * // => { 'true': 2, 'false': 1 }
19
- * @category Collection
20
- * @param iteratee - The iteratee to transform keys.
21
- * @param collection - The array or record to iterate over.
22
- * @returns Returns the composed aggregate object.
23
- */
24
-
25
- export function countBy<TInput, TKey extends RecordKey>(collection: ArrayOrRecord<TInput>, iteratee: (value: TInput) => TKey): Record<TKey, number> {
26
- const result = {} as Record<TKey, number>;
27
- const values = getValuesFromCollection(collection);
28
- for (const value of values) {
29
- const key = iteratee(value);
30
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
31
- if (result[key] === undefined) {
32
- result[key] = 1;
33
- } else {
34
- result[key] += 1;
35
- }
36
- }
37
- return result;
38
- }
@@ -1,32 +0,0 @@
1
- import type { RecordKey, ArrayOrRecord } from '../types';
2
-
3
- import { getValuesFromCollection } from '@helpers/collections';
4
-
5
-
6
- /**
7
- * Creates an object composed of keys generated from the results of running
8
- * each element of `collection` thru `iteratee`. The order of grouped values
9
- * is determined by the order they occur in `collection`. The corresponding
10
- * value of each key is an array of elements responsible for generating the
11
- * key.
12
- *
13
- * @example
14
- * groupBy([6.1, 4.2, 6.3], Math.floor)
15
- * // => { '4': [4.2], '6': [6.1, 6.3] }
16
- * @category Collection
17
- * @param collection - The array or object to iterate over.
18
- * @param iteratee - The iteratee to transform keys.
19
- * @returns Returns the composed aggregate object.
20
- */
21
-
22
- export function groupBy<T, U extends RecordKey>(collection: ArrayOrRecord<T>, iteratee: (value: T) => U): Record<U, T[]> {
23
- const result = {} as Record<U, T[]>;
24
- const values = getValuesFromCollection(collection);
25
- for (const value of values) {
26
- const key = iteratee(value);
27
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
28
- result[key] = result[key] ?? [];
29
- result[key].push(value);
30
- }
31
- return result;
32
- }
@@ -1,3 +0,0 @@
1
- export * from './countBy';
2
- export * from './groupBy';
3
- export * from './sortBy';
@@ -1,15 +0,0 @@
1
- import type { NoUnion } from '../types';
2
-
3
- export function sortBy<T, U>(iteratee: (item: T) => NoUnion<number | bigint | Date | string, U>, array: T[]): T[] {
4
- return array.sort((a, b) => {
5
- const aValue = iteratee(a);
6
- const bValue = iteratee(b);
7
- if (aValue < bValue) {
8
- return -1;
9
- }
10
- if (aValue > bValue) {
11
- return 1;
12
- }
13
- return 0;
14
- });
15
- }
@@ -1,29 +0,0 @@
1
- import type { GenericFunction } from '../types.js';
2
-
3
- /**
4
- * The opposite of `before`. This method creates a function that invokes `func` once it's called `n` or more times.
5
- *
6
- * @category Function
7
- * @param n The number of calls before `func` is invoked.
8
- * @param func The function to restrict.
9
- * @returns Returns the new restricted function.
10
- * @example
11
- * const caution = () => console.log("Caution!");
12
- *
13
- * const afterFN = after(2, caution);
14
- *
15
- * afterFN()
16
- * afterFN()
17
- * afterFN()
18
- * // => `caution` is invoked after called twice
19
- */
20
-
21
- export function after<TFunc extends GenericFunction<TFunc>>(n: number, func: TFunc) {
22
- let count = 1;
23
- return (...args: Parameters<TFunc>): ReturnType<TFunc> | undefined => {
24
- if (count >= n) {
25
- return func(...args);
26
- }
27
- count += 1;
28
- };
29
- }