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
|
node_modules
|
||||||
yarn.lock
|
dist
|
||||||
lib
|
|
||||||
|
|
||||||
.idea
|
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
*
|
|
||||||
!lib/index.js
|
|
||||||
!lib/index.d.ts
|
|
34
package.json
34
package.json
@ -1,24 +1,36 @@
|
|||||||
{
|
{
|
||||||
"name": "uhrwerk",
|
"name": "uhrwerk",
|
||||||
"version": "1.0.2",
|
"version": "1.1.0",
|
||||||
"description": "time utility",
|
"description": "time utility",
|
||||||
"main": "./lib/index.js",
|
"author": "Niccolo Borgioli",
|
||||||
"types": "./lib/index.d.ts",
|
"license": "MIT",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsup src/index.ts --format cjs,esm --dts --sourcemap",
|
||||||
"test": "yarn run build && mocha",
|
"test": "pnpm run build && mocha",
|
||||||
"prepublishOnly": "npm run test"
|
"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": [
|
"keywords": [
|
||||||
"time",
|
"time",
|
||||||
"interval",
|
"interval",
|
||||||
"humand-readable",
|
"humand-readable",
|
||||||
"utility"
|
"utility"
|
||||||
],
|
],
|
||||||
"author": "Niccolo Borgioli",
|
|
||||||
"license": "MIT",
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "6.x",
|
"mocha": "~10.0.0",
|
||||||
"typescript": "3.x"
|
"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
248
src/index.ts
248
src/index.ts
@ -1,25 +1,25 @@
|
|||||||
type Intervals =
|
type Intervals =
|
||||||
'millisecond'
|
| 'millisecond'
|
||||||
| 'milliseconds'
|
| 'milliseconds'
|
||||||
| 'ms'
|
| 'ms'
|
||||||
| 'second'
|
| 'second'
|
||||||
| 'seconds'
|
| 'seconds'
|
||||||
| 's'
|
| 's'
|
||||||
| 'minute'
|
| 'minute'
|
||||||
| 'minutes'
|
| 'minutes'
|
||||||
| 'm'
|
| 'm'
|
||||||
| 'hour'
|
| 'hour'
|
||||||
| 'hours'
|
| 'hours'
|
||||||
| 'h'
|
| 'h'
|
||||||
| 'day'
|
| 'day'
|
||||||
| 'days'
|
| 'days'
|
||||||
| 'd'
|
| 'd'
|
||||||
| 'week'
|
| 'week'
|
||||||
| 'weeks'
|
| 'weeks'
|
||||||
| 'w'
|
| 'w'
|
||||||
| 'year'
|
| 'year'
|
||||||
| 'years'
|
| 'years'
|
||||||
| 'y'
|
| 'y'
|
||||||
|
|
||||||
const Millisecond = 1
|
const Millisecond = 1
|
||||||
const Second = Millisecond * 1000
|
const Second = Millisecond * 1000
|
||||||
@ -34,132 +34,128 @@ export type HumanizerReturnFN = (duration: Duration) => string
|
|||||||
export type Humanizer = [HumanizerTestFN, HumanizerReturnFN][]
|
export type Humanizer = [HumanizerTestFN, HumanizerReturnFN][]
|
||||||
|
|
||||||
const defaultHumanizer: Humanizer = [
|
const defaultHumanizer: Humanizer = [
|
||||||
[d => d.years() > 0, d => `${d.years()} years`],
|
[(d) => d.years() > 0, (d) => `${d.years()} years`],
|
||||||
[d => d.weeks() > 1, d => `${d.weeks()} weeks`],
|
[(d) => d.weeks() > 1, (d) => `${d.weeks()} weeks`],
|
||||||
[d => d.days() > 0, d => `${d.days()} days`],
|
[(d) => d.days() > 0, (d) => `${d.days()} days`],
|
||||||
[d => d.hours() > 0, d => `${d.hours()} hours`],
|
[(d) => d.hours() > 0, (d) => `${d.hours()} hours`],
|
||||||
[d => d.minutes() > 5, d => `${d.minutes()} minutes`],
|
[(d) => d.minutes() > 5, (d) => `${d.minutes()} minutes`],
|
||||||
[d => d.minutes() > 0, _ => `a few minutes`],
|
[(d) => d.minutes() > 0, (_) => `a few minutes`],
|
||||||
[() => true, () => `a moment`],
|
[() => true, () => `a moment`],
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
export class Duration {
|
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) {
|
private static ProcessInterval(amount: number, interval: Intervals): number {
|
||||||
this.duration = Duration.ProcessInterval(amount, interval)
|
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 {
|
public add(amount: number, interval: Intervals): Duration {
|
||||||
switch (interval.toLowerCase()) {
|
this.duration += Duration.ProcessInterval(amount, interval)
|
||||||
case 'millisecond':
|
return this
|
||||||
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 {
|
public subtract(amount: number, interval: Intervals): Duration {
|
||||||
this.duration += Duration.ProcessInterval(amount, interval)
|
this.duration -= Duration.ProcessInterval(amount, interval)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public subtract(amount: number, interval: Intervals): Duration {
|
public asMilliseconds(): number {
|
||||||
this.duration -= Duration.ProcessInterval(amount, interval)
|
return this.duration
|
||||||
return this
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public asMilliseconds(): number {
|
public asSeconds(): number {
|
||||||
return this.duration
|
return this.duration / Second
|
||||||
}
|
}
|
||||||
|
|
||||||
public asSeconds(): number {
|
public asMinutes(): number {
|
||||||
return this.duration / Second
|
return this.duration / Minute
|
||||||
}
|
}
|
||||||
|
|
||||||
public asMinutes(): number {
|
public asHours(): number {
|
||||||
return this.duration / Minute
|
return this.duration / Hour
|
||||||
}
|
}
|
||||||
|
|
||||||
public asHours(): number {
|
public asDays(): number {
|
||||||
return this.duration / Hour
|
return this.duration / Day
|
||||||
}
|
}
|
||||||
|
|
||||||
public asDays(): number {
|
public asWeeks(): number {
|
||||||
return this.duration / Day
|
return this.duration / Week
|
||||||
}
|
}
|
||||||
|
|
||||||
public asWeeks(): number {
|
public asYears(): number {
|
||||||
return this.duration / Week
|
return this.duration / Year
|
||||||
}
|
}
|
||||||
|
|
||||||
public asYears(): number {
|
public milliseconds(): number {
|
||||||
return this.duration / Year
|
return ((((((this.duration % Year) % Day) % Hour) % Minute) % Second) / Millisecond) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public milliseconds(): number {
|
public seconds(): number {
|
||||||
return (this.duration % Year % Day % Hour % Minute % Second) / Millisecond | 0
|
return (((((this.duration % Year) % Day) % Hour) % Minute) / Second) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public seconds(): number {
|
public minutes(): number {
|
||||||
return (this.duration % Year % Day % Hour % Minute) / Second | 0
|
return ((((this.duration % Year) % Day) % Hour) / Minute) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public minutes(): number {
|
public hours(): number {
|
||||||
return (this.duration % Year % Day % Hour) / Minute | 0
|
return (((this.duration % Year) % Day) / Hour) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public hours(): number {
|
public days(): number {
|
||||||
return (this.duration % Year % Day) / Hour | 0
|
return ((this.duration % Year) / Day) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public days(): number {
|
public weeks(): number {
|
||||||
return (this.duration % Year) / Day | 0
|
return (this.duration / Week) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public weeks(): number {
|
public years(): number {
|
||||||
return (this.duration) / Week | 0
|
return (this.duration / Year) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public years(): number {
|
public humanize(humanizer?: Humanizer): string {
|
||||||
return (this.duration) / Year | 0
|
if (!humanizer) humanizer = defaultHumanizer
|
||||||
}
|
|
||||||
|
|
||||||
public humanize(humanizer?: Humanizer): string {
|
for (const [control, value] of humanizer) if (control(this)) return value(this)
|
||||||
if (!humanizer) humanizer = defaultHumanizer
|
|
||||||
|
|
||||||
for (const [control, value] of humanizer)
|
return ''
|
||||||
if (control(this))
|
}
|
||||||
return value(this)
|
}
|
||||||
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "esnext",
|
"target": "es2020",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"outDir": "./lib",
|
"outDir": "./lib",
|
||||||
@ -20,4 +20,4 @@
|
|||||||
"noFallthroughCasesInSwitch": true,
|
"noFallthroughCasesInSwitch": true,
|
||||||
"esModuleInterop": true
|
"esModuleInterop": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user