diff --git a/src/array/forEachRight.ts b/src/array/forEachRight.ts index 352348db..1cac2a56 100644 --- a/src/array/forEachRight.ts +++ b/src/array/forEachRight.ts @@ -20,10 +20,34 @@ * * console.log(result) // Output: [3, 2, 1] */ +export function forEachRight(arr: T[], callback: (value: T, index: number, arr: T[]) => void): void +/** + * Iterates over elements of 'arr' from right to left and invokes 'callback' for each element. + * + * @template T - The type of elements in the array. + * @param {T[]} arr - The array to iterate over. + * @param {(value: T, index: number, arr: T[]) => void} callback - The function invoked per iteration. + * The callback function receives three arguments: + * - 'value': The current element being processed in the array. + * - 'index': The index of the current element being processed in the array. + * - 'arr': The array 'forEachRight' was called upon. + * + * @example + * const array = [1, 2, 3]; + * const result: number[] = []; + * + * // Use the forEachRight function to iterate through the array and add each element to the result array. + * forEachRight(array, (value) => { + * result.push(value); + * }) + * + * console.log(result) // Output: [3, 2, 1] + */ +export function forEachRight(arr: readonly T[], callback: (value: T, index: number, arr: readonly T[]) => void): void -export function forEachRight(arr: T[], callback: (value: T, index: number, arr: T[]) => void): void { +export function forEachRight(arr: readonly T[], callback: (value: T, index: number, arr: T[]) => void): void { for (let i = arr.length - 1; i >= 0; i--) { const element = arr[i]; - callback(element, i, arr); + callback(element, i, arr as T[]); } } diff --git a/src/array/orderBy.ts b/src/array/orderBy.ts index a81dbce8..62d0418a 100644 --- a/src/array/orderBy.ts +++ b/src/array/orderBy.ts @@ -35,7 +35,7 @@ import { compareValues } from '../_internal/compareValues'; * // ] */ export function orderBy( - arr: T[], + arr: readonly T[], criteria: Array<((item: T) => unknown) | keyof T>, orders: Array<'asc' | 'desc'> ): T[] { diff --git a/src/array/sortBy.ts b/src/array/sortBy.ts index 172ef34b..95aade0c 100644 --- a/src/array/sortBy.ts +++ b/src/array/sortBy.ts @@ -32,6 +32,6 @@ import { orderBy } from './orderBy'; * // { user : 'foo', age: 24 }, * // ] */ -export function sortBy(arr: T[], criteria: Array<((item: T) => unknown) | keyof T>): T[] { +export function sortBy(arr: readonly T[], criteria: Array<((item: T) => unknown) | keyof T>): T[] { return orderBy(arr, criteria, ['asc']); } diff --git a/src/array/unzip.ts b/src/array/unzip.ts index 042abbeb..e0e16cb5 100644 --- a/src/array/unzip.ts +++ b/src/array/unzip.ts @@ -11,7 +11,7 @@ * const result = unzip(zipped); * // result will be [['a', 'b'], [true, false], [1, 2]] */ -export function unzip(zipped: Array<[...T]>): Unzip { +export function unzip(zipped: readonly [...T][]): Unzip { // For performance reasons, use this implementation instead of // const maxLen = Math.max(...zipped.map(arr => arr.length)); let maxLen = 0; diff --git a/src/array/zipObject.ts b/src/array/zipObject.ts index 2bbb0697..c1005578 100644 --- a/src/array/zipObject.ts +++ b/src/array/zipObject.ts @@ -28,7 +28,7 @@ * const result2 = zipObject(keys2, values2); * // result2 will be { a: 1, b: 2 } */ -export function zipObject

