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 Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,25 @@
type Intervals =
'millisecond'
| 'milliseconds'
| 'ms'
| 'second'
| 'seconds'
| 's'
| 'minute'
| 'minutes'
| 'm'
| 'hour'
| 'hours'
| 'h'
| 'day'
| 'days'
| 'd'
| 'week'
| 'weeks'
| 'w'
| 'year'
| 'years'
| 'y'
| 'millisecond'
| 'milliseconds'
| 'ms'
| 'second'
| 'seconds'
| 's'
| 'minute'
| 'minutes'
| 'm'
| 'hour'
| 'hours'
| 'h'
| 'day'
| 'days'
| 'd'
| 'week'
| 'weeks'
| 'w'
| 'year'
| 'years'
| 'y'
const Millisecond = 1
const Second = Millisecond * 1000
@ -34,132 +34,128 @@ 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`],
[() => true, () => `a moment`],
[(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
private duration: number
constructor(amount: number, interval: Intervals) {
this.duration = Duration.ProcessInterval(amount, interval)
}
constructor(amount: number, interval: Intervals) {
this.duration = Duration.ProcessInterval(amount, interval)
}
private static ProcessInterval(amount: number, interval: Intervals): number {
switch (interval.toLowerCase()) {
case 'millisecond':
case 'milliseconds':
case 'ms':
return amount * Millisecond
case 'second':
case 'seconds':
case 's':
return amount * Second
case 'minute':
case 'minutes':
case 'm':
return amount * Minute
case 'hour':
case 'hours':
case 'h':
return amount * Hour
case 'day':
case 'days':
case 'd':
return amount * Day
case 'week':
case 'weeks':
case 'w':
return amount * Week
case 'year':
case 'years':
case 'y':
return amount * Year
default:
throw new Error('Wrong interval')
}
}
private static ProcessInterval(amount: number, interval: Intervals): number {
switch (interval.toLowerCase()) {
case 'millisecond':
case 'milliseconds':
case 'ms':
return amount * Millisecond
case 'second':
case 'seconds':
case 's':
return amount * Second
case 'minute':
case 'minutes':
case 'm':
return amount * Minute
case 'hour':
case 'hours':
case 'h':
return amount * Hour
case 'day':
case 'days':
case 'd':
return amount * Day
case 'week':
case 'weeks':
case 'w':
return amount * Week
case 'year':
case 'years':
case 'y':
return amount * Year
default:
throw new Error('Wrong interval')
}
}
public add(amount: number, interval: Intervals): Duration {
this.duration += Duration.ProcessInterval(amount, interval)
return this
}
public add(amount: number, interval: Intervals): Duration {
this.duration += Duration.ProcessInterval(amount, interval)
return this
}
public subtract(amount: number, interval: Intervals): Duration {
this.duration -= Duration.ProcessInterval(amount, interval)
return this
}
public subtract(amount: number, interval: Intervals): Duration {
this.duration -= Duration.ProcessInterval(amount, interval)
return this
}
public asMilliseconds(): number {
return this.duration
}
public asMilliseconds(): number {
return this.duration
}
public asSeconds(): number {
return this.duration / Second
}
public asSeconds(): number {
return this.duration / Second
}
public asMinutes(): number {
return this.duration / Minute
}
public asMinutes(): number {
return this.duration / Minute
}
public asHours(): number {
return this.duration / Hour
}
public asHours(): number {
return this.duration / Hour
}
public asDays(): number {
return this.duration / Day
}
public asDays(): number {
return this.duration / Day
}
public asWeeks(): number {
return this.duration / Week
}
public asWeeks(): number {
return this.duration / Week
}
public asYears(): number {
return this.duration / Year
}
public asYears(): number {
return this.duration / Year
}
public milliseconds(): number {
return ((((((this.duration % Year) % Day) % Hour) % Minute) % Second) / Millisecond) | 0
}
public milliseconds(): number {
return (this.duration % Year % Day % Hour % Minute % Second) / Millisecond | 0
}
public seconds(): number {
return (((((this.duration % Year) % Day) % Hour) % Minute) / Second) | 0
}
public seconds(): number {
return (this.duration % Year % Day % Hour % Minute) / Second | 0
}
public minutes(): number {
return ((((this.duration % Year) % Day) % Hour) / Minute) | 0
}
public minutes(): number {
return (this.duration % Year % Day % Hour) / Minute | 0
}
public hours(): number {
return (((this.duration % Year) % Day) / Hour) | 0
}
public hours(): number {
return (this.duration % Year % Day) / Hour | 0
}
public days(): number {
return ((this.duration % Year) / Day) | 0
}
public days(): number {
return (this.duration % Year) / Day | 0
}
public weeks(): number {
return (this.duration / Week) | 0
}
public weeks(): number {
return (this.duration) / Week | 0
}
public years(): number {
return (this.duration / Year) | 0
}
public years(): number {
return (this.duration) / Year | 0
}
public humanize(humanizer?: Humanizer): string {
if (!humanizer) humanizer = defaultHumanizer
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 ''
}
}
return ''
}
}

View File

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