Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(flatten/flattenDeep/flattenDepth/slice/zipObjectDeep): fix lodash compatibility #716

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions docs/ja/reference/compat/array/flattenDepth.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
## インターフェース

```typescript
function flattenDepth<T, D extends number = 1>(value: T[] | object, depth: D): Array<FlatArray<T[], D>> | [];
function flattenDepth<T, D extends number = 1>(value: T[], depth: D): Array<FlatArray<T[], D>> | [];
```

### パラメータ

- `value` (`T[] | object`): フラット化する値。
- `value` (`T[]`): フラット化する値。

::: info `value` は `ArrayLike<T>`、`null`、または `undefined` になります。

lodash との完全な互換性を確保するため、`flattenDepth` 関数は `value` を次のように処理します。

- `value` が `ArrayLike<T>` の場合、`Array.from(...)` を使用して配列に変換されます。
- `value` が `null` または `undefined` の場合、空の配列として扱われます。

:::

- `depth` (`D`): ネストされた配列構造をどの深さまでフラット化するかを指定する深さレベル。デフォルトは1です。

### 戻り値
Expand Down
10 changes: 10 additions & 0 deletions docs/ja/reference/compat/array/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ function slice<T>(array: T[], start?: number, end?: number): T[];
### パラメータ

- `array` (`T[]`): 部分配列を作成する配列。

::: info `array` は `ArrayLike<T>`、`null`、または `undefined` になります。

lodash との完全な互換性を確保するため、`slice` 関数は `array` を次のように処理します。

- `array` が `ArrayLike<T>` の場合、`Array.from(...)` を使用して配列に変換されます。
- `array` が `null` または `undefined` の場合、空の配列として扱われます。

:::

- `start` (`number`): 開始位置。デフォルトは `0` です。
- `end` (`number`): 終了位置。デフォルトは `array.length` です。

Expand Down
6 changes: 3 additions & 3 deletions docs/ja/reference/compat/array/zipObjectDeep.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
## インターフェース

```typescript
function zipObjectDeep<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V };
function zipObjectDeep<P extends PropertyKey, V>(keys: ArrayLike<P | P[]>, values: ArrayLike<V>): { [K in P]: V };
```

### パラメータ

- `keys` (`P[]`): プロパティパスを含む配列。
- `values` (`V[]`): 対応する値を含む配列。
- `keys` (`ArrayLike<P | P[]>`): プロパティパスを含む配列。
- `values` (`ArrayLike<V>`): 対応する値を含む配列。

### 戻り値

Expand Down
14 changes: 12 additions & 2 deletions docs/ko/reference/compat/array/flattenDepth.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
## 인터페이스

```typescript
function flattenDepth<T, D extends number = 1>(value: T[] | object, depth: D): Array<FlatArray<T[], D>> | [];
function flattenDepth<T, D extends number = 1>(value: T[], depth: D): Array<FlatArray<T[], D>> | [];
```

### 파라미터

- `value` (`T[] | object`): 평평하게 할 값이에요.
- `value` (`T[]`): 평평하게 할 값이에요.

::: info `value`는 `ArrayLike<T>`, `null`, 또는 `undefined`가 될 수 있어요.

lodash와 완전한 호환성을 보장하기 위해, `flattenDepth` 함수는 `value`를 다음과 같이 처리해요.

- `value`가 `ArrayLike<T>`인 경우, `Array.from(...)`을 사용하여 배열로 변환돼요.
- `value`가 `null` 또는 `undefined`인 경우, 빈 배열로 처리돼요.

:::

- `depth` (`D`): 중첩 배열 구조가 얼마나 깊게 평평해져야 하는지 지정하는 깊이 수준이에요. 기본값은 1이에요.

### 반환 값
Expand Down
10 changes: 10 additions & 0 deletions docs/ko/reference/compat/array/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ function slice<T>(array: T[], start?: number, end?: number): T[];
### 파라미터

- `array` (`T[]`): 부분 배열을 만들 배열.

