Skip to content

Commit

Permalink
Merge 2f89c52 into ac05e17
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Oct 8, 2023
2 parents ac05e17 + 2f89c52 commit f6ed472
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm install @kikobeats/time-span --save

```js
const { setTimeout } = require('timers/promises')
const timeSpan = require('@kikobeats/time-span')
const timeSpan = require('@kikobeats/time-span')()

const duration = timeSpan()
await setTimeout(5000)
Expand All @@ -26,7 +26,11 @@ console.log(duration()) // => 5001.870375
It also accepts a `format` function:

```js
const duration = timeSpan({ format: n => `${Math.round(n)}ms`})
const timeSpan = require('@kikobeats/time-span')({
format: n => `${Math.round(n)}ms`
})

const duration = timeSpan()
await setTimeout(5000)
console.log(duration()) // => 5000ms
```
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports =
({ start = process.hrtime.bigint(), format = n => n } = {}) =>
() =>
format(Number(process.hrtime.bigint() - start) / 1e6)
({ format = n => n } = {}) =>
(start = process.hrtime.bigint()) =>
() =>
format(Number(process.hrtime.bigint() - start) / 1e6)
12 changes: 8 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

const { setTimeout } = require('timers/promises')
const test = require('ava')

const timeSpan = require('..')
const createTimeSpan = require('..')

test('default options', async t => {
const timeSpan = createTimeSpan()
const duration = timeSpan()
await setTimeout(100)
const end = duration()
Expand All @@ -15,14 +15,18 @@ test('default options', async t => {
test('pass `start`', async t => {
const start = process.hrtime.bigint()
await setTimeout(100)
const duration = timeSpan({ start })
const timeSpan = createTimeSpan()
const duration = timeSpan(start)
await setTimeout(100)
const end = duration()
t.true(end > 200 && end < 200 + 200 * 0.02)
})

test('pass `format`', async t => {
const duration = timeSpan({ format: n => `${Math.round(n)}ms` })
const format = n => `${Math.round(n)}ms`
const timeSpan = createTimeSpan({ format })
const duration = timeSpan()
console.log(duration)
await setTimeout(100)
const end = duration()
t.true(end.endsWith('ms'))
Expand Down

0 comments on commit f6ed472

Please sign in to comment.