UNPKG

typedash

Version:

modern, type-safe collection of utility functions

1 lines 1.62 kB
{"version":3,"file":"count-CoRRYXRX.cjs","names":["filter"],"sources":["../src/functions/count/count.ts"],"sourcesContent":["import type { Maybe } from '../../types';\nimport { filter } from '../_internal/filterIterable';\n\n/**\n * Returns the number of elements in an iterable.\n * @param source The iterable to count.\n * @returns The number of elements.\n * @example\n * ```ts\n * count([1, 2, 3]); // 3\n * count([]); // 0\n * count(null); // 0\n * ```\n */\nexport function count<T>(source: Maybe<Iterable<T>>): number;\n/**\n * Returns the number of elements in an iterable that satisfy a predicate.\n * @param source The iterable to count.\n * @param predicate The predicate function used to determine if an element is a match.\n * @returns The number of matching elements.\n * @example\n * ```ts\n * count([1, 2, 3], x => x % 2 === 0); // 1\n * count([1, 2, 3], x => x >= 2); // 2\n * count([], x => x >= 1); // 0\n * count(null, x => x >= 1); // 0\n * ```\n */\nexport function count<T>(\n source: Maybe<Iterable<T>>,\n predicate?: (value: T, index: number) => boolean\n): number;\n/**\n * Implementation for all overloads.\n * @param source The iterable to count.\n * @param predicate The predicate function used to determine if an element is a match.\n * @returns The number of matching elements.\n */\nexport function count<T>(\n source: Maybe<Iterable<T>>,\n predicate?: (value: T, index: number) => boolean\n): number {\n const relevantItems = filter(source, predicate);\n return relevantItems?.length ?? 0;\n}\n"],"mappings":";;;;;;;;;AAsCA,SAAgB,MACd,QACA,WACQ;AAER,QADsBA,8BAAO,QAAQ,UAAU,EACzB,UAAU"}