::: info `array`는 `ArrayLike<T>`, `null`, 또는 `undefined`가 될 수 있어요.

lodash와 완전한 호환성을 보장하기 위해, `slice` 함수는 `array`를 다음과 같이 처리해요.

- `array`가 `ArrayLike<T>`인 경우, `Array.from(...)`을 사용하여 배열로 변환돼요.
- `array`가 `null` 또는 `undefined`인 경우, 빈 배열로 처리돼요.

:::

- `start` (`number`): 시작 위치. 기본값은 `0`이에요.
- `end` (`number`): 끝 위치. 기본값은 `array.length`예요.

Expand Down
6 changes: 3 additions & 3 deletions docs/ko/reference/compat/array/zipObjectDeep.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
## 인터페이스

```typescript
function zipObjectDeep<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V };
function zipObjectDeep<P extends PropertyKey, V>(keys: ArrayLike<P | P[]>, values: ArrayLike<V>): { [K in P]: V };
```

### 파라미터

- `keys` (`P[]`): 프로퍼티 경로가 포함된 배열.
- `values` (`V[]`): 값에 대응되는 값이 포함된 배열.
- `keys` (`ArrayLike<P | P[]>`): 프로퍼티 경로가 포함된 배열.
- `values` (`ArrayLike<V>`): 값에 대응되는 값이 포함된 배열.

### 반환 값

Expand Down
14 changes: 12 additions & 2 deletions docs/reference/compat/array/flattenDepth.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ Flattens an array up to the specified depth.
## Signature

```typescript
function flattenDepth<T, D extends number = 1>(value: T[] | object, depth: D): Array<FlatArray<T[], D>> | [];
function flattenDepth<T, D extends number = 1>(value: T[], depth: D): Array<FlatArray<T[], D>> | [];
```

### Parameters

- `value` (`T[] | object`): The value to flatten.
- `value` (`T[]`): The value to flatten.

::: info `value` can be `ArrayLike<T>`, `null`, or `undefined`

To ensure full compatibility with lodash, the `flattenDepth` function handles `value` in this way:

- If `value` is an `ArrayLike<T>`, it gets converted into an array using `Array.from(...)`.
- If `value` is `null` or `undefined`, it will be treated as an empty array.

:::

- `depth` (`D`): The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.

### Returns
Expand Down
10 changes: 10 additions & 0 deletions docs/reference/compat/array/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ function slice<T>(array: T[], start?: number, end?: number): T[];
### Parameters

- `array` (`T[]`): The array to slice.

::: info `array` can be `ArrayLike<T>`, `null`, or `undefined`

To ensure full compatibility with lodash, the `slice` function handles `array` in this way:

- If `array` is an `ArrayLike<T>`, it gets converted into an array using `Array.from(...)`.
- If `array` is `null` or `undefined`, it will be treated as an empty array.

:::

- `start` (`number`): The start position. Defaults to `0`.
- `end` (`number`): The end position. Defaults to `array.length`.

Expand Down
6 changes: 3 additions & 3 deletions docs/reference/compat/array/zipObjectDeep.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ Paths can be dot-separated strings or arrays of property names. If the `keys` ar
## Signature

```typescript
function zipObjectDeep<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V };
function zipObjectDeep<P extends PropertyKey, V>(keys: ArrayLike<P | P[]>, values: ArrayLike<V>): { [K in P]: V };
```

### Parameters

- `keys` (`P[]`): An array of property names.
- `values` (`V[]`): An array of values corresponding to the property names.
- `keys` (`ArrayLike<P | P[]>`): An array of property names.
- `values` (`ArrayLike<V>`): An array of values corresponding to the property names.

### Returns

Expand Down
14 changes: 12 additions & 2 deletions docs/zh_hans/reference/compat/array/flattenDepth.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
## 签名

```typescript
function flattenDepth<T, D extends number = 1>(value: T[] | object, depth: D): Array<FlatArray<T[], D>> | [];
function flattenDepth<T, D extends number = 1>(value: T[], depth: D): Array<FlatArray<T[], D>> | [];
```

