/
countBy.ts
92 lines (86 loc) · 3.19 KB
/
countBy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* eslint-disable jsdoc/require-param -- ignore for deprecated files */
import type { Pred, PredIndexed, PredIndexedOptional } from "./_types";
import { purry } from "./purry";
const _countBy =
(indexed: boolean) =>
<T>(array: ReadonlyArray<T>, fn: PredIndexedOptional<T, boolean>) => {
let out = 0;
for (let index = 0; index < array.length; index++) {
// TODO: Once we bump our TypeScript target above ES5 we can use Array.prototype.entries to iterate over the indices and values at the same time.
const item = array[index]!;
const value = indexed ? fn(item, index, array) : fn(item);
out += value ? 1 : 0;
}
return out;
};
/**
* Counts how many values of the collection pass the specified predicate.
*
* ! **DEPRECATED**: Use `R.filter(items, fn).length`. Will be removed in v2!
*
* @param items - The input data.
* @param fn - The predicate.
* @signature
* R.countBy(array, fn)
* @example
* R.countBy([1, 2, 3, 4, 5], x => x % 2 === 0) // => 2
* @dataFirst
* @indexed
* @category Deprecated
* @deprecated Use `R.filter(items, fn).length`. Will be removed in v2.
*/
export function countBy<T>(
items: ReadonlyArray<T>,
fn: Pred<T, boolean>,
): number;
/**
* Counts how many values of the collection pass the specified predicate.
*
* ! **DEPRECATED**: Use `<T>(items: ReadonlyArray<T>) => R.filter(items, fn).length` or if in a pipe: `R.pipe(..., R.filter(fn), R.length(), ...)`. Will be removed in v2!
*
* @param fn - The predicate.
* @signature
* R.countBy(fn)(array)
* @example
* R.pipe([1, 2, 3, 4, 5], R.countBy(x => x % 2 === 0)) // => 2
* @dataLast
* @indexed
* @category Deprecated
* @deprecated Use `<T>(items: ReadonlyArray<T>) => R.filter(items, fn).length` or if in a pipe: `R.pipe(..., R.filter(fn), R.length(), ...)`. Will be removed in v2.
*/
export function countBy<T>(
fn: Pred<T, boolean>,
): (array: ReadonlyArray<T>) => number;
export function countBy(): unknown {
return purry(_countBy(false), arguments);
}
export namespace countBy {
/**
* Counts how many values of the collection pass the specified predicate.
*
* ! **DEPRECATED**: Use `R.filter.indexed(items, fn).length`. Will be removed in v2!
*
* @example
* R.pipe([1, 2, 3, 4, 5], R.countBy(x => x % 2 === 0)) // => 2
* @deprecated Use `R.filter.indexed(items, fn).length`. Will be removed in v2.
*/
export function indexed<T>(
array: ReadonlyArray<T>,
fn: PredIndexed<T, boolean>,
): number;
/**
* Counts how many values of the collection pass the specified predicate.
*
* ! **DEPRECATED**: Use `<T>(items: ReadonlyArray<T>) => R.filter.indexed(items, fn).length` or if in a pipe: `R.pipe(..., R.filter.indexed(fn), R.length(), ...)`. Will be removed in v2!
*
* @example
* R.pipe([1, 2, 3, 4, 5], R.countBy(x => x % 2 === 0)) // => 2
* @deprecated Use `<T>(items: ReadonlyArray<T>) => R.filter.indexed(items, fn).length` or if in a pipe: `R.pipe(..., R.filter.indexed(fn), R.length(), ...)`. Will be removed in v2.
*/
export function indexed<T>(
fn: PredIndexed<T, boolean>,
): (array: ReadonlyArray<T>) => number;
export function indexed(): unknown {
return purry(_countBy(true), arguments);
}
}