Skip to content

Commit

Permalink
Migrate to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Dec 11, 2022
1 parent 29be7e8 commit 7570fa0
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Node](https://img.shields.io/badge/-Node.js-808080?logo=node.js&colorA=404040&logoColor=66cc33)](https://www.npmjs.com/package/precise-now)
[![Browsers](https://img.shields.io/badge/-Browsers-808080?logo=firefox&colorA=404040)](https://unpkg.com/precise-now?module)
[![TypeScript](https://img.shields.io/badge/-Typed-808080?logo=typescript&colorA=404040&logoColor=0096ff)](/src/main.d.ts)
[![TypeScript](https://img.shields.io/badge/-Typed-808080?logo=typescript&colorA=404040&logoColor=0096ff)](/src/main.ts)
[![Codecov](https://img.shields.io/badge/-Tested%20100%25-808080?logo=codecov&colorA=404040)](https://codecov.io/gh/ehmicky/precise-now)
[![Minified size](https://img.shields.io/bundlephobia/minzip/precise-now?label&colorA=404040&colorB=808080&logo=webpack)](https://bundlephobia.com/package/precise-now)
[![Mastodon](https://img.shields.io/badge/-Mastodon-808080.svg?logo=mastodon&colorA=404040&logoColor=9590F9)](https://fosstodon.org/@ehmicky)
Expand Down
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"devDependencies": {
"@ehmicky/dev-tasks": "^2.0.57",
"@types/node": "^18.11.16",
"spyd": "^0.6.1"
},
"engines": {
Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions src/helpers/remove.test.js → src/helpers/remove.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// TODO: remove after dropping support for Node <16.0.0
// eslint-disable-next-line no-shadow
import performance from 'node:perf_hooks'
// eslint-disable-next-line @typescript-eslint/no-shadow
import { performance } from 'node:perf_hooks'

// Mimics browsers by removing Node.js specific API
export const removeHrtime = () => {
// eslint-disable-next-line fp/no-mutation, n/prefer-global/process
globalThis.process.hrtime.bigint = undefined
globalThis.process.hrtime.bigint = undefined as never
}

// Mimics platforms without `performance.now()` by removing it
export const removePerformanceNow = () => {
// eslint-disable-next-line fp/no-mutation
performance.now = undefined
performance.now = undefined as never

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (globalThis?.performance?.now !== undefined) {
// eslint-disable-next-line fp/no-mutating-methods
Object.defineProperty(globalThis.performance, 'now', {
Expand Down
1 change: 0 additions & 1 deletion src/helpers/tests.test.js → src/helpers/tests.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line ava/no-ignored-test-files
import { promisify } from 'node:util'

import test from 'ava'
Expand Down
File renamed without changes.
11 changes: 0 additions & 11 deletions src/main.d.ts

This file was deleted.

23 changes: 16 additions & 7 deletions src/main.js → src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Returns the number of nanoseconds since the library was loaded.
/* eslint-disable @typescript-eslint/no-unnecessary-condition, n/prefer-global/process */
// Meant to calculate time differences:
// - precisely: use nanoseconds instead of milliseconds
// - accurately: does not use huge integers since those are not exact anymore
Expand Down Expand Up @@ -27,9 +27,7 @@
// - duration since machine was started
// - nanoseconds
const nowFunc = () => {
// eslint-disable-next-line n/prefer-global/process
if (globalThis?.process?.hrtime?.bigint !== undefined) {
// eslint-disable-next-line n/prefer-global/process
return hrtime.bind(undefined, globalThis.process.hrtime.bigint())
}

Expand All @@ -40,14 +38,25 @@ const nowFunc = () => {
return dateNow.bind(undefined, Date.now())
}

// eslint-disable-next-line n/prefer-global/process
const hrtime = (start) => Number(globalThis.process.hrtime.bigint() - start)
const hrtime = (start: bigint) =>
Number(globalThis.process.hrtime.bigint() - start)

const performanceNow = (start) =>
const performanceNow = (start: number) =>
Math.round((globalThis.performance.now() - start) * NANOSECS_TO_MILLISECS)

const dateNow = (start) => (Date.now() - start) * NANOSECS_TO_MILLISECS
const dateNow = (start: number) => (Date.now() - start) * NANOSECS_TO_MILLISECS

const NANOSECS_TO_MILLISECS = 1e6

/**
* Return the number of nanoseconds since the time origin.
*
* @example
* ```js
* const start = preciseNow()
* const end = preciseNow()
* const duration = end - start
* ```
*/
export default nowFunc()
/* eslint-enable @typescript-eslint/no-unnecessary-condition, n/prefer-global/process */
File renamed without changes.
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "@ehmicky/dev-tasks"
"extends": "@ehmicky/dev-tasks",
"compilerOptions": {
"lib": ["esnext", "dom"]
}
}

0 comments on commit 7570fa0

Please sign in to comment.