This commit is contained in:
cupcakearmy 2022-07-16 17:34:20 +02:00
parent 90fd28ef95
commit ec25164cba
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
3 changed files with 74 additions and 79 deletions

View File

@ -8,7 +8,7 @@
"scripts": { "scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts --sourcemap", "build": "tsup src/index.ts --format cjs,esm --dts --sourcemap",
"test": "pnpm run build && mocha", "test": "pnpm run build && mocha",
"prepublishOnly": "pnpm run test && pnpm run build" "prepublishOnly": "pnpm run test"
}, },
"main": "./dist/index.cjs", "main": "./dist/index.cjs",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",

View File

@ -1,78 +0,0 @@
const { describe, it } = require('mocha')
const a = require('assert')
const { Duration } = require('../')
describe('Duration', () => {
describe('Basics', () => {
it('as unit', () => {
const d = new Duration(.5, 'year')
a.strictEqual(d.asMilliseconds(), 1000 * 60 * 60 * 24 * 365.25 / 2)
a.strictEqual(d.asSeconds(), 60 * 60 * 24 * 365.25 / 2)
a.strictEqual(d.asMinutes(), 60 * 24 * 365.25 / 2)
a.strictEqual(d.asHours(), 24 * 365.25 / 2)
a.strictEqual(d.asDays(), 365.25 / 2)
a.strictEqual(d.asWeeks(), 365.25 / 7 / 2)
a.strictEqual(d.asYears(), .5)
})
it('add / subtract', () => {
const d = new Duration(1, 'day')
d.add(4, 'hours')
a.strictEqual(d.asHours(), 28)
d.subtract(.5, 'day')
a.strictEqual(d.asHours(), 16)
})
it('exact units', () => {
const d = new Duration(1, 'day')
d.add(42, 'milliseconds')
a.strictEqual(d.milliseconds(), 42)
d.add(5, 'seconds')
a.strictEqual(d.seconds(), 5)
d.add(27, 'minutes')
a.strictEqual(d.minutes(), 27)
d.add(8, 'hours')
a.strictEqual(d.hours(), 8)
d.add(3, 'days')
a.strictEqual(d.days(), 4)
d.add(17, 'weeks')
a.strictEqual(d.weeks(), 17)
d.add(2, 'years')
a.strictEqual(d.years(), 2)
})
})
describe('Humanize', () => {
it('few minutes', () => {
a.strictEqual(new Duration(2, 'minutes').humanize(), 'a few minutes')
a.strictEqual(new Duration(16, 'minutes').humanize(), '16 minutes')
a.strictEqual(new Duration(30, 'seconds').humanize(), 'a moment')
})
it('custom humanizer', () => {
const humanizer = [
[d => d.days() > 0, d => `yus for ${d.days()}`],
[() => true, () => 'cool'],
]
const d = new Duration(5, 'minutes')
a.strictEqual(d.humanize(humanizer), 'cool')
d.add(2, 'days')
a.strictEqual(d.humanize(humanizer), `yus for 2`)
})
})
})

73
test/main.mjs Normal file
View File

@ -0,0 +1,73 @@
import { strictEqual } from 'assert'
import { describe, it } from 'mocha'
import { Duration } from '../dist/index.js'
describe('Duration', () => {
describe('Basics', () => {
it('as unit', () => {
const d = new Duration(0.5, 'year')
strictEqual(d.asMilliseconds(), (1000 * 60 * 60 * 24 * 365.25) / 2)
strictEqual(d.asSeconds(), (60 * 60 * 24 * 365.25) / 2)
strictEqual(d.asMinutes(), (60 * 24 * 365.25) / 2)
strictEqual(d.asHours(), (24 * 365.25) / 2)
strictEqual(d.asDays(), 365.25 / 2)
strictEqual(d.asWeeks(), 365.25 / 7 / 2)
strictEqual(d.asYears(), 0.5)
})
it('add / subtract', () => {
const d = new Duration(1, 'day')
d.add(4, 'hours')
strictEqual(d.asHours(), 28)
d.subtract(0.5, 'day')
strictEqual(d.asHours(), 16)
})
it('exact units', () => {
const d = new Duration(1, 'day')
d.add(42, 'milliseconds')
strictEqual(d.milliseconds(), 42)
d.add(5, 'seconds')
strictEqual(d.seconds(), 5)
d.add(27, 'minutes')
strictEqual(d.minutes(), 27)
d.add(8, 'hours')
strictEqual(d.hours(), 8)
d.add(3, 'days')
strictEqual(d.days(), 4)
d.add(17, 'weeks')
strictEqual(d.weeks(), 17)
d.add(2, 'years')
strictEqual(d.years(), 2)
})
})
describe('Humanize', () => {
it('few minutes', () => {
strictEqual(new Duration(2, 'minutes').humanize(), 'a few minutes')
strictEqual(new Duration(16, 'minutes').humanize(), '16 minutes')
strictEqual(new Duration(30, 'seconds').humanize(), 'a moment')
})
it('custom humanizer', () => {
const humanizer = [
[(d) => d.days() > 0, (d) => `yus for ${d.days()}`],
[() => true, () => 'cool'],
]
const d = new Duration(5, 'minutes')
strictEqual(d.humanize(humanizer), 'cool')
d.add(2, 'days')
strictEqual(d.humanize(humanizer), `yus for 2`)
})
})
})