Skip to content

Commit

Permalink
Merge branch 'main' into imp/invariant
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi02014 authored Nov 9, 2024
2 parents 1a8c087 + 0feabb4 commit 7c8fa9e
Show file tree
Hide file tree
Showing 42 changed files with 554 additions and 132 deletions.
10 changes: 10 additions & 0 deletions benchmarks/performance/uniqBy.bench.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { bench, describe } from 'vitest';
import { uniqBy as uniqByToolkit_ } from 'es-toolkit';
import { uniqBy as uniqByToolkitCompat_ } from 'es-toolkit/compat';
import { randomInt } from 'crypto';
import { uniqBy as uniqByLodash_ } from 'lodash';

const uniqByToolkit = uniqByToolkit_;
const uniqByToolkitCompat = uniqByToolkitCompat_;
const uniqByLodash = uniqByLodash_;

describe('uniqBy, small arrays', () => {
bench('es-toolkit/uniqBy', () => {
uniqByToolkit([2.1, 1.2, 2.3], Math.floor);
});

bench('es-toolkit/compat/uniqBy', () => {
uniqByToolkitCompat([2.1, 1.2, 2.3], Math.floor);
});

bench('lodash/uniqBy', () => {
uniqByLodash([2.1, 1.2, 2.3], Math.floor);
});
Expand All @@ -23,6 +29,10 @@ describe('uniqBy, large arrays', () => {
uniqByToolkit(largeArray, Math.floor);
});

bench('es-toolkit/compat/uniqBy', () => {
uniqByToolkitCompat(largeArray, Math.floor);
});

bench('lodash/uniqBy', () => {
uniqByLodash(largeArray, Math.floor);
});
Expand Down
2 changes: 1 addition & 1 deletion docs/ja/reference/compat/predicate/isEmpty.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function isEmpty(): true;
function isEmpty(value: string): value is '';
function isEmpty(value: Map<any, any>): boolean;
function isEmpty(value: Set<any>): boolean;
function isEmpty(value: Array<any>): value is [];
function isEmpty(value: any[]): value is [];
function isEmpty<T extends Record<any, any>>(
value: T | null | undefined
): value is Record<keyof T, never> | null | undefined;
Expand Down
9 changes: 6 additions & 3 deletions docs/ja/reference/compat/util/iteratee.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ function iteratee(value: symbol | number | string | object): (...args: any[]) =>

```typescript
const func = iteratee();

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [{ a: 1 }, { a: 2 }, { a: 3 }]

const func = iteratee((object) => object.a);

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3]

const func = iteratee('a');

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3]

const func = iteratee({ a: 1 });
[{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 }

const func = iteratee(['a', 1]);
[{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 }
```
12 changes: 6 additions & 6 deletions docs/ja/reference/object/mapKeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
## インターフェース

```typescript
function mapKeys<T extends Record<PropertyKey, unknown>, K1 extends keyof T, K2 extends PropertyKey>(
function mapKeys<T extends Record<PropertyKey, any>, K extends PropertyKey>(
object: T,
getNewKey: (value: T[K1], key: K1, object: T) => N
): Record<K2, T[K]>;
getNewKey: (value: T[keyof T], key: keyof T, object: T) => K
): Record<K, T[keyof T]>;
```

### パラメータ

- `obj` (`T extends object`): キーを置き換えるオブジェクト。
- `getNewKey`: (`(value: T[K1], key: K1, object: T) => N`): 新しいキーを生成する関数。
- `obj` (`T extends Record<PropertyKey, any>`): キーを置き換えるオブジェクト。
- `getNewKey`: (`(value: T[keyof T], key: keyof T, object: T) => K`): 新しいキーを生成する関数。

### 戻り値

(`Record<K2, T[K]>`): キーが置き換えられた新しいオブジェクト。
(`Record<K, T[keyof T]>`): キーが置き換えられた新しいオブジェクト。

##

Expand Down
6 changes: 3 additions & 3 deletions docs/ja/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ bun add es-toolkit
::: code-group

```html [jsdelivr]
<script src="https://cdn.jsdelivr.net/npm/es-toolkit@^1"></script>
<script src="https://cdn.jsdelivr.net/npm/es-toolkit@%5E1"></script>
<script>
var arr = _.chunk([1, 2, 3, 4, 5, 6], 3);
</script>
```

```html [unpkg]
<script src="https://unpkg.com/es-toolkit@^1"></script>
<script src="https://unpkg.com/es-toolkit@%5E1"></script>
<script>
var arr = _.chunk([1, 2, 3, 4, 5, 6], 3);
</script>
Expand All @@ -88,7 +88,7 @@ bun add es-toolkit
<script type="importmap">
{
"imports": {
"es-toolkit": "https://esm.sh/es-toolkit@^1"
"es-toolkit": "https://esm.sh/es-toolkit@%5E1"
}
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/ko/reference/compat/predicate/isEmpty.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function isEmpty(): true;
function isEmpty(value: string): value is '';
function isEmpty(value: Map<any, any>): boolean;
function isEmpty(value: Set<any>): boolean;
function isEmpty(value: Array<any>): value is [];
function isEmpty(value: any[]): value is [];
function isEmpty<T extends Record<any, any>>(
value: T | null | undefined
): value is Record<keyof T, never> | null | undefined;
Expand Down
9 changes: 6 additions & 3 deletions docs/ko/reference/compat/util/iteratee.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ function iteratee(

```typescript
const func = iteratee();

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [{ a: 1 }, { a: 2 }, { a: 3 }]

const func = iteratee((object) => object.a);

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3]

const func = iteratee('a');

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3]