(keys: P[], values: V[]): Record { +export function zipObject

(keys: readonly P[], values: readonly V[]): Record { const result = {} as Record; for (let i = 0; i < keys.length; i++) { diff --git a/src/compat/array/flatten.ts b/src/compat/array/flatten.ts index acd6f7e8..803fcdb4 100644 --- a/src/compat/array/flatten.ts +++ b/src/compat/array/flatten.ts @@ -14,7 +14,7 @@ * const arr = flatten([1, [2, 3], [4, [5, 6]]], 2); * // Returns: [1, 2, 3, 4, 5, 6] */ -export function flatten(value: T[] | object, depth = 1 as D): Array> | [] { +export function flatten(value: readonly T[] | object, depth = 1 as D): Array> | [] { const result: Array> = []; const flooredDepth = Math.floor(depth); @@ -22,7 +22,7 @@ export function flatten(value: T[] | object, depth = 1 return result; } - const recursive = (arr: T[], currentDepth: number) => { + const recursive = (arr: readonly T[], currentDepth: number) => { for (const item of arr) { if ( currentDepth < flooredDepth && diff --git a/src/compat/array/flattenDepth.ts b/src/compat/array/flattenDepth.ts index 09d59b9f..349b4b60 100644 --- a/src/compat/array/flattenDepth.ts +++ b/src/compat/array/flattenDepth.ts @@ -17,7 +17,7 @@ import { flatten } from './flatten.ts'; * // Returns: [1, 2, 3, 4, 5, 6] */ export function flattenDepth( - value: T[] | object, + value: readonly T[] | object, depth = 1 as D ): Array> | [] { return flatten(value, depth); diff --git a/src/compat/array/orderBy.ts b/src/compat/array/orderBy.ts index 67c304ee..08e76f9f 100644 --- a/src/compat/array/orderBy.ts +++ b/src/compat/array/orderBy.ts @@ -34,7 +34,7 @@ export type Criterion = ((item: T) => unknown) | PropertyKey | PropertyKey[] * // ] */ export function orderBy( - collection: T[] | object | number | null | undefined, + collection: readonly T[] | object | number | null | undefined, criteria?: Criterion | Array>, orders?: unknown | unknown[] ): T[] { diff --git a/src/compat/array/size.ts b/src/compat/array/size.ts index 009c8465..dca64166 100644 --- a/src/compat/array/size.ts +++ b/src/compat/array/size.ts @@ -36,7 +36,7 @@ import { isNil } from '../../predicate/isNil.ts'; * const emptyObjSize = size(emptyObj); * // emptyObjSize will be 0 */ -export function size(target: T[] | object | string | Map | Set | null | undefined): number { +export function size(target: readonly T[] | object | string | Map | Set | null | undefined): number { if (isNil(target)) { return 0; } diff --git a/src/compat/array/sortBy.ts b/src/compat/array/sortBy.ts index 3f0b0486..1718c5ec 100644 --- a/src/compat/array/sortBy.ts +++ b/src/compat/array/sortBy.ts @@ -30,7 +30,7 @@ import { Criterion, orderBy } from './orderBy'; * // ] */ export function sortBy( - collection: T[] | object | number | null | undefined, + collection: readonly T[] | object | number | null | undefined, criteria?: Criterion | Array> ): T[] { return orderBy(collection, criteria, ['asc']); diff --git a/src/compat/array/zipObjectDeep.ts b/src/compat/array/zipObjectDeep.ts index f309c317..be5c3fe6 100644 --- a/src/compat/array/zipObjectDeep.ts +++ b/src/compat/array/zipObjectDeep.ts @@ -34,7 +34,7 @@ import { set } from '../object/set.ts'; * const result = zipObjectDeep(paths, values); * // result will be { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } } */ -export function zipObjectDeep

(keys: P[] | P[][], values: V[]): { [K in P]: V } { +export function zipObjectDeep

(keys: readonly P[] | readonly P[][], values: readonly V[]): { [K in P]: V } { const result = {} as { [K in P]: V }; const zipped = zip

(keys, values); diff --git a/src/compat/object/fromPairs.ts b/src/compat/object/fromPairs.ts index e4196a11..15857209 100644 --- a/src/compat/object/fromPairs.ts +++ b/src/compat/object/fromPairs.ts @@ -11,7 +11,7 @@ import { isArrayLike } from '../predicate/isArrayLike.ts'; * const result = fromPairs(pairs); * // result will be: { a: 1, b: 2 } */ -export function fromPairs(pairs: any[]): Record; +export function fromPairs(pairs: readonly any[]): Record; /** * Converts an array of key-value pairs into an object. * @@ -26,7 +26,7 @@ export function fromPairs(pairs: any[]): Record; * const result = fromPairs(pairs); * // result will be: { a: 1, b: 2 } */ -export function fromPairs(pairs: Array<[T, U]> | Map): Record; +export function fromPairs(pairs: ReadonlyArray<[T, U]> | Map): Record; /** * Converts an array of key-value pairs into an object. * @@ -41,7 +41,7 @@ export function fromPairs(pairs: Array<[T, U]> | Map(pairs: Array<[T, U]> | Map): Record { +export function fromPairs(pairs: ReadonlyArray<[T, U]> | Map): Record { if (!isArrayLike(pairs) && !(pairs instanceof Map)) { return {} as Record; } diff --git a/src/compat/object/get.ts b/src/compat/object/get.ts index b9f6e8df..93ea56b4 100644 --- a/src/compat/object/get.ts +++ b/src/compat/object/get.ts @@ -14,7 +14,7 @@ import type { Get } from './get.types.ts'; * @param {K | [K]} path - The path of the property to get. * @returns {T[K]} - Returns the resolved value. */ -export function get(object: T, path: K | [K]): T[K]; +export function get(object: T, path: K | readonly [K]): T[K]; /** * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead. * @@ -25,7 +25,7 @@ export function get(object: T, path: K | [K * @param {K | [K]} path - The path of the property to get. * @returns {T[K] | undefined} - Returns the resolved value. */ -export function get(object: T | null | undefined, path: K | [K]): T[K] | undefined; +export function get(object: T | null | undefined, path: K | readonly [K]): T[K] | undefined; /** * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead. * @@ -40,7 +40,7 @@ export function get(object: T | null | unde */ export function get( object: T | null | undefined, - path: K | [K], + path: K | readonly [K], defaultValue: D ): Exclude | D; /** @@ -54,7 +54,7 @@ export function get( * @param {[K1, K2]} path - The path of the property to get. * @returns {T[K1][K2]} - Returns the resolved value. */ -export function get(object: T, path: [K1, K2]): T[K1][K2]; +export function get(object: T, path: readonly [K1, K2]): T[K1][K2]; /** * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead. * @@ -68,7 +68,7 @@ export function get( object: T | null | undefined, - path: [K1, K2] + path: readonly [K1, K2] ): T[K1][K2] | undefined; /** * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead. @@ -85,7 +85,7 @@ export function get( object: T | null | undefined, - path: [K1, K2], + path: readonly [K1, K2], defaultValue: D ): Exclude | D; /** @@ -102,7 +102,7 @@ export function get( object: T, - path: [K1, K2, K3] + path: readonly [K1, K2, K3] ): T[K1][K2][K3]; /** * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead. @@ -118,7 +118,7 @@ export function get( object: T | null | undefined, - path: [K1, K2, K3] + path: readonly [K1, K2, K3] ): T[K1][K2][K3] | undefined; /** * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead. @@ -136,7 +136,7 @@ export function get( object: T | null | undefined, - path: [K1, K2, K3], + path: readonly [K1, K2, K3], defaultValue: D ): Exclude | D; /** @@ -158,7 +158,7 @@ export function get< K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], ->(object: T, path: [K1, K2, K3, K4]): T[K1][K2][K3][K4]; +>(object: T, path: readonly [K1, K2, K3, K4]): T[K1][K2][K3][K4]; /** * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead. * @@ -178,7 +178,7 @@ export function get< K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], ->(object: T | null | undefined, path: [K1, K2, K3, K4]): T[K1][K2][K3][K4] | undefined; +>(object: T | null | undefined, path: readonly [K1, K2, K3, K4]): T[K1][K2][K3][K4] | undefined; /** * Retrieves the value at a given path from an object. If the resolved value is undefined, the defaultValue is returned instead. * @@ -201,7 +201,7 @@ export function get< K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], D, ->(object: T | null | undefined, path: [K1, K2, K3, K4], defaultValue: D): Exclude | D; +>(object: T | null | undefined, path: readonly [K1, K2, K3, K4], defaultValue: D): Exclude | D; /** * Retrieves the value at a given path from an object with numeric keys. If the resolved value is undefined, the defaultValue is returned instead. * diff --git a/src/compat/object/pick.ts b/src/compat/object/pick.ts index 5b8806c6..8d49f5cc 100644 --- a/src/compat/object/pick.ts +++ b/src/compat/object/pick.ts @@ -19,7 +19,7 @@ import { set } from './set'; * const result = pick(obj, ['a', 'c']); * // result will be { a: 1, c: 3 } */ -export function pick, K extends keyof T>(obj: T, keys: K[]): Pick; +export function pick, K extends keyof T>(obj: T, keys: readonly K[]): Pick; /** * Creates a new object composed of the picked object properties. @@ -48,7 +48,7 @@ export function pick, K extends keyof T>(obj: T, k */ export function pick( obj: T | null | undefined, - ...keys: Array + ...keys: Array ): Partial; /** @@ -78,7 +78,7 @@ export function pick( */ export function pick( obj: T | null | undefined, - ...keysArr: Array + ...keysArr: Array ): Partial { if (isNil(obj)) { return {}; @@ -90,6 +90,8 @@ export function pick( switch (typeof keys) { case 'object': { if (!Array.isArray(keys)) { + // eslint-disable-next-line + // @ts-ignore keys = Array.from(keys) as PropertyKey[]; } break; diff --git a/src/object/pick.ts b/src/object/pick.ts index a8af09ca..f3df7569 100644 --- a/src/object/pick.ts +++ b/src/object/pick.ts @@ -15,7 +15,7 @@ * const result = pick(obj, ['a', 'c']); * // result will be { a: 1, c: 3 } */ -export function pick, K extends keyof T>(obj: T, keys: K[]): Pick { +export function pick, K extends keyof T>(obj: T, keys: readonly K[]): Pick { const result = {} as Pick; for (const key of keys) {