From 16290ababd84a6605e3635a63403e0d4f00acf0e Mon Sep 17 00:00:00 2001 From: Sojin Park Date: Sun, 1 Dec 2024 21:41:35 +0900 Subject: [PATCH] feat(now): Add now --- docs/ja/reference/compat/util/now.md | 31 +++++++++++++++++++++++ docs/ko/reference/compat/util/now.md | 31 +++++++++++++++++++++++ docs/reference/compat/util/now.md | 31 +++++++++++++++++++++++ docs/zh_hans/reference/compat/util/now.md | 31 +++++++++++++++++++++++ src/compat/index.ts | 1 + src/compat/util/now.spec.ts | 16 ++++++++++++ src/compat/util/now.ts | 18 +++++++++++++ 7 files changed, 159 insertions(+) create mode 100644 docs/ja/reference/compat/util/now.md create mode 100644 docs/ko/reference/compat/util/now.md create mode 100644 docs/reference/compat/util/now.md create mode 100644 docs/zh_hans/reference/compat/util/now.md create mode 100644 src/compat/util/now.spec.ts create mode 100644 src/compat/util/now.ts diff --git a/docs/ja/reference/compat/util/now.md b/docs/ja/reference/compat/util/now.md new file mode 100644 index 000000000..a348651ed --- /dev/null +++ b/docs/ja/reference/compat/util/now.md @@ -0,0 +1,31 @@ +# now + +::: info +この関数は互換性のために `es-toolkit/compat` からのみインポートできます。代替可能なネイティブ JavaScript API があるか、まだ十分に最適化されていないためです。 + +`es-toolkit/compat` からこの関数をインポートすると、[lodash と完全に同じように動作](../../../compatibility.md)します。 +::: + +1970年1月1日00:00:00 UTCから経過したミリ秒数を返します。 + +## インターフェース + +```typescript +function now(): number; +``` + +### 戻り値 + +(`number`): 1970年1月1日00:00:00 UTC以降に経過したミリ秒。 + +## 例 + +```typescript +const currentTime = now(); +console.log(currentTime); // Outputs the current time in milliseconds + +const startTime = now(); +// Some time-consuming operation +const endTime = now(); +console.log(`Operation took ${endTime - startTime} milliseconds`); +``` diff --git a/docs/ko/reference/compat/util/now.md b/docs/ko/reference/compat/util/now.md new file mode 100644 index 000000000..fe954944f --- /dev/null +++ b/docs/ko/reference/compat/util/now.md @@ -0,0 +1,31 @@ +# now + +::: info +이 함수는 호환성을 위한 `es-toolkit/compat` 에서만 가져올 수 있어요. 대체할 수 있는 네이티브 JavaScript API가 있거나, 아직 충분히 최적화되지 않았기 때문이에요. + +`es-toolkit/compat`에서 이 함수를 가져오면, [lodash와 완전히 똑같이 동작](../../../compatibility.md)해요. +::: + +1970년 1월 1일 00:00:00 UTC 이후 경과된 밀리초를 반환해요. + +## 인터페이스 + +```typescript +function now(): number; +``` + +### 반환 값 + +(`number`): 1970년 1월 1일 00:00:00 UTC 이후 경과된 밀리초. + +## 예시 + +```typescript +const currentTime = now(); +console.log(currentTime); // Outputs the current time in milliseconds + +const startTime = now(); +// Some time-consuming operation +const endTime = now(); +console.log(`Operation took ${endTime - startTime} milliseconds`); +``` diff --git a/docs/reference/compat/util/now.md b/docs/reference/compat/util/now.md new file mode 100644 index 000000000..80942a0c8 --- /dev/null +++ b/docs/reference/compat/util/now.md @@ -0,0 +1,31 @@ +# now + +::: 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). +::: + +Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. + +## Signature + +```typescript +function now(): number; +``` + +### Returns + +(`number`): The current time in milliseconds. + +## Examples + +```typescript +const currentTime = now(); +console.log(currentTime); // Outputs the current time in milliseconds + +const startTime = now(); +// Some time-consuming operation +const endTime = now(); +console.log(`Operation took ${endTime - startTime} milliseconds`); +``` diff --git a/docs/zh_hans/reference/compat/util/now.md b/docs/zh_hans/reference/compat/util/now.md new file mode 100644 index 000000000..f8954f9ca --- /dev/null +++ b/docs/zh_hans/reference/compat/util/now.md @@ -0,0 +1,31 @@ +# now + +::: info +出于兼容性原因,此函数仅在 `es-toolkit/compat` 中提供。它可能具有替代的原生 JavaScript API,或者尚未完全优化。 + +从 `es-toolkit/compat` 导入时,它的行为与 lodash 完全一致,并提供相同的功能,详情请见 [这里](../../../compatibility.md)。 +::: + +返回自1970年1月1日00:00:00 UTC以来经过的毫秒数。 + +## 签名 + +```typescript +function now(): number; +``` + +### 返回值 + +(`number`): 自1970年1月1日00:00:00 UTC以来经过的毫秒数。 + +## 示例 + +```typescript +const currentTime = now(); +console.log(currentTime); // Outputs the current time in milliseconds + +const startTime = now(); +// Some time-consuming operation +const endTime = now(); +console.log(`Operation took ${endTime - startTime} milliseconds`); +``` diff --git a/src/compat/index.ts b/src/compat/index.ts index 664a0d571..5ca9f9a75 100644 --- a/src/compat/index.ts +++ b/src/compat/index.ts @@ -185,6 +185,7 @@ export { eq } from './util/eq.ts'; export { gt } from './util/gt.ts'; export { gte } from './util/gte.ts'; export { iteratee } from './util/iteratee.ts'; +export { now } from './util/now.ts'; export { stubArray } from './util/stubArray.ts'; export { stubFalse } from './util/stubFalse.ts'; export { stubObject } from './util/stubObject.ts'; diff --git a/src/compat/util/now.spec.ts b/src/compat/util/now.spec.ts new file mode 100644 index 000000000..621d3375e --- /dev/null +++ b/src/compat/util/now.spec.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from 'vitest'; +import { now } from './now'; +import { delay } from '../../promise/delay'; + +describe('now', () => { + it('should return the number of milliseconds that have elapsed since the Unix epoch', async () => { + const stamp = +new Date(); + const actual = now(); + + expect(actual).toBeGreaterThanOrEqual(stamp); + + await delay(32); + + expect(now() > actual); + }); +}); diff --git a/src/compat/util/now.ts b/src/compat/util/now.ts new file mode 100644 index 000000000..5d2439fb6 --- /dev/null +++ b/src/compat/util/now.ts @@ -0,0 +1,18 @@ +/** + * Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. + * + * @returns {number} The current time in milliseconds. + * + * @example + * const currentTime = now(); + * console.log(currentTime); // Outputs the current time in milliseconds + * + * @example + * const startTime = now(); + * // Some time-consuming operation + * const endTime = now(); + * console.log(`Operation took ${endTime - startTime} milliseconds`); + */ +export function now(): number { + return Date.now(); +}