@pvorona/duration 0.0.3 → 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 +66 -110
- package/dist/index.js +165 -150
- package/dist/lib/duration.d.ts +62 -34
- 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
|
-
const a =
|
|
29
|
-
const b =
|
|
31
|
+
const a = seconds(1);
|
|
32
|
+
const b = milliseconds(900);
|
|
30
33
|
|
|
31
34
|
a.greaterThan(b); // true
|
|
32
35
|
a.compare(b); // 1
|
|
@@ -35,53 +38,45 @@ a.compare(b); // 1
|
|
|
35
38
|
### Arithmetic
|
|
36
39
|
|
|
37
40
|
```ts
|
|
38
|
-
import {
|
|
41
|
+
import { add, milliseconds, seconds } from '@pvorona/duration';
|
|
39
42
|
|
|
40
|
-
const total =
|
|
41
|
-
total.
|
|
43
|
+
const total = add(seconds(1), milliseconds(500));
|
|
44
|
+
total.toMilliseconds(); // 1500
|
|
42
45
|
```
|
|
43
46
|
|
|
44
47
|
### Between dates
|
|
45
48
|
|
|
46
49
|
```ts
|
|
47
|
-
import {
|
|
50
|
+
import { since } from '@pvorona/duration';
|
|
48
51
|
|
|
49
52
|
const startedAt = new Date(Date.now() - 2_000);
|
|
50
|
-
const elapsed =
|
|
53
|
+
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 `
|
|
94
|
-
- **`isInstant: boolean`**: `true` for zero duration (`
|
|
87
|
+
- **`isFinite: boolean`**: `true` for finite durations
|
|
88
|
+
- **`isInfinite: boolean`**: `true` only for `infinite`
|
|
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,104 +109,65 @@ 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 type
|
|
121
|
-
import { Duration as DurationNS } from '@pvorona/duration';
|
|
115
|
+
import { milliseconds, seconds, type Duration } from '@pvorona/duration';
|
|
122
116
|
|
|
123
117
|
function isShort(d: Duration) {
|
|
124
118
|
return d.isFinite && d.toSeconds() < 5;
|
|
125
119
|
}
|
|
126
120
|
|
|
127
|
-
const a =
|
|
128
|
-
const b =
|
|
121
|
+
const a = seconds(1);
|
|
122
|
+
const b = milliseconds(900);
|
|
129
123
|
|
|
130
124
|
isShort(a); // true
|
|
131
125
|
a.greaterThan(b); // true
|
|
132
126
|
```
|
|
133
127
|
|
|
134
|
-
###
|
|
135
|
-
|
|
136
|
-
Namespace-style factory + utilities.
|
|
128
|
+
### Function API
|
|
137
129
|
|
|
138
130
|
#### Constructors
|
|
139
131
|
|
|
140
|
-
-
|
|
141
|
-
-
|
|
142
|
-
-
|
|
143
|
-
-
|
|
144
|
-
-
|
|
145
|
-
-
|
|
146
|
-
-
|
|
147
|
-
-
|
|
148
|
-
-
|
|
149
|
-
|
|
150
|
-
Example:
|
|
151
|
-
|
|
152
|
-
```ts
|
|
153
|
-
import { Duration } from '@pvorona/duration';
|
|
154
|
-
|
|
155
|
-
Duration.ofHours(1).toMinutes(); // 60
|
|
156
|
-
Duration.ofWeeks(2).toDays(); // 14
|
|
157
|
-
```
|
|
132
|
+
- `duration(value: number, unit: TimeUnit): Duration`
|
|
133
|
+
- `milliseconds(value: number): Duration`
|
|
134
|
+
- `seconds(value: number): Duration`
|
|
135
|
+
- `minutes(value: number): Duration`
|
|
136
|
+
- `hours(value: number): Duration`
|
|
137
|
+
- `days(value: number): Duration`
|
|
138
|
+
- `weeks(value: number): Duration`
|
|
139
|
+
- `months(value: number): Duration`
|
|
140
|
+
- `years(value: number): Duration`
|
|
158
141
|
|
|
159
142
|
#### Date helpers
|
|
160
143
|
|
|
161
|
-
-
|
|
162
|
-
-
|
|
163
|
-
|
|
164
|
-
Example:
|
|
165
|
-
|
|
166
|
-
```ts
|
|
167
|
-
import { Duration } from '@pvorona/duration';
|
|
168
|
-
|
|
169
|
-
const d = Duration.between(new Date(0), new Date(1_000));
|
|
170
|
-
d.toSeconds(); // 1
|
|
171
|
-
```
|
|
144
|
+
- `between(start: Date, end: Date): Duration`
|
|
145
|
+
- `since(start: Date): Duration`
|
|
172
146
|
|
|
173
147
|
#### Arithmetic helpers
|
|
174
148
|
|
|
175
|
-
-
|
|
176
|
-
-
|
|
177
|
-
-
|
|
178
|
-
-
|
|
179
|
-
|
|
180
|
-
Example:
|
|
181
|
-
|
|
182
|
-
```ts
|
|
183
|
-
import { Duration } from '@pvorona/duration';
|
|
184
|
-
|
|
185
|
-
const d = Duration.multiply(Duration.ofSeconds(2), 3);
|
|
186
|
-
d.toSeconds(); // 6
|
|
187
|
-
```
|
|
149
|
+
- `add(a: Duration, b: Duration): Duration`
|
|
150
|
+
- `subtract(a: Duration, b: Duration): Duration`
|
|
151
|
+
- `multiply(a: Duration, b: number): Duration`
|
|
152
|
+
- `divide(a: Duration, b: number): Duration`
|
|
188
153
|
|
|
189
154
|
#### Constants
|
|
190
155
|
|
|
191
|
-
-
|
|
192
|
-
-
|
|
193
|
-
|
|
194
|
-
Example:
|
|
195
|
-
|
|
196
|
-
```ts
|
|
197
|
-
import { Duration } from '@pvorona/duration';
|
|
198
|
-
|
|
199
|
-
Duration.ofInstant.isInstant; // true
|
|
200
|
-
Duration.ofInfinite.isInfinite; // true
|
|
201
|
-
```
|
|
156
|
+
- `instant: Duration`
|
|
157
|
+
- `infinite: Duration`
|
|
202
158
|
|
|
203
159
|
#### Guards / equality
|
|
204
160
|
|
|
205
|
-
-
|
|
206
|
-
-
|
|
161
|
+
- `isDuration(value: unknown): value is Duration`
|
|
162
|
+
- `isEqual(a: Duration, b: Duration): boolean`
|
|
207
163
|
|
|
208
164
|
Example:
|
|
209
165
|
|
|
210
166
|
```ts
|
|
211
|
-
import {
|
|
167
|
+
import { isDuration, seconds } from '@pvorona/duration';
|
|
212
168
|
|
|
213
|
-
const maybe: unknown =
|
|
214
|
-
if (
|
|
215
|
-
maybe.
|
|
169
|
+
const maybe: unknown = seconds(1);
|
|
170
|
+
if (isDuration(maybe)) {
|
|
171
|
+
maybe.toMilliseconds(); // ok, narrowed
|
|
216
172
|
}
|
|
217
173
|
```
|
package/dist/index.js
CHANGED
|
@@ -1,188 +1,203 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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,
|
|
10
16
|
/** Note: Does not account for leap second */
|
|
11
|
-
|
|
12
|
-
|
|
17
|
+
[t.Day]: 1440 * 60 * 1e3,
|
|
18
|
+
[t.Week]: 10080 * 60 * 1e3,
|
|
13
19
|
/** Note: Does not account for leap year */
|
|
14
|
-
|
|
20
|
+
[t.Month]: (
|
|
15
21
|
/* 30 days */
|
|
16
22
|
720 * 60 * 60 * 1e3
|
|
17
23
|
),
|
|
18
24
|
/** Note: Does not account for leap year */
|
|
19
|
-
|
|
25
|
+
[t.Year]: (
|
|
20
26
|
/* 365.25 days */
|
|
21
27
|
365.25 * 24 * 60 * 60 * 1e3
|
|
22
28
|
)
|
|
23
|
-
}
|
|
24
|
-
|
|
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,
|
|
25
55
|
isFinite: !1,
|
|
26
56
|
isInfinite: !1,
|
|
27
57
|
isInstant: !1,
|
|
28
|
-
to(
|
|
29
|
-
return this[
|
|
58
|
+
to(n) {
|
|
59
|
+
return this[i] / l[d(n)];
|
|
30
60
|
},
|
|
31
|
-
|
|
32
|
-
return this.to(
|
|
33
|
-
0
|
|
34
|
-
/* MilliSecond */
|
|
35
|
-
);
|
|
61
|
+
toMilliseconds() {
|
|
62
|
+
return this.to(t.Millisecond);
|
|
36
63
|
},
|
|
37
64
|
toSeconds() {
|
|
38
|
-
return this.to(
|
|
39
|
-
1
|
|
40
|
-
/* Second */
|
|
41
|
-
);
|
|
65
|
+
return this.to(t.Second);
|
|
42
66
|
},
|
|
43
67
|
toMinutes() {
|
|
44
|
-
return this.to(
|
|
45
|
-
2
|
|
46
|
-
/* Minute */
|
|
47
|
-
);
|
|
68
|
+
return this.to(t.Minute);
|
|
48
69
|
},
|
|
49
70
|
toHours() {
|
|
50
|
-
return this.to(
|
|
51
|
-
3
|
|
52
|
-
/* Hour */
|
|
53
|
-
);
|
|
71
|
+
return this.to(t.Hour);
|
|
54
72
|
},
|
|
55
73
|
toDays() {
|
|
56
|
-
return this.to(
|
|
57
|
-
4
|
|
58
|
-
/* Day */
|
|
59
|
-
);
|
|
74
|
+
return this.to(t.Day);
|
|
60
75
|
},
|
|
61
76
|
toWeeks() {
|
|
62
|
-
return this.to(
|
|
63
|
-
5
|
|
64
|
-
/* Week */
|
|
65
|
-
);
|
|
77
|
+
return this.to(t.Week);
|
|
66
78
|
},
|
|
67
79
|
toMonths() {
|
|
68
|
-
return this.to(
|
|
69
|
-
6
|
|
70
|
-
/* Month */
|
|
71
|
-
);
|
|
80
|
+
return this.to(t.Month);
|
|
72
81
|
},
|
|
73
82
|
toYears() {
|
|
74
|
-
return this.to(
|
|
75
|
-
7
|
|
76
|
-
/* Year */
|
|
77
|
-
);
|
|
83
|
+
return this.to(t.Year);
|
|
78
84
|
},
|
|
79
|
-
equals(
|
|
80
|
-
return this[
|
|
85
|
+
equals(n) {
|
|
86
|
+
return this[i] === n[i];
|
|
81
87
|
},
|
|
82
|
-
lessThan(
|
|
83
|
-
return this[
|
|
88
|
+
lessThan(n) {
|
|
89
|
+
return this[i] < n[i];
|
|
84
90
|
},
|
|
85
|
-
lessThanOrEqual(
|
|
86
|
-
return this[
|
|
91
|
+
lessThanOrEqual(n) {
|
|
92
|
+
return this[i] <= n[i];
|
|
87
93
|
},
|
|
88
|
-
greaterThan(
|
|
89
|
-
return this[
|
|
94
|
+
greaterThan(n) {
|
|
95
|
+
return this[i] > n[i];
|
|
90
96
|
},
|
|
91
|
-
greaterThanOrEqual(
|
|
92
|
-
return this[
|
|
97
|
+
greaterThanOrEqual(n) {
|
|
98
|
+
return this[i] >= n[i];
|
|
93
99
|
},
|
|
94
|
-
compare(
|
|
95
|
-
return this.lessThan(
|
|
100
|
+
compare(n) {
|
|
101
|
+
return this.lessThan(n) ? -1 : this.greaterThan(n) ? 1 : 0;
|
|
96
102
|
}
|
|
97
103
|
};
|
|
98
|
-
function
|
|
99
|
-
const o = Object.create(
|
|
100
|
-
return o[
|
|
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);
|
|
107
|
+
}
|
|
108
|
+
function s(n, e) {
|
|
109
|
+
const r = c(n, "value"), o = d(e);
|
|
110
|
+
return u(r * l[o]);
|
|
111
|
+
}
|
|
112
|
+
function E(n) {
|
|
113
|
+
return s(n, t.Millisecond);
|
|
114
|
+
}
|
|
115
|
+
function M(n) {
|
|
116
|
+
return s(n, t.Second);
|
|
117
|
+
}
|
|
118
|
+
function N(n) {
|
|
119
|
+
return s(n, t.Minute);
|
|
120
|
+
}
|
|
121
|
+
function S(n) {
|
|
122
|
+
return s(n, t.Hour);
|
|
123
|
+
}
|
|
124
|
+
function D(n) {
|
|
125
|
+
return s(n, t.Day);
|
|
126
|
+
}
|
|
127
|
+
function O(n) {
|
|
128
|
+
return s(n, t.Week);
|
|
129
|
+
}
|
|
130
|
+
function v(n) {
|
|
131
|
+
return s(n, t.Month);
|
|
132
|
+
}
|
|
133
|
+
function U(n) {
|
|
134
|
+
return s(n, t.Year);
|
|
135
|
+
}
|
|
136
|
+
function p(n, e) {
|
|
137
|
+
const r = f(n, "start"), o = f(e, "end");
|
|
138
|
+
return u(o - r);
|
|
139
|
+
}
|
|
140
|
+
function g(n) {
|
|
141
|
+
return p(n, /* @__PURE__ */ new Date());
|
|
142
|
+
}
|
|
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
|
+
);
|
|
164
|
+
}
|
|
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.");
|
|
174
|
+
}
|
|
175
|
+
const a = u(1 / 0, { allowInfinite: !0 }), C = E(0);
|
|
176
|
+
function F(n) {
|
|
177
|
+
return h(n) && m(n, i);
|
|
178
|
+
}
|
|
179
|
+
function Y(n, e) {
|
|
180
|
+
return n[i] === e[i];
|
|
101
181
|
}
|
|
102
|
-
const n = Object.freeze({
|
|
103
|
-
of(t, r) {
|
|
104
|
-
return s(t, r);
|
|
105
|
-
},
|
|
106
|
-
ofMilliSeconds(t) {
|
|
107
|
-
return n.of(
|
|
108
|
-
t,
|
|
109
|
-
0
|
|
110
|
-
/* MilliSecond */
|
|
111
|
-
);
|
|
112
|
-
},
|
|
113
|
-
ofSeconds(t) {
|
|
114
|
-
return n.of(
|
|
115
|
-
t,
|
|
116
|
-
1
|
|
117
|
-
/* Second */
|
|
118
|
-
);
|
|
119
|
-
},
|
|
120
|
-
ofMinutes(t) {
|
|
121
|
-
return n.of(
|
|
122
|
-
t,
|
|
123
|
-
2
|
|
124
|
-
/* Minute */
|
|
125
|
-
);
|
|
126
|
-
},
|
|
127
|
-
ofHours(t) {
|
|
128
|
-
return n.of(
|
|
129
|
-
t,
|
|
130
|
-
3
|
|
131
|
-
/* Hour */
|
|
132
|
-
);
|
|
133
|
-
},
|
|
134
|
-
ofDays(t) {
|
|
135
|
-
return n.of(
|
|
136
|
-
t,
|
|
137
|
-
4
|
|
138
|
-
/* Day */
|
|
139
|
-
);
|
|
140
|
-
},
|
|
141
|
-
ofWeeks(t) {
|
|
142
|
-
return n.of(
|
|
143
|
-
t,
|
|
144
|
-
5
|
|
145
|
-
/* Week */
|
|
146
|
-
);
|
|
147
|
-
},
|
|
148
|
-
ofMonths(t) {
|
|
149
|
-
return n.of(
|
|
150
|
-
t,
|
|
151
|
-
6
|
|
152
|
-
/* Month */
|
|
153
|
-
);
|
|
154
|
-
},
|
|
155
|
-
ofYears(t) {
|
|
156
|
-
return n.of(
|
|
157
|
-
t,
|
|
158
|
-
7
|
|
159
|
-
/* Year */
|
|
160
|
-
);
|
|
161
|
-
},
|
|
162
|
-
between: (t, r) => n.of(
|
|
163
|
-
r.getTime() - t.getTime(),
|
|
164
|
-
0
|
|
165
|
-
/* MilliSecond */
|
|
166
|
-
),
|
|
167
|
-
since: (t) => n.between(t, /* @__PURE__ */ new Date()),
|
|
168
|
-
add: (t, r) => n.ofMilliSeconds(t[e] + r[e]),
|
|
169
|
-
subtract: (t, r) => n.ofMilliSeconds(t[e] - r[e]),
|
|
170
|
-
multiply: (t, r) => n.ofMilliSeconds(t[e] * r),
|
|
171
|
-
divide: (t, r) => n.ofMilliSeconds(t[e] / r),
|
|
172
|
-
ofInfinite: s(
|
|
173
|
-
1 / 0,
|
|
174
|
-
0
|
|
175
|
-
/* MilliSecond */
|
|
176
|
-
),
|
|
177
|
-
ofInstant: s(
|
|
178
|
-
0,
|
|
179
|
-
0
|
|
180
|
-
/* MilliSecond */
|
|
181
|
-
),
|
|
182
|
-
isDuration: (t) => i(t) && e in t,
|
|
183
|
-
isEqual: (t, r) => t[e] === r[e]
|
|
184
|
-
});
|
|
185
182
|
export {
|
|
186
|
-
|
|
187
|
-
|
|
183
|
+
t as TimeUnit,
|
|
184
|
+
z as add,
|
|
185
|
+
p as between,
|
|
186
|
+
D as days,
|
|
187
|
+
x as divide,
|
|
188
|
+
s as duration,
|
|
189
|
+
S as hours,
|
|
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,
|
|
197
|
+
k as multiply,
|
|
198
|
+
M as seconds,
|
|
199
|
+
g as since,
|
|
200
|
+
_ as subtract,
|
|
201
|
+
O as weeks,
|
|
202
|
+
U as years
|
|
188
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
|
-
}
|
|
11
|
-
|
|
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
|
+
};
|
|
21
|
+
declare const millisecondsTag: unique symbol;
|
|
22
|
+
/** Immutable duration value with conversions, comparisons, and state flags. */
|
|
12
23
|
export type Duration = {
|
|
13
|
-
readonly [
|
|
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,26 +41,43 @@ export type Duration = {
|
|
|
30
41
|
greaterThanOrEqual(other: Duration): boolean;
|
|
31
42
|
compare(other: Duration): -1 | 0 | 1;
|
|
32
43
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
44
|
+
/** Creates a duration from a finite numeric value in the provided unit. */
|
|
45
|
+
export declare function duration(value: number, unit: TimeUnit): Duration;
|
|
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. */
|
|
49
|
+
export declare function seconds(value: number): Duration;
|
|
50
|
+
/** Creates a duration from a finite minute value. */
|
|
51
|
+
export declare function minutes(value: number): Duration;
|
|
52
|
+
/** Creates a duration from a finite hour value. */
|
|
53
|
+
export declare function hours(value: number): Duration;
|
|
54
|
+
/** Creates a duration from a finite day value. */
|
|
55
|
+
export declare function days(value: number): Duration;
|
|
56
|
+
/** Creates a duration from a finite week value. */
|
|
57
|
+
export declare function weeks(value: number): Duration;
|
|
58
|
+
/** Creates a duration from a finite approximate month value (30 days each). */
|
|
59
|
+
export declare function months(value: number): Duration;
|
|
60
|
+
/** Creates a duration from a finite approximate year value (365.25 days each). */
|
|
61
|
+
export declare function years(value: number): Duration;
|
|
62
|
+
/** Returns the duration between two valid dates. The result may be negative. */
|
|
63
|
+
export declare function between(start: Date, end: Date): Duration;
|
|
64
|
+
/** Returns the duration since a valid start date. The result may be negative. */
|
|
65
|
+
export declare function since(start: Date): Duration;
|
|
66
|
+
/** Adds two durations and preserves the explicit `infinite` sentinel. */
|
|
67
|
+
export declare function add(a: Duration, b: Duration): Duration;
|
|
68
|
+
/** Subtracts one duration from another and rejects undefined `infinite` cases. */
|
|
69
|
+
export declare function subtract(a: Duration, b: Duration): Duration;
|
|
70
|
+
/** Multiplies a duration by a finite scalar. */
|
|
71
|
+
export declare function multiply(a: Duration, b: number): Duration;
|
|
72
|
+
/** Divides a duration by a finite, non-zero scalar. */
|
|
73
|
+
export declare function divide(a: Duration, b: number): Duration;
|
|
74
|
+
/** The explicit non-finite duration sentinel supported by this package. */
|
|
75
|
+
export declare const infinite: Duration;
|
|
76
|
+
/** The zero-length duration constant. */
|
|
77
|
+
export declare const instant: Duration;
|
|
78
|
+
/** Returns `true` when the value is a duration created by this package. */
|
|
79
|
+
export declare function isDuration(value: unknown): value is Duration;
|
|
80
|
+
/** Compares two durations by their normalized millisecond payload. */
|
|
81
|
+
export declare function isEqual(a: Duration, b: Duration): boolean;
|
|
54
82
|
export {};
|
|
55
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":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pvorona/duration",
|
|
3
|
-
"version": "0.
|
|
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
|
}
|