@prexjs/ms 0.0.0-stage → 1.0.1
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/LICENSE +21 -0
- package/README.md +342 -3
- package/lib/index.cjs +186 -0
- package/lib/index.d.cts +53 -0
- package/lib/index.d.mts +53 -0
- package/lib/index.mjs +178 -0
- package/package.json +52 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Prexjs (https://prexjs.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,342 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @prexjs/ms
|
|
2
|
+
|
|
3
|
+
**A lightweight TypeScript utility for converting time durations to milliseconds and formatting milliseconds into human-readable strings.**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@prexjs/ms)
|
|
6
|
+
[](https://www.npmjs.com/package/@prexjs/ms)
|
|
7
|
+
[](./LICENSE)
|
|
8
|
+
[](https://www.typescriptlang.org/)
|
|
9
|
+
|
|
10
|
+
`@prexjs/ms` converts human-readable time strings such as `2 days`, `10h`, and `5s` into milliseconds. It can also format millisecond values into compact or human-readable duration strings.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- Convert time strings into milliseconds.
|
|
15
|
+
- Convert milliseconds into short or long duration strings.
|
|
16
|
+
- Support positive, negative, and fractional durations.
|
|
17
|
+
- Support years, months, weeks, days, hours, minutes, seconds, and milliseconds.
|
|
18
|
+
- Accept common unit aliases and case variations.
|
|
19
|
+
- Provide TypeScript definitions and template literal types.
|
|
20
|
+
- Export separate `parse`, `parseStrict`, and `format` helpers.
|
|
21
|
+
- Work without runtime dependencies.
|
|
22
|
+
- Suitable for browser and JavaScript runtime environments when built for those targets.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
Install using your preferred package manager.
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
# npm
|
|
30
|
+
npm install @prexjs/ms
|
|
31
|
+
|
|
32
|
+
# pnpm
|
|
33
|
+
pnpm add @prexjs/ms
|
|
34
|
+
|
|
35
|
+
# Yarn
|
|
36
|
+
yarn add @prexjs/ms
|
|
37
|
+
|
|
38
|
+
# Bun
|
|
39
|
+
bun add @prexjs/ms
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Quick start
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import ms from '@prexjs/ms';
|
|
46
|
+
|
|
47
|
+
ms('2 days'); // 172800000
|
|
48
|
+
ms('10h'); // 36000000
|
|
49
|
+
ms('2h'); // 7200000
|
|
50
|
+
ms('1m'); // 60000
|
|
51
|
+
ms('5s'); // 5000
|
|
52
|
+
|
|
53
|
+
ms(60000); // '1m'
|
|
54
|
+
ms(7200000); // '2h'
|
|
55
|
+
|
|
56
|
+
ms(60000, { long: true }); // '1 minute'
|
|
57
|
+
ms(7200000, { long: true }); // '2 hours'
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Named imports are also supported:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { ms, parse, format, parseStrict } from '@prexjs/ms';
|
|
64
|
+
|
|
65
|
+
parse('1h'); // 3600000
|
|
66
|
+
format(60000); // '1m'
|
|
67
|
+
parseStrict('5s'); // 5000
|
|
68
|
+
ms(60000, { long: true }); // '1 minute'
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Examples
|
|
72
|
+
|
|
73
|
+
### Convert time strings to milliseconds
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import ms from '@prexjs/ms';
|
|
77
|
+
|
|
78
|
+
ms('2 days'); // 172800000
|
|
79
|
+
ms('1d'); // 86400000
|
|
80
|
+
ms('10h'); // 36000000
|
|
81
|
+
ms('2.5 hrs'); // 9000000
|
|
82
|
+
ms('2h'); // 7200000
|
|
83
|
+
ms('1m'); // 60000
|
|
84
|
+
ms('5s'); // 5000
|
|
85
|
+
ms('1y'); // 31557600000
|
|
86
|
+
|
|
87
|
+
// A unit is optional; the default is milliseconds.
|
|
88
|
+
ms('100'); // 100
|
|
89
|
+
|
|
90
|
+
// Negative durations
|
|
91
|
+
ms('-3 days'); // -259200000
|
|
92
|
+
ms('-1h'); // -3600000
|
|
93
|
+
ms('-200'); // -200
|
|
94
|
+
|
|
95
|
+
// Fractional durations
|
|
96
|
+
ms('0.5m'); // 30000
|
|
97
|
+
ms('-0.5m'); // -30000
|
|
98
|
+
ms('.5m'); // 30000
|
|
99
|
+
ms('-.5m'); // -30000
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Convert milliseconds to a short string
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import ms from '@prexjs/ms';
|
|
106
|
+
|
|
107
|
+
ms(1000); // '1s'
|
|
108
|
+
ms(60000); // '1m'
|
|
109
|
+
ms(2 * 60000); // '2m'
|
|
110
|
+
ms(-3 * 60000); // '-3m'
|
|
111
|
+
ms(3600000); // '1h'
|
|
112
|
+
ms(86400000); // '1d'
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
The formatter selects the largest applicable unit and rounds to the nearest whole unit. Values smaller than one second are represented in milliseconds.
|
|
116
|
+
|
|
117
|
+
### Use long formatting
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import ms from '@prexjs/ms';
|
|
121
|
+
|
|
122
|
+
ms(60000, { long: true }); // '1 minute'
|
|
123
|
+
ms(2 * 60000, { long: true }); // '2 minutes'
|
|
124
|
+
ms(-3 * 60000, { long: true }); // '-3 minutes'
|
|
125
|
+
|
|
126
|
+
ms(ms('10 hours'), { long: true }); // '10 hours'
|
|
127
|
+
ms(1000, { long: true }); // '1 second'
|
|
128
|
+
ms(2000, { long: true }); // '2 seconds'
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Use the helper functions
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { parse, parseStrict, format } from '@prexjs/ms';
|
|
135
|
+
|
|
136
|
+
// Parse a string into milliseconds.
|
|
137
|
+
parse('1h'); // 3600000
|
|
138
|
+
|
|
139
|
+
// Format milliseconds.
|
|
140
|
+
format(2000); // '2s'
|
|
141
|
+
|
|
142
|
+
// Long formatting.
|
|
143
|
+
format(60000, { long: true }); // '1 minute'
|
|
144
|
+
|
|
145
|
+
// Type-safe parsing.
|
|
146
|
+
parseStrict('1h'); // 3600000
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Supported units
|
|
150
|
+
|
|
151
|
+
| Unit | Supported aliases |
|
|
152
|
+
|---|---|
|
|
153
|
+
| Years | `years`, `year`, `yrs`, `yr`, `y` |
|
|
154
|
+
| Months | `months`, `month`, `mo` |
|
|
155
|
+
| Weeks | `weeks`, `week`, `w` |
|
|
156
|
+
| Days | `days`, `day`, `d` |
|
|
157
|
+
| Hours | `hours`, `hour`, `hrs`, `hr`, `h` |
|
|
158
|
+
| Minutes | `minutes`, `minute`, `mins`, `min`, `m` |
|
|
159
|
+
| Seconds | `seconds`, `second`, `secs`, `sec`, `s` |
|
|
160
|
+
| Milliseconds | `milliseconds`, `millisecond`, `msecs`, `msec`, `ms` |
|
|
161
|
+
|
|
162
|
+
### Spacing and capitalization
|
|
163
|
+
|
|
164
|
+
Unit matching is case-insensitive.
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
ms('2 minutes'); // 120000
|
|
168
|
+
ms('2minutes'); // 120000
|
|
169
|
+
ms('2 MINUTES'); // 120000
|
|
170
|
+
ms('2 Minutes'); // 120000
|
|
171
|
+
ms('2 H'); // 7200000
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
If no unit is specified, the value defaults to milliseconds.
|
|
175
|
+
|
|
176
|
+
### Duration constants
|
|
177
|
+
|
|
178
|
+
The implementation uses these duration definitions:
|
|
179
|
+
|
|
180
|
+
- 1 second = 1,000 milliseconds
|
|
181
|
+
- 1 minute = 60 seconds
|
|
182
|
+
- 1 hour = 60 minutes
|
|
183
|
+
- 1 day = 24 hours
|
|
184
|
+
- 1 week = 7 days
|
|
185
|
+
- 1 year = 365.25 days
|
|
186
|
+
- 1 month = one-twelfth of a year
|
|
187
|
+
|
|
188
|
+
Months and years are fixed-duration approximations, not calendar-aware date calculations.
|
|
189
|
+
|
|
190
|
+
## TypeScript support
|
|
191
|
+
|
|
192
|
+
The package includes TypeScript declarations and a `StringValue` template literal type.
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
import { ms, type StringValue } from '@prexjs/ms';
|
|
196
|
+
|
|
197
|
+
function convertDuration(value: StringValue): number {
|
|
198
|
+
return ms(value);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
convertDuration('1 h'); // 3600000
|
|
202
|
+
convertDuration('5.2 days'); // 449280000
|
|
203
|
+
convertDuration('500'); // 500
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Custom duration types
|
|
207
|
+
|
|
208
|
+
You can restrict accepted duration strings using your own template literal types.
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { ms } from '@prexjs/ms';
|
|
212
|
+
|
|
213
|
+
type DaysOrWeeks = `${number} ${'days' | 'weeks'}`;
|
|
214
|
+
|
|
215
|
+
function convertLongDuration(value: DaysOrWeeks): number {
|
|
216
|
+
return ms(value);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
convertLongDuration('5.2 days'); // 449280000
|
|
220
|
+
convertLongDuration('2 weeks'); // 1209600000
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Dynamic strings
|
|
224
|
+
|
|
225
|
+
When a value comes from a runtime source, TypeScript cannot automatically guarantee that it matches `StringValue`.
|
|
226
|
+
|
|
227
|
+
Validate untrusted input at runtime before passing it to the parser. Avoid using type assertions as a substitute for validation.
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import { parse } from '@prexjs/ms';
|
|
231
|
+
|
|
232
|
+
const input: string = "5 minutesx";
|
|
233
|
+
const result = parse(input);
|
|
234
|
+
|
|
235
|
+
if (Number.isNaN(result)) {
|
|
236
|
+
console.error("Invalid duration"); // Invalid duration
|
|
237
|
+
} else {
|
|
238
|
+
console.log(result); // 300000
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## API reference
|
|
243
|
+
|
|
244
|
+
### `ms(value, options?)`
|
|
245
|
+
|
|
246
|
+
The main overloaded conversion function.
|
|
247
|
+
|
|
248
|
+
**String input:** returns the duration in milliseconds.
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
ms('5s'); // 5000
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
**Number input:** returns a formatted duration string.
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
ms(5000); // '5s'
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
**Options:**
|
|
261
|
+
|
|
262
|
+
| Option | Type | Default | Description |
|
|
263
|
+
|---|---|---|---|
|
|
264
|
+
| `long` | `boolean` | `false` | Use verbose formatting when `true` |
|
|
265
|
+
|
|
266
|
+
### `parse(value)`
|
|
267
|
+
|
|
268
|
+
Parses a string and returns its duration in milliseconds.
|
|
269
|
+
|
|
270
|
+
```ts
|
|
271
|
+
parse('1h'); // 3600000
|
|
272
|
+
parse('invalid'); // NaN
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
The parser throws for empty strings, non-string values, and strings longer than its configured limit. Unrecognized duration syntax returns `NaN`.
|
|
276
|
+
|
|
277
|
+
### `parseStrict(value)`
|
|
278
|
+
|
|
279
|
+
Parses a `StringValue` input with compile-time type checking.
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
parseStrict('1h'); // 3600000
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
The function provides type safety at compile time. It does not replace runtime validation for untrusted input.
|
|
286
|
+
|
|
287
|
+
### `format(value, options?)`
|
|
288
|
+
|
|
289
|
+
Formats a finite number of milliseconds as a short or long duration string.
|
|
290
|
+
|
|
291
|
+
```ts
|
|
292
|
+
format(2000); // '2s'
|
|
293
|
+
format(60000, { long: true }); // '1 minute'
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
Non-finite numeric inputs such as `NaN` and `Infinity` cause an error.
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
## Error handling
|
|
300
|
+
|
|
301
|
+
For values received from users, environment variables, API requests, or configuration files, validate the result before using it.
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { parse } from '@prexjs/ms';
|
|
305
|
+
|
|
306
|
+
function parseDuration(input: string): number | undefined {
|
|
307
|
+
try {
|
|
308
|
+
const duration = parse(input);
|
|
309
|
+
|
|
310
|
+
if (!Number.isFinite(duration)) {
|
|
311
|
+
return undefined;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return duration;
|
|
315
|
+
} catch {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const timeout = parseDuration("30s"); // 30000
|
|
321
|
+
|
|
322
|
+
if (timeout !== undefined) {
|
|
323
|
+
console.log(`Timeout: ${timeout}ms`); // Timeout: 30000ms
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## Compatibility
|
|
328
|
+
|
|
329
|
+
`@prexjs/ms` is designed for JavaScript and TypeScript projects. Browser, Node.js, Deno, Bun, and edge-runtime compatibility depends on the package's published build formats and the target runtime's supported JavaScript features.
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
## License
|
|
333
|
+
|
|
334
|
+
This project is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.
|
|
335
|
+
|
|
336
|
+
## Related projects
|
|
337
|
+
|
|
338
|
+
- [prex.js](https://github.com/prexjs/prex.js) — prex.js monorepo and ecosystem.
|
|
339
|
+
|
|
340
|
+
---
|
|
341
|
+
|
|
342
|
+
**Note:** This package documents duration conversion, not calendar arithmetic or timezone-aware date calculations.
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @prexjs/ms
|
|
3
|
+
* Copyright(c) 2026 Sorabh <sorabh@backady.com>
|
|
4
|
+
* MIT Licensed
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
10
|
+
|
|
11
|
+
const s = 1000;
|
|
12
|
+
const m = s * 60;
|
|
13
|
+
const h = m * 60;
|
|
14
|
+
const d = h * 24;
|
|
15
|
+
const w = d * 7;
|
|
16
|
+
const y = d * 365.25;
|
|
17
|
+
const mo = y / 12;
|
|
18
|
+
function ms(value, options) {
|
|
19
|
+
if (typeof value === "string") {
|
|
20
|
+
return parse(value);
|
|
21
|
+
}
|
|
22
|
+
else if (typeof value === "number") {
|
|
23
|
+
return format(value, options);
|
|
24
|
+
}
|
|
25
|
+
throw new Error(`Value provided to ms() must be a string or number. value=${JSON.stringify(value)}`);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Parse the given string and return milliseconds.
|
|
29
|
+
*
|
|
30
|
+
* @param str - A string to parse to milliseconds
|
|
31
|
+
* @returns The parsed value in milliseconds, or `NaN` if the string can't be
|
|
32
|
+
* parsed
|
|
33
|
+
*/
|
|
34
|
+
function parse(str) {
|
|
35
|
+
if (typeof str !== "string" || str.length === 0 || str.length > 100) {
|
|
36
|
+
throw new Error(`Value provided to ms.parse() must be a string with length between 1 and 99. value=${JSON.stringify(str)}`);
|
|
37
|
+
}
|
|
38
|
+
const match = /^(?<value>-?\d*\.?\d+) *(?<unit>milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|months?|mo|years?|yrs?|y)?$/i.exec(str);
|
|
39
|
+
if (!match?.groups) {
|
|
40
|
+
return NaN;
|
|
41
|
+
}
|
|
42
|
+
// Named capture groups need to be manually typed today.
|
|
43
|
+
// https://github.com/microsoft/TypeScript/issues/32098
|
|
44
|
+
const { value, unit = "ms" } = match.groups;
|
|
45
|
+
const n = parseFloat(value);
|
|
46
|
+
const matchUnit = unit.toLowerCase();
|
|
47
|
+
/* istanbul ignore next - istanbul doesn't understand, but thankfully the TypeScript the exhaustiveness check in the default case keeps us type safe here */
|
|
48
|
+
switch (matchUnit) {
|
|
49
|
+
case "years":
|
|
50
|
+
case "year":
|
|
51
|
+
case "yrs":
|
|
52
|
+
case "yr":
|
|
53
|
+
case "y":
|
|
54
|
+
return n * y;
|
|
55
|
+
case "months":
|
|
56
|
+
case "month":
|
|
57
|
+
case "mo":
|
|
58
|
+
return n * mo;
|
|
59
|
+
case "weeks":
|
|
60
|
+
case "week":
|
|
61
|
+
case "w":
|
|
62
|
+
return n * w;
|
|
63
|
+
case "days":
|
|
64
|
+
case "day":
|
|
65
|
+
case "d":
|
|
66
|
+
return n * d;
|
|
67
|
+
case "hours":
|
|
68
|
+
case "hour":
|
|
69
|
+
case "hrs":
|
|
70
|
+
case "hr":
|
|
71
|
+
case "h":
|
|
72
|
+
return n * h;
|
|
73
|
+
case "minutes":
|
|
74
|
+
case "minute":
|
|
75
|
+
case "mins":
|
|
76
|
+
case "min":
|
|
77
|
+
case "m":
|
|
78
|
+
return n * m;
|
|
79
|
+
case "seconds":
|
|
80
|
+
case "second":
|
|
81
|
+
case "secs":
|
|
82
|
+
case "sec":
|
|
83
|
+
case "s":
|
|
84
|
+
return n * s;
|
|
85
|
+
case "milliseconds":
|
|
86
|
+
case "millisecond":
|
|
87
|
+
case "msecs":
|
|
88
|
+
case "msec":
|
|
89
|
+
case "ms":
|
|
90
|
+
return n;
|
|
91
|
+
default:
|
|
92
|
+
throw new Error(`Unknown unit "${matchUnit}" provided to ms.parse(). value=${JSON.stringify(str)}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Parse the given StringValue and return milliseconds.
|
|
97
|
+
*
|
|
98
|
+
* @param value - A typesafe StringValue to parse to milliseconds
|
|
99
|
+
* @returns The parsed value in milliseconds, or `NaN` if the string can't be
|
|
100
|
+
* parsed
|
|
101
|
+
*/
|
|
102
|
+
function parseStrict(value) {
|
|
103
|
+
return parse(value);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Short format for `ms`.
|
|
107
|
+
*/
|
|
108
|
+
function fmtShort(ms) {
|
|
109
|
+
const msAbs = Math.abs(ms);
|
|
110
|
+
if (msAbs >= y) {
|
|
111
|
+
return `${Math.round(ms / y)}y`;
|
|
112
|
+
}
|
|
113
|
+
if (msAbs >= mo) {
|
|
114
|
+
return `${Math.round(ms / mo)}mo`;
|
|
115
|
+
}
|
|
116
|
+
if (msAbs >= w) {
|
|
117
|
+
return `${Math.round(ms / w)}w`;
|
|
118
|
+
}
|
|
119
|
+
if (msAbs >= d) {
|
|
120
|
+
return `${Math.round(ms / d)}d`;
|
|
121
|
+
}
|
|
122
|
+
if (msAbs >= h) {
|
|
123
|
+
return `${Math.round(ms / h)}h`;
|
|
124
|
+
}
|
|
125
|
+
if (msAbs >= m) {
|
|
126
|
+
return `${Math.round(ms / m)}m`;
|
|
127
|
+
}
|
|
128
|
+
if (msAbs >= s) {
|
|
129
|
+
return `${Math.round(ms / s)}s`;
|
|
130
|
+
}
|
|
131
|
+
return `${ms}ms`;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Long format for `ms`.
|
|
135
|
+
*/
|
|
136
|
+
function fmtLong(ms) {
|
|
137
|
+
const msAbs = Math.abs(ms);
|
|
138
|
+
if (msAbs >= y) {
|
|
139
|
+
return plural(ms, msAbs, y, "year");
|
|
140
|
+
}
|
|
141
|
+
if (msAbs >= mo) {
|
|
142
|
+
return plural(ms, msAbs, mo, "month");
|
|
143
|
+
}
|
|
144
|
+
if (msAbs >= w) {
|
|
145
|
+
return plural(ms, msAbs, w, "week");
|
|
146
|
+
}
|
|
147
|
+
if (msAbs >= d) {
|
|
148
|
+
return plural(ms, msAbs, d, "day");
|
|
149
|
+
}
|
|
150
|
+
if (msAbs >= h) {
|
|
151
|
+
return plural(ms, msAbs, h, "hour");
|
|
152
|
+
}
|
|
153
|
+
if (msAbs >= m) {
|
|
154
|
+
return plural(ms, msAbs, m, "minute");
|
|
155
|
+
}
|
|
156
|
+
if (msAbs >= s) {
|
|
157
|
+
return plural(ms, msAbs, s, "second");
|
|
158
|
+
}
|
|
159
|
+
return `${ms} ms`;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Format the given integer as a string.
|
|
163
|
+
*
|
|
164
|
+
* @param ms - milliseconds
|
|
165
|
+
* @param options - Options for the conversion
|
|
166
|
+
* @returns The formatted string
|
|
167
|
+
*/
|
|
168
|
+
function format(ms, options) {
|
|
169
|
+
if (typeof ms !== "number" || !Number.isFinite(ms)) {
|
|
170
|
+
throw new Error("Value provided to ms.format() must be of type number.");
|
|
171
|
+
}
|
|
172
|
+
return options?.long ? fmtLong(ms) : fmtShort(ms);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Pluralization helper.
|
|
176
|
+
*/
|
|
177
|
+
function plural(ms, msAbs, n, name) {
|
|
178
|
+
const isPlural = msAbs >= n * 1.5;
|
|
179
|
+
return `${Math.round(ms / n)} ${name}${isPlural ? "s" : ""}`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
exports.default = ms;
|
|
183
|
+
exports.format = format;
|
|
184
|
+
exports.ms = ms;
|
|
185
|
+
exports.parse = parse;
|
|
186
|
+
exports.parseStrict = parseStrict;
|
package/lib/index.d.cts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
type Years = "years" | "year" | "yrs" | "yr" | "y";
|
|
2
|
+
type Months = "months" | "month" | "mo";
|
|
3
|
+
type Weeks = "weeks" | "week" | "w";
|
|
4
|
+
type Days = "days" | "day" | "d";
|
|
5
|
+
type Hours = "hours" | "hour" | "hrs" | "hr" | "h";
|
|
6
|
+
type Minutes = "minutes" | "minute" | "mins" | "min" | "m";
|
|
7
|
+
type Seconds = "seconds" | "second" | "secs" | "sec" | "s";
|
|
8
|
+
type Milliseconds = "milliseconds" | "millisecond" | "msecs" | "msec" | "ms";
|
|
9
|
+
type Unit = Years | Months | Weeks | Days | Hours | Minutes | Seconds | Milliseconds;
|
|
10
|
+
type UnitAnyCase = Capitalize<Unit> | Uppercase<Unit> | Unit;
|
|
11
|
+
type StringValue = `${number}` | `${number}${UnitAnyCase}` | `${number} ${UnitAnyCase}`;
|
|
12
|
+
interface Options {
|
|
13
|
+
/**
|
|
14
|
+
* Set to `true` to use verbose formatting. Defaults to `false`.
|
|
15
|
+
*/
|
|
16
|
+
long?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Parse or format the given value.
|
|
20
|
+
*
|
|
21
|
+
* @param value - The string or number to convert
|
|
22
|
+
* @param options - Options for the conversion
|
|
23
|
+
* @throws Error if `value` is not a non-empty string or a number
|
|
24
|
+
*/
|
|
25
|
+
declare function ms(value: StringValue, options?: Options): number;
|
|
26
|
+
declare function ms(value: number, options?: Options): string;
|
|
27
|
+
/**
|
|
28
|
+
* Parse the given string and return milliseconds.
|
|
29
|
+
*
|
|
30
|
+
* @param str - A string to parse to milliseconds
|
|
31
|
+
* @returns The parsed value in milliseconds, or `NaN` if the string can't be
|
|
32
|
+
* parsed
|
|
33
|
+
*/
|
|
34
|
+
declare function parse(str: string): number;
|
|
35
|
+
/**
|
|
36
|
+
* Parse the given StringValue and return milliseconds.
|
|
37
|
+
*
|
|
38
|
+
* @param value - A typesafe StringValue to parse to milliseconds
|
|
39
|
+
* @returns The parsed value in milliseconds, or `NaN` if the string can't be
|
|
40
|
+
* parsed
|
|
41
|
+
*/
|
|
42
|
+
declare function parseStrict(value: StringValue): number;
|
|
43
|
+
/**
|
|
44
|
+
* Format the given integer as a string.
|
|
45
|
+
*
|
|
46
|
+
* @param ms - milliseconds
|
|
47
|
+
* @param options - Options for the conversion
|
|
48
|
+
* @returns The formatted string
|
|
49
|
+
*/
|
|
50
|
+
declare function format(ms: number, options?: Options): string;
|
|
51
|
+
|
|
52
|
+
export { ms as default, format, ms, parse, parseStrict };
|
|
53
|
+
export type { StringValue };
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
type Years = "years" | "year" | "yrs" | "yr" | "y";
|
|
2
|
+
type Months = "months" | "month" | "mo";
|
|
3
|
+
type Weeks = "weeks" | "week" | "w";
|
|
4
|
+
type Days = "days" | "day" | "d";
|
|
5
|
+
type Hours = "hours" | "hour" | "hrs" | "hr" | "h";
|
|
6
|
+
type Minutes = "minutes" | "minute" | "mins" | "min" | "m";
|
|
7
|
+
type Seconds = "seconds" | "second" | "secs" | "sec" | "s";
|
|
8
|
+
type Milliseconds = "milliseconds" | "millisecond" | "msecs" | "msec" | "ms";
|
|
9
|
+
type Unit = Years | Months | Weeks | Days | Hours | Minutes | Seconds | Milliseconds;
|
|
10
|
+
type UnitAnyCase = Capitalize<Unit> | Uppercase<Unit> | Unit;
|
|
11
|
+
type StringValue = `${number}` | `${number}${UnitAnyCase}` | `${number} ${UnitAnyCase}`;
|
|
12
|
+
interface Options {
|
|
13
|
+
/**
|
|
14
|
+
* Set to `true` to use verbose formatting. Defaults to `false`.
|
|
15
|
+
*/
|
|
16
|
+
long?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Parse or format the given value.
|
|
20
|
+
*
|
|
21
|
+
* @param value - The string or number to convert
|
|
22
|
+
* @param options - Options for the conversion
|
|
23
|
+
* @throws Error if `value` is not a non-empty string or a number
|
|
24
|
+
*/
|
|
25
|
+
declare function ms(value: StringValue, options?: Options): number;
|
|
26
|
+
declare function ms(value: number, options?: Options): string;
|
|
27
|
+
/**
|
|
28
|
+
* Parse the given string and return milliseconds.
|
|
29
|
+
*
|
|
30
|
+
* @param str - A string to parse to milliseconds
|
|
31
|
+
* @returns The parsed value in milliseconds, or `NaN` if the string can't be
|
|
32
|
+
* parsed
|
|
33
|
+
*/
|
|
34
|
+
declare function parse(str: string): number;
|
|
35
|
+
/**
|
|
36
|
+
* Parse the given StringValue and return milliseconds.
|
|
37
|
+
*
|
|
38
|
+
* @param value - A typesafe StringValue to parse to milliseconds
|
|
39
|
+
* @returns The parsed value in milliseconds, or `NaN` if the string can't be
|
|
40
|
+
* parsed
|
|
41
|
+
*/
|
|
42
|
+
declare function parseStrict(value: StringValue): number;
|
|
43
|
+
/**
|
|
44
|
+
* Format the given integer as a string.
|
|
45
|
+
*
|
|
46
|
+
* @param ms - milliseconds
|
|
47
|
+
* @param options - Options for the conversion
|
|
48
|
+
* @returns The formatted string
|
|
49
|
+
*/
|
|
50
|
+
declare function format(ms: number, options?: Options): string;
|
|
51
|
+
|
|
52
|
+
export { ms as default, format, ms, parse, parseStrict };
|
|
53
|
+
export type { StringValue };
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @prexjs/ms
|
|
3
|
+
* Copyright(c) 2026 Sorabh <sorabh@backady.com>
|
|
4
|
+
* MIT Licensed
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const s = 1000;
|
|
8
|
+
const m = s * 60;
|
|
9
|
+
const h = m * 60;
|
|
10
|
+
const d = h * 24;
|
|
11
|
+
const w = d * 7;
|
|
12
|
+
const y = d * 365.25;
|
|
13
|
+
const mo = y / 12;
|
|
14
|
+
function ms(value, options) {
|
|
15
|
+
if (typeof value === "string") {
|
|
16
|
+
return parse(value);
|
|
17
|
+
}
|
|
18
|
+
else if (typeof value === "number") {
|
|
19
|
+
return format(value, options);
|
|
20
|
+
}
|
|
21
|
+
throw new Error(`Value provided to ms() must be a string or number. value=${JSON.stringify(value)}`);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parse the given string and return milliseconds.
|
|
25
|
+
*
|
|
26
|
+
* @param str - A string to parse to milliseconds
|
|
27
|
+
* @returns The parsed value in milliseconds, or `NaN` if the string can't be
|
|
28
|
+
* parsed
|
|
29
|
+
*/
|
|
30
|
+
function parse(str) {
|
|
31
|
+
if (typeof str !== "string" || str.length === 0 || str.length > 100) {
|
|
32
|
+
throw new Error(`Value provided to ms.parse() must be a string with length between 1 and 99. value=${JSON.stringify(str)}`);
|
|
33
|
+
}
|
|
34
|
+
const match = /^(?<value>-?\d*\.?\d+) *(?<unit>milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|months?|mo|years?|yrs?|y)?$/i.exec(str);
|
|
35
|
+
if (!match?.groups) {
|
|
36
|
+
return NaN;
|
|
37
|
+
}
|
|
38
|
+
// Named capture groups need to be manually typed today.
|
|
39
|
+
// https://github.com/microsoft/TypeScript/issues/32098
|
|
40
|
+
const { value, unit = "ms" } = match.groups;
|
|
41
|
+
const n = parseFloat(value);
|
|
42
|
+
const matchUnit = unit.toLowerCase();
|
|
43
|
+
/* istanbul ignore next - istanbul doesn't understand, but thankfully the TypeScript the exhaustiveness check in the default case keeps us type safe here */
|
|
44
|
+
switch (matchUnit) {
|
|
45
|
+
case "years":
|
|
46
|
+
case "year":
|
|
47
|
+
case "yrs":
|
|
48
|
+
case "yr":
|
|
49
|
+
case "y":
|
|
50
|
+
return n * y;
|
|
51
|
+
case "months":
|
|
52
|
+
case "month":
|
|
53
|
+
case "mo":
|
|
54
|
+
return n * mo;
|
|
55
|
+
case "weeks":
|
|
56
|
+
case "week":
|
|
57
|
+
case "w":
|
|
58
|
+
return n * w;
|
|
59
|
+
case "days":
|
|
60
|
+
case "day":
|
|
61
|
+
case "d":
|
|
62
|
+
return n * d;
|
|
63
|
+
case "hours":
|
|
64
|
+
case "hour":
|
|
65
|
+
case "hrs":
|
|
66
|
+
case "hr":
|
|
67
|
+
case "h":
|
|
68
|
+
return n * h;
|
|
69
|
+
case "minutes":
|
|
70
|
+
case "minute":
|
|
71
|
+
case "mins":
|
|
72
|
+
case "min":
|
|
73
|
+
case "m":
|
|
74
|
+
return n * m;
|
|
75
|
+
case "seconds":
|
|
76
|
+
case "second":
|
|
77
|
+
case "secs":
|
|
78
|
+
case "sec":
|
|
79
|
+
case "s":
|
|
80
|
+
return n * s;
|
|
81
|
+
case "milliseconds":
|
|
82
|
+
case "millisecond":
|
|
83
|
+
case "msecs":
|
|
84
|
+
case "msec":
|
|
85
|
+
case "ms":
|
|
86
|
+
return n;
|
|
87
|
+
default:
|
|
88
|
+
throw new Error(`Unknown unit "${matchUnit}" provided to ms.parse(). value=${JSON.stringify(str)}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Parse the given StringValue and return milliseconds.
|
|
93
|
+
*
|
|
94
|
+
* @param value - A typesafe StringValue to parse to milliseconds
|
|
95
|
+
* @returns The parsed value in milliseconds, or `NaN` if the string can't be
|
|
96
|
+
* parsed
|
|
97
|
+
*/
|
|
98
|
+
function parseStrict(value) {
|
|
99
|
+
return parse(value);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Short format for `ms`.
|
|
103
|
+
*/
|
|
104
|
+
function fmtShort(ms) {
|
|
105
|
+
const msAbs = Math.abs(ms);
|
|
106
|
+
if (msAbs >= y) {
|
|
107
|
+
return `${Math.round(ms / y)}y`;
|
|
108
|
+
}
|
|
109
|
+
if (msAbs >= mo) {
|
|
110
|
+
return `${Math.round(ms / mo)}mo`;
|
|
111
|
+
}
|
|
112
|
+
if (msAbs >= w) {
|
|
113
|
+
return `${Math.round(ms / w)}w`;
|
|
114
|
+
}
|
|
115
|
+
if (msAbs >= d) {
|
|
116
|
+
return `${Math.round(ms / d)}d`;
|
|
117
|
+
}
|
|
118
|
+
if (msAbs >= h) {
|
|
119
|
+
return `${Math.round(ms / h)}h`;
|
|
120
|
+
}
|
|
121
|
+
if (msAbs >= m) {
|
|
122
|
+
return `${Math.round(ms / m)}m`;
|
|
123
|
+
}
|
|
124
|
+
if (msAbs >= s) {
|
|
125
|
+
return `${Math.round(ms / s)}s`;
|
|
126
|
+
}
|
|
127
|
+
return `${ms}ms`;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Long format for `ms`.
|
|
131
|
+
*/
|
|
132
|
+
function fmtLong(ms) {
|
|
133
|
+
const msAbs = Math.abs(ms);
|
|
134
|
+
if (msAbs >= y) {
|
|
135
|
+
return plural(ms, msAbs, y, "year");
|
|
136
|
+
}
|
|
137
|
+
if (msAbs >= mo) {
|
|
138
|
+
return plural(ms, msAbs, mo, "month");
|
|
139
|
+
}
|
|
140
|
+
if (msAbs >= w) {
|
|
141
|
+
return plural(ms, msAbs, w, "week");
|
|
142
|
+
}
|
|
143
|
+
if (msAbs >= d) {
|
|
144
|
+
return plural(ms, msAbs, d, "day");
|
|
145
|
+
}
|
|
146
|
+
if (msAbs >= h) {
|
|
147
|
+
return plural(ms, msAbs, h, "hour");
|
|
148
|
+
}
|
|
149
|
+
if (msAbs >= m) {
|
|
150
|
+
return plural(ms, msAbs, m, "minute");
|
|
151
|
+
}
|
|
152
|
+
if (msAbs >= s) {
|
|
153
|
+
return plural(ms, msAbs, s, "second");
|
|
154
|
+
}
|
|
155
|
+
return `${ms} ms`;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Format the given integer as a string.
|
|
159
|
+
*
|
|
160
|
+
* @param ms - milliseconds
|
|
161
|
+
* @param options - Options for the conversion
|
|
162
|
+
* @returns The formatted string
|
|
163
|
+
*/
|
|
164
|
+
function format(ms, options) {
|
|
165
|
+
if (typeof ms !== "number" || !Number.isFinite(ms)) {
|
|
166
|
+
throw new Error("Value provided to ms.format() must be of type number.");
|
|
167
|
+
}
|
|
168
|
+
return options?.long ? fmtLong(ms) : fmtShort(ms);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Pluralization helper.
|
|
172
|
+
*/
|
|
173
|
+
function plural(ms, msAbs, n, name) {
|
|
174
|
+
const isPlural = msAbs >= n * 1.5;
|
|
175
|
+
return `${Math.round(ms / n)} ${name}${isPlural ? "s" : ""}`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { ms as default, format, ms, parse, parseStrict };
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prexjs/ms",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Lightweight TypeScript utility for converting time durations to milliseconds and formatting milliseconds to human-readable strings.",
|
|
5
|
+
"author": "Sorabh <sorabh@prexjs.com> (https://prexjs.com)",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"files": [
|
|
10
|
+
"lib",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"main": "./lib/index.cjs",
|
|
15
|
+
"module": "./lib/index.mjs",
|
|
16
|
+
"types": "./lib/index.d.cts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": {
|
|
20
|
+
"types": "./lib/index.d.mts",
|
|
21
|
+
"default": "./lib/index.mjs"
|
|
22
|
+
},
|
|
23
|
+
"require": {
|
|
24
|
+
"types": "./lib/index.d.cts",
|
|
25
|
+
"default": "./lib/index.cjs"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/prexjs/prex.js.git",
|
|
33
|
+
"directory": "packages/ms"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/prexjs/prex.js/tree/main/packages/ms#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/prexjs/prex.js/issues"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"ms",
|
|
44
|
+
"milliseconds",
|
|
45
|
+
"duration",
|
|
46
|
+
"time",
|
|
47
|
+
"parse",
|
|
48
|
+
"format",
|
|
49
|
+
"units",
|
|
50
|
+
"convert",
|
|
51
|
+
"timestamp",
|
|
52
|
+
"typescript"
|
|
53
|
+
]
|
|
54
|
+
}
|