Skip to content

Commit

Permalink
feat(uniqueId): Add uniqueId to compat layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Na-hyunwoo committed Oct 15, 2024
1 parent d19a581 commit c55c221
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 0 deletions.
31 changes: 31 additions & 0 deletions benchmarks/performance/uniqueId.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { bench, describe } from 'vitest';
import { uniqueId as uniqueIdToolkitCompat } from 'es-toolkit/compat';
import { uniqueId as uniqueIdLodash } from 'lodash';

describe('uniqueId', () => {
bench('es-toolkit/compat/uniqueId', () => {
uniqueIdToolkitCompat();
uniqueIdToolkitCompat();
uniqueIdToolkitCompat();
uniqueIdToolkitCompat();
uniqueIdToolkitCompat();
uniqueIdToolkitCompat('prefix_');
uniqueIdToolkitCompat('prefix_');
uniqueIdToolkitCompat('prefix_');
uniqueIdToolkitCompat('prefix_');
uniqueIdToolkitCompat('prefix_');
});

bench('lodash/uniqueId', () => {
uniqueIdLodash();
uniqueIdLodash();
uniqueIdLodash();
uniqueIdLodash();
uniqueIdLodash();
uniqueIdLodash('prefix_');
uniqueIdLodash('prefix_');
uniqueIdLodash('prefix_');
uniqueIdLodash('prefix_');
uniqueIdLodash('prefix_');
});
});
30 changes: 30 additions & 0 deletions docs/ja/reference/compat/util/uniqueId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# toString

::: info
この関数は互換性のために `es-toolkit/compat` からのみインポートできます。代替可能なネイティブ JavaScript API があるか、まだ十分に最適化されていないためです。

`es-toolkit/compat` からこの関数をインポートすると、[lodash と完全に同じように動作](../../../compatibility.md)します。
:::

Generates a unique ID. If prefix is given, the ID is appended to it.

## インターフェース

```typescript
function uniqueId(value?: string): string;
```

### パラメータ

- `value` (`string`, optional): ID の先頭に付ける値。

### 戻り値

(`string`): 一意の ID を返します。

##

```typescript
uniqueId('contact_'); // => 'contact_104'
uniqueId(); // => '105'
```
30 changes: 30 additions & 0 deletions docs/ko/reference/compat/util/uniqueId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# uniqueId

::: info
이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요.

`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](../../../compatibility.md)해요.
:::

고유 ID를 생성해요. 접두사가 주어지면 ID가 추가돼요.

## 인터페이스

```typescript
function uniqueId(value?: string): string;
```

### 파라미터

- `value` (`string`, optional): ID 앞에 붙는 값.

### 반환 값

(`string`): 고유 ID 값.

## 예시

```typescript
uniqueId('contact_'); // => 'contact_104'
uniqueId(); // => '105'
```
30 changes: 30 additions & 0 deletions docs/reference/compat/util/uniqueId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# constant

::: info
This function is only available in `es-toolkit/compat` for compatibility reasons. It either has alternative native JavaScript APIs or isn’t fully optimized yet.

When imported from `es-toolkit/compat`, it behaves exactly like lodash and provides the same functionalities, as detailed [here](../../../compatibility.md).
:::

Generates a unique ID. If prefix is given, the ID is appended to it.

## Signature

```typescript
function uniqueId(value?: string): string;
```

### Parameters

- `value` (`string`, optional): The value to prefix the ID with.

### Returns

(`string`): Returns the unique ID.

## Examples

```typescript
uniqueId('contact_'); // => 'contact_104'
uniqueId(); // => '105'
```
30 changes: 30 additions & 0 deletions docs/zh_hans/reference/compat/util/uniqueId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# toString

::: info
出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。

`es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](../../../compatibility.md)
:::

生成唯一的 ID。如果给出了前缀,则将 ID 附加到其上。

## 签名

```typescript
function uniqueId(value?: string): string;
```

### 参数

- `value` (`string`, optional): ID 的前缀值。

### 返回值

(`string`): 返回唯一 ID。

## 示例

```typescript
uniqueId('contact_'); // => 'contact_104'
uniqueId(); // => '105'
```
1 change: 1 addition & 0 deletions src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,4 @@ export { toNumber } from './util/toNumber.ts';
export { toPath } from './util/toPath.ts';
export { toSafeInteger } from './util/toSafeInteger.ts';
export { toString } from './util/toString.ts';
export { uniqueId } from './util/uniqueId.ts';
31 changes: 31 additions & 0 deletions src/compat/util/uniqueId.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from 'vitest';
import { uniqueId } from './uniqueId';

describe('uniqueId', () => {
it('should generate unique numeric IDs when called without a prefix', () => {
const id1 = uniqueId();
const id2 = uniqueId();

expect(id1).toBe('1');
expect(id2).toBe('2');
});

it('should append increasing numeric IDs to the given prefix', () => {
const prefixedId1 = uniqueId('test_');
const prefixedId2 = uniqueId('test_');

expect(prefixedId1).toBe('test_3');
expect(prefixedId2).toBe('test_4');
});

it('should handle empty string prefix', () => {
const id = uniqueId('');
expect(id).toBe('5');
});

it('should generate consecutive IDs across different prefix types', () => {
expect(uniqueId('prefix_')).toBe('prefix_6');
expect(uniqueId()).toBe('7');
expect(uniqueId('another_')).toBe('another_8');
});
});
36 changes: 36 additions & 0 deletions src/compat/util/uniqueId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { toString } from './toString';

/** Counter used to generate unique numeric identifiers. */
let idCounter = 0;

/**
* Generates a unique identifier, optionally prefixed with a given string.
*
* This function increments an internal counter to ensure uniqueness within
* the current session. If a prefix is provided, it is prepended to the
* generated identifier.
*
* @param {string} [prefix] - An optional string to prefix the unique identifier.
* If not provided or not a string, only the unique
* numeric identifier is returned.
* @returns {string} A string containing the unique identifier, with the optional
* prefix if provided.
*
* @example
* // Generate a unique ID with a prefix
* uniqueId('user_'); // => 'user_1'
*
* @example
* // Generate a unique ID without a prefix
* uniqueId(); // => '2'
*
* @example
* // Subsequent calls increment the internal counter
* uniqueId('item_'); // => 'item_3'
* uniqueId(); // => '4'
*/
export function uniqueId(prefix?: string): string {
const id = ++idCounter;

return toString(prefix) + id;
}

0 comments on commit c55c221

Please sign in to comment.