/
githubmirror
/
immutable-js
Обзор
Документация
Войти
/
githubmirror
/
immutable-js
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v5.1.7
src/CollectionHelperMethods.ts
54 строки
2 KB
Julien Deniau
Better type for reduce if not ininitial value is given
01 июн 2026, 19:33
01 июн 2026, 19:33
c67d400
Код
Авторство
О чём код?
import type { Collection } from '../type-definitions/immutable'; import assertNotInfinite from './utils/assertNotInfinite'; export function reduce<K, V, R, C extends Collection<K, V>>( collection: C, reducer: (reduction: V | R, value: V, key: K, iter: C) => R, reduction: V | R | undefined, context: unknown, useFirst: boolean, reverse: boolean ): V | R | undefined { // @ts-expect-error Migrate to CollectionImpl in v6 assertNotInfinite(collection.size); // @ts-expect-error Migrate to CollectionImpl in v6 collection.__iterate((v: V, k: K, c: C) => { if (useFirst) { useFirst = false; reduction = v; } else { // `reduction` has already been seeded here (either with the provided // initial value or with the first iterated value), so it is never the // `undefined` placeholder — only a `V` or a `R`. reduction = reducer.call(context, reduction!, v, k, c); } }, reverse); return reduction; } export function keyMapper<K, V>(v: V, k: K): K { return k; } export function entryMapper<K, V>(v: V, k: K): [K, V] { return [k, v]; } export function not(predicate: (...args: unknown[]) => boolean) { return function (this: unknown, ...args: unknown[]): boolean { return !predicate.apply(this, args); }; } export function neg(predicate: (...args: unknown[]) => number) { return function (this: unknown, ...args: unknown[]): number { return -predicate.apply(this, args); }; } export function defaultNegComparator( a: number | string, b: number | string ): number { return a < b ? 1 : a > b ? -1 : 0; }