From 05261a91ef8e0acbb1cda58cd82c5cbf6eec9f95 Mon Sep 17 00:00:00 2001 From: Dongho Kim <70563791+mass2527@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:46:30 +0900 Subject: [PATCH] feat(gte): Add `gte` to compatibility layer (#850) Co-authored-by: Sojin Park --- benchmarks/performance/gte.bench.ts | 20 ++++++++++++++ docs/ja/reference/compat/util/gte.md | 32 ++++++++++++++++++++++ docs/ko/reference/compat/util/gte.md | 33 +++++++++++++++++++++++ docs/reference/compat/util/gte.md | 32 ++++++++++++++++++++++ docs/zh_hans/reference/compat/util/gte.md | 32 ++++++++++++++++++++++ src/compat/index.ts | 1 + src/compat/util/gte.spec.ts | 16 +++++++++++ src/compat/util/gte.ts | 21 +++++++++++++++ 8 files changed, 187 insertions(+) create mode 100644 benchmarks/performance/gte.bench.ts create mode 100644 docs/ja/reference/compat/util/gte.md create mode 100644 docs/ko/reference/compat/util/gte.md create mode 100644 docs/reference/compat/util/gte.md create mode 100644 docs/zh_hans/reference/compat/util/gte.md create mode 100644 src/compat/util/gte.spec.ts create mode 100644 src/compat/util/gte.ts diff --git a/benchmarks/performance/gte.bench.ts b/benchmarks/performance/gte.bench.ts new file mode 100644 index 000000000..c3649760c --- /dev/null +++ b/benchmarks/performance/gte.bench.ts @@ -0,0 +1,20 @@ +import { bench, describe } from 'vitest'; +import { gte as gteToolkitCompat_ } from 'es-toolkit/compat'; +import { gte as gteLodash_ } from 'lodash'; + +const gteToolkitCompat = gteToolkitCompat_; +const gteLodash = gteLodash_; + +describe('gte', () => { + bench('es-toolkit/compat/gte', () => { + gteToolkitCompat(3, 1); + gteToolkitCompat(3, 3); + gteToolkitCompat(1, 3); + }); + + bench('lodash/gte', () => { + gteLodash(3, 1); + gteLodash(3, 3); + gteLodash(1, 3); + }); +}); diff --git a/docs/ja/reference/compat/util/gte.md b/docs/ja/reference/compat/util/gte.md new file mode 100644 index 000000000..2c3822604 --- /dev/null +++ b/docs/ja/reference/compat/util/gte.md @@ -0,0 +1,32 @@ +# gte + +::: info +この関数は互換性のために `es-toolkit/compat` からのみインポートできます。代替可能なネイティブ JavaScript API があるか、まだ十分に最適化されていないためです。 + +`es-toolkit/compat` からこの関数をインポートすると、[lodash と完全に同じように動作](../../../compatibility.md)します。 +::: + +値が他の値以上であるかどうかを確認します。 + +## インターフェース + +```typescript +function gte(value: unknown, other: unknown): boolean; +``` + +### パラメータ + +- `value` (`unknown`): 比較する値です。 +- `other` (`unknown`): 比較する他の値です。 + +### 戻り値 + +(`boolean`): 値が他の値以上である場合は`true`を、それ以外の場合は`false`を返します。 + +## 例 + +```typescript +gte(3, 1); // => true +gte(3, 3); // => true +gte(1, 3); // => false +``` \ No newline at end of file diff --git a/docs/ko/reference/compat/util/gte.md b/docs/ko/reference/compat/util/gte.md new file mode 100644 index 000000000..24884fcff --- /dev/null +++ b/docs/ko/reference/compat/util/gte.md @@ -0,0 +1,33 @@ +# gte + +::: info +이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요. + +`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](../../../compatibility.md)해요. +::: + +값이 다른 값보다 크거나 같은지 확인해요. + +## 인터페이스 + +```typescript +function gte(value: unknown, other: unknown): boolean; +``` + +### 파라미터 + +- `value` (`unknown`): 비교할 값이에요. 문자열. +- `other` (`unknown`): 비교할 다른 값이에요. 문자열. + +### 반환 값 + +(`boolean`): 값이 다른 값보다 크거나 같으면 `true`를 반환하고, 그렇지 않으면 `false`를 반환해요. + 문자열. + +## 예시 + +```typescript +gte(3, 1); // => true +gte(3, 3); // => true +gte(1, 3); // => false +``` \ No newline at end of file diff --git a/docs/reference/compat/util/gte.md b/docs/reference/compat/util/gte.md new file mode 100644 index 000000000..ca7c7330b --- /dev/null +++ b/docs/reference/compat/util/gte.md @@ -0,0 +1,32 @@ +# gte + +::: 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). +::: + +Checks if value is greater than or equal to other. + +## Signature + +```typescript +function gte(value: unknown, other: unknown): boolean; +``` + +### Parameters + +- `value` (`unknown`): The value to compare. +- `other` (`unknown`): The other value to compare. + +### Returns + +(`boolean`): Returns `true` if value is greater than or equal to other, else `false`. + +## Examples + +```typescript +gte(3, 1); // => true +gte(3, 3); // => true +gte(1, 3); // => false +``` \ No newline at end of file diff --git a/docs/zh_hans/reference/compat/util/gte.md b/docs/zh_hans/reference/compat/util/gte.md new file mode 100644 index 000000000..4e3468e47 --- /dev/null +++ b/docs/zh_hans/reference/compat/util/gte.md @@ -0,0 +1,32 @@ +# gte + +::: info +出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。 + +从 `es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](../../../compatibility.md)。 +::: + +检查值是否大于或等于另一个值。 + +## 签名 + +```typescript +function gte(value: unknown, other: unknown): boolean; +``` + +### 参数 + +- `value` (`unknown`): 要比较的值。 +- `other` (`unknown`): 要比较的另一个值。 + +### 返回值 + +(`boolean`): 如果值大于或等于另一个值,则返回`true`,否则返回`false`。 + +## 示例 + +```typescript +gte(3, 1); // => true +gte(3, 3); // => true +gte(1, 3); // => false +``` \ No newline at end of file diff --git a/src/compat/index.ts b/src/compat/index.ts index 985b83763..916f75034 100644 --- a/src/compat/index.ts +++ b/src/compat/index.ts @@ -178,6 +178,7 @@ export { words } from './string/words.ts'; export { constant } from './util/constant.ts'; export { defaultTo } from './util/defaultTo.ts'; export { eq } from './util/eq.ts'; +export { gte } from './util/gte.ts'; export { gt } from './util/gt.ts'; export { iteratee } from './util/iteratee.ts'; export { times } from './util/times.ts'; diff --git a/src/compat/util/gte.spec.ts b/src/compat/util/gte.spec.ts new file mode 100644 index 000000000..824276c3d --- /dev/null +++ b/src/compat/util/gte.spec.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from 'vitest'; +import { gte } from './gte'; + +describe('gte', () => { + it('should return `true` if `value` >= `other`', () => { + expect(gte(3, 1)).toBe(true); + expect(gte(3, 3)).toBe(true); + expect(gte('def', 'abc')).toBe(true); + expect(gte('def', 'def')).toBe(true); + }); + + it('should return `false` if `value` is less than `other`', () => { + expect(gte(1, 3)).toBe(false); + expect(gte('abc', 'def')).toBe(false); + }); +}); diff --git a/src/compat/util/gte.ts b/src/compat/util/gte.ts new file mode 100644 index 000000000..0dc867f61 --- /dev/null +++ b/src/compat/util/gte.ts @@ -0,0 +1,21 @@ +import { toNumber } from './toNumber.ts'; + +/** + * Checks if value is greater than or equal to other. + * + * @param {unknown} value The value to compare. + * @param {unknown} other The other value to compare. + * @returns {boolean} Returns `true` if value is greater than or equal to other, else `false`. + * + * @example + * gte(3, 1); // => true + * gte(3, 3); // => true + * gte(1, 3); // => false + */ +export function gte(value: unknown, other: unknown): boolean { + if (typeof value === 'string' && typeof other === 'string') { + return value >= other; + } + + return toNumber(value) >= toNumber(other); +}