@alwatr/parse-duration 5.5.31 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js ADDED
@@ -0,0 +1,5 @@
1
+ /* 📦 @alwatr/parse-duration v6.0.0 */
2
+ import{toNumber as A}from"@alwatr/is-number";var z=Object.freeze({s:1000,m:60000,h:3600000,d:86400000,w:604800000,M:2592000000,y:31536000000}),D=(h,k)=>{let j;if(typeof h==="number")j=h;else{if(h.length<2)throw Error("invalid_format",{cause:{duration:h}});let q=h.slice(-1),x=z[q];if(x===void 0)throw Error("invalid_unit",{cause:{durationUnit:q}});let y=A(h.slice(0,-1));if(y===null)throw Error("not_a_number",{cause:{duration:h}});j=y*x}if(k===void 0)return j;let p=z[k];if(p===void 0)throw Error("invalid_unit",{cause:{toUnit:k}});return j/p};export{D as parseDuration};
3
+
4
+ //# debugId=1C5D6051084B13EC64756E2164756E21
5
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/main.ts"],
4
+ "sourcesContent": [
5
+ "import {toNumber} from '@alwatr/is-number';\n\n/**\n * Unit conversion table (milliseconds)\n */\nconst unitConversion = Object.freeze({\n s: 1_000,\n m: 60_000,\n h: 3_600_000,\n d: 86_400_000,\n w: 604_800_000,\n M: 2_592_000_000,\n y: 31_536_000_000,\n} as const);\n\n/**\n * Duration unit: `s` for seconds, `m` for minutes, `h` for hours, `d` for days, `w` for weeks, `M` for months, `y` for years.\n */\nexport type DurationUnit = keyof typeof unitConversion;\n\n/**\n * Duration string format: `number + unit`, for example `10m` means 10 minutes.\n */\nexport type Duration = `${number}${DurationUnit}` | number;\n\n/**\n * Error types that can be thrown by parseDuration\n */\nexport type DurationError = 'not_a_number' | 'invalid_unit' | 'invalid_format';\n\n/**\n * Parse duration string to milliseconds number.\n *\n * @param duration - Duration string or number, for example `10m` means 10 minutes.\n * @param toUnit - Convert to unit, default is `ms` for milliseconds.\n * @throws {Error} With message 'not_a_number' if duration string doesn't contain a valid number.\n * @throws {Error} With message 'invalid_unit' if the unit is not recognized.\n * @throws {Error} With message 'invalid_format' if the duration format is invalid.\n * @returns Duration in specified unit (or milliseconds by default).\n *\n * @example\n * ```ts\n * parseDuration('10m'); // 600000\n * parseDuration('10m', 's'); // 600\n * parseDuration(120_000, 'm'); // 2\n * ```\n */\nexport const parseDuration = (duration: Duration, toUnit?: DurationUnit): number => {\n let ms: number;\n\n // Convert input to milliseconds\n if (typeof duration === 'number') {\n ms = duration;\n }\n else {\n if (duration.length < 2) {\n throw new Error('invalid_format', {cause: {duration}});\n }\n\n const durationUnit = duration.slice(-1) as DurationUnit;\n const unitConversionFactor = unitConversion[durationUnit];\n\n if (unitConversionFactor === undefined) {\n throw new Error('invalid_unit', {cause: {durationUnit}});\n }\n\n const durationNumber = toNumber(duration.slice(0, -1));\n if (durationNumber === null) {\n throw new Error('not_a_number', {cause: {duration}});\n }\n\n ms = durationNumber * unitConversionFactor;\n }\n\n // Return as is if no conversion needed\n if (toUnit === undefined) {\n return ms;\n }\n\n // Convert to target unit\n const toFactor = unitConversion[toUnit];\n if (toFactor === undefined) {\n throw new Error('invalid_unit', {cause: {toUnit}});\n }\n\n return ms / toFactor;\n};\n"
6
+ ],
7
+ "mappings": ";AAAA,mBAAQ,0BAKR,IAAM,EAAiB,OAAO,OAAO,CACnC,EAAG,KACH,EAAG,MACH,EAAG,QACH,EAAG,SACH,EAAG,UACH,EAAG,WACH,EAAG,WACL,CAAU,EAkCG,EAAgB,CAAC,EAAoB,IAAkC,CAClF,IAAI,EAGJ,GAAI,OAAO,IAAa,SACtB,EAAK,EAEF,KACH,GAAI,EAAS,OAAS,EACpB,MAAU,MAAM,iBAAkB,CAAC,MAAO,CAAC,UAAQ,CAAC,CAAC,EAGvD,IAAM,EAAe,EAAS,MAAM,EAAE,EAChC,EAAuB,EAAe,GAE5C,GAAI,IAAyB,OAC3B,MAAU,MAAM,eAAgB,CAAC,MAAO,CAAC,cAAY,CAAC,CAAC,EAGzD,IAAM,EAAiB,EAAS,EAAS,MAAM,EAAG,EAAE,CAAC,EACrD,GAAI,IAAmB,KACrB,MAAU,MAAM,eAAgB,CAAC,MAAO,CAAC,UAAQ,CAAC,CAAC,EAGrD,EAAK,EAAiB,EAIxB,GAAI,IAAW,OACb,OAAO,EAIT,IAAM,EAAW,EAAe,GAChC,GAAI,IAAa,OACf,MAAU,MAAM,eAAgB,CAAC,MAAO,CAAC,QAAM,CAAC,CAAC,EAGnD,OAAO,EAAK",
8
+ "debugId": "1C5D6051084B13EC64756E2164756E21",
9
+ "names": []
10
+ }
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
1
  {
2
2
  "name": "@alwatr/parse-duration",
3
3
  "description": "A simple utility to parse a duration string into milliseconds number.",
4
- "version": "5.5.31",
4
+ "version": "6.0.0",
5
5
  "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
6
6
  "bugs": "https://github.com/Alwatr/nanolib/issues",
7
7
  "dependencies": {
8
- "@alwatr/is-number": "5.7.27"
8
+ "@alwatr/is-number": "6.0.0"
9
9
  },
10
10
  "devDependencies": {
11
- "@alwatr/nano-build": "6.4.2",
12
- "@alwatr/prettier-config": "6.0.2",
13
- "@alwatr/tsconfig-base": "6.0.4",
11
+ "@alwatr/nano-build": "7.0.0",
12
+ "@alwatr/prettier-config": "7.0.0",
13
+ "@alwatr/tsconfig-base": "7.0.0",
14
14
  "typescript": "^5.9.3"
15
15
  },
16
16
  "exports": {
17
17
  ".": {
18
18
  "types": "./dist/main.d.ts",
19
- "import": "./dist/main.mjs",
20
- "require": "./dist/main.cjs"
19
+ "default": "./dist/main.js"
21
20
  }
22
21
  },
23
22
  "files": [
24
- "**/*.{js,mjs,cjs,map,d.ts,html,md,LEGAL.txt}",
23
+ "**/*.{js,mjs,cjs,ts,map,d.ts,html,LEGAL.txt}",
24
+ "README.md",
25
25
  "LICENSE",
26
26
  "!demo/**/*",
27
27
  "!**/*.test.js"
@@ -48,8 +48,6 @@
48
48
  "utils"
49
49
  ],
50
50
  "license": "MPL-2.0",
51
- "main": "./dist/main.cjs",
52
- "module": "./dist/main.mjs",
53
51
  "prettier": "@alwatr/prettier-config",
54
52
  "publishConfig": {
55
53
  "access": "public"
@@ -62,12 +60,12 @@
62
60
  "scripts": {
63
61
  "b": "bun run build",
64
62
  "build": "bun run build:ts && bun run build:es",
65
- "build:es": "nano-build --preset=module",
63
+ "build:es": "nano-build --preset=module src/main.ts",
66
64
  "build:ts": "tsc --build",
67
65
  "c": "bun run clean",
68
66
  "cb": "bun run clean && bun run build",
69
67
  "clean": "rm -rfv dist *.tsbuildinfo",
70
- "d": "bun run build:es && bun --enable-source-maps --trace-warnings",
68
+ "d": "bun run build:es && bun",
71
69
  "t": "bun run test",
72
70
  "test": "bun test",
73
71
  "w": "bun run watch",
@@ -78,5 +76,5 @@
78
76
  "sideEffects": false,
79
77
  "type": "module",
80
78
  "types": "./dist/main.d.ts",
81
- "gitHead": "def1decf8f496f4b0d3d3ab952c346c689c30160"
79
+ "gitHead": "056102a1c8a563bbae8a290b6830450f467322a3"
82
80
  }
package/src/main.ts ADDED
@@ -0,0 +1,87 @@
1
+ import {toNumber} from '@alwatr/is-number';
2
+
3
+ /**
4
+ * Unit conversion table (milliseconds)
5
+ */
6
+ const unitConversion = Object.freeze({
7
+ s: 1_000,
8
+ m: 60_000,
9
+ h: 3_600_000,
10
+ d: 86_400_000,
11
+ w: 604_800_000,
12
+ M: 2_592_000_000,
13
+ y: 31_536_000_000,
14
+ } as const);
15
+
16
+ /**
17
+ * Duration unit: `s` for seconds, `m` for minutes, `h` for hours, `d` for days, `w` for weeks, `M` for months, `y` for years.
18
+ */
19
+ export type DurationUnit = keyof typeof unitConversion;
20
+
21
+ /**
22
+ * Duration string format: `number + unit`, for example `10m` means 10 minutes.
23
+ */
24
+ export type Duration = `${number}${DurationUnit}` | number;
25
+
26
+ /**
27
+ * Error types that can be thrown by parseDuration
28
+ */
29
+ export type DurationError = 'not_a_number' | 'invalid_unit' | 'invalid_format';
30
+
31
+ /**
32
+ * Parse duration string to milliseconds number.
33
+ *
34
+ * @param duration - Duration string or number, for example `10m` means 10 minutes.
35
+ * @param toUnit - Convert to unit, default is `ms` for milliseconds.
36
+ * @throws {Error} With message 'not_a_number' if duration string doesn't contain a valid number.
37
+ * @throws {Error} With message 'invalid_unit' if the unit is not recognized.
38
+ * @throws {Error} With message 'invalid_format' if the duration format is invalid.
39
+ * @returns Duration in specified unit (or milliseconds by default).
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * parseDuration('10m'); // 600000
44
+ * parseDuration('10m', 's'); // 600
45
+ * parseDuration(120_000, 'm'); // 2
46
+ * ```
47
+ */
48
+ export const parseDuration = (duration: Duration, toUnit?: DurationUnit): number => {
49
+ let ms: number;
50
+
51
+ // Convert input to milliseconds
52
+ if (typeof duration === 'number') {
53
+ ms = duration;
54
+ }
55
+ else {
56
+ if (duration.length < 2) {
57
+ throw new Error('invalid_format', {cause: {duration}});
58
+ }
59
+
60
+ const durationUnit = duration.slice(-1) as DurationUnit;
61
+ const unitConversionFactor = unitConversion[durationUnit];
62
+
63
+ if (unitConversionFactor === undefined) {
64
+ throw new Error('invalid_unit', {cause: {durationUnit}});
65
+ }
66
+
67
+ const durationNumber = toNumber(duration.slice(0, -1));
68
+ if (durationNumber === null) {
69
+ throw new Error('not_a_number', {cause: {duration}});
70
+ }
71
+
72
+ ms = durationNumber * unitConversionFactor;
73
+ }
74
+
75
+ // Return as is if no conversion needed
76
+ if (toUnit === undefined) {
77
+ return ms;
78
+ }
79
+
80
+ // Convert to target unit
81
+ const toFactor = unitConversion[toUnit];
82
+ if (toFactor === undefined) {
83
+ throw new Error('invalid_unit', {cause: {toUnit}});
84
+ }
85
+
86
+ return ms / toFactor;
87
+ };
package/CHANGELOG.md DELETED
@@ -1,393 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- ## [5.5.31](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.30...@alwatr/parse-duration@5.5.31) (2026-03-18)
7
-
8
- **Note:** Version bump only for package @alwatr/parse-duration
9
-
10
- ## [5.5.30](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.29...@alwatr/parse-duration@5.5.30) (2026-03-16)
11
-
12
- ### 🔨 Code Refactoring
13
-
14
- * migrate build scripts from yarn to bun across multiple packages ([d90e962](https://github.com/Alwatr/nanolib/commit/d90e962f15e5c951e191d5f02341279b6472abc3))
15
-
16
- ## [5.5.29](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.28...@alwatr/parse-duration@5.5.29) (2026-02-18)
17
-
18
- **Note:** Version bump only for package @alwatr/parse-duration
19
-
20
- ## [5.5.28](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.27...@alwatr/parse-duration@5.5.28) (2025-12-23)
21
-
22
- **Note:** Version bump only for package @alwatr/parse-duration
23
-
24
- ## [5.5.27](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.26...@alwatr/parse-duration@5.5.27) (2025-12-13)
25
-
26
- **Note:** Version bump only for package @alwatr/parse-duration
27
-
28
- ## [5.5.26](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.25...@alwatr/parse-duration@5.5.26) (2025-12-10)
29
-
30
- **Note:** Version bump only for package @alwatr/parse-duration
31
-
32
- ## [5.5.25](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.24...@alwatr/parse-duration@5.5.25) (2025-11-18)
33
-
34
- ### 🔨 Code Refactoring
35
-
36
- * remove unnecessary type declarations from tsconfig.json files ([89bcc7d](https://github.com/Alwatr/nanolib/commit/89bcc7db839807110b80f8ba34414ea9734d9c75))
37
-
38
- ## [5.5.24](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.23...@alwatr/parse-duration@5.5.24) (2025-11-15)
39
-
40
- **Note:** Version bump only for package @alwatr/parse-duration
41
-
42
- ## [5.5.23](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.22...@alwatr/parse-duration@5.5.23) (2025-11-15)
43
-
44
- **Note:** Version bump only for package @alwatr/parse-duration
45
-
46
- ## [5.5.22](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.21...@alwatr/parse-duration@5.5.22) (2025-11-04)
47
-
48
- **Note:** Version bump only for package @alwatr/parse-duration
49
-
50
- ## [5.5.21](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.20...@alwatr/parse-duration@5.5.21) (2025-10-06)
51
-
52
- ### 🔗 Dependencies update
53
-
54
- * bump the npm-dependencies group with 4 updates ([9825815](https://github.com/Alwatr/nanolib/commit/982581552bbb4b97dca52af5e93a80937f0c3109))
55
-
56
- ## [5.5.20](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.19...@alwatr/parse-duration@5.5.20) (2025-09-27)
57
-
58
- ### 🧹 Miscellaneous Chores
59
-
60
- * exclude test files from package distribution ([86f4f2f](https://github.com/Alwatr/nanolib/commit/86f4f2f5985845c5cf3a3a9398de7b2f98ce53e7))
61
-
62
- ## [5.5.19](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.18...@alwatr/parse-duration@5.5.19) (2025-09-22)
63
-
64
- **Note:** Version bump only for package @alwatr/parse-duration
65
-
66
- ## [5.5.18](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.17...@alwatr/parse-duration@5.5.18) (2025-09-22)
67
-
68
- **Note:** Version bump only for package @alwatr/parse-duration
69
-
70
- ## [5.5.17](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.16...@alwatr/parse-duration@5.5.17) (2025-09-21)
71
-
72
- **Note:** Version bump only for package @alwatr/parse-duration
73
-
74
- ## [5.5.16](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.15...@alwatr/parse-duration@5.5.16) (2025-09-20)
75
-
76
- ### 🐛 Bug Fixes
77
-
78
- * add sideEffects property to package.json files for better tree-shaking ([c7b9e74](https://github.com/Alwatr/nanolib/commit/c7b9e74e1920c8e35b438742de61883ca62da58c))
79
- * add sideEffects property to package.json files for better tree-shaking ([e8402c4](https://github.com/Alwatr/nanolib/commit/e8402c481a14a1f807a37aaa862a936713d26176))
80
- * remove unnecessary pure annotations ([adeb916](https://github.com/Alwatr/nanolib/commit/adeb9166f8e911f59269032b76c36cb1888332cf))
81
-
82
- ### 🧹 Miscellaneous Chores
83
-
84
- * remove duplicate sideEffects property from multiple package.json files ([b123f86](https://github.com/Alwatr/nanolib/commit/b123f86be81481de2314aae9bb2eeb629743d24c))
85
-
86
- ## [5.5.15](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.14...@alwatr/parse-duration@5.5.15) (2025-09-19)
87
-
88
- **Note:** Version bump only for package @alwatr/parse-duration
89
-
90
- ## [5.5.14](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.13...@alwatr/parse-duration@5.5.14) (2025-09-15)
91
-
92
- **Note:** Version bump only for package @alwatr/parse-duration
93
-
94
- ## [5.5.13](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.12...@alwatr/parse-duration@5.5.13) (2025-09-13)
95
-
96
- **Note:** Version bump only for package @alwatr/parse-duration
97
-
98
- ## [5.5.12](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.11...@alwatr/parse-duration@5.5.12) (2025-09-13)
99
-
100
- ### 🧹 Miscellaneous Chores
101
-
102
- * remove package-tracer dependency and related code from fetch package ([96fe4e9](https://github.com/Alwatr/nanolib/commit/96fe4e9552a205f218ceed187c55e4e904a07089))
103
-
104
- ## [5.5.11](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.10...@alwatr/parse-duration@5.5.11) (2025-09-09)
105
-
106
- ### 🧹 Miscellaneous Chores
107
-
108
- * remove trailing newlines from contributing sections in README files ([e8ab1bc](https://github.com/Alwatr/nanolib/commit/e8ab1bc43e0addea5ccd4c897c2cec597cb9e15f))
109
-
110
- ## [5.5.10](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.9...@alwatr/parse-duration@5.5.10) (2025-09-06)
111
-
112
- **Note:** Version bump only for package @alwatr/parse-duration
113
-
114
- ## [5.5.9](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.8...@alwatr/parse-duration@5.5.9) (2025-09-05)
115
-
116
- ### 🔗 Dependencies update
117
-
118
- * update jest to version 30.1.3 and @types/node to version 22.18.1 ([754212b](https://github.com/Alwatr/nanolib/commit/754212b1523cfc4cfe26c9e9f6d634aa8311e0b7))
119
-
120
- ## [5.5.8](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.7...@alwatr/parse-duration@5.5.8) (2025-09-01)
121
-
122
- ### 🔗 Dependencies update
123
-
124
- * update lerna-lite dependencies to version 4.7.3 and jest to 30.1.2 ([95d7870](https://github.com/Alwatr/nanolib/commit/95d7870ec7ad1e6ed2688bafddcabf46857f6981))
125
-
126
- ## [5.5.7](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.6...@alwatr/parse-duration@5.5.7) (2025-08-23)
127
-
128
- **Note:** Version bump only for package @alwatr/parse-duration
129
-
130
- ## [5.5.6](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.4...@alwatr/parse-duration@5.5.6) (2025-08-23)
131
-
132
- ### 🐛 Bug Fixes
133
-
134
- * update license from AGPL-3.0-only to MPL-2.0 ([d20968e](https://github.com/Alwatr/nanolib/commit/d20968e60cc89b1dcdf9b96507178da6ed562f55))
135
- * update package versions in multiple package.json files ([7638b1c](https://github.com/Alwatr/nanolib/commit/7638b1cafee2b4e0f97db7a89ac9fba6384b9b10))
136
-
137
- ### 🔨 Code Refactoring
138
-
139
- * Updated all package.json files in the project to change dependency version specifiers from "workspace:^" to "workspace:*" for consistency and to allow for more flexible version resolution. ([db6a4f7](https://github.com/Alwatr/nanolib/commit/db6a4f76deec2d1d8039978144e4bc51b6f1a0e3))
140
-
141
- ### 🧹 Miscellaneous Chores
142
-
143
- * reformat all package.json files ([ceda45d](https://github.com/Alwatr/nanolib/commit/ceda45de186667790474f729cb4b161a5148ce19))
144
-
145
- ### 🔗 Dependencies update
146
-
147
- * update TypeScript and Jest versions across all packages to improve compatibility and performance ([31baf36](https://github.com/Alwatr/nanolib/commit/31baf366101e92e27db66a21c849fb101f19be47))
148
-
149
- ## [5.5.5](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.4...@alwatr/parse-duration@5.5.5) (2025-08-23)
150
-
151
- ### Code Refactoring
152
-
153
- * Updated all package.json files in the project to change dependency version specifiers from "workspace:^" to "workspace:*" for consistency and to allow for more flexible version resolution. ([db6a4f7](https://github.com/Alwatr/nanolib/commit/db6a4f76deec2d1d8039978144e4bc51b6f1a0e3)) by @alimd
154
-
155
- ## [5.5.4](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.3...@alwatr/parse-duration@5.5.4) (2025-04-20)
156
-
157
- **Note:** Version bump only for package @alwatr/parse-duration
158
-
159
- ## <small>5.5.3 (2025-04-15)</small>
160
-
161
- **Note:** Version bump only for package @alwatr/parse-duration
162
-
163
- ## [5.5.2](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.1...@alwatr/parse-duration@5.5.2) (2025-04-01)
164
-
165
- **Note:** Version bump only for package @alwatr/parse-duration
166
-
167
- ## [5.5.1](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.5.0...@alwatr/parse-duration@5.5.1) (2025-03-18)
168
-
169
- ### Code Refactoring
170
-
171
- * **parse-duration:** replace isNumber with toNumber for improved number conversion and enhance error handling ([4d63892](https://github.com/Alwatr/nanolib/commit/4d63892f914cebe73bd1dc1e688c2f4ea5b53de1)) by @alimd
172
-
173
- ## [5.5.0](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@5.4.0...@alwatr/parse-duration@5.5.0) (2025-03-06)
174
-
175
- ### Miscellaneous Chores
176
-
177
- * update username casing in changelog entries ([9722ac9](https://github.com/Alwatr/nanolib/commit/9722ac9a078438a4e8ebfa5826ea70e0e3a52ca6)) by @
178
-
179
- ### Dependencies update
180
-
181
- * bump the development-dependencies group across 1 directory with 11 updates ([720c395](https://github.com/Alwatr/nanolib/commit/720c3954da55c929fe8fb16957121f4c51fb7f0c)) by @dependabot[bot]
182
-
183
- ## [5.4.0](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.1.8...@alwatr/parse-duration@5.4.0) (2025-02-18)
184
-
185
- ## 5.3.0 (2025-02-03)
186
-
187
- ### Miscellaneous Chores
188
-
189
- * edit README ([3860b3d](https://github.com/Alwatr/nanolib/commit/3860b3df48ab82dc479d5236c2e8579df614aabf)) by @
190
-
191
- ### Dependencies update
192
-
193
- * bump the development-dependencies group across 1 directory with 11 updates ([cb79d07](https://github.com/Alwatr/nanolib/commit/cb79d072a57c79e1c01abff1a293d6757bb65350)) by @
194
- * update typescript and @types/node to version 5.7.3 and 22.13.0 respectively across multiple packages ([ddab05b](https://github.com/Alwatr/nanolib/commit/ddab05b5d767c30191f36a065e4bc88744e8e3fe)) by @
195
-
196
- ## 5.0.0 (2024-11-02)
197
-
198
- ### ⚠ BREAKING CHANGES
199
-
200
- * To simplify version management and ensure consistency, all nanolib packages now use the same version as @alwatr/nanolib. This may require updates to your project's dependencies.
201
-
202
- ### Code Refactoring
203
-
204
- * use the same version as @alwatr/nanolib ([60eb860](https://github.com/Alwatr/nanolib/commit/60eb860a0e33dfffe2d1d95e63ce54c60876be06)) by @
205
-
206
- ## [5.3.0](https://github.com/Alwatr/nanolib/compare/v5.2.1...v5.3.0) (2025-02-03)
207
-
208
- ### Miscellaneous Chores
209
-
210
- * edit README ([3860b3d](https://github.com/Alwatr/nanolib/commit/3860b3df48ab82dc479d5236c2e8579df614aabf)) by @ArmanAsadian
211
-
212
- ### Dependencies update
213
-
214
- * bump the development-dependencies group across 1 directory with 11 updates ([cb79d07](https://github.com/Alwatr/nanolib/commit/cb79d072a57c79e1c01abff1a293d6757bb65350)) by @dependabot[bot]
215
- * update typescript and @types/node to version 5.7.3 and 22.13.0 respectively across multiple packages ([ddab05b](https://github.com/Alwatr/nanolib/commit/ddab05b5d767c30191f36a065e4bc88744e8e3fe)) by @alimd
216
-
217
- ## 5.0.0 (2024-11-02)
218
-
219
- ### ⚠ BREAKING CHANGES
220
-
221
- * To simplify version management and ensure consistency, all nanolib packages now use the same version as @alwatr/nanolib. This may require updates to your project's dependencies.
222
-
223
- ### Features
224
-
225
- * **parse-duration:** rewrite module 🤦🏻 ([be62163](https://github.com/Alwatr/nanolib/commit/be6216345cb3d4458307f55b8ae44f5ac60dda89)) by @
226
- * **parse-duration:** rewrite with document ([43ffecc](https://github.com/Alwatr/nanolib/commit/43ffeccbc4a859ad838938a63fd52c82654cc9bb)) by @
227
- * **parse-duration:** seperate parseDuration from `@alwatr/math` ([f5e32d0](https://github.com/Alwatr/nanolib/commit/f5e32d080be1b5355463f77a2f23fbd0955b8b67)) by @
228
- * **parse-duration:** update parseDuration function to accept duration as number or string ([af413f5](https://github.com/Alwatr/nanolib/commit/af413f5399b7bb8c01d4e14ae1f9238e44368184)) by @
229
- * use `package-tracer` ([cc3c5f9](https://github.com/Alwatr/nanolib/commit/cc3c5f9c1a3d03f0d81b46835665f16a0426fd0d)) by @
230
-
231
- ### Bug Fixes
232
-
233
- * all dependeny topology ([1c17f34](https://github.com/Alwatr/nanolib/commit/1c17f349adf3e98e2a80ab2da4f0f81028dc9c5f)) by @
234
- * **parse-duration:** remove unused variables ([500f272](https://github.com/Alwatr/nanolib/commit/500f2727373daf12b6b8b84032244a88f197948e)) by @
235
-
236
- ### Code Refactoring
237
-
238
- * prevent side-effects ([01e00e1](https://github.com/Alwatr/nanolib/commit/01e00e191385cc92b28677df0c01a085916ae677)) by @
239
- * use the same version as @alwatr/nanolib ([60eb860](https://github.com/Alwatr/nanolib/commit/60eb860a0e33dfffe2d1d95e63ce54c60876be06)) by @
240
-
241
- ### Miscellaneous Chores
242
-
243
- * **deps:** update ([1a45030](https://github.com/Alwatr/nanolib/commit/1a450305440b710a300787d4ca24b1ed8c6a39d7)) by @
244
- * **deps:** update ([8e70dff](https://github.com/Alwatr/nanolib/commit/8e70dffb1e751496ef2e72d6cffd685f1fea44e3)) by @
245
- * include LICENSE and LEGAL files to publish ([09f366f](https://github.com/Alwatr/nanolib/commit/09f366f680bfa9fb26acb2cd1ccbc68c5a9e9ad8)) by @
246
- * **parse-duration:** add tsconfig.json ([e799038](https://github.com/Alwatr/nanolib/commit/e7990381f8820a3f6a449b908c0fc5da4e475abd)) by @
247
- * **parse-duration:** change the license to AGPL-3.0 ([896df6f](https://github.com/Alwatr/nanolib/commit/896df6f64bb1df5954d6c46694e18bfb3e944f22)) by @
248
- * **parse-duration:** deps ([1898f2e](https://github.com/Alwatr/nanolib/commit/1898f2ee41bb5561949d758a62754fd5086eb0c7)) by @
249
- * **parse-duration:** package.json ([97f970d](https://github.com/Alwatr/nanolib/commit/97f970dfeb4a84ad66050cc5dd2611a013853c15)) by @
250
- * **parse-duration:** rename main file ([be0d2fc](https://github.com/Alwatr/nanolib/commit/be0d2fc3d37f298936c9a85f7e095d670c805970)) by @
251
- * Update build and lint scripts ([392d0b7](https://github.com/Alwatr/nanolib/commit/392d0b71f446bce336b0256119a80f07aff794ba)) by @
252
- * Update package.json exports for [@alwatr](https://github.com/alwatr) packages ([dacb362](https://github.com/Alwatr/nanolib/commit/dacb362b145e3c51b4aba00ff643687a3fac11d2)) by @
253
- * Update test scripts and dependencies ([93d2fe6](https://github.com/Alwatr/nanolib/commit/93d2fe6d7ce9c38a300e0c7ed75874916767a14b)) by @
254
-
255
- ### Dependencies update
256
-
257
- * bump @types/node ([3d80fed](https://github.com/Alwatr/nanolib/commit/3d80fedaf720af792feb060c2f81c737ebb84e11)) by @
258
- * bump the development-dependencies group across 1 directory with 10 updates ([9ed98ff](https://github.com/Alwatr/nanolib/commit/9ed98ffd0668d5a36e255c82edab3af53bffda8f)) by @
259
- * bump the development-dependencies group with 10 updates ([fa4aaf0](https://github.com/Alwatr/nanolib/commit/fa4aaf04c907ecae06aa14000ce35216170c15ad)) by @
260
- * bump the development-dependencies group with 2 updates ([be5d6c2](https://github.com/Alwatr/nanolib/commit/be5d6c2d86b937f32cebc6848aaff85af07055dd)) by @
261
- * upd ([451d025](https://github.com/Alwatr/nanolib/commit/451d0255ba96ed55f897a6f44f62cf4e6d2b12be)) by @
262
- * update ([c36ed50](https://github.com/Alwatr/nanolib/commit/c36ed50f68da2f5608ccd96119963a16cfacb4ce)) by @
263
- * update all ([53342f6](https://github.com/Alwatr/nanolib/commit/53342f67a8a013127f073540bc11929f1813c05c)) by @
264
- * update all dependencies ([1e0c30e](https://github.com/Alwatr/nanolib/commit/1e0c30e6a3a8e19deb5185814e24ab6c08dca573)) by @
265
- * update all dependencies ([0e908b4](https://github.com/Alwatr/nanolib/commit/0e908b476a6b976ec2447f864c8cafcbb8a0f099)) by @
266
- * upgrade ([6dbd300](https://github.com/Alwatr/nanolib/commit/6dbd300642c9bcc9e7d0b281e244bf1b06eb1c38)) by @
267
-
268
- ## [1.1.8](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.1.7...@alwatr/parse-duration@1.1.8) (2024-11-02)
269
-
270
- **Note:** Version bump only for package @alwatr/parse-duration
271
-
272
- ## [1.1.7](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.1.6...@alwatr/parse-duration@1.1.7) (2024-10-25)
273
-
274
- **Note:** Version bump only for package @alwatr/parse-duration
275
-
276
- ## [1.1.6](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.1.5...@alwatr/parse-duration@1.1.6) (2024-10-12)
277
-
278
- **Note:** Version bump only for package @alwatr/parse-duration
279
-
280
- ## [1.1.5](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.1.4...@alwatr/parse-duration@1.1.5) (2024-10-11)
281
-
282
- ### Code Refactoring
283
-
284
- - prevent side-effects ([01e00e1](https://github.com/Alwatr/nanolib/commit/01e00e191385cc92b28677df0c01a085916ae677)) by @mohammadhonarvar
285
-
286
- ## [1.1.4](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.1.3...@alwatr/parse-duration@1.1.4) (2024-10-11)
287
-
288
- ### Miscellaneous Chores
289
-
290
- - include LICENSE and LEGAL files to publish ([09f366f](https://github.com/Alwatr/nanolib/commit/09f366f680bfa9fb26acb2cd1ccbc68c5a9e9ad8)) by @alimd
291
-
292
- ## [1.1.3](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.1.2...@alwatr/parse-duration@1.1.3) (2024-10-11)
293
-
294
- **Note:** Version bump only for package @alwatr/parse-duration
295
-
296
- ## [1.1.2](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.1.1...@alwatr/parse-duration@1.1.2) (2024-10-10)
297
-
298
- ### Dependencies update
299
-
300
- - bump the development-dependencies group with 10 updates ([fa4aaf0](https://github.com/Alwatr/nanolib/commit/fa4aaf04c907ecae06aa14000ce35216170c15ad)) by @dependabot[bot]
301
-
302
- ## [1.1.1](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.1.0...@alwatr/parse-duration@1.1.1) (2024-10-08)
303
-
304
- **Note:** Version bump only for package @alwatr/parse-duration
305
-
306
- ## [1.1.0](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.11...@alwatr/parse-duration@1.1.0) (2024-09-29)
307
-
308
- ### Features
309
-
310
- - **parse-duration:** update parseDuration function to accept duration as number or string ([af413f5](https://github.com/Alwatr/nanolib/commit/af413f5399b7bb8c01d4e14ae1f9238e44368184)) by @alimd
311
- - use `package-tracer` ([cc3c5f9](https://github.com/Alwatr/nanolib/commit/cc3c5f9c1a3d03f0d81b46835665f16a0426fd0d)) by @mohammadhonarvar
312
-
313
- ### Bug Fixes
314
-
315
- - all dependeny topology ([1c17f34](https://github.com/Alwatr/nanolib/commit/1c17f349adf3e98e2a80ab2da4f0f81028dc9c5f)) by @mohammadhonarvar
316
-
317
- ### Miscellaneous Chores
318
-
319
- - **parse-duration:** change the license to AGPL-3.0 ([896df6f](https://github.com/Alwatr/nanolib/commit/896df6f64bb1df5954d6c46694e18bfb3e944f22)) by @ArmanAsadian
320
- - Update build and lint scripts ([392d0b7](https://github.com/Alwatr/nanolib/commit/392d0b71f446bce336b0256119a80f07aff794ba)) by @alimd
321
-
322
- ### Dependencies update
323
-
324
- - bump @types/node ([3d80fed](https://github.com/Alwatr/nanolib/commit/3d80fedaf720af792feb060c2f81c737ebb84e11)) by @dependabot[bot]
325
-
326
- ## [1.0.11](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.10...@alwatr/parse-duration@1.0.11) (2024-09-21)
327
-
328
- **Note:** Version bump only for package @alwatr/parse-duration
329
-
330
- ## [1.0.10](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.9...@alwatr/parse-duration@1.0.10) (2024-09-15)
331
-
332
- ### Dependencies update
333
-
334
- - bump the development-dependencies group across 1 directory with 10 updates ([9ed98ff](https://github.com/Alwatr/nanolib/commit/9ed98ffd0668d5a36e255c82edab3af53bffda8f)) by @dependabot[bot]
335
- - update ([c36ed50](https://github.com/Alwatr/nanolib/commit/c36ed50f68da2f5608ccd96119963a16cfacb4ce)) by @alimd
336
-
337
- ## [1.0.9](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.8...@alwatr/parse-duration@1.0.9) (2024-08-31)
338
-
339
- ### Miscellaneous Chores
340
-
341
- - Update package.json exports for [@alwatr](https://github.com/alwatr) packages ([dacb362](https://github.com/Alwatr/nanolib/commit/dacb362b145e3c51b4aba00ff643687a3fac11d2)) by @
342
-
343
- ## [1.0.8](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.7...@alwatr/parse-duration@1.0.8) (2024-08-31)
344
-
345
- ### Dependencies update
346
-
347
- - update all dependencies ([1e0c30e](https://github.com/Alwatr/nanolib/commit/1e0c30e6a3a8e19deb5185814e24ab6c08dca573)) by @alimd
348
-
349
- ## [1.0.7](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.6...@alwatr/parse-duration@1.0.7) (2024-07-04)
350
-
351
- ### Dependencies update
352
-
353
- - update all dependencies ([0e908b4](https://github.com/Alwatr/nanolib/commit/0e908b476a6b976ec2447f864c8cafcbb8a0f099)) by @
354
-
355
- ## [1.0.6](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.5...@alwatr/parse-duration@1.0.6) (2024-05-12)
356
-
357
- ### Dependencies update
358
-
359
- - upgrade ([6dbd300](https://github.com/Alwatr/nanolib/commit/6dbd300642c9bcc9e7d0b281e244bf1b06eb1c38)) by @alimd
360
-
361
- ## [1.0.5](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.4...@alwatr/parse-duration@1.0.5) (2024-04-25)
362
-
363
- **Note:** Version bump only for package @alwatr/parse-duration
364
-
365
- ## [1.0.4](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.3...@alwatr/parse-duration@1.0.4) (2024-03-28)
366
-
367
- **Note:** Version bump only for package @alwatr/parse-duration
368
-
369
- ## [1.0.3](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.2...@alwatr/parse-duration@1.0.3) (2024-01-31)
370
-
371
- ### Miscellaneous Chores
372
-
373
- - **deps:** update ([1a45030](https://github.com/Alwatr/nanolib/commit/1a450305440b710a300787d4ca24b1ed8c6a39d7)) by @alimd
374
-
375
- ## [1.0.2](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.1...@alwatr/parse-duration@1.0.2) (2024-01-24)
376
-
377
- **Note:** Version bump only for package @alwatr/parse-duration
378
-
379
- ## [1.0.1](https://github.com/Alwatr/nanolib/compare/@alwatr/parse-duration@1.0.0...@alwatr/parse-duration@1.0.1) (2024-01-20)
380
-
381
- **Note:** Version bump only for package @alwatr/parse-duration
382
-
383
- # 1.0.0 (2024-01-16)
384
-
385
- ### Bug Fixes
386
-
387
- - **parse-duration:** remove unused variables ([500f272](https://github.com/Alwatr/nanolib/commit/500f2727373daf12b6b8b84032244a88f197948e)) by @adltalab
388
-
389
- ### Features
390
-
391
- - **parse-duration:** rewrite module 🤦🏻 ([be62163](https://github.com/Alwatr/nanolib/commit/be6216345cb3d4458307f55b8ae44f5ac60dda89)) by @alimd
392
- - **parse-duration:** rewrite with document ([43ffecc](https://github.com/Alwatr/nanolib/commit/43ffeccbc4a859ad838938a63fd52c82654cc9bb)) by @njfamirm
393
- - **parse-duration:** seperate parseDuration from `@alwatr/math` ([f5e32d0](https://github.com/Alwatr/nanolib/commit/f5e32d080be1b5355463f77a2f23fbd0955b8b67)) by @adltalab
package/dist/main.cjs DELETED
@@ -1,3 +0,0 @@
1
- /** 📦 @alwatr/parse-duration v5.5.31 */
2
- "use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var main_exports={};__export(main_exports,{parseDuration:()=>parseDuration});module.exports=__toCommonJS(main_exports);var import_is_number=require("@alwatr/is-number");var unitConversion=Object.freeze({s:1e3,m:6e4,h:36e5,d:864e5,w:6048e5,M:2592e6,y:31536e6});var parseDuration=(duration,toUnit)=>{let ms;if(typeof duration==="number"){ms=duration}else{if(duration.length<2){throw new Error("invalid_format",{cause:{duration}})}const durationUnit=duration.slice(-1);const unitConversionFactor=unitConversion[durationUnit];if(unitConversionFactor===void 0){throw new Error("invalid_unit",{cause:{durationUnit}})}const durationNumber=(0,import_is_number.toNumber)(duration.slice(0,-1));if(durationNumber===null){throw new Error("not_a_number",{cause:{duration}})}ms=durationNumber*unitConversionFactor}if(toUnit===void 0){return ms}const toFactor=unitConversion[toUnit];if(toFactor===void 0){throw new Error("invalid_unit",{cause:{toUnit}})}return ms/toFactor};0&&(module.exports={parseDuration});
3
- //# sourceMappingURL=main.cjs.map
package/dist/main.cjs.map DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/main.ts"],
4
- "sourcesContent": ["import {toNumber} from '@alwatr/is-number';\n\n/**\n * Unit conversion table (milliseconds)\n */\nconst unitConversion = Object.freeze({\n s: 1_000,\n m: 60_000,\n h: 3_600_000,\n d: 86_400_000,\n w: 604_800_000,\n M: 2_592_000_000,\n y: 31_536_000_000,\n} as const);\n\n/**\n * Duration unit: `s` for seconds, `m` for minutes, `h` for hours, `d` for days, `w` for weeks, `M` for months, `y` for years.\n */\nexport type DurationUnit = keyof typeof unitConversion;\n\n/**\n * Duration string format: `number + unit`, for example `10m` means 10 minutes.\n */\nexport type Duration = `${number}${DurationUnit}` | number;\n\n/**\n * Error types that can be thrown by parseDuration\n */\nexport type DurationError = 'not_a_number' | 'invalid_unit' | 'invalid_format';\n\n/**\n * Parse duration string to milliseconds number.\n *\n * @param duration - Duration string or number, for example `10m` means 10 minutes.\n * @param toUnit - Convert to unit, default is `ms` for milliseconds.\n * @throws {Error} With message 'not_a_number' if duration string doesn't contain a valid number.\n * @throws {Error} With message 'invalid_unit' if the unit is not recognized.\n * @throws {Error} With message 'invalid_format' if the duration format is invalid.\n * @returns Duration in specified unit (or milliseconds by default).\n *\n * @example\n * ```ts\n * parseDuration('10m'); // 600000\n * parseDuration('10m', 's'); // 600\n * parseDuration(120_000, 'm'); // 2\n * ```\n */\nexport const parseDuration = (duration: Duration, toUnit?: DurationUnit): number => {\n let ms: number;\n\n // Convert input to milliseconds\n if (typeof duration === 'number') {\n ms = duration;\n }\n else {\n if (duration.length < 2) {\n throw new Error('invalid_format', {cause: {duration}});\n }\n\n const durationUnit = duration.slice(-1) as DurationUnit;\n const unitConversionFactor = unitConversion[durationUnit];\n\n if (unitConversionFactor === undefined) {\n throw new Error('invalid_unit', {cause: {durationUnit}});\n }\n\n const durationNumber = toNumber(duration.slice(0, -1));\n if (durationNumber === null) {\n throw new Error('not_a_number', {cause: {duration}});\n }\n\n ms = durationNumber * unitConversionFactor;\n }\n\n // Return as is if no conversion needed\n if (toUnit === undefined) {\n return ms;\n }\n\n // Convert to target unit\n const toFactor = unitConversion[toUnit];\n if (toFactor === undefined) {\n throw new Error('invalid_unit', {cause: {toUnit}});\n }\n\n return ms / toFactor;\n};\n"],
5
- "mappings": ";qqBAAA,4IAAuB,6BAKvB,IAAM,eAAiB,OAAO,OAAO,CACnC,EAAG,IACH,EAAG,IACH,EAAG,KACH,EAAG,MACH,EAAG,OACH,EAAG,OACH,EAAG,OACL,CAAU,EAkCH,IAAM,cAAgB,CAAC,SAAoB,SAAkC,CAClF,IAAI,GAGJ,GAAI,OAAO,WAAa,SAAU,CAChC,GAAK,QACP,KACK,CACH,GAAI,SAAS,OAAS,EAAG,CACvB,MAAM,IAAI,MAAM,iBAAkB,CAAC,MAAO,CAAC,QAAQ,CAAC,CAAC,CACvD,CAEA,MAAM,aAAe,SAAS,MAAM,EAAE,EACtC,MAAM,qBAAuB,eAAe,YAAY,EAExD,GAAI,uBAAyB,OAAW,CACtC,MAAM,IAAI,MAAM,eAAgB,CAAC,MAAO,CAAC,YAAY,CAAC,CAAC,CACzD,CAEA,MAAM,kBAAiB,2BAAS,SAAS,MAAM,EAAG,EAAE,CAAC,EACrD,GAAI,iBAAmB,KAAM,CAC3B,MAAM,IAAI,MAAM,eAAgB,CAAC,MAAO,CAAC,QAAQ,CAAC,CAAC,CACrD,CAEA,GAAK,eAAiB,oBACxB,CAGA,GAAI,SAAW,OAAW,CACxB,OAAO,EACT,CAGA,MAAM,SAAW,eAAe,MAAM,EACtC,GAAI,WAAa,OAAW,CAC1B,MAAM,IAAI,MAAM,eAAgB,CAAC,MAAO,CAAC,MAAM,CAAC,CAAC,CACnD,CAEA,OAAO,GAAK,QACd",
6
- "names": []
7
- }
package/dist/main.mjs DELETED
@@ -1,3 +0,0 @@
1
- /** 📦 @alwatr/parse-duration v5.5.31 */
2
- import{toNumber}from"@alwatr/is-number";var unitConversion=Object.freeze({s:1e3,m:6e4,h:36e5,d:864e5,w:6048e5,M:2592e6,y:31536e6});var parseDuration=(duration,toUnit)=>{let ms;if(typeof duration==="number"){ms=duration}else{if(duration.length<2){throw new Error("invalid_format",{cause:{duration}})}const durationUnit=duration.slice(-1);const unitConversionFactor=unitConversion[durationUnit];if(unitConversionFactor===void 0){throw new Error("invalid_unit",{cause:{durationUnit}})}const durationNumber=toNumber(duration.slice(0,-1));if(durationNumber===null){throw new Error("not_a_number",{cause:{duration}})}ms=durationNumber*unitConversionFactor}if(toUnit===void 0){return ms}const toFactor=unitConversion[toUnit];if(toFactor===void 0){throw new Error("invalid_unit",{cause:{toUnit}})}return ms/toFactor};export{parseDuration};
3
- //# sourceMappingURL=main.mjs.map
package/dist/main.mjs.map DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/main.ts"],
4
- "sourcesContent": ["import {toNumber} from '@alwatr/is-number';\n\n/**\n * Unit conversion table (milliseconds)\n */\nconst unitConversion = Object.freeze({\n s: 1_000,\n m: 60_000,\n h: 3_600_000,\n d: 86_400_000,\n w: 604_800_000,\n M: 2_592_000_000,\n y: 31_536_000_000,\n} as const);\n\n/**\n * Duration unit: `s` for seconds, `m` for minutes, `h` for hours, `d` for days, `w` for weeks, `M` for months, `y` for years.\n */\nexport type DurationUnit = keyof typeof unitConversion;\n\n/**\n * Duration string format: `number + unit`, for example `10m` means 10 minutes.\n */\nexport type Duration = `${number}${DurationUnit}` | number;\n\n/**\n * Error types that can be thrown by parseDuration\n */\nexport type DurationError = 'not_a_number' | 'invalid_unit' | 'invalid_format';\n\n/**\n * Parse duration string to milliseconds number.\n *\n * @param duration - Duration string or number, for example `10m` means 10 minutes.\n * @param toUnit - Convert to unit, default is `ms` for milliseconds.\n * @throws {Error} With message 'not_a_number' if duration string doesn't contain a valid number.\n * @throws {Error} With message 'invalid_unit' if the unit is not recognized.\n * @throws {Error} With message 'invalid_format' if the duration format is invalid.\n * @returns Duration in specified unit (or milliseconds by default).\n *\n * @example\n * ```ts\n * parseDuration('10m'); // 600000\n * parseDuration('10m', 's'); // 600\n * parseDuration(120_000, 'm'); // 2\n * ```\n */\nexport const parseDuration = (duration: Duration, toUnit?: DurationUnit): number => {\n let ms: number;\n\n // Convert input to milliseconds\n if (typeof duration === 'number') {\n ms = duration;\n }\n else {\n if (duration.length < 2) {\n throw new Error('invalid_format', {cause: {duration}});\n }\n\n const durationUnit = duration.slice(-1) as DurationUnit;\n const unitConversionFactor = unitConversion[durationUnit];\n\n if (unitConversionFactor === undefined) {\n throw new Error('invalid_unit', {cause: {durationUnit}});\n }\n\n const durationNumber = toNumber(duration.slice(0, -1));\n if (durationNumber === null) {\n throw new Error('not_a_number', {cause: {duration}});\n }\n\n ms = durationNumber * unitConversionFactor;\n }\n\n // Return as is if no conversion needed\n if (toUnit === undefined) {\n return ms;\n }\n\n // Convert to target unit\n const toFactor = unitConversion[toUnit];\n if (toFactor === undefined) {\n throw new Error('invalid_unit', {cause: {toUnit}});\n }\n\n return ms / toFactor;\n};\n"],
5
- "mappings": ";AAAA,OAAQ,aAAe,oBAKvB,IAAM,eAAiB,OAAO,OAAO,CACnC,EAAG,IACH,EAAG,IACH,EAAG,KACH,EAAG,MACH,EAAG,OACH,EAAG,OACH,EAAG,OACL,CAAU,EAkCH,IAAM,cAAgB,CAAC,SAAoB,SAAkC,CAClF,IAAI,GAGJ,GAAI,OAAO,WAAa,SAAU,CAChC,GAAK,QACP,KACK,CACH,GAAI,SAAS,OAAS,EAAG,CACvB,MAAM,IAAI,MAAM,iBAAkB,CAAC,MAAO,CAAC,QAAQ,CAAC,CAAC,CACvD,CAEA,MAAM,aAAe,SAAS,MAAM,EAAE,EACtC,MAAM,qBAAuB,eAAe,YAAY,EAExD,GAAI,uBAAyB,OAAW,CACtC,MAAM,IAAI,MAAM,eAAgB,CAAC,MAAO,CAAC,YAAY,CAAC,CAAC,CACzD,CAEA,MAAM,eAAiB,SAAS,SAAS,MAAM,EAAG,EAAE,CAAC,EACrD,GAAI,iBAAmB,KAAM,CAC3B,MAAM,IAAI,MAAM,eAAgB,CAAC,MAAO,CAAC,QAAQ,CAAC,CAAC,CACrD,CAEA,GAAK,eAAiB,oBACxB,CAGA,GAAI,SAAW,OAAW,CACxB,OAAO,EACT,CAGA,MAAM,SAAW,eAAe,MAAM,EACtC,GAAI,WAAa,OAAW,CAC1B,MAAM,IAAI,MAAM,eAAgB,CAAC,MAAO,CAAC,MAAM,CAAC,CAAC,CACnD,CAEA,OAAO,GAAK,QACd",
6
- "names": []
7
- }