mirror of
https://github.com/cupcakearmy/uhrwerk.git
synced 2024-12-21 15:06:25 +00:00
update deps and add esm support
This commit is contained in:
parent
72d460e41f
commit
270b27d905
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,5 +1,2 @@
|
||||
node_modules
|
||||
yarn.lock
|
||||
lib
|
||||
|
||||
.idea
|
||||
dist
|
||||
|
@ -1,3 +0,0 @@
|
||||
*
|
||||
!lib/index.js
|
||||
!lib/index.d.ts
|
32
package.json
32
package.json
@ -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
1173
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
34
src/index.ts
34
src/index.ts
@ -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 ''
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"target": "es2020",
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"outDir": "./lib",
|
||||
|
Loading…
Reference in New Issue
Block a user