### 参数

- `value` (`T[] | object`): 要展平的值。
- `value` (`T[]`): 要展平的值。

::: info `value` 可以是 `ArrayLike<T>`、`null` 或 `undefined`。

为了确保与 lodash 的完全兼容性,`flattenDepth` 函数以以下方式处理 `value`:

- 如果 `value` 是 `ArrayLike<T>`,则会使用 `Array.from(...)` 将其转换为数组。
- 如果 `value` 是 `null` 或 `undefined`,则会将其视为一个空数组。

:::

- `depth` (`D`): 指定嵌套数组结构展平深度的级别。默认值为1。

### 返回值
Expand Down
10 changes: 10 additions & 0 deletions docs/zh_hans/reference/compat/array/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ function slice<T>(array: T[], start?: number, end?: number): T[];
### 参数

- `array` (`T[]`): 用于创建部分数组的数组。

::: info `array` 可以是 `ArrayLike<T>`、`null` 或 `undefined`。

为了确保与 lodash 的完全兼容性,`slice` 函数以以下方式处理 `array`:

- 如果 `array` 是 `ArrayLike<T>`,则会使用 `Array.from(...)` 将其转换为数组。
- 如果 `array` 是 `null` 或 `undefined`,则会将其视为一个空数组。

:::

- `start` (`number`): 开始位置。默认值为 `0`。
- `end` (`number`): 结束位置。默认值为 `array.length`。

Expand Down
6 changes: 3 additions & 3 deletions docs/zh_hans/reference/compat/array/zipObjectDeep.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
## 签名

```typescript
function zipObjectDeep<P extends string | number | symbol, V>(keys: P[], values: V[]): { [K in P]: V };
function zipObjectDeep<P extends PropertyKey, V>(keys: ArrayLike<P | P[]>, values: ArrayLike<V>): { [K in P]: V };
```

### 参数

- `keys` (`P[]`): 属性名称的数组。
- `values` (`V[]`): 与属性名称对应的值的数组。
- `keys` (`ArrayLike<P | P[]>`): 属性名称的数组。
- `values` (`ArrayLike<V>`): 与属性名称对应的值的数组。

### 返回值

Expand Down
8 changes: 7 additions & 1 deletion src/compat/array/flatten.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ describe('flatten', () => {
it('should return an empty array for non array-like objects', () => {
const nonArray = { 0: 'a' };
const expected: [] = [];
const actual = flatten(nonArray);
const actual = flatten(nonArray as any);

expect(actual).toEqual(expected);
});

it('should support array-like', () => {
expect(flatten({ 0: [1, 2, 3], length: 1 })).toEqual([1, 2, 3]);
expect(flatten('123')).toEqual(['1', '2', '3']);
expect(flatten(args)).toEqual([1, 2, 3]);
});
});
10 changes: 6 additions & 4 deletions src/compat/array/flatten.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { isArrayLike } from '../predicate/isArrayLike.ts';

