@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 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 { Duration } from '@pvorona/duration';
18
+ import { duration, TimeUnit } from '@pvorona/duration';
17
19
 
18
- const d = Duration.ofMinutes(5);
19
- d.toSeconds(); // 300
20
- d.toMilliSeconds(); // 300_000
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 { Duration } from '@pvorona/duration';
29
+ import { milliseconds, seconds } from '@pvorona/duration';
27
30
 
28
- const a = Duration.ofSeconds(1);
29
- const b = Duration.ofMilliSeconds(900);
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 { Duration } from '@pvorona/duration';
41
+ import { add, milliseconds, seconds } from '@pvorona/duration';
39
42
 
40
- const total = Duration.add(Duration.ofSeconds(1), Duration.ofMilliSeconds(500));
41
- total.toMilliSeconds(); // 1500
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 { Duration } from '@pvorona/duration';
50
+ import { since } from '@pvorona/duration';
48
51
 
49
52
  const startedAt = new Date(Date.now() - 2_000);
50
- const elapsed = Duration.since(startedAt);
53
+ const elapsed = since(startedAt);
51
54
  elapsed.toSeconds(); // ~2
52
55
  ```
53
56
 
54
- ## API
55
-
56
- ### `const enum TimeUnit`
57
+ ## Environment and semantics
57
58
 
58
- Time units supported by `Duration.of(value, unit)` and `duration.to(unit)`.
59
-
60
- ```ts
61
- export const enum TimeUnit {
62
- MilliSecond,
63
- Second,
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
- Notes:
66
+ ## API
74
67
 
75
- - `Month` is treated as **30 days**
76
- - `Year` is treated as **365.25 days**
68
+ ### `TimeUnit`
77
69
 
78
- Example:
70
+ Runtime unit constants supported by `duration(value, unit)` and `d.to(unit)`.
79
71
 
80
- ```ts
81
- import { Duration, TimeUnit } from '@pvorona/duration';
82
-
83
- Duration.of(2, TimeUnit.Hour).toMinutes(); // 120
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` unless the duration is infinite
93
- - **`isInfinite: boolean`**: `true` for `Duration.ofInfinite`
94
- - **`isInstant: boolean`**: `true` for zero duration (`Duration.ofInstant`)
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
- - **`toMilliSeconds(): number`**
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 (properties + conversion + comparison):
112
+ Example:
118
113
 
119
114
  ```ts
120
- import type { Duration } from '@pvorona/duration';
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 = DurationNS.ofSeconds(1);
128
- const b = DurationNS.ofMilliSeconds(900);
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
- ### `const Duration`
135
-
136
- Namespace-style factory + utilities.
128
+ ### Function API
137
129
 
138
130
  #### Constructors
139
131
 
140
- - **`Duration.of(value: number, unit: TimeUnit): Duration`**
141
- - **`Duration.ofMilliSeconds(value: number): Duration`**
142
- - **`Duration.ofSeconds(value: number): Duration`**
143
- - **`Duration.ofMinutes(value: number): Duration`**
144
- - **`Duration.ofHours(value: number): Duration`**
145
- - **`Duration.ofDays(value: number): Duration`**
146
- - **`Duration.ofWeeks(value: number): Duration`**
147
- - **`Duration.ofMonths(value: number): Duration`** (30-day months)
148
- - **`Duration.ofYears(value: number): Duration`** (365.25-day years)
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
- - **`Duration.between(start: Date, end: Date): Duration`**
162
- - **`Duration.since(start: Date): Duration`** (until “now”)
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
- - **`Duration.add(a: Duration, b: Duration): Duration`**
176
- - **`Duration.subtract(a: Duration, b: Duration): Duration`**
177
- - **`Duration.multiply(a: Duration, b: number): Duration`**
178
- - **`Duration.divide(a: Duration, b: number): Duration`**
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
- - **`Duration.ofInfinite: Duration`**
192
- - **`Duration.ofInstant: Duration`**
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
- - **`Duration.isDuration(value: unknown): value is Duration`**
206
- - **`Duration.isEqual(a: Duration, b: Duration): boolean`**
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 { Duration } from '@pvorona/duration';
167
+ import { isDuration, seconds } from '@pvorona/duration';
212
168
 
213
- const maybe: unknown = Duration.ofSeconds(1);
214
- if (Duration.isDuration(maybe)) {
215
- maybe.toMilliSeconds(); // ok, narrowed
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
- function i(t) {
2
- return t !== null && typeof t == "object";
3
- }
4
- var f = /* @__PURE__ */ ((t) => (t[t.MilliSecond = 0] = "MilliSecond", t[t.Second = 1] = "Second", t[t.Minute = 2] = "Minute", t[t.Hour = 3] = "Hour", t[t.Day = 4] = "Day", t[t.Week = 5] = "Week", t[t.Month = 6] = "Month", t[t.Year = 7] = "Year", t))(f || {});
5
- const e = /* @__PURE__ */ Symbol("milliSeconds"), u = {
6
- 0: 1,
7
- 1: 1e3,
8
- 2: 60 * 1e3,
9
- 3: 3600 * 1e3,
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
- 4: 1440 * 60 * 1e3,
12
- 5: 10080 * 60 * 1e3,
17
+ [t.Day]: 1440 * 60 * 1e3,
18
+ [t.Week]: 10080 * 60 * 1e3,
13
19
  /** Note: Does not account for leap year */
14
- 6: (
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
- 7: (
25
+ [t.Year]: (
20
26
  /* 365.25 days */
21
27
  365.25 * 24 * 60 * 60 * 1e3
22
28
  )
23
- }, a = {
24
- [e]: 0,
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(t) {
29
- return this[e] / u[t];
58
+ to(n) {
59
+ return this[i] / l[d(n)];
30
60
  },
31
- toMilliSeconds() {
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(t) {
80
- return this[e] === t[e];
85
+ equals(n) {
86
+ return this[i] === n[i];
81
87
  },
82
- lessThan(t) {
83
- return this[e] < t[e];
88
+ lessThan(n) {
89
+ return this[i] < n[i];
84
90
  },
85
- lessThanOrEqual(t) {
86
- return this[e] <= t[e];
91
+ lessThanOrEqual(n) {
92
+ return this[i] <= n[i];
87
93
  },
88
- greaterThan(t) {
89
- return this[e] > t[e];
94
+ greaterThan(n) {
95
+ return this[i] > n[i];
90
96
  },
91
- greaterThanOrEqual(t) {
92
- return this[e] >= t[e];
97
+ greaterThanOrEqual(n) {
98
+ return this[i] >= n[i];
93
99
  },
94
- compare(t) {
95
- return this.lessThan(t) ? -1 : this.greaterThan(t) ? 1 : 0;
100
+ compare(n) {
101
+ return this.lessThan(n) ? -1 : this.greaterThan(n) ? 1 : 0;
96
102
  }
97
103
  };
98
- function s(t, r) {
99
- const o = Object.create(a);
100
- return o[e] = t * u[r], o.isFinite = t !== 1 / 0, o.isInfinite = t === 1 / 0, o.isInstant = t === 0, Object.freeze(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
- n as Duration,
187
- f as TimeUnit
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
  };
@@ -1,21 +1,32 @@
1
- export declare const enum TimeUnit {
2
- MilliSecond = 0,
3
- Second = 1,
4
- Minute = 2,
5
- Hour = 3,
6
- Day = 4,
7
- Week = 5,
8
- Month = 6,
9
- Year = 7
10
- }
11
- declare const milliSeconds: unique symbol;
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 [milliSeconds]: number;
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
- toMilliSeconds(): number;
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
- export declare const Duration: Readonly<{
34
- of(value: number, unit: TimeUnit): Duration;
35
- ofMilliSeconds(value: number): Duration;
36
- ofSeconds(value: number): Duration;
37
- ofMinutes(value: number): Duration;
38
- ofHours(value: number): Duration;
39
- ofDays(value: number): Duration;
40
- ofWeeks(value: number): Duration;
41
- ofMonths(value: number): Duration;
42
- ofYears(value: number): Duration;
43
- between: (start: Date, end: Date) => Duration;
44
- since: (start: Date) => Duration;
45
- add: (a: Duration, b: Duration) => Duration;
46
- subtract: (a: Duration, b: Duration) => Duration;
47
- multiply: (a: Duration, b: number) => Duration;
48
- divide: (a: Duration, b: number) => Duration;
49
- ofInfinite: Duration;
50
- ofInstant: Duration;
51
- isDuration: (value: unknown) => value is Duration;
52
- isEqual: (a: Duration, b: Duration) => boolean;
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,0BAAkB,QAAQ;IACxB,WAAW,IAAA;IACX,MAAM,IAAA;IACN,MAAM,IAAA;IACN,IAAI,IAAA;IACJ,GAAG,IAAA;IACH,IAAI,IAAA;IACJ,KAAK,IAAA;IACL,IAAI,IAAA;CACL;AAED,QAAA,MAAM,YAAY,eAAyB,CAAC;AAE5C,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,CAAC,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B,EAAE,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC3B,cAAc,IAAI,MAAM,CAAC;IACzB,SAAS,IAAI,MAAM,CAAC;IACpB,SAAS,IAAI,MAAM,CAAC;IACpB,OAAO,IAAI,MAAM,CAAC;IAClB,MAAM,IAAI,MAAM,CAAC;IACjB,OAAO,IAAI,MAAM,CAAC;IAClB,QAAQ,IAAI,MAAM,CAAC;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AM,QAAQ,EAAE,QAA4D,CAAC;AAEpF,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.0.3",
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.2"
25
+ "@pvorona/assert": "~0.1.0"
27
26
  }
28
27
  }