@pvorona/duration 0.0.1 → 0.0.3

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.
Files changed (2) hide show
  1. package/README.md +212 -6
  2. package/package.json +5 -4
package/README.md CHANGED
@@ -1,11 +1,217 @@
1
- # duration
1
+ # @pvorona/duration
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ An immutable duration type with unit conversions, comparisons, and basic arithmetic.
4
4
 
5
- ## Building
5
+ ## Install
6
6
 
7
- Run `nx build duration` to build the library.
7
+ ```bash
8
+ npm i @pvorona/duration
9
+ ```
8
10
 
9
- ## Running unit tests
11
+ ## Usage
10
12
 
11
- Run `nx test duration` to execute the unit tests via [Vitest](https://vitest.dev/).
13
+ ### Create and convert
14
+
15
+ ```ts
16
+ import { Duration } from '@pvorona/duration';
17
+
18
+ const d = Duration.ofMinutes(5);
19
+ d.toSeconds(); // 300
20
+ d.toMilliSeconds(); // 300_000
21
+ ```
22
+
23
+ ### Compare
24
+
25
+ ```ts
26
+ import { Duration } from '@pvorona/duration';
27
+
28
+ const a = Duration.ofSeconds(1);
29
+ const b = Duration.ofMilliSeconds(900);
30
+
31
+ a.greaterThan(b); // true
32
+ a.compare(b); // 1
33
+ ```
34
+
35
+ ### Arithmetic
36
+
37
+ ```ts
38
+ import { Duration } from '@pvorona/duration';
39
+
40
+ const total = Duration.add(Duration.ofSeconds(1), Duration.ofMilliSeconds(500));
41
+ total.toMilliSeconds(); // 1500
42
+ ```
43
+
44
+ ### Between dates
45
+
46
+ ```ts
47
+ import { Duration } from '@pvorona/duration';
48
+
49
+ const startedAt = new Date(Date.now() - 2_000);
50
+ const elapsed = Duration.since(startedAt);
51
+ elapsed.toSeconds(); // ~2
52
+ ```
53
+
54
+ ## API
55
+
56
+ ### `const enum TimeUnit`
57
+
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
+ ```
72
+
73
+ Notes:
74
+
75
+ - `Month` is treated as **30 days**
76
+ - `Year` is treated as **365.25 days**
77
+
78
+ Example:
79
+
80
+ ```ts
81
+ import { Duration, TimeUnit } from '@pvorona/duration';
82
+
83
+ Duration.of(2, TimeUnit.Hour).toMinutes(); // 120
84
+ ```
85
+
86
+ ### `type Duration`
87
+
88
+ An opaque, immutable duration value.
89
+
90
+ #### Properties
91
+
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`)
95
+
96
+ #### Conversions
97
+
98
+ - **`to(unit: TimeUnit): number`**: convert to an arbitrary unit
99
+ - **`toMilliSeconds(): number`**
100
+ - **`toSeconds(): number`**
101
+ - **`toMinutes(): number`**
102
+ - **`toHours(): number`**
103
+ - **`toDays(): number`**
104
+ - **`toWeeks(): number`**
105
+ - **`toMonths(): number`** (30-day months)
106
+ - **`toYears(): number`** (365.25-day years)
107
+
108
+ #### Comparisons
109
+
110
+ - **`equals(other: Duration): boolean`**
111
+ - **`lessThan(other: Duration): boolean`**
112
+ - **`lessThanOrEqual(other: Duration): boolean`**
113
+ - **`greaterThan(other: Duration): boolean`**
114
+ - **`greaterThanOrEqual(other: Duration): boolean`**
115
+ - **`compare(other: Duration): -1 | 0 | 1`**
116
+
117
+ Example (properties + conversion + comparison):
118
+
119
+ ```ts
120
+ import type { Duration } from '@pvorona/duration';
121
+ import { Duration as DurationNS } from '@pvorona/duration';
122
+
123
+ function isShort(d: Duration) {
124
+ return d.isFinite && d.toSeconds() < 5;
125
+ }
126
+
127
+ const a = DurationNS.ofSeconds(1);
128
+ const b = DurationNS.ofMilliSeconds(900);
129
+
130
+ isShort(a); // true
131
+ a.greaterThan(b); // true
132
+ ```
133
+
134
+ ### `const Duration`
135
+
136
+ Namespace-style factory + utilities.
137
+
138
+ #### Constructors
139
+
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
+ ```
158
+
159
+ #### Date helpers
160
+
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
+ ```
172
+
173
+ #### Arithmetic helpers
174
+
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
+ ```
188
+
189
+ #### Constants
190
+
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
+ ```
202
+
203
+ #### Guards / equality
204
+
205
+ - **`Duration.isDuration(value: unknown): value is Duration`**
206
+ - **`Duration.isEqual(a: Duration, b: Duration): boolean`**
207
+
208
+ Example:
209
+
210
+ ```ts
211
+ import { Duration } from '@pvorona/duration';
212
+
213
+ const maybe: unknown = Duration.ofSeconds(1);
214
+ if (Duration.isDuration(maybe)) {
215
+ maybe.toMilliSeconds(); // ok, narrowed
216
+ }
217
+ ```
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@pvorona/duration",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
+ "license": "MIT",
4
5
  "type": "module",
5
6
  "main": "./dist/index.js",
6
7
  "module": "./dist/index.js",
@@ -18,10 +19,10 @@
18
19
  "dist",
19
20
  "!**/*.tsbuildinfo"
20
21
  ],
21
- "nx": {
22
- "name": "duration"
22
+ "publishConfig": {
23
+ "access": "public"
23
24
  },
24
25
  "dependencies": {
25
- "@pvorona/assert": "^0.0.1"
26
+ "@pvorona/assert": "~0.0.2"
26
27
  }
27
28
  }