/**
* Flattens an array up to the specified depth.
*
* @template T - The type of elements within the array.
* @template D - The depth to which the array should be flattened.
* @param {T[] | object} value - The object to flatten.
* @param {ArrayLike<T> | null | undefined} value - The object to flatten.
* @param {D} depth - The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
* @returns {Array<FlatArray<T[], D>> | []} A new array that has been flattened.
*
Expand All @@ -15,13 +17,13 @@
* // Returns: [1, 2, 3, 4, 5, 6]
*/
export function flatten<T, D extends number = 1>(
value: readonly T[] | object,
value: ArrayLike<T> | null | undefined,
depth = 1 as D
): Array<FlatArray<T[], D>> | [] {
const result: Array<FlatArray<T[], D>> = [];
const flooredDepth = Math.floor(depth);

if (!Array.isArray(value)) {
if (!isArrayLike(value)) {
return result;
}

Expand All @@ -45,7 +47,7 @@ export function flatten<T, D extends number = 1>(
}
};

recursive(value, 0);
recursive(Array.from(value), 0);

return result;
}
8 changes: 7 additions & 1 deletion src/compat/array/flattenDeep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ describe('flattenDeep', () => {
it('should return an empty array for non array-like objects', () => {
const nonArray = { 0: 'a' };
const expected: [] = [];
const actual = flattenDeep(nonArray);
const actual = flattenDeep(nonArray as any);

expect(actual).toEqual(expected);
});

it('should support array-like', () => {
expect(flattenDeep({ 0: [1, 2, [3]], length: 1 })).toEqual([1, 2, 3]);
expect(flattenDeep('123')).toEqual(['1', '2', '3']);
expect(flattenDeep(args)).toEqual([1, 2, 3]);
});
});
4 changes: 2 additions & 2 deletions src/compat/array/flattenDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ type ExtractNestedArrayType<T> = T extends ReadonlyArray<infer U> ? ExtractNeste
* Flattens all depths of a nested array.
*
* @template T - The type of elements within the array.
* @param {T[] | object} value - The value to flatten.
* @param {ArrayLike<T>} value - The value to flatten.
* @returns {Array<ExtractNestedArrayType<T>> | []} A new array that has been flattened.
*
* @example
* const value = flattenDeep([1, [2, [3]], [4, [5, 6]]]);
* // Returns: [1, 2, 3, 4, 5, 6]
*/
export function flattenDeep<T>(value: readonly T[] | object): Array<ExtractNestedArrayType<T>> | [] {
export function flattenDeep<T>(value: ArrayLike<T> | null | undefined): Array<ExtractNestedArrayType<T>> | [] {
return flatten(value, Infinity) as Array<ExtractNestedArrayType<T>>;
}
8 changes: 7 additions & 1 deletion src/compat/array/flattenDepth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('flattenDepth', () => {
it('should return an empty array for non array-like objects', () => {
const nonArray = { 0: 'a' };
const expected: [] = [];
const actual = flattenDepth(nonArray, 2);
const actual = flattenDepth(nonArray as any, 2);

expect(actual).toEqual(expected);
});
Expand All @@ -68,4 +68,10 @@ describe('flattenDepth', () => {
const array = [1, [2, [3, [4]], 5]];
expect(flattenDepth(array, 2.2)).toEqual([1, 2, 3, [4], 5]);
});

it('should support array-like', () => {
expect(flattenDepth({ 0: [1, 2, 3], length: 1 })).toEqual([1, 2, 3]);
expect(flattenDepth('123')).toEqual(['1', '2', '3']);
expect(flattenDepth(args)).toEqual([1, 2, 3]);
});
});
4 changes: 2 additions & 2 deletions src/compat/array/flattenDepth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { flatten } from './flatten.ts';
*
* @template T - The type of elements within the array.
* @template D - The depth to which the array should be flattened.
* @param {T[] | object} value - The value to flatten.
* @param {ArrayLike<T> | null | undefined} value - The value to flatten.
* @param {D} depth - The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
* @returns {Array<FlatArray<T[], D>> | []} A new array that has been flattened.
*
Expand All @@ -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: readonly T[] | object,
value: ArrayLike<T> | null | undefined,
depth = 1 as D
): Array<FlatArray<T[], D>> | [] {
return flatten(value, depth);
Expand Down
7 changes: 7 additions & 0 deletions src/compat/array/slice.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
import { slice } from './slice';
import { args } from '../_internal/args';
import { falsey } from '../_internal/falsey';

describe('slice', () => {
Expand Down Expand Up @@ -101,4 +102,10 @@ describe('slice', () => {
expect(slice(null as any)).toEqual([]);
expect(slice(undefined as any)).toEqual([]);
});

it('should support array-like', () => {
expect(slice({ 0: 1, 1: 2, 2: 3, length: 3 })).toEqual([1, 2, 3]);
expect(slice('123')).toEqual(['1', '2', '3']);
expect(slice(args)).toEqual([1, 2, 3]);
});
});
Loading