@pvorona/duration 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/README.md +39 -44
- package/dist/index.js +148 -150
- package/dist/lib/duration.d.ts +42 -12
- package/dist/lib/duration.d.ts.map +1 -1
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @pvorona/duration
|
|
2
2
|
|
|
3
|
-
An immutable duration type with unit conversions, comparisons, and basic arithmetic.
|
|
3
|
+
An immutable ESM-only duration type with unit conversions, comparisons, and basic arithmetic.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -8,25 +8,28 @@ An immutable duration type with unit conversions, comparisons, and basic arithme
|
|
|
8
8
|
npm i @pvorona/duration
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
This package is ESM-only. Import it from ESM modules instead of `require(...)`.
|
|
12
|
+
|
|
11
13
|
## Usage
|
|
12
14
|
|
|
13
15
|
### Create and convert
|
|
14
16
|
|
|
15
17
|
```ts
|
|
16
|
-
import {
|
|
18
|
+
import { duration, TimeUnit } from '@pvorona/duration';
|
|
17
19
|
|
|
18
|
-
const d =
|
|
19
|
-
d.toSeconds(); //
|
|
20
|
-
d.
|
|
20
|
+
const d = duration(2, TimeUnit.Minute);
|
|
21
|
+
d.toSeconds(); // 120
|
|
22
|
+
d.toMilliseconds(); // 300_000
|
|
23
|
+
duration(250, TimeUnit.Millisecond).toSeconds(); // 0.25
|
|
21
24
|
```
|
|
22
25
|
|
|
23
26
|
### Compare
|
|
24
27
|
|
|
25
28
|
```ts
|
|
26
|
-
import {
|
|
29
|
+
import { milliseconds, seconds } from '@pvorona/duration';
|
|
27
30
|
|
|
28
31
|
const a = seconds(1);
|
|
29
|
-
const b =
|
|
32
|
+
const b = milliseconds(900);
|
|
30
33
|
|
|
31
34
|
a.greaterThan(b); // true
|
|
32
35
|
a.compare(b); // 1
|
|
@@ -35,10 +38,10 @@ a.compare(b); // 1
|
|
|
35
38
|
### Arithmetic
|
|
36
39
|
|
|
37
40
|
```ts
|
|
38
|
-
import { add,
|
|
41
|
+
import { add, milliseconds, seconds } from '@pvorona/duration';
|
|
39
42
|
|
|
40
|
-
const total = add(seconds(1),
|
|
41
|
-
total.
|
|
43
|
+
const total = add(seconds(1), milliseconds(500));
|
|
44
|
+
total.toMilliseconds(); // 1500
|
|
42
45
|
```
|
|
43
46
|
|
|
44
47
|
### Between dates
|
|
@@ -51,37 +54,29 @@ const elapsed = since(startedAt);
|
|
|
51
54
|
elapsed.toSeconds(); // ~2
|
|
52
55
|
```
|
|
53
56
|
|
|
54
|
-
##
|
|
55
|
-
|
|
56
|
-
### `const enum TimeUnit`
|
|
57
|
+
## Environment and semantics
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
Minute,
|
|
65
|
-
Hour,
|
|
66
|
-
Day,
|
|
67
|
-
Week,
|
|
68
|
-
Month,
|
|
69
|
-
Year,
|
|
70
|
-
}
|
|
71
|
-
```
|
|
59
|
+
- ESM-only package: use `import`, not `require(...)`.
|
|
60
|
+
- `Month` is treated as 30 days and `Year` as 365.25 days. These are approximations, not calendar-aware durations.
|
|
61
|
+
- Negative finite durations are valid. `between(...)`, `since(...)`, arithmetic, and comparisons can produce or operate on negative values.
|
|
62
|
+
- Constructors accept only finite numeric inputs. `infinite` is the only supported non-finite duration.
|
|
63
|
+
- `add(infinite, x)`, `subtract(infinite, finite)`, `multiply(infinite, positive finite)`, and `divide(infinite, positive finite)` return `infinite`.
|
|
64
|
+
- Invalid units, invalid dates, non-finite scalar inputs, divide-by-zero, `subtract(infinite, infinite)`, `subtract(finite, infinite)`, `multiply(infinite, 0)`, `multiply(infinite, negative)`, and `divide(infinite, negative)` throw `TypeError`.
|
|
72
65
|
|
|
73
|
-
|
|
66
|
+
## API
|
|
74
67
|
|
|
75
|
-
|
|
76
|
-
- `Year` is treated as **365.25 days**
|
|
68
|
+
### `TimeUnit`
|
|
77
69
|
|
|
78
|
-
|
|
70
|
+
Runtime unit constants supported by `duration(value, unit)` and `d.to(unit)`.
|
|
79
71
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
72
|
+
- `TimeUnit.Millisecond`
|
|
73
|
+
- `TimeUnit.Second`
|
|
74
|
+
- `TimeUnit.Minute`
|
|
75
|
+
- `TimeUnit.Hour`
|
|
76
|
+
- `TimeUnit.Day`
|
|
77
|
+
- `TimeUnit.Week`
|
|
78
|
+
- `TimeUnit.Month`
|
|
79
|
+
- `TimeUnit.Year`
|
|
85
80
|
|
|
86
81
|
### `type Duration`
|
|
87
82
|
|
|
@@ -89,14 +84,14 @@ An opaque, immutable duration value.
|
|
|
89
84
|
|
|
90
85
|
#### Properties
|
|
91
86
|
|
|
92
|
-
- **`isFinite: boolean`**: `true`
|
|
93
|
-
- **`isInfinite: boolean`**: `true` for `infinite`
|
|
87
|
+
- **`isFinite: boolean`**: `true` for finite durations
|
|
88
|
+
- **`isInfinite: boolean`**: `true` only for `infinite`
|
|
94
89
|
- **`isInstant: boolean`**: `true` for zero duration (`instant`)
|
|
95
90
|
|
|
96
91
|
#### Conversions
|
|
97
92
|
|
|
98
93
|
- **`to(unit: TimeUnit): number`**: convert to an arbitrary unit
|
|
99
|
-
- **`
|
|
94
|
+
- **`toMilliseconds(): number`**
|
|
100
95
|
- **`toSeconds(): number`**
|
|
101
96
|
- **`toMinutes(): number`**
|
|
102
97
|
- **`toHours(): number`**
|
|
@@ -114,17 +109,17 @@ An opaque, immutable duration value.
|
|
|
114
109
|
- **`greaterThanOrEqual(other: Duration): boolean`**
|
|
115
110
|
- **`compare(other: Duration): -1 | 0 | 1`**
|
|
116
111
|
|
|
117
|
-
Example
|
|
112
|
+
Example:
|
|
118
113
|
|
|
119
114
|
```ts
|
|
120
|
-
import {
|
|
115
|
+
import { milliseconds, seconds, type Duration } from '@pvorona/duration';
|
|
121
116
|
|
|
122
117
|
function isShort(d: Duration) {
|
|
123
118
|
return d.isFinite && d.toSeconds() < 5;
|
|
124
119
|
}
|
|
125
120
|
|
|
126
121
|
const a = seconds(1);
|
|
127
|
-
const b =
|
|
122
|
+
const b = milliseconds(900);
|
|
128
123
|
|
|
129
124
|
isShort(a); // true
|
|
130
125
|
a.greaterThan(b); // true
|
|
@@ -135,7 +130,7 @@ a.greaterThan(b); // true
|
|
|
135
130
|
#### Constructors
|
|
136
131
|
|
|
137
132
|
- `duration(value: number, unit: TimeUnit): Duration`
|
|
138
|
-
- `
|
|
133
|
+
- `milliseconds(value: number): Duration`
|
|
139
134
|
- `seconds(value: number): Duration`
|
|
140
135
|
- `minutes(value: number): Duration`
|
|
141
136
|
- `hours(value: number): Duration`
|
|
@@ -173,6 +168,6 @@ import { isDuration, seconds } from '@pvorona/duration';
|
|
|
173
168
|
|
|
174
169
|
const maybe: unknown = seconds(1);
|
|
175
170
|
if (isDuration(maybe)) {
|
|
176
|
-
maybe.
|
|
171
|
+
maybe.toMilliseconds(); // ok, narrowed
|
|
177
172
|
}
|
|
178
173
|
```
|
package/dist/index.js
CHANGED
|
@@ -1,205 +1,203 @@
|
|
|
1
|
-
import { isObject as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { isObject as h, hasOwnKey as m } from "@pvorona/assert";
|
|
2
|
+
const I = {
|
|
3
|
+
Millisecond: 0,
|
|
4
|
+
Second: 1,
|
|
5
|
+
Minute: 2,
|
|
6
|
+
Hour: 3,
|
|
7
|
+
Day: 4,
|
|
8
|
+
Week: 5,
|
|
9
|
+
Month: 6,
|
|
10
|
+
Year: 7
|
|
11
|
+
}, t = I, i = /* @__PURE__ */ Symbol("milliseconds"), T = new Set(Object.values(t)), l = {
|
|
12
|
+
[t.Millisecond]: 1,
|
|
13
|
+
[t.Second]: 1e3,
|
|
14
|
+
[t.Minute]: 60 * 1e3,
|
|
15
|
+
[t.Hour]: 3600 * 1e3,
|
|
8
16
|
/** Note: Does not account for leap second */
|
|
9
|
-
|
|
10
|
-
|
|
17
|
+
[t.Day]: 1440 * 60 * 1e3,
|
|
18
|
+
[t.Week]: 10080 * 60 * 1e3,
|
|
11
19
|
/** Note: Does not account for leap year */
|
|
12
|
-
|
|
20
|
+
[t.Month]: (
|
|
13
21
|
/* 30 days */
|
|
14
22
|
720 * 60 * 60 * 1e3
|
|
15
23
|
),
|
|
16
24
|
/** Note: Does not account for leap year */
|
|
17
|
-
|
|
25
|
+
[t.Year]: (
|
|
18
26
|
/* 365.25 days */
|
|
19
27
|
365.25 * 24 * 60 * 60 * 1e3
|
|
20
28
|
)
|
|
21
|
-
}
|
|
22
|
-
|
|
29
|
+
};
|
|
30
|
+
function d(n) {
|
|
31
|
+
if (T.has(n))
|
|
32
|
+
return n;
|
|
33
|
+
throw new TypeError("Expected a valid time unit.");
|
|
34
|
+
}
|
|
35
|
+
function c(n, e) {
|
|
36
|
+
if (Number.isFinite(n))
|
|
37
|
+
return n;
|
|
38
|
+
throw new TypeError(`Expected \`${e}\` to be finite.`);
|
|
39
|
+
}
|
|
40
|
+
function f(n, e) {
|
|
41
|
+
const r = n.getTime();
|
|
42
|
+
if (!Number.isNaN(r))
|
|
43
|
+
return r;
|
|
44
|
+
throw new TypeError(`Expected \`${e}\` to be a valid date.`);
|
|
45
|
+
}
|
|
46
|
+
function y(n, e) {
|
|
47
|
+
if (Number.isNaN(n) || n === Number.NEGATIVE_INFINITY)
|
|
48
|
+
throw new TypeError("Expected duration milliseconds to be finite or `Infinity`.");
|
|
49
|
+
if (n !== 1 / 0 || e?.allowInfinite)
|
|
50
|
+
return n;
|
|
51
|
+
throw new TypeError("Expected duration milliseconds to be finite.");
|
|
52
|
+
}
|
|
53
|
+
const w = {
|
|
54
|
+
[i]: 0,
|
|
23
55
|
isFinite: !1,
|
|
24
56
|
isInfinite: !1,
|
|
25
57
|
isInstant: !1,
|
|
26
|
-
to(
|
|
27
|
-
return this[
|
|
58
|
+
to(n) {
|
|
59
|
+
return this[i] / l[d(n)];
|
|
28
60
|
},
|
|
29
|
-
|
|
30
|
-
return this.to(
|
|
31
|
-
0
|
|
32
|
-
/* MilliSecond */
|
|
33
|
-
);
|
|
61
|
+
toMilliseconds() {
|
|
62
|
+
return this.to(t.Millisecond);
|
|
34
63
|
},
|
|
35
64
|
toSeconds() {
|
|
36
|
-
return this.to(
|
|
37
|
-
1
|
|
38
|
-
/* Second */
|
|
39
|
-
);
|
|
65
|
+
return this.to(t.Second);
|
|
40
66
|
},
|
|
41
67
|
toMinutes() {
|
|
42
|
-
return this.to(
|
|
43
|
-
2
|
|
44
|
-
/* Minute */
|
|
45
|
-
);
|
|
68
|
+
return this.to(t.Minute);
|
|
46
69
|
},
|
|
47
70
|
toHours() {
|
|
48
|
-
return this.to(
|
|
49
|
-
3
|
|
50
|
-
/* Hour */
|
|
51
|
-
);
|
|
71
|
+
return this.to(t.Hour);
|
|
52
72
|
},
|
|
53
73
|
toDays() {
|
|
54
|
-
return this.to(
|
|
55
|
-
4
|
|
56
|
-
/* Day */
|
|
57
|
-
);
|
|
74
|
+
return this.to(t.Day);
|
|
58
75
|
},
|
|
59
76
|
toWeeks() {
|
|
60
|
-
return this.to(
|
|
61
|
-
5
|
|
62
|
-
/* Week */
|
|
63
|
-
);
|
|
77
|
+
return this.to(t.Week);
|
|
64
78
|
},
|
|
65
79
|
toMonths() {
|
|
66
|
-
return this.to(
|
|
67
|
-
6
|
|
68
|
-
/* Month */
|
|
69
|
-
);
|
|
80
|
+
return this.to(t.Month);
|
|
70
81
|
},
|
|
71
82
|
toYears() {
|
|
72
|
-
return this.to(
|
|
73
|
-
7
|
|
74
|
-
/* Year */
|
|
75
|
-
);
|
|
83
|
+
return this.to(t.Year);
|
|
76
84
|
},
|
|
77
|
-
equals(
|
|
78
|
-
return this[
|
|
85
|
+
equals(n) {
|
|
86
|
+
return this[i] === n[i];
|
|
79
87
|
},
|
|
80
|
-
lessThan(
|
|
81
|
-
return this[
|
|
88
|
+
lessThan(n) {
|
|
89
|
+
return this[i] < n[i];
|
|
82
90
|
},
|
|
83
|
-
lessThanOrEqual(
|
|
84
|
-
return this[
|
|
91
|
+
lessThanOrEqual(n) {
|
|
92
|
+
return this[i] <= n[i];
|
|
85
93
|
},
|
|
86
|
-
greaterThan(
|
|
87
|
-
return this[
|
|
94
|
+
greaterThan(n) {
|
|
95
|
+
return this[i] > n[i];
|
|
88
96
|
},
|
|
89
|
-
greaterThanOrEqual(
|
|
90
|
-
return this[
|
|
97
|
+
greaterThanOrEqual(n) {
|
|
98
|
+
return this[i] >= n[i];
|
|
91
99
|
},
|
|
92
|
-
compare(
|
|
93
|
-
return this.lessThan(
|
|
100
|
+
compare(n) {
|
|
101
|
+
return this.lessThan(n) ? -1 : this.greaterThan(n) ? 1 : 0;
|
|
94
102
|
}
|
|
95
103
|
};
|
|
96
|
-
function
|
|
97
|
-
const
|
|
98
|
-
return
|
|
99
|
-
}
|
|
100
|
-
function e(t, r) {
|
|
101
|
-
return f(t, r);
|
|
102
|
-
}
|
|
103
|
-
function u(t) {
|
|
104
|
-
return e(
|
|
105
|
-
t,
|
|
106
|
-
0
|
|
107
|
-
/* MilliSecond */
|
|
108
|
-
);
|
|
104
|
+
function u(n, e) {
|
|
105
|
+
const r = y(n, e), o = Object.create(w);
|
|
106
|
+
return o[i] = r, o.isFinite = Number.isFinite(r), o.isInfinite = r === 1 / 0, o.isInstant = r === 0, Object.freeze(o);
|
|
109
107
|
}
|
|
110
|
-
function
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
1
|
|
114
|
-
/* Second */
|
|
115
|
-
);
|
|
108
|
+
function s(n, e) {
|
|
109
|
+
const r = c(n, "value"), o = d(e);
|
|
110
|
+
return u(r * l[o]);
|
|
116
111
|
}
|
|
117
|
-
function
|
|
118
|
-
return
|
|
119
|
-
t,
|
|
120
|
-
2
|
|
121
|
-
/* Minute */
|
|
122
|
-
);
|
|
112
|
+
function E(n) {
|
|
113
|
+
return s(n, t.Millisecond);
|
|
123
114
|
}
|
|
124
|
-
function
|
|
125
|
-
return
|
|
126
|
-
t,
|
|
127
|
-
3
|
|
128
|
-
/* Hour */
|
|
129
|
-
);
|
|
115
|
+
function M(n) {
|
|
116
|
+
return s(n, t.Second);
|
|
130
117
|
}
|
|
131
|
-
function
|
|
132
|
-
return
|
|
133
|
-
t,
|
|
134
|
-
4
|
|
135
|
-
/* Day */
|
|
136
|
-
);
|
|
118
|
+
function N(n) {
|
|
119
|
+
return s(n, t.Minute);
|
|
137
120
|
}
|
|
138
|
-
function
|
|
139
|
-
return
|
|
140
|
-
t,
|
|
141
|
-
5
|
|
142
|
-
/* Week */
|
|
143
|
-
);
|
|
121
|
+
function S(n) {
|
|
122
|
+
return s(n, t.Hour);
|
|
144
123
|
}
|
|
145
|
-
function D(
|
|
146
|
-
return
|
|
147
|
-
t,
|
|
148
|
-
6
|
|
149
|
-
/* Month */
|
|
150
|
-
);
|
|
124
|
+
function D(n) {
|
|
125
|
+
return s(n, t.Day);
|
|
151
126
|
}
|
|
152
|
-
function O(
|
|
153
|
-
return
|
|
154
|
-
t,
|
|
155
|
-
7
|
|
156
|
-
/* Year */
|
|
157
|
-
);
|
|
127
|
+
function O(n) {
|
|
128
|
+
return s(n, t.Week);
|
|
158
129
|
}
|
|
159
|
-
function
|
|
160
|
-
return
|
|
130
|
+
function v(n) {
|
|
131
|
+
return s(n, t.Month);
|
|
161
132
|
}
|
|
162
|
-
function
|
|
163
|
-
return
|
|
133
|
+
function U(n) {
|
|
134
|
+
return s(n, t.Year);
|
|
164
135
|
}
|
|
165
|
-
function
|
|
166
|
-
|
|
136
|
+
function p(n, e) {
|
|
137
|
+
const r = f(n, "start"), o = f(e, "end");
|
|
138
|
+
return u(o - r);
|
|
167
139
|
}
|
|
168
|
-
function
|
|
169
|
-
return
|
|
140
|
+
function g(n) {
|
|
141
|
+
return p(n, /* @__PURE__ */ new Date());
|
|
170
142
|
}
|
|
171
|
-
function
|
|
172
|
-
return u(
|
|
143
|
+
function z(n, e) {
|
|
144
|
+
return n.isInfinite || e.isInfinite ? a : u(n[i] + e[i]);
|
|
145
|
+
}
|
|
146
|
+
function _(n, e) {
|
|
147
|
+
if (n.isInfinite && e.isInfinite)
|
|
148
|
+
throw new TypeError("Cannot subtract `infinite` from `infinite`.");
|
|
149
|
+
if (n.isInfinite)
|
|
150
|
+
return a;
|
|
151
|
+
if (e.isInfinite)
|
|
152
|
+
throw new TypeError("Cannot subtract `infinite` from a finite duration.");
|
|
153
|
+
return u(n[i] - e[i]);
|
|
154
|
+
}
|
|
155
|
+
function k(n, e) {
|
|
156
|
+
const r = c(e, "multiplier");
|
|
157
|
+
if (!n.isInfinite)
|
|
158
|
+
return u(n[i] * r);
|
|
159
|
+
if (r > 0)
|
|
160
|
+
return a;
|
|
161
|
+
throw new TypeError(
|
|
162
|
+
"Cannot multiply `infinite` by zero or a negative number."
|
|
163
|
+
);
|
|
173
164
|
}
|
|
174
|
-
function
|
|
175
|
-
|
|
165
|
+
function x(n, e) {
|
|
166
|
+
const r = c(e, "divisor");
|
|
167
|
+
if (r === 0)
|
|
168
|
+
throw new TypeError("Cannot divide by zero.");
|
|
169
|
+
if (!n.isInfinite)
|
|
170
|
+
return u(n[i] / r);
|
|
171
|
+
if (r > 0)
|
|
172
|
+
return a;
|
|
173
|
+
throw new TypeError("Cannot divide `infinite` by a negative number.");
|
|
176
174
|
}
|
|
177
|
-
const
|
|
178
|
-
function
|
|
179
|
-
return
|
|
175
|
+
const a = u(1 / 0, { allowInfinite: !0 }), C = E(0);
|
|
176
|
+
function F(n) {
|
|
177
|
+
return h(n) && m(n, i);
|
|
180
178
|
}
|
|
181
|
-
function
|
|
182
|
-
return
|
|
179
|
+
function Y(n, e) {
|
|
180
|
+
return n[i] === e[i];
|
|
183
181
|
}
|
|
184
182
|
export {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
183
|
+
t as TimeUnit,
|
|
184
|
+
z as add,
|
|
185
|
+
p as between,
|
|
186
|
+
D as days,
|
|
187
|
+
x as divide,
|
|
188
|
+
s as duration,
|
|
191
189
|
S as hours,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
190
|
+
a as infinite,
|
|
191
|
+
C as instant,
|
|
192
|
+
F as isDuration,
|
|
193
|
+
Y as isEqual,
|
|
194
|
+
E as milliseconds,
|
|
195
|
+
N as minutes,
|
|
196
|
+
v as months,
|
|
199
197
|
k as multiply,
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
198
|
+
M as seconds,
|
|
199
|
+
g as since,
|
|
200
|
+
_ as subtract,
|
|
201
|
+
O as weeks,
|
|
202
|
+
U as years
|
|
205
203
|
};
|
package/dist/lib/duration.d.ts
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Second
|
|
4
|
-
Minute
|
|
5
|
-
Hour
|
|
6
|
-
Day
|
|
7
|
-
Week
|
|
8
|
-
Month
|
|
9
|
-
Year
|
|
10
|
-
}
|
|
1
|
+
declare const TIME_UNIT_VALUES: {
|
|
2
|
+
readonly Millisecond: 0;
|
|
3
|
+
readonly Second: 1;
|
|
4
|
+
readonly Minute: 2;
|
|
5
|
+
readonly Hour: 3;
|
|
6
|
+
readonly Day: 4;
|
|
7
|
+
readonly Week: 5;
|
|
8
|
+
readonly Month: 6;
|
|
9
|
+
readonly Year: 7;
|
|
10
|
+
};
|
|
11
|
+
/** Supported runtime unit values used by `TimeUnit`. */
|
|
12
|
+
export type TimeUnit = (typeof TIME_UNIT_VALUES)[keyof typeof TIME_UNIT_VALUES];
|
|
13
|
+
/**
|
|
14
|
+
* Runtime time-unit constants accepted by `duration(value, unit)` and `d.to(unit)`.
|
|
15
|
+
*
|
|
16
|
+
* `Month` uses a 30-day approximation and `Year` uses a 365.25-day approximation.
|
|
17
|
+
*/
|
|
18
|
+
export declare const TimeUnit: {
|
|
19
|
+
readonly [Key in keyof typeof TIME_UNIT_VALUES]: TimeUnit;
|
|
20
|
+
};
|
|
11
21
|
declare const millisecondsTag: unique symbol;
|
|
22
|
+
/** Immutable duration value with conversions, comparisons, and state flags. */
|
|
12
23
|
export type Duration = {
|
|
13
24
|
readonly [millisecondsTag]: number;
|
|
14
25
|
readonly isFinite: boolean;
|
|
15
26
|
readonly isInfinite: boolean;
|
|
16
27
|
readonly isInstant: boolean;
|
|
17
28
|
to(unit: TimeUnit): number;
|
|
18
|
-
|
|
29
|
+
toMilliseconds(): number;
|
|
19
30
|
toSeconds(): number;
|
|
20
31
|
toMinutes(): number;
|
|
21
32
|
toHours(): number;
|
|
@@ -30,24 +41,43 @@ export type Duration = {
|
|
|
30
41
|
greaterThanOrEqual(other: Duration): boolean;
|
|
31
42
|
compare(other: Duration): -1 | 0 | 1;
|
|
32
43
|
};
|
|
44
|
+
/** Creates a duration from a finite numeric value in the provided unit. */
|
|
33
45
|
export declare function duration(value: number, unit: TimeUnit): Duration;
|
|
34
|
-
|
|
46
|
+
/** Creates a duration from a finite millisecond value. */
|
|
47
|
+
export declare function milliseconds(value: number): Duration;
|
|
48
|
+
/** Creates a duration from a finite second value. */
|
|
35
49
|
export declare function seconds(value: number): Duration;
|
|
50
|
+
/** Creates a duration from a finite minute value. */
|
|
36
51
|
export declare function minutes(value: number): Duration;
|
|
52
|
+
/** Creates a duration from a finite hour value. */
|
|
37
53
|
export declare function hours(value: number): Duration;
|
|
54
|
+
/** Creates a duration from a finite day value. */
|
|
38
55
|
export declare function days(value: number): Duration;
|
|
56
|
+
/** Creates a duration from a finite week value. */
|
|
39
57
|
export declare function weeks(value: number): Duration;
|
|
58
|
+
/** Creates a duration from a finite approximate month value (30 days each). */
|
|
40
59
|
export declare function months(value: number): Duration;
|
|
60
|
+
/** Creates a duration from a finite approximate year value (365.25 days each). */
|
|
41
61
|
export declare function years(value: number): Duration;
|
|
62
|
+
/** Returns the duration between two valid dates. The result may be negative. */
|
|
42
63
|
export declare function between(start: Date, end: Date): Duration;
|
|
64
|
+
/** Returns the duration since a valid start date. The result may be negative. */
|
|
43
65
|
export declare function since(start: Date): Duration;
|
|
66
|
+
/** Adds two durations and preserves the explicit `infinite` sentinel. */
|
|
44
67
|
export declare function add(a: Duration, b: Duration): Duration;
|
|
68
|
+
/** Subtracts one duration from another and rejects undefined `infinite` cases. */
|
|
45
69
|
export declare function subtract(a: Duration, b: Duration): Duration;
|
|
70
|
+
/** Multiplies a duration by a finite scalar. */
|
|
46
71
|
export declare function multiply(a: Duration, b: number): Duration;
|
|
72
|
+
/** Divides a duration by a finite, non-zero scalar. */
|
|
47
73
|
export declare function divide(a: Duration, b: number): Duration;
|
|
74
|
+
/** The explicit non-finite duration sentinel supported by this package. */
|
|
48
75
|
export declare const infinite: Duration;
|
|
76
|
+
/** The zero-length duration constant. */
|
|
49
77
|
export declare const instant: Duration;
|
|
78
|
+
/** Returns `true` when the value is a duration created by this package. */
|
|
50
79
|
export declare function isDuration(value: unknown): value is Duration;
|
|
80
|
+
/** Compares two durations by their normalized millisecond payload. */
|
|
51
81
|
export declare function isEqual(a: Duration, b: Duration): boolean;
|
|
52
82
|
export {};
|
|
53
83
|
//# sourceMappingURL=duration.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"duration.d.ts","sourceRoot":"","sources":["../../src/lib/duration.ts"],"names":[],"mappings":"AAMA,
|
|
1
|
+
{"version":3,"file":"duration.d.ts","sourceRoot":"","sources":["../../src/lib/duration.ts"],"names":[],"mappings":"AAMA,QAAA,MAAM,gBAAgB;;;;;;;;;CASZ,CAAC;AAEX,wDAAwD;AACxD,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,OAAO,gBAAgB,CAAC,CAAC;AAEhF;;;;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F,yCAAyC;AACzC,eAAO,MAAM,OAAO,EAAE,QAA0B,CAAC;AAEjD,2EAA2E;AAC3E,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAE5D;AAED,sEAAsE;AACtE,wBAAgB,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,GAAG,OAAO,CAEzD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pvorona/duration",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
"exports": {
|
|
10
10
|
"./package.json": "./package.json",
|
|
11
11
|
".": {
|
|
12
|
-
"@pvorona/source": "./src/index.ts",
|
|
13
12
|
"types": "./dist/index.d.ts",
|
|
14
13
|
"import": "./dist/index.js",
|
|
15
14
|
"default": "./dist/index.js"
|
|
@@ -23,6 +22,6 @@
|
|
|
23
22
|
"access": "public"
|
|
24
23
|
},
|
|
25
24
|
"dependencies": {
|
|
26
|
-
"@pvorona/assert": "~0.0
|
|
25
|
+
"@pvorona/assert": "~0.1.0"
|
|
27
26
|
}
|
|
28
27
|
}
|