mirror of
https://github.com/cupcakearmy/uhrwerk.git
synced 2024-12-21 23:16:24 +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
|
|
32
package.json
32
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
34
src/index.ts
34
src/index.ts
@ -1,5 +1,5 @@
|
|||||||
type Intervals =
|
type Intervals =
|
||||||
'millisecond'
|
| 'millisecond'
|
||||||
| 'milliseconds'
|
| 'milliseconds'
|
||||||
| 'ms'
|
| 'ms'
|
||||||
| 'second'
|
| 'second'
|
||||||
@ -34,18 +34,16 @@ 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) {
|
constructor(amount: number, interval: Intervals) {
|
||||||
@ -126,39 +124,37 @@ export class Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public milliseconds(): number {
|
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 {
|
public seconds(): number {
|
||||||
return (this.duration % Year % Day % Hour % Minute) / Second | 0
|
return (((((this.duration % Year) % Day) % Hour) % Minute) / Second) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public minutes(): number {
|
public minutes(): number {
|
||||||
return (this.duration % Year % Day % Hour) / Minute | 0
|
return ((((this.duration % Year) % Day) % Hour) / Minute) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public hours(): number {
|
public hours(): number {
|
||||||
return (this.duration % Year % Day) / Hour | 0
|
return (((this.duration % Year) % Day) / Hour) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public days(): number {
|
public days(): number {
|
||||||
return (this.duration % Year) / Day | 0
|
return ((this.duration % Year) / Day) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public weeks(): number {
|
public weeks(): number {
|
||||||
return (this.duration) / Week | 0
|
return (this.duration / Week) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public years(): number {
|
public years(): number {
|
||||||
return (this.duration) / Year | 0
|
return (this.duration / Year) | 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public humanize(humanizer?: Humanizer): string {
|
public humanize(humanizer?: Humanizer): string {
|
||||||
if (!humanizer) humanizer = defaultHumanizer
|
if (!humanizer) humanizer = defaultHumanizer
|
||||||
|
|
||||||
for (const [control, value] of humanizer)
|
for (const [control, value] of humanizer) if (control(this)) return value(this)
|
||||||
if (control(this))
|
|
||||||
return value(this)
|
|
||||||
|
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "esnext",
|
"target": "es2020",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"outDir": "./lib",
|
"outDir": "./lib",
|
||||||
|
Loading…
Reference in New Issue
Block a user