Skip to content

Commit

Permalink
feat(now): Add now
Browse files Browse the repository at this point in the history
  • Loading branch information
raon0211 committed Dec 1, 2024
1 parent 537e8a4 commit 16290ab
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/ja/reference/compat/util/now.md
Original file line number Diff line number Diff line change
@@ -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`);
```
31 changes: 31 additions & 0 deletions docs/ko/reference/compat/util/now.md
Original file line number Diff line number Diff line change
@@ -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`);
```
31 changes: 31 additions & 0 deletions docs/reference/compat/util/now.md
Original file line number Diff line number Diff line change
@@ -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`);
```
31 changes: 31 additions & 0 deletions docs/zh_hans/reference/compat/util/now.md
Original file line number Diff line number Diff line change
@@ -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`);
```
1 change: 1 addition & 0 deletions src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
16 changes: 16 additions & 0 deletions src/compat/util/now.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
18 changes: 18 additions & 0 deletions src/compat/util/now.ts
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 16290ab

Please sign in to comment.