-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parse-duration): add new package (#36)
- Loading branch information
Showing
20 changed files
with
2,567 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,19 +39,19 @@ | |
"devDependencies": { | ||
"@alwatr/eslint-config": "workspace:^", | ||
"@alwatr/prettier-config": "workspace:^", | ||
"@lerna-lite/changed": "^3.2.0", | ||
"@lerna-lite/cli": "^3.2.0", | ||
"@lerna-lite/diff": "^3.2.0", | ||
"@lerna-lite/exec": "^3.2.0", | ||
"@lerna-lite/publish": "^3.2.0", | ||
"@lerna-lite/run": "^3.2.0", | ||
"@lerna-lite/version": "^3.2.0", | ||
"@typescript-eslint/eslint-plugin": "^6.18.1", | ||
"@typescript-eslint/parser": "^6.18.1", | ||
"@lerna-lite/changed": "^3.2.1", | ||
"@lerna-lite/cli": "^3.2.1", | ||
"@lerna-lite/diff": "^3.2.1", | ||
"@lerna-lite/exec": "^3.2.1", | ||
"@lerna-lite/publish": "^3.2.1", | ||
"@lerna-lite/run": "^3.2.1", | ||
"@lerna-lite/version": "^3.2.1", | ||
"@typescript-eslint/eslint-plugin": "^6.19.0", | ||
"@typescript-eslint/parser": "^6.19.0", | ||
"eslint": "^8.56.0", | ||
"eslint-import-resolver-typescript": "^3.6.1", | ||
"eslint-plugin-import": "^2.29.1", | ||
"prettier": "^3.1.1", | ||
"prettier": "^3.2.2", | ||
"typescript": "^5.3.3" | ||
}, | ||
"packageManager": "[email protected]" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Parse-duration | ||
|
||
A simple utility to parse a duration string into milliseconds number. | ||
|
||
## Installation | ||
|
||
```bash | ||
yarn add @alwatr/parse-duration | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
import {parseDuration} from '@alwatr/parse-duration'; | ||
|
||
parseDuration('10s'); // 10,000 | ||
parseDuration('10m'); // 600,000 | ||
parseDuration('10h'); // 36,000,000 | ||
parseDuration('10d'); // 864,000,000 | ||
parseDuration('10w'); // 6,048,000,000 | ||
parseDuration('10M'); // 25,920,000,000 | ||
parseDuration('10y'); // 315,360,000,000 | ||
parseDuration('10d', 'h'); // 240 | ||
``` | ||
|
||
### Unit Table | ||
|
||
| Unit | Description | | ||
|------|--------| | ||
| `s` | Second | | ||
| `m` | Minute | | ||
| `h` | Hour | | ||
| `d` | Day | | ||
| `w` | Week | | ||
| `M` | Month | | ||
| `y` | Year | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{ | ||
"name": "@alwatr/parse-duration", | ||
"version": "1.0.0-alpha.0", | ||
"description": "A simple utility to parse a duration string into milliseconds number.", | ||
"author": "S. Ali Mihandoost <[email protected]>", | ||
"keywords": [ | ||
"parse", | ||
"duration", | ||
"time", | ||
"parse-duration", | ||
"cross-platform", | ||
"ECMAScript", | ||
"typescript", | ||
"javascript", | ||
"node", | ||
"nodejs", | ||
"browser", | ||
"esm", | ||
"module", | ||
"utility", | ||
"util", | ||
"utils", | ||
"nanolib", | ||
"alwatr" | ||
], | ||
"type": "module", | ||
"main": "./dist/main.cjs", | ||
"module": "./dist/main.mjs", | ||
"types": "./dist/main.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/main.mjs", | ||
"require": "./dist/main.cjs", | ||
"types": "./dist/main.d.ts" | ||
} | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"**/*.{js,mjs,cjs,map,d.ts,html,md}", | ||
"!demo/**/*" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Alwatr/nanolib", | ||
"directory": "packages/parse-duration" | ||
}, | ||
"homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/parse-duration#readme", | ||
"bugs": { | ||
"url": "https://github.com/Alwatr/nanolib/issues" | ||
}, | ||
"prettier": "@alwatr/prettier-config", | ||
"scripts": { | ||
"b": "yarn run build", | ||
"t": "yarn run test", | ||
"w": "yarn run watch", | ||
"c": "yarn run clean", | ||
"cb": "yarn run clean && yarn run build", | ||
"d": "yarn run build:es && yarn node --enable-source-maps --trace-warnings", | ||
"build": "yarn run build:ts & yarn run build:es", | ||
"build:es": "nano-build --preset=module", | ||
"build:ts": "tsc --build", | ||
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\" jest", | ||
"watch": "yarn run watch:ts & yarn run watch:es", | ||
"watch:es": "yarn run build:es --watch", | ||
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput", | ||
"clean": "rm -rfv dist *.tsbuildinfo" | ||
}, | ||
"dependencies": { | ||
"@alwatr/is-number": "workspace:^" | ||
}, | ||
"devDependencies": { | ||
"@alwatr/nano-build": "workspace:^", | ||
"@alwatr/prettier-config": "workspace:^", | ||
"@alwatr/tsconfig-base": "workspace:^", | ||
"@types/jest": "^29.5.11", | ||
"@types/node": "^20.11.3", | ||
"jest": "^29.7.0", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {parseDuration} from '@alwatr/parse-duration'; | ||
|
||
describe('@alwatr/parse-duration', () => { | ||
it('should parse duration in seconds', () => { | ||
expect(parseDuration('5s')).toBe(5 * 1000); | ||
}); | ||
|
||
it('should parse duration in minutes', () => { | ||
expect(parseDuration('3m')).toBe(3 * 60 * 1000); | ||
}); | ||
|
||
it('should parse duration in hours', () => { | ||
expect(parseDuration('2h')).toBe(2 * 60 * 60 * 1000); | ||
}); | ||
|
||
it('should convert duration to specified unit', () => { | ||
expect(parseDuration('2h', 'm')).toBe(2 * 60); | ||
}); | ||
|
||
it('should convert duration to different units', () => { | ||
expect(parseDuration('1d', 'h')).toBe(24); | ||
expect(parseDuration('1w', 'd')).toBe(7); | ||
expect(parseDuration('1M', 'm')).toBe(30 * 24 * 60); | ||
expect(parseDuration('1y', 'd')).toBe(365); | ||
}); | ||
|
||
it('should throw error for invalid duration', () => { | ||
expect(() => parseDuration('1x')).toThrow('invalid_unit'); | ||
}); | ||
|
||
it('should throw error for invalid conversion unit', () => { | ||
expect(() => parseDuration('1h', 'x')).toThrow('invalid_unit'); | ||
}); | ||
|
||
it('should throw error for non-numeric duration', () => { | ||
expect(() => parseDuration('xh')).toThrow('not_a_number'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {isNumber} from '@alwatr/is-number'; | ||
|
||
/** | ||
* Unit conversion table | ||
*/ | ||
const unitConversion_ = { | ||
s: 1_000, | ||
m: 60_000, | ||
h: 3_600_000, | ||
d: 86_400_000, | ||
w: 604_800_000, | ||
M: 2_592_000_000, | ||
y: 31_536_000_000, | ||
} as const; | ||
|
||
/** | ||
* Duration unit: `s` for seconds, `m` for minutes, `h` for hours, `d` for days, `w` for weeks, `M` for months, `y` for years. | ||
*/ | ||
export type DurationUnit = keyof typeof unitConversion_; | ||
|
||
/** | ||
* Duration string format: `number + unit`, for example `10m` means 10 minutes. | ||
*/ | ||
export type DurationString = `${number}${DurationUnit}`; | ||
|
||
/** | ||
* Parse duration string to milliseconds number. | ||
* | ||
* @param duration - duration string, for example `10m` means 10 minutes. | ||
* @param toUnit - convert to unit, default is `ms` for milliseconds. | ||
* @return duration in milliseconds. | ||
* @example | ||
* ```ts | ||
* parseDuration('10m'); // 600000 | ||
* parseDuration('10m', 's'); // 600 | ||
* ``` | ||
*/ | ||
export const parseDuration = (duration: DurationString, toUnit?: DurationUnit): number => { | ||
const durationNumberStr = duration.slice(0, duration.length - 1); | ||
if (!isNumber(durationNumberStr)) { | ||
throw new Error(`not_a_number`); | ||
} | ||
const durationNumber = +durationNumberStr; | ||
const durationUnit = duration.slice(-1) as DurationUnit; | ||
const factor = unitConversion_[durationUnit]; | ||
if (factor === undefined) { | ||
throw new Error(`invalid_unit`, {cause: {duration}}); | ||
} | ||
const ms = durationNumber * factor; | ||
if (toUnit === undefined) { | ||
return ms; | ||
} | ||
// else | ||
const toFactor = unitConversion_[toUnit]; | ||
if (toFactor === undefined) { | ||
throw new Error(`invalid_unit`, {cause: {toUnit}}); | ||
} | ||
return ms / toFactor; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "@alwatr/tsconfig-base/tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"emitDeclarationOnly": true, | ||
"composite": true, | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"references": [ | ||
{"path": "../is-number"} | ||
] | ||
} |
Oops, something went wrong.