UNPKG

macoolka-collection

Version:

`macoolka-collection` Define Data Collection Interface.

164 lines (163 loc) 5.44 kB
/** * Collection Reader Interface * @desczh * 可读集合接口 * @file * @since 0.2.0 */ import { Kind, URIS } from 'fp-ts/HKT'; import { Option } from 'fp-ts/Option'; import { Predicate } from 'fp-ts/function'; import { PredicateWithIndex } from 'fp-ts/FilterableWithIndex'; import { ReaderCollectionInput, ReaderCollection } from './ReaderCollection'; /** * Collection Reader Input Interface * @desczh * 可读集合输入接口 */ export interface IndexReaderCollectionInput<URI extends URIS> { /** * This function read a value at a particular index from an `Collection` * @desczh * 用key读取集合数据 * @example * import { getAt } from 'fp-ts/Array' * import { some, none } from 'fp-ts/Option' * * assert.deepStrictEqual(getAt(1)([1, 2, 3]), some(2)) * assert.deepStrictEqual(getAt(3)([1, 2, 3]), none) * * @since 0.5.0 */ _getAt: (i: number) => <A>(f: Kind<URI, A>) => A | undefined | null; } /** * Collection Reader Output Interface * @desczh * 可读集合输出接口 */ export interface IndexReaderCollection<F extends URIS> extends ReaderCollectionInput<F>, ReaderCollection<F>, IndexReaderCollectionInput<F> { /** * This function read a value at a particular index from an `Collection` * @desczh * 从集合中用key得到记录 * @example * import { getAt } from 'fp-ts/Array' * import { some, none } from 'fp-ts/Option' * * assert.deepStrictEqual(getAt(1)([1, 2, 3]), some(2)) * assert.deepStrictEqual(getAt(3)([1, 2, 3]), none) * * @since 0.5.0 */ getAt: (i: number) => <A>(f: Kind<F, A>) => Option<A>; /** * True if a particular index exists from an `Collection` * @desczh * 判断给定的key是否有对应记录 * @example * import { existAt } from 'fp-ts/Array' * * assert.deepStrictEqual(existAt(1)([1, 2, 3]), true) * assert.deepStrictEqual(existAt(3)([1, 2, 3]), false) * * @since 2.0.0 */ existAt: (i: number) => <A>(f: Kind<F, A>) => boolean; /** * Find the first index for which a predicate holds * @desczh * 得到第一个满足条件的key * @example * import { findFirstIndex } from 'fp-ts/Array' * import { some, none } from 'fp-ts/Option' * * assert.deepStrictEqual(findFirstIndex((n: number) => n === 2)([1, 2, 3]), some(1)) * assert.deepStrictEqual(findFirstIndex((n: number) => n === 2)([]), none) * * @since 0.5.0 */ findFirstIndex: <A>(predicate: Predicate<A>) => (f: Kind<F, A>) => Option<number>; /** * Returns the index of the last element of the list which matches the predicate * @desczh * 得到最后一个满足条件的key * @example * import { findLastIndex } from 'fp-ts/List' * import { some, none } from 'fp-ts/Option' * * interface X { * a: number * b: number * } * const xs: List<X> = [{ a: 1, b: 0 }, { a: 1, b: 1 }] * assert.deepStrictEqual(findLastIndex((x: { a: number }) => x.a === 1)(xs), some(1)) * assert.deepStrictEqual(findLastIndex((x: { a: number }) => x.a === 4)(xs), none) * * * @since 0.5.0 */ findLastIndex<A>(predicate: Predicate<A>): (as: Kind<F, A>) => Option<number>; /** * Returns the index of the element of the list which matches the predicate * @desczh * 得到一个满足条件的key数组 * @example * import { findIndex } from 'fp-ts/List' * * interface X { * a: number * b: number * } * const xs: List<X> = [{ a: 1, b: 0 }, { a: 1, b: 1 }] * assert.deepStrictEqual(findIndex((x: { a: number }) => x.a === 1)(xs), [1]) * assert.deepStrictEqual(findIndex((x: { a: number }) => x.a === 4)(xs), []) * * * @since 0.5.0 */ findIndex<A>(predicate: Predicate<A>): (as: Kind<F, A>) => number[]; /** * The sideEffect is executed for every entry in the `Collection`. * * break when function return false * @desczh * 遍历每一个元素 * 函数返回false时,中断遍历 * @example * import { forEach } from 'fp-ts/Array' * * const values=[] * forEach(a=>values.push(a))([1, 2, 3]) * assert.deepStrictEqual(values, [1, 2, 3]) * * @since 0.5.0 */ forEachIndex: <A>(f: PredicateWithIndex<number, A>) => (as: Kind<F, A>) => void; /** * True if predicate returns true for all entries in the `Collection`. * @desczh * 判断是否所有元素都满足条件 * @example * import { every } from 'fp-ts/Array' * * const values=[] * assert.deepStrictEqual(every(a=>a==1)([1,2,3]), false) * * @since 0.5.0 */ everyIndex<A>(predicate: PredicateWithIndex<number, A>): (as: Kind<F, A>) => boolean; /** * True if predicate returns true for any entry in the `Collection`. * @desczh * 判断是否有一个元素都满足条件 * @example * import { some } from 'fp-ts/Array' * * assert.deepStrictEqual(some(a=>a===1)([1,2,3]), true) * * @since 0.5.0 */ someIndex<A>(predicate: PredicateWithIndex<number, A>): (as: Kind<F, A>) => boolean; } export declare function initIndexReaderCollection<URI extends URIS>(options: IndexReaderCollectionInput<URI> & ReaderCollection<URI>): IndexReaderCollection<URI>;