update deps and add esm support

This commit is contained in:
cupcakearmy 2022-07-16 17:29:22 +02:00
parent 72d460e41f
commit 270b27d905
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
6 changed files with 1321 additions and 146 deletions

5
.gitignore vendored
View File

@ -1,5 +1,2 @@
node_modules
yarn.lock
lib
.idea
dist

View File

@ -1,3 +0,0 @@
*
!lib/index.js
!lib/index.d.ts

View File

@ -1,24 +1,36 @@
{
"name": "uhrwerk",
"version": "1.0.2",
"version": "1.1.0",
"description": "time utility",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"author": "Niccolo Borgioli",
"license": "MIT",
"type": "module",
"scripts": {
"build": "tsc",
"test": "yarn run build && mocha",
"prepublishOnly": "npm run test"
"build": "tsup src/index.ts --format cjs,esm --dts --sourcemap",
"test": "pnpm run build && mocha",
"prepublishOnly": "pnpm run test && pnpm run build"
},
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"files": [
"dist"
],
"keywords": [
"time",
"interval",
"humand-readable",
"utility"
],
"author": "Niccolo Borgioli",
"license": "MIT",
"devDependencies": {
"mocha": "6.x",
"typescript": "3.x"
"mocha": "~10.0.0",
"tsup": "^6.1.3",
"typescript": "~4.7.4"
}
}

1173
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
type Intervals =
'millisecond'
| 'millisecond'
| 'milliseconds'
| 'ms'
| 'second'
@ -34,18 +34,16 @@ export type HumanizerReturnFN = (duration: Duration) => string
export type Humanizer = [HumanizerTestFN, HumanizerReturnFN][]
const defaultHumanizer: Humanizer = [
[d => d.years() > 0, d => `${d.years()} years`],
[d => d.weeks() > 1, d => `${d.weeks()} weeks`],
[d => d.days() > 0, d => `${d.days()} days`],
[d => d.hours() > 0, d => `${d.hours()} hours`],
[d => d.minutes() > 5, d => `${d.minutes()} minutes`],
[d => d.minutes() > 0, _ => `a few minutes`],
[(d) => d.years() > 0, (d) => `${d.years()} years`],
[(d) => d.weeks() > 1, (d) => `${d.weeks()} weeks`],
[(d) => d.days() > 0, (d) => `${d.days()} days`],
[(d) => d.hours() > 0, (d) => `${d.hours()} hours`],
[(d) => d.minutes() > 5, (d) => `${d.minutes()} minutes`],
[(d) => d.minutes() > 0, (_) => `a few minutes`],
[() => true, () => `a moment`],
]
export class Duration {
private duration: number
constructor(amount: number, interval: Intervals) {
@ -126,39 +124,37 @@ export class Duration {
}
public milliseconds(): number {
return (this.duration % Year % Day % Hour % Minute % Second) / Millisecond | 0
return ((((((this.duration % Year) % Day) % Hour) % Minute) % Second) / Millisecond) | 0
}
public seconds(): number {
return (this.duration % Year % Day % Hour % Minute) / Second | 0
return (((((this.duration % Year) % Day) % Hour) % Minute) / Second) | 0
}
public minutes(): number {
return (this.duration % Year % Day % Hour) / Minute | 0
return ((((this.duration % Year) % Day) % Hour) / Minute) | 0
}
public hours(): number {
return (this.duration % Year % Day) / Hour | 0
return (((this.duration % Year) % Day) / Hour) | 0
}
public days(): number {
return (this.duration % Year) / Day | 0
return ((this.duration % Year) / Day) | 0
}
public weeks(): number {
return (this.duration) / Week | 0
return (this.duration / Week) | 0
}
public years(): number {
return (this.duration) / Year | 0
return (this.duration / Year) | 0
}
public humanize(humanizer?: Humanizer): string {
if (!humanizer) humanizer = defaultHumanizer
for (const [control, value] of humanizer)
if (control(this))
return value(this)
for (const [control, value] of humanizer) if (control(this)) return value(this)
return ''
}

View File

@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "esnext",
"target": "es2020",
"module": "commonjs",
"declaration": true,
"outDir": "./lib",