mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-24 20:35:54 +03:00
fix: Add missing readonlys to functions
This commit is contained in:
parent
06f68c20b9
commit
2f2c94d44f
@ -20,10 +20,34 @@
|
||||
*
|
||||
* console.log(result) // Output: [3, 2, 1]
|
||||
*/
|
||||
export function forEachRight<T>(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<T>(arr: readonly T[], callback: (value: T, index: number, arr: readonly T[]) => void): void
|
||||
|
||||
export function forEachRight<T>(arr: T[], callback: (value: T, index: number, arr: T[]) => void): void {
|
||||
export function forEachRight<T>(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[]);
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ import { compareValues } from '../_internal/compareValues';
|
||||
* // ]
|
||||
*/
|
||||
export function orderBy<T extends object>(
|
||||
arr: T[],
|
||||
arr: readonly T[],
|
||||
criteria: Array<((item: T) => unknown) | keyof T>,
|
||||
orders: Array<'asc' | 'desc'>
|
||||
): T[] {
|
||||
|
@ -32,6 +32,6 @@ import { orderBy } from './orderBy';
|
||||
* // { user : 'foo', age: 24 },
|
||||
* // ]
|
||||
*/
|
||||
export function sortBy<T extends object>(arr: T[], criteria: Array<((item: T) => unknown) | keyof T>): T[] {
|
||||
export function sortBy<T extends object>(arr: readonly T[], criteria: Array<((item: T) => unknown) | keyof T>): T[] {
|
||||
return orderBy(arr, criteria, ['asc']);
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
* const result = unzip(zipped);
|
||||
* // result will be [['a', 'b'], [true, false], [1, 2]]
|
||||
*/
|
||||
export function unzip<T extends unknown[]>(zipped: Array<[...T]>): Unzip<T> {
|
||||
export function unzip<T extends unknown[]>(zipped: readonly [...T][]): Unzip<T> {
|
||||
// For performance reasons, use this implementation instead of
|
||||
// const maxLen = Math.max(...zipped.map(arr => arr.length));
|
||||
let maxLen = 0;
|
||||
|
@ -28,7 +28,7 @@
|
||||
* const result2 = zipObject(keys2, values2);
|
||||
* // result2 will be { a: 1, b: 2 }
|
||||
*/
|
||||
export function zipObject<P extends PropertyKey, V>(keys: P[], values: V[]): Record<P, V> {
|
||||
export function zipObject<P extends PropertyKey, V>(keys: readonly P[], values: readonly V[]): Record<P, V> {
|
||||
const result = {} as Record<P, V>;
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
|
@ -14,7 +14,7 @@
|
||||
* const arr = flatten([1, [2, 3], [4, [5, 6]]], 2);
|
||||
* // Returns: [1, 2, 3, 4, 5, 6]
|
||||
*/
|
||||
export function flatten<T, D extends number = 1>(value: T[] | object, depth = 1 as D): Array<FlatArray<T[], D>> | [] {
|
||||
export function flatten<T, D extends number = 1>(value: readonly T[] | object, depth = 1 as D): Array<FlatArray<T[], D>> | [] {
|
||||
const result: Array<FlatArray<T[], D>> = [];
|
||||
const flooredDepth = Math.floor(depth);
|
||||
|
||||
@ -22,7 +22,7 @@ export function flatten<T, D extends number = 1>(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 &&
|
||||
|
@ -17,7 +17,7 @@ import { flatten } from './flatten.ts';
|
||||
* // Returns: [1, 2, 3, 4, 5, 6]
|
||||
*/
|
||||
export function flattenDepth<T, D extends number = 1>(
|
||||
value: T[] | object,
|
||||
value: readonly T[] | object,
|
||||
depth = 1 as D
|
||||
): Array<FlatArray<T[], D>> | [] {
|
||||
return flatten(value, depth);
|
||||
|
@ -34,7 +34,7 @@ export type Criterion<T> = ((item: T) => unknown) | PropertyKey | PropertyKey[]
|
||||
* // ]
|
||||
*/
|
||||
export function orderBy<T>(
|
||||
collection: T[] | object | number | null | undefined,
|
||||
collection: readonly T[] | object | number | null | undefined,
|
||||
criteria?: Criterion<T> | Array<Criterion<T>>,
|
||||
orders?: unknown | unknown[]
|
||||
): T[] {
|
||||
|
@ -36,7 +36,7 @@ import { isNil } from '../../predicate/isNil.ts';
|
||||
* const emptyObjSize = size(emptyObj);
|
||||
* // emptyObjSize will be 0
|
||||
*/
|
||||
export function size<T>(target: T[] | object | string | Map<unknown, T> | Set<T> | null | undefined): number {
|
||||
export function size<T>(target: readonly T[] | object | string | Map<unknown, T> | Set<T> | null | undefined): number {
|
||||
if (isNil(target)) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ import { Criterion, orderBy } from './orderBy';
|
||||
* // ]
|
||||
*/
|
||||
export function sortBy<T>(
|
||||
collection: T[] | object | number | null | undefined,
|
||||
collection: readonly T[] | object | number | null | undefined,
|
||||
criteria?: Criterion<T> | Array<Criterion<T>>
|
||||
): T[] {
|
||||
return orderBy(collection, criteria, ['asc']);
|
||||
|
@ -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<P extends PropertyKey, V>(keys: P[] | P[][], values: V[]): { [K in P]: V } {
|
||||
export function zipObjectDeep<P extends PropertyKey, V>(keys: readonly P[] | readonly P[][], values: readonly V[]): { [K in P]: V } {
|
||||
const result = {} as { [K in P]: V };
|
||||
const zipped = zip<P | P[], V>(keys, values);
|
||||
|
||||
|
@ -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<string, any>;
|
||||
export function fromPairs(pairs: readonly any[]): Record<string, any>;
|
||||
/**
|
||||
* Converts an array of key-value pairs into an object.
|
||||
*
|
||||
@ -26,7 +26,7 @@ export function fromPairs(pairs: any[]): Record<string, any>;
|
||||
* const result = fromPairs(pairs);
|
||||
* // result will be: { a: 1, b: 2 }
|
||||
*/
|
||||
export function fromPairs<T extends PropertyKey, U>(pairs: Array<[T, U]> | Map<T, U>): Record<T, U>;
|
||||
export function fromPairs<T extends PropertyKey, U>(pairs: ReadonlyArray<[T, U]> | Map<T, U>): Record<T, U>;
|
||||
/**
|
||||
* Converts an array of key-value pairs into an object.
|
||||
*
|
||||
@ -41,7 +41,7 @@ export function fromPairs<T extends PropertyKey, U>(pairs: Array<[T, U]> | Map<T
|
||||
* const result = fromPairs(pairs);
|
||||
* // result will be: { a: 1, b: 2 }
|
||||
*/
|
||||
export function fromPairs<T extends PropertyKey, U>(pairs: Array<[T, U]> | Map<T, U>): Record<T, U> {
|
||||
export function fromPairs<T extends PropertyKey, U>(pairs: ReadonlyArray<[T, U]> | Map<T, U>): Record<T, U> {
|
||||
if (!isArrayLike(pairs) && !(pairs instanceof Map)) {
|
||||
return {} as Record<T, U>;
|
||||
}
|
||||
|
@ -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<T extends object, K extends keyof T>(object: T, path: K | [K]): T[K];
|
||||
export function get<T extends object, K extends keyof T>(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<T extends object, K extends keyof T>(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<T extends object, K extends keyof T>(object: T | null | undefined, path: K | [K]): T[K] | undefined;
|
||||
export function get<T extends object, K extends keyof T>(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<T extends object, K extends keyof T>(object: T | null | unde
|
||||
*/
|
||||
export function get<T extends object, K extends keyof T, D>(
|
||||
object: T | null | undefined,
|
||||
path: K | [K],
|
||||
path: K | readonly [K],
|
||||
defaultValue: D
|
||||
): Exclude<T[K], undefined> | D;
|
||||
/**
|
||||
@ -54,7 +54,7 @@ export function get<T extends object, K extends keyof T, D>(
|
||||
* @param {[K1, K2]} path - The path of the property to get.
|
||||
* @returns {T[K1][K2]} - Returns the resolved value.
|
||||
*/
|
||||
export function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1]>(object: T, path: [K1, K2]): T[K1][K2];
|
||||
export function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1]>(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<T extends object, K1 extends keyof T, K2 extends keyof T[K1]
|
||||
*/
|
||||
export function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1]>(
|
||||
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<T extends object, K1 extends keyof T, K2 extends keyof T[K1]
|
||||
*/
|
||||
export function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1], D>(
|
||||
object: T | null | undefined,
|
||||
path: [K1, K2],
|
||||
path: readonly [K1, K2],
|
||||
defaultValue: D
|
||||
): Exclude<T[K1][K2], undefined> | D;
|
||||
/**
|
||||
@ -102,7 +102,7 @@ export function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1]
|
||||
*/
|
||||
export function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(
|
||||
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<T extends object, K1 extends keyof T, K2 extends keyof T[K1]
|
||||
*/
|
||||
export function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(
|
||||
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<T extends object, K1 extends keyof T, K2 extends keyof T[K1]
|
||||
*/
|
||||
export function get<T extends object, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], D>(
|
||||
object: T | null | undefined,
|
||||
path: [K1, K2, K3],
|
||||
path: readonly [K1, K2, K3],
|
||||
defaultValue: D
|
||||
): Exclude<T[K1][K2][K3], undefined> | 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<T[K1][K2][K3][K4], undefined> | D;
|
||||
>(object: T | null | undefined, path: readonly [K1, K2, K3, K4], defaultValue: D): Exclude<T[K1][K2][K3][K4], undefined> | 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.
|
||||
*
|
||||
|
@ -19,7 +19,7 @@ import { set } from './set';
|
||||
* const result = pick(obj, ['a', 'c']);
|
||||
* // result will be { a: 1, c: 3 }
|
||||
*/
|
||||
export function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
|
||||
export function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: readonly K[]): Pick<T, K>;
|
||||
|
||||
/**
|
||||
* Creates a new object composed of the picked object properties.
|
||||
@ -48,7 +48,7 @@ export function pick<T extends Record<string, any>, K extends keyof T>(obj: T, k
|
||||
*/
|
||||
export function pick<T extends {}>(
|
||||
obj: T | null | undefined,
|
||||
...keys: Array<PropertyKey | PropertyKey[] | PropertyKey[][]>
|
||||
...keys: Array<PropertyKey | readonly PropertyKey[] | readonly (readonly PropertyKey[])[]>
|
||||
): Partial<T>;
|
||||
|
||||
/**
|
||||
@ -78,7 +78,7 @@ export function pick<T extends {}>(
|
||||
*/
|
||||
export function pick<T extends {}>(
|
||||
obj: T | null | undefined,
|
||||
...keysArr: Array<PropertyKey | PropertyKey[] | PropertyKey[][]>
|
||||
...keysArr: Array<PropertyKey | readonly PropertyKey[] | readonly (readonly PropertyKey[])[]>
|
||||
): Partial<T> {
|
||||
if (isNil(obj)) {
|
||||
return {};
|
||||
@ -90,6 +90,8 @@ export function pick<T extends {}>(
|
||||
switch (typeof keys) {
|
||||
case 'object': {
|
||||
if (!Array.isArray(keys)) {
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
keys = Array.from(keys) as PropertyKey[];
|
||||
}
|
||||
break;
|
||||
|
@ -15,7 +15,7 @@
|
||||
* const result = pick(obj, ['a', 'c']);
|
||||
* // result will be { a: 1, c: 3 }
|
||||
*/
|
||||
export function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
|
||||
export function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: readonly K[]): Pick<T, K> {
|
||||
const result = {} as Pick<T, K>;
|
||||
|
||||
for (const key of keys) {
|
||||
|
Loading…
Reference in New Issue
Block a user