const func = iteratee({ a: 1 });
[{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 }

const func = iteratee(['a', 1]);
[{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 }
```
12 changes: 6 additions & 6 deletions docs/ko/reference/object/mapKeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
## 인터페이스

```typescript
function mapKeys<T extends Record<PropertyKey, unknown>, K1 extends keyof T, K2 extends PropertyKey>(
function mapKeys<T extends Record<PropertyKey, any>, K extends PropertyKey>(
object: T,
getNewKey: (value: T[K1], key: K1, object: T) => N
): Record<K2, T[K]>;
getNewKey: (value: T[keyof T], key: keyof T, object: T) => K
): Record<K, T[keyof T]>;
```

### 파라미터

- `obj` (`T extends object`): 키를 바꿀 객체.
- `getNewKey`: (`(value: T[K1], key: K1, object: T) => N`): 새로운 키를 생성하는 함수.
- `obj` (`T extends Record<PropertyKey, any>`): 키를 바꿀 객체.
- `getNewKey`: (`(value: T[keyof T], key: keyof T, object: T) => K`): 새로운 키를 생성하는 함수.

### 반환 값

(`Record<K2, T[K]>`): 키가 바뀐 새로운 객체.
(`Record<K, T[keyof T]>`): 키가 바뀐 새로운 객체.

## 예시

Expand Down
6 changes: 3 additions & 3 deletions docs/ko/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ bun add es-toolkit
::: code-group

```html [jsdelivr]
<script src="https://cdn.jsdelivr.net/npm/es-toolkit"></script>
<script src="https://cdn.jsdelivr.net/npm/es-toolkit@%5E1"></script>
<script>
var arr = _.chunk([1, 2, 3, 4, 5, 6], 3);
</script>
```

```html [unpkg]
<script src="https://unpkg.com/es-toolkit"></script>
<script src="https://unpkg.com/es-toolkit@%5E1"></script>
<script>
var arr = _.chunk([1, 2, 3, 4, 5, 6], 3);
</script>
Expand All @@ -88,7 +88,7 @@ bun add es-toolkit
<script type="importmap">
{
"imports": {
"es-toolkit": "https://esm.sh/es-toolkit"
"es-toolkit": "https://esm.sh/es-toolkit@%5E1"
}
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/compat/predicate/isEmpty.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Checks if a given value is empty.
function isEmpty(value: string): value is '';
function isEmpty(value: Map<any, any>): boolean;
function isEmpty(value: Set<any>): boolean;
function isEmpty(value: Array<any>): value is [];
function isEmpty(value: any[]): value is [];
function isEmpty<T extends Record<any, any>>(
value: T | null | undefined
): value is Record<keyof T, never> | null | undefined;
Expand Down
11 changes: 7 additions & 4 deletions docs/reference/compat/util/iteratee.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You can call `iteratee` with the following types of arguments:
- **Property-value pair**: Returns a boolean indicating whether the element's property matches the given value.
- **Partial object**: Returns a boolean indicating whether the element matches the properties of the partial object.

If you don't provide any arguments or pass `null`, this function will return a function that simply returns its input unchanged.
If you don't provide any arguments or pass `null`, this function will return a [function that simply returns its input unchanged](../../function/identity.md).

## Signature

Expand All @@ -40,14 +40,17 @@ function iteratee(

```typescript
const func = iteratee();

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [{ a: 1 }, { a: 2 }, { a: 3 }]

const func = iteratee((object) => object.a);

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3]

const func = iteratee('a');

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3]

const func = iteratee({ a: 1 });
[{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 }

const func = iteratee(['a', 1]);
[{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 }
```
12 changes: 6 additions & 6 deletions docs/reference/object/mapKeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ Creates a new object with the same values as the given object, but with keys gen
## Signature

```typescript
function mapKeys<T extends Record<PropertyKey, unknown>, K1 extends keyof T, K2 extends PropertyKey>(
function mapKeys<T extends Record<PropertyKey, any>, K extends PropertyKey>(
object: T,
getNewKey: (value: T[K1], key: K1, object: T) => N
): Record<K2, T[K]>;
getNewKey: (value: T[keyof T], key: keyof T, object: T) => K
): Record<K, T[keyof T]>;
```

### Parameters

- `obj` (`T extends object`): The object to iterate over.
- `getNewKey`: (`(value: T[K1], key: K1, object: T) => N`): The function invoked per own enumerable property.
- `obj` (`T extends Record<PropertyKey, any>`): The object to iterate over.
- `getNewKey`: (`(value: T[keyof T], key: keyof T, object: T) => K`): The function invoked per own enumerable property.

### Returns

(`Record<K2, T[K]>`): The new mapped object.
(`Record<K, T[keyof T]>`): The new mapped object.

## Examples

Expand Down
2 changes: 1 addition & 1 deletion docs/zh_hans/reference/compat/predicate/isEmpty.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function isEmpty(): true;
function isEmpty(value: string): value is '';
function isEmpty(value: Map<any, any>): boolean;
function isEmpty(value: Set<any>): boolean;
function isEmpty(value: Array<any>): value is [];
function isEmpty(value: any[]): value is [];
function isEmpty<T extends Record<any, any>>(
value: T | null | undefined
): value is Record<keyof T, never> | null | undefined;
Expand Down
11 changes: 7 additions & 4 deletions docs/zh_hans/reference/compat/util/iteratee.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- **属性-值对**: 返回一个布尔值,指示元素的属性是否与给定值匹配。
- **部分对象**: 返回一个布尔值,指示元素是否与部分对象的属性匹配。

如果你不提供任何参数或传递 `null`此函数将返回一个简单返回其输入的函数
如果你不提供任何参数或传递 `null`此函数将返回一个[简单返回其输入的函数](../../function/identity.md)

## 签名

Expand All @@ -39,14 +39,17 @@ function iteratee(

```typescript
const func = iteratee();

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [{ a: 1 }, { a: 2 }, { a: 3 }]

const func = iteratee((object) => object.a);

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3]

const func = iteratee('a');

[{ a: 1 }, { a: 2 }, { a: 3 }].map(func) // => [1, 2, 3]

const func = iteratee({ a: 1 });
[{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 }

const func = iteratee(['a', 1]);
[{ a: 1 }, { a: 2 }, { a: 3 }].find(func) // => { a: 1 }
```
12 changes: 6 additions & 6 deletions docs/zh_hans/reference/object/mapKeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
## 签名

```typescript
function mapKeys<T extends Record<PropertyKey, unknown>, K1 extends keyof T, K2 extends PropertyKey>(
function mapKeys<T extends Record<PropertyKey, any>, K extends PropertyKey>(
object: T,
getNewKey: (value: T[K1], key: K1, object: T) => K2
): Record<K2, T[K1]>;
getNewKey: (value: T[keyof T], key: keyof T, object: T) => K
): Record<K, T[keyof T]>;
```

### 参数

- `obj` (`T extends object`): 要迭代的对象。
- `getNewKey`: (`(value: T[K1], key: K1, object: T) => N`): 每个自身可枚举属性调用的函数。
- `obj` (`T extends Record<PropertyKey, any>`): 要迭代的对象。
- `getNewKey`: (`(value: T[keyof T], key: keyof T, object: T) => K`): 每个自身可枚举属性调用的函数。

### 返回值

(`Record<K2, T[K]>`): 新映射的对象。
(`Record<K, T[keyof T]>`): 新映射的对象。

## 示例

Expand Down
6 changes: 3 additions & 3 deletions docs/zh_hans/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ bun add es-toolkit
::: code-group

```html [jsdelivr]
<script src="https://cdn.jsdelivr.net/npm/es-toolkit"></script>
<script src="https://cdn.jsdelivr.net/npm/es-toolkit@%5E1"></script>
<script>
var arr = _.chunk([1, 2, 3, 4, 5, 6], 3);
</script>
```

```html [unpkg]
<script src="https://unpkg.com/es-toolkit"></script>
<script src="https://unpkg.com/es-toolkit@%5E1"></script>
<script>
var arr = _.chunk([1, 2, 3, 4, 5, 6], 3);
</script>
Expand All @@ -88,7 +88,7 @@ es-toolkit 也可在 [esm.sh](https://esm.sh) 上用于现代浏览器。
<script type="importmap">
{
"imports": {
"es-toolkit": "https://esm.sh/es-toolkit"
"es-toolkit": "https://esm.sh/es-toolkit@%5E1"
}
}
</script>
Expand Down
39 changes: 39 additions & 0 deletions src/compat/_internal/flattenArrayLike.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from 'vitest';
import { flattenArrayLike } from './flattenArrayLike';

describe('flattenArrayLike', () => {
it('should flatten an array of array-like objects', () => {
const input: Array<ArrayLike<string>> = [
{ length: 3, 0: 'a', 1: 'b', 2: 'c' },
{ length: 2, 0: 'd', 1: 'e' },
{ length: 0 },
];
const expectedOutput = ['a', 'b', 'c', 'd', 'e'];
expect(flattenArrayLike(input)).toEqual(expectedOutput);
});

it('should ignore non-array-like objects', () => {
const input: any[] = [{ length: 2, 0: 'x', 1: 'y' }, 3, { length: 1, 0: 'z' }];
const expectedOutput = ['x', 'y', 'z'];
expect(flattenArrayLike(input)).toEqual(expectedOutput);
});

it('should return an empty array when input is empty', () => {
const input: Array<ArrayLike<any>> = [];
const expectedOutput: any[] = [];
expect(flattenArrayLike(input)).toEqual(expectedOutput);
});

it('should handle nested array-like objects', () => {
const input: Array<ArrayLike<number[]>> = [
{ length: 2, 0: [1, 2], 1: [3, 4] },
{ length: 1, 0: [5, 6] },
];
const expectedOutput = [
[1, 2],
[3, 4],
[5, 6],
];
expect(flattenArrayLike(input)).toEqual(expectedOutput);
});
});
Loading

0 comments on commit 7c8fa9e

Please sign in to comment.