@verifyhash/human-time 0.1.0 → 0.1.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 +1 -1
- package/README.md +11 -1
- package/index.d.ts +59 -0
- package/package.json +4 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -13,6 +13,16 @@ No install step, no `node_modules`, no network, no servers. One file:
|
|
|
13
13
|
> `Intl.RelativeTimeFormat` or a full i18n library. This module optimizes for
|
|
14
14
|
> being small, dependency-free, and predictable.
|
|
15
15
|
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @verifyhash/human-time
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Published as [`@verifyhash/human-time`](https://www.npmjs.com/package/@verifyhash/human-time);
|
|
23
|
+
source lives in the [verifyhash/libs](https://github.com/verifyhash/libs) monorepo.
|
|
24
|
+
Zero runtime dependencies — you can also vendor the folder directly.
|
|
25
|
+
|
|
16
26
|
## Who it's for
|
|
17
27
|
|
|
18
28
|
Anyone rendering timestamps or durations in a CLI, log line, dashboard, or
|
|
@@ -27,7 +37,7 @@ approximate human phrasing and compact `ms`-style strings.
|
|
|
27
37
|
Copy the folder in, or `require` it directly — there is nothing to build.
|
|
28
38
|
|
|
29
39
|
```js
|
|
30
|
-
const { relative, format, parse } = require('human-time'); // main = src/index.js
|
|
40
|
+
const { relative, format, parse } = require('@verifyhash/human-time'); // main = src/index.js
|
|
31
41
|
```
|
|
32
42
|
|
|
33
43
|
## API
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type declarations for @verifyhash/human-time — zero-dependency
|
|
3
|
+
* human-readable relative time ("5 minutes ago", "in 3 days") plus compact
|
|
4
|
+
* ms-style duration format/parse ("2h30m" <-> milliseconds).
|
|
5
|
+
*
|
|
6
|
+
* Output is ENGLISH-ONLY by design (use Intl.RelativeTimeFormat for i18n).
|
|
7
|
+
* All functions are pure and throw on invalid input rather than silently
|
|
8
|
+
* returning 0.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** A moment in time: a JS Date or epoch milliseconds. */
|
|
12
|
+
export type TimeInput = Date | number;
|
|
13
|
+
|
|
14
|
+
/** The unit constants exposed as `units` (all in milliseconds). */
|
|
15
|
+
export interface TimeUnits {
|
|
16
|
+
/** 1000 ms. */
|
|
17
|
+
SECOND: number;
|
|
18
|
+
/** 60 seconds. */
|
|
19
|
+
MINUTE: number;
|
|
20
|
+
/** 60 minutes. */
|
|
21
|
+
HOUR: number;
|
|
22
|
+
/** 24 hours. */
|
|
23
|
+
DAY: number;
|
|
24
|
+
/** 7 days. */
|
|
25
|
+
WEEK: number;
|
|
26
|
+
/** Mean Gregorian month (~30.44 days), rounded to whole ms. */
|
|
27
|
+
MONTH: number;
|
|
28
|
+
/** Mean Gregorian year (365.2425 days), rounded to whole ms. */
|
|
29
|
+
YEAR: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* English relative phrase describing `from` as seen from the reference `to`
|
|
34
|
+
* (default: now). Past-tense when `from` is earlier ("5 minutes ago"),
|
|
35
|
+
* future-tense when later ("in 5 minutes"); sub-second gaps collapse to
|
|
36
|
+
* "just now". Months/years use mean calendar lengths — the phrasing is
|
|
37
|
+
* approximate by nature. Throws TypeError on an invalid Date or non-finite
|
|
38
|
+
* number.
|
|
39
|
+
*/
|
|
40
|
+
export function relative(from: TimeInput, to?: TimeInput): string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Compact duration string from milliseconds, e.g. 9000000 -> "2h30m",
|
|
44
|
+
* 45000 -> "45s". Emits every non-zero component from days down to ms so the
|
|
45
|
+
* result round-trips exactly through parse(); zero yields "0ms"; negative
|
|
46
|
+
* input is prefixed with "-"; fractional input is rounded to the nearest ms.
|
|
47
|
+
*/
|
|
48
|
+
export function format(ms: number): string;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Milliseconds from a compact duration string — the inverse of format().
|
|
52
|
+
* Case-insensitive and tolerant of internal spacing ("2h30m", "2H 30M");
|
|
53
|
+
* understands d, h, m, s and ms; a leading "-" (or "+") sets the sign.
|
|
54
|
+
* Throws on empty/garbage input or unknown units.
|
|
55
|
+
*/
|
|
56
|
+
export function parse(str: string): number;
|
|
57
|
+
|
|
58
|
+
/** The module's unit constants (all in milliseconds), exposed for callers. */
|
|
59
|
+
export const units: TimeUnits;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verifyhash/human-time",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Zero-dependency human-readable relative time ('5 minutes ago', 'in 3 days') plus compact ms-style duration format/parse ('2h30m' <-> ms) for Node.js. English-only.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"relative-time",
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"zero-dependency"
|
|
16
16
|
],
|
|
17
17
|
"main": "src/index.js",
|
|
18
|
+
"types": "index.d.ts",
|
|
18
19
|
"scripts": {
|
|
19
20
|
"test": "node test/index.test.js"
|
|
20
21
|
},
|
|
@@ -22,7 +23,8 @@
|
|
|
22
23
|
"files": [
|
|
23
24
|
"src/index.js",
|
|
24
25
|
"README.md",
|
|
25
|
-
"LICENSE"
|
|
26
|
+
"LICENSE",
|
|
27
|
+
"index.d.ts"
|
|
26
28
|
],
|
|
27
29
|
"publishConfig": {
|
|
28
30
|
"access": "public"
|