UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

168 lines 7.02 kB
/** * Returns a new array with an element at the specified index replaced. * * @example * * ```ts * const scores: number[] = [10, 20, 30]; * * const updated = Arr.set(scores, 1, 25); * * assert.deepStrictEqual(updated, [10, 25, 30]); * ``` */ export declare function set<const Ar extends readonly unknown[], const V = Ar[number]>(array: Ar, index: ArgArrayIndex<Ar>, newValue: V): IsFixedLengthList<Ar> extends true ? Readonly<{ [K in keyof Ar]: Ar[K] | V; }> : Ar extends NonEmptyArray<unknown> ? NonEmptyArray<Ar[number] | V> : readonly (Ar[number] | V)[]; export declare function set<const V>(index: SizeType.ArgArr, newValue: V): <const Ar extends readonly unknown[]>(array: Ar) => IsFixedLengthList<Ar> extends true ? Readonly<{ [K in keyof Ar]: Ar[K] | V; }> : Ar extends NonEmptyArray<unknown> ? NonEmptyArray<Ar[number] | V> : readonly (Ar[number] | V)[]; /** * Returns a new array with an element at the specified index updated by applying a function. * * @example * * ```ts * const temperatures: number[] = [20, 21, 22]; * * const increased = Arr.toUpdated(temperatures, 1, (value) => value + 5); * * const incrementLast = Arr.toUpdated<number>( * 2, * (value) => value + 1, * )(temperatures); * * assert.deepStrictEqual(increased, [20, 26, 22]); * * assert.deepStrictEqual(incrementLast, [20, 21, 23]); * ``` */ export declare function toUpdated<const Ar extends readonly unknown[], const V = Ar[number]>(array: Ar, index: ArgArrayIndex<Ar>, updater: (prev: Ar[number]) => V): IsFixedLengthList<Ar> extends true ? Readonly<{ [K in keyof Ar]: Ar[K] | V; }> : Ar extends NonEmptyArray<unknown> ? NonEmptyArray<Ar[number] | V> : readonly (Ar[number] | V)[]; export declare function toUpdated<E, const V = E>(index: SizeType.ArgArr, updater: (prev: E) => V): <const Ar extends readonly E[]>(array: Ar) => IsFixedLengthList<Ar> extends true ? Readonly<{ [K in keyof Ar]: Ar[K] | V; }> : Ar extends NonEmptyArray<unknown> ? NonEmptyArray<Ar[number] | V> : readonly (Ar[number] | V)[]; /** * Returns a new array with a value inserted at the specified index. * * @example * * ```ts * const numbers = [1, 2, 4] as const; * * const withThree = Arr.toInserted(numbers, 2, 3); * * const appendFive = Arr.toInserted(3, 5)(numbers); * * assert.deepStrictEqual(withThree, [1, 2, 3, 4]); * * assert.deepStrictEqual(appendFive, [1, 2, 4, 5]); * ``` */ export declare function toInserted<const Ar extends readonly unknown[], const V = Ar[number]>(array: Ar, index: ArgArrayIndexWithNegative<Ar>, newValue: V): IsFixedLengthList<Ar> extends true ? ArrayOfLength<CastToNumber<Increment<Ar['length']>>, Ar[number] | V> : NonEmptyArray<Ar[number] | V>; export declare function toInserted<const V>(index: SizeType.ArgArrWithNegative, newValue: V): <const Ar extends readonly unknown[]>(array: Ar) => IsFixedLengthList<Ar> extends true ? ArrayOfLength<CastToNumber<Increment<Ar['length']>>, Ar[number] | V> : NonEmptyArray<Ar[number] | V>; type CastToNumber<T> = T extends number ? T : never; /** * Returns a new array with the element at the specified index removed. * * @example * * ```ts * const letters = ['a', 'b', 'c', 'd'] as const; * * const withoutSecond = Arr.toRemoved(letters, 1); * * const withoutFirstCurried = Arr.toRemoved(0)(letters); * * assert.deepStrictEqual(withoutSecond, ['a', 'c', 'd']); * * assert.deepStrictEqual(withoutFirstCurried, ['b', 'c', 'd']); * ``` */ export declare function toRemoved<const Ar extends readonly unknown[]>(array: Ar, index: ArgArrayIndexWithNegative<Ar>): readonly Ar[number][]; export declare function toRemoved(index: SizeType.ArgArrWithNegative): <E>(array: readonly E[]) => readonly E[]; /** * Returns a new array with a value appended to the end. * * @example * * ```ts * const base = [1, 2] as const; * * const appended = Arr.toPushed(base, 3); * * const appendedCurried = Arr.toPushed(4)(base); * * assert.deepStrictEqual(appended, [1, 2, 3]); * * assert.deepStrictEqual(appendedCurried, [1, 2, 4]); * ``` */ export declare function toPushed<const Ar extends readonly unknown[], const V>(array: Ar, newValue: V): readonly [...Ar, V]; export declare function toPushed<const V>(newValue: V): <const Ar extends readonly unknown[]>(array: Ar) => readonly [...Ar, V]; /** * Returns a new array with a value prepended to the beginning. * * @example * * ```ts * const base = [2, 3] as const; * * const prefixed = Arr.toUnshifted(base, 1); * * const prefixedCurried = Arr.toUnshifted(0)(base); * * assert.deepStrictEqual(prefixed, [1, 2, 3]); * * assert.deepStrictEqual(prefixedCurried, [0, 2, 3]); * ``` */ export declare function toUnshifted<const Ar extends readonly unknown[], const V>(array: Ar, newValue: V): readonly [V, ...Ar]; export declare function toUnshifted<const V>(newValue: V): <const Ar extends readonly unknown[]>(array: Ar) => readonly [V, ...Ar]; /** * Returns a new array with all elements replaced by the specified value. * * @example * * ```ts * const base = [1, 2, 3]; * * const filled = Arr.toFilled(base, 0); * * const filledCurried = Arr.toFilled('x')(base); * * assert.deepStrictEqual(filled, [0, 0, 0]); * * assert.deepStrictEqual(filledCurried, ['x', 'x', 'x']); * ``` */ export declare function toFilled<const Ar extends readonly unknown[], const V>(array: Ar, value: V): IsFixedLengthList<Ar> extends true ? ArrayOfLength<Ar['length'], V> : Ar extends NonEmptyArray<unknown> ? NonEmptyArray<V> : readonly V[]; export declare function toFilled<const V>(value: V): <const Ar extends readonly unknown[]>(array: Ar) => IsFixedLengthList<Ar> extends true ? ArrayOfLength<Ar['length'], V> : Ar extends NonEmptyArray<unknown> ? NonEmptyArray<V> : readonly V[]; /** * Returns a new array with elements in the specified range replaced by the specified value. * * @example * * ```ts * const base = [0, 1, 2, 3, 4]; * * const filledMiddle = Arr.toRangeFilled(base, 9, [1, 4]); * * const filledPrefix = Arr.toRangeFilled(8, [0, 2])(base); * * assert.deepStrictEqual(filledMiddle, [0, 9, 9, 9, 4]); * * assert.deepStrictEqual(filledPrefix, [8, 8, 2, 3, 4]); * ``` */ export declare function toRangeFilled<const Ar extends readonly unknown[], const V>(array: Ar, value: V, fillRange: readonly [ start: ArgArrayIndexWithNegative<Ar>, end: ArgArrayIndexWithNegative<Ar> ]): IsFixedLengthList<Ar> extends true ? ArrayOfLength<Ar['length'], V | Ar[number]> : Ar extends NonEmptyArray<unknown> ? NonEmptyArray<V | Ar[number]> : readonly (V | Ar[number])[]; export declare function toRangeFilled<const V>(value: V, fillRange: readonly [ start: SizeType.ArgArrWithNegative, end: SizeType.ArgArrWithNegative ]): <const Ar extends readonly unknown[]>(array: Ar) => IsFixedLengthList<Ar> extends true ? ArrayOfLength<Ar['length'], V | Ar[number]> : Ar extends NonEmptyArray<unknown> ? NonEmptyArray<V | Ar[number]> : readonly (V | Ar[number])[]; export {}; //# sourceMappingURL=array-utils-modification.d.mts.map