@bereasoftware/time-guard 2.7.2 → 2.9.0
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.en.md +5 -1
- package/README.md +5 -1
- package/dist/adapters/temporal.adapter.d.ts +6 -2
- package/dist/angular/index.cjs +1 -148
- package/dist/angular/index.es.js +63 -112
- package/dist/calendars/index.cjs +1 -357
- package/dist/calendars/index.es.js +66 -99
- package/dist/chunk-C6qsp3rV.cjs +1 -0
- package/dist/core-BDQnStek.cjs +1 -0
- package/dist/core-GHK7isoa.js +1200 -0
- package/dist/core.d.ts +27 -1
- package/dist/index.d.ts +4 -2
- package/dist/locales/index.cjs +1 -7
- package/dist/locales/index.d.ts +11 -0
- package/dist/locales/index.es.js +3 -3
- package/dist/{locales-CGdn0DJN.cjs → locales-Dwe5oGoy.js} +201 -82
- package/dist/locales-bc-5kDuZ.cjs +1 -0
- package/dist/native/index.cjs +1 -48
- package/dist/native/index.es.js +8 -18
- package/dist/plugins/advanced-format/index.d.ts +8 -0
- package/dist/plugins/advanced-format.cjs +1 -105
- package/dist/plugins/advanced-format.es.js +48 -63
- package/dist/plugins/duration/index.d.ts +6 -0
- package/dist/plugins/duration.cjs +1 -258
- package/dist/plugins/duration.es.js +63 -165
- package/dist/plugins/manager.d.ts +6 -2
- package/dist/plugins/relative-time/index.d.ts +6 -0
- package/dist/plugins/relative-time.cjs +1 -166
- package/dist/plugins/relative-time.es.js +39 -74
- package/dist/qwik/index.cjs +1 -104
- package/dist/qwik/index.es.js +36 -94
- package/dist/qwik.d.ts +5 -1
- package/dist/react/index.cjs +1 -112
- package/dist/react/index.es.js +57 -91
- package/dist/solid/index.cjs +1 -127
- package/dist/solid/index.es.js +36 -116
- package/dist/solid.d.ts +5 -2
- package/dist/svelte/index.cjs +1 -398
- package/dist/svelte/index.es.js +85 -344
- package/dist/time-guard.cjs +3 -7359
- package/dist/time-guard.es.js +3412 -3625
- package/dist/time-guard.iife.js +3 -13549
- package/dist/time-guard.umd.js +3 -13550
- package/dist/types/index.d.ts +10 -0
- package/dist/vue/index.cjs +1 -143
- package/dist/vue/index.es.js +62 -116
- package/dist/vue.d.ts +5 -3
- package/package.json +3 -4
- package/dist/chunk-S8Y6Ng9i.cjs +0 -36
- package/dist/core-BIDjudpO.cjs +0 -1892
- package/dist/core-DWPXk3rz.js +0 -1809
- package/dist/locales-BUE_aq8K.js +0 -3514
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
/*! time-guard v2.
|
|
1
|
+
/*! time-guard v2.9.0 | (c) 2026 Berea-Soft | MIT License | https://github.com/Berea-Soft/time-guard */
|
|
2
2
|
//#region src/plugins/duration/index.ts
|
|
3
|
-
|
|
4
|
-
* Duration class - represents time span following ISO 8601 standard
|
|
5
|
-
*/
|
|
6
|
-
var Duration = class Duration {
|
|
3
|
+
var e = class e {
|
|
7
4
|
years = 0;
|
|
8
5
|
months = 0;
|
|
9
6
|
weeks = 0;
|
|
@@ -12,79 +9,49 @@ var Duration = class Duration {
|
|
|
12
9
|
minutes = 0;
|
|
13
10
|
seconds = 0;
|
|
14
11
|
milliseconds = 0;
|
|
15
|
-
constructor(
|
|
16
|
-
this.years =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const match = iso.match(/^(-)?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:([\d.]+)S)?)?$/);
|
|
31
|
-
if (!match) throw new Error(`Invalid ISO 8601 duration: ${iso}`);
|
|
32
|
-
const [, negative, years, months, , days, hours, minutes, seconds] = match;
|
|
33
|
-
const multiplier = negative ? -1 : 1;
|
|
34
|
-
return new Duration({
|
|
35
|
-
years: parseInt(years || "0", 10) * multiplier,
|
|
36
|
-
months: parseInt(months || "0", 10) * multiplier,
|
|
37
|
-
days: parseInt(days || "0", 10) * multiplier,
|
|
38
|
-
hours: parseInt(hours || "0", 10) * multiplier,
|
|
39
|
-
minutes: parseInt(minutes || "0", 10) * multiplier,
|
|
40
|
-
seconds: parseFloat(seconds || "0") * multiplier
|
|
12
|
+
constructor(e) {
|
|
13
|
+
this.years = e.years || 0, this.months = e.months || 0, this.weeks = e.weeks || 0, this.days = e.days || 0, this.hours = e.hours || 0, this.minutes = e.minutes || 0, this.seconds = e.seconds || 0, this.milliseconds = e.milliseconds || 0;
|
|
14
|
+
}
|
|
15
|
+
static fromISO(t) {
|
|
16
|
+
let n = t.match(/^(-)?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:([\d.]+)S)?)?$/);
|
|
17
|
+
if (!n) throw Error(`Invalid ISO 8601 duration: ${t}`);
|
|
18
|
+
let [, r, i, a, o, s, c, l, u] = n, d = r ? -1 : 1;
|
|
19
|
+
return new e({
|
|
20
|
+
years: parseInt(i || "0", 10) * d,
|
|
21
|
+
months: parseInt(a || "0", 10) * d,
|
|
22
|
+
weeks: parseInt(o || "0", 10) * d,
|
|
23
|
+
days: parseInt(s || "0", 10) * d,
|
|
24
|
+
hours: parseInt(c || "0", 10) * d,
|
|
25
|
+
minutes: parseInt(l || "0", 10) * d,
|
|
26
|
+
seconds: parseFloat(u || "0") * d
|
|
41
27
|
});
|
|
42
28
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const duration = to.toTemporal().since(fromDT);
|
|
49
|
-
return new Duration({
|
|
50
|
-
years: duration.years || 0,
|
|
51
|
-
months: duration.months || 0,
|
|
29
|
+
static between(t, n) {
|
|
30
|
+
let r = t.toTemporal(), i = n.toTemporal().since(r);
|
|
31
|
+
return new e({
|
|
32
|
+
years: i.years || 0,
|
|
33
|
+
months: i.months || 0,
|
|
52
34
|
weeks: 0,
|
|
53
|
-
days:
|
|
54
|
-
hours:
|
|
55
|
-
minutes:
|
|
56
|
-
seconds:
|
|
57
|
-
milliseconds:
|
|
35
|
+
days: i.days || 0,
|
|
36
|
+
hours: i.hours || 0,
|
|
37
|
+
minutes: i.minutes || 0,
|
|
38
|
+
seconds: i.seconds || 0,
|
|
39
|
+
milliseconds: i.milliseconds || 0
|
|
58
40
|
});
|
|
59
41
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const minutes = Math.floor(absMilhs % (1e3 * 60 * 60) / (1e3 * 60));
|
|
71
|
-
const seconds = Math.floor(absMilhs % (1e3 * 60) / 1e3);
|
|
72
|
-
const milliseconds = Math.floor(absMilhs % 1e3);
|
|
73
|
-
const multiplier = isNegative ? -1 : 1;
|
|
74
|
-
return new Duration({
|
|
75
|
-
years: years * multiplier,
|
|
76
|
-
months: months * multiplier,
|
|
77
|
-
days: days * multiplier,
|
|
78
|
-
hours: hours * multiplier,
|
|
79
|
-
minutes: minutes * multiplier,
|
|
80
|
-
seconds: seconds * multiplier,
|
|
81
|
-
milliseconds: milliseconds * multiplier
|
|
42
|
+
static fromMilliseconds(t) {
|
|
43
|
+
let n = t < 0, r = Math.abs(t), i = Math.floor(r / (1e3 * 60 * 60 * 24 * 365)), a = Math.floor(r % (1e3 * 60 * 60 * 24 * 365) / (1e3 * 60 * 60 * 24 * 30)), o = Math.floor(r % (1e3 * 60 * 60 * 24 * 30) / (1e3 * 60 * 60 * 24)), s = Math.floor(r % (1e3 * 60 * 60 * 24) / (1e3 * 60 * 60)), c = Math.floor(r % (1e3 * 60 * 60) / (1e3 * 60)), l = Math.floor(r % (1e3 * 60) / 1e3), u = Math.floor(r % 1e3), d = n ? -1 : 1;
|
|
44
|
+
return new e({
|
|
45
|
+
years: i * d,
|
|
46
|
+
months: a * d,
|
|
47
|
+
days: o * d,
|
|
48
|
+
hours: s * d,
|
|
49
|
+
minutes: c * d,
|
|
50
|
+
seconds: l * d,
|
|
51
|
+
milliseconds: u * d
|
|
82
52
|
});
|
|
83
53
|
}
|
|
84
|
-
|
|
85
|
-
* Get duration in specific unit
|
|
86
|
-
*/
|
|
87
|
-
as(unit) {
|
|
54
|
+
as(e) {
|
|
88
55
|
return this.asMilliseconds() / {
|
|
89
56
|
milliseconds: 1,
|
|
90
57
|
seconds: 1e3,
|
|
@@ -94,59 +61,32 @@ var Duration = class Duration {
|
|
|
94
61
|
weeks: 1e3 * 60 * 60 * 24 * 7,
|
|
95
62
|
months: 1e3 * 60 * 60 * 24 * 30,
|
|
96
63
|
years: 1e3 * 60 * 60 * 24 * 365
|
|
97
|
-
}[
|
|
64
|
+
}[e];
|
|
98
65
|
}
|
|
99
|
-
/**
|
|
100
|
-
* Get duration as milliseconds
|
|
101
|
-
*/
|
|
102
66
|
asMilliseconds() {
|
|
103
67
|
return this.years * 1e3 * 60 * 60 * 24 * 365 + this.months * 1e3 * 60 * 60 * 24 * 30 + this.weeks * 1e3 * 60 * 60 * 24 * 7 + this.days * 1e3 * 60 * 60 * 24 + this.hours * 1e3 * 60 * 60 + this.minutes * 1e3 * 60 + this.seconds * 1e3 + this.milliseconds;
|
|
104
68
|
}
|
|
105
|
-
/**
|
|
106
|
-
* Get duration as seconds
|
|
107
|
-
*/
|
|
108
69
|
asSeconds() {
|
|
109
70
|
return this.asMilliseconds() / 1e3;
|
|
110
71
|
}
|
|
111
|
-
/**
|
|
112
|
-
* Get duration as minutes
|
|
113
|
-
*/
|
|
114
72
|
asMinutes() {
|
|
115
73
|
return this.asSeconds() / 60;
|
|
116
74
|
}
|
|
117
|
-
/**
|
|
118
|
-
* Get duration as hours
|
|
119
|
-
*/
|
|
120
75
|
asHours() {
|
|
121
76
|
return this.asMinutes() / 60;
|
|
122
77
|
}
|
|
123
|
-
/**
|
|
124
|
-
* Get duration as days
|
|
125
|
-
*/
|
|
126
78
|
asDays() {
|
|
127
79
|
return this.asHours() / 24;
|
|
128
80
|
}
|
|
129
|
-
/**
|
|
130
|
-
* Get duration as weeks
|
|
131
|
-
*/
|
|
132
81
|
asWeeks() {
|
|
133
82
|
return this.asDays() / 7;
|
|
134
83
|
}
|
|
135
|
-
/**
|
|
136
|
-
* Get duration as months
|
|
137
|
-
*/
|
|
138
84
|
asMonths() {
|
|
139
85
|
return this.asDays() / 30;
|
|
140
86
|
}
|
|
141
|
-
/**
|
|
142
|
-
* Get duration as years
|
|
143
|
-
*/
|
|
144
87
|
asYears() {
|
|
145
88
|
return this.asDays() / 365;
|
|
146
89
|
}
|
|
147
|
-
/**
|
|
148
|
-
* Get all components
|
|
149
|
-
*/
|
|
150
90
|
toObject() {
|
|
151
91
|
return {
|
|
152
92
|
years: this.years,
|
|
@@ -159,49 +99,23 @@ var Duration = class Duration {
|
|
|
159
99
|
milliseconds: this.milliseconds
|
|
160
100
|
};
|
|
161
101
|
}
|
|
162
|
-
/**
|
|
163
|
-
* Get ISO 8601 string representation
|
|
164
|
-
* @example "P3Y6M4DT12H30M5S"
|
|
165
|
-
*/
|
|
166
102
|
toISO() {
|
|
167
|
-
let
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (this.hours) timePart += `${this.hours}H`;
|
|
173
|
-
if (this.minutes) timePart += `${this.minutes}M`;
|
|
174
|
-
if (this.seconds || this.milliseconds) timePart += `${this.seconds + this.milliseconds / 1e3}S`;
|
|
175
|
-
return `${this.isNegative() ? "-" : ""}P${datePart}${timePart ? `T${timePart}` : ""}`;
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Get human-readable string
|
|
179
|
-
*/
|
|
103
|
+
let e = "";
|
|
104
|
+
this.years && (e += `${this.years}Y`), this.months && (e += `${this.months}M`), (this.weeks || this.days) && (e += `${this.weeks * 7 + this.days}D`);
|
|
105
|
+
let t = "";
|
|
106
|
+
return this.hours && (t += `${this.hours}H`), this.minutes && (t += `${this.minutes}M`), (this.seconds || this.milliseconds) && (t += `${this.seconds + this.milliseconds / 1e3}S`), `${this.isNegative() ? "-" : ""}P${e}${t ? `T${t}` : ""}`;
|
|
107
|
+
}
|
|
180
108
|
humanize() {
|
|
181
|
-
|
|
182
|
-
if (this.years
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (this.hours) parts.push(`${Math.abs(this.hours)} hour${Math.abs(this.hours) !== 1 ? "s" : ""}`);
|
|
187
|
-
if (this.minutes) parts.push(`${Math.abs(this.minutes)} minute${Math.abs(this.minutes) !== 1 ? "s" : ""}`);
|
|
188
|
-
if (this.seconds) parts.push(`${Math.abs(this.seconds)} second${Math.abs(this.seconds) !== 1 ? "s" : ""}`);
|
|
189
|
-
if (this.milliseconds) parts.push(`${Math.abs(this.milliseconds)} ms`);
|
|
190
|
-
if (parts.length === 0) return "0 seconds";
|
|
191
|
-
const text = parts.join(", ");
|
|
192
|
-
return this.isNegative() ? `-${text}` : text;
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Check if duration is negative
|
|
196
|
-
*/
|
|
109
|
+
let e = [];
|
|
110
|
+
if (this.years && e.push(`${Math.abs(this.years)} year${Math.abs(this.years) === 1 ? "" : "s"}`), this.months && e.push(`${Math.abs(this.months)} month${Math.abs(this.months) === 1 ? "" : "s"}`), this.weeks && e.push(`${Math.abs(this.weeks)} week${Math.abs(this.weeks) === 1 ? "" : "s"}`), this.days && e.push(`${Math.abs(this.days)} day${Math.abs(this.days) === 1 ? "" : "s"}`), this.hours && e.push(`${Math.abs(this.hours)} hour${Math.abs(this.hours) === 1 ? "" : "s"}`), this.minutes && e.push(`${Math.abs(this.minutes)} minute${Math.abs(this.minutes) === 1 ? "" : "s"}`), this.seconds && e.push(`${Math.abs(this.seconds)} second${Math.abs(this.seconds) === 1 ? "" : "s"}`), this.milliseconds && e.push(`${Math.abs(this.milliseconds)} ms`), e.length === 0) return "0 seconds";
|
|
111
|
+
let t = e.join(", ");
|
|
112
|
+
return this.isNegative() ? `-${t}` : t;
|
|
113
|
+
}
|
|
197
114
|
isNegative() {
|
|
198
115
|
return this.years < 0 || this.months < 0 || this.weeks < 0 || this.days < 0 || this.hours < 0 || this.minutes < 0 || this.seconds < 0 || this.milliseconds < 0;
|
|
199
116
|
}
|
|
200
|
-
/**
|
|
201
|
-
* Get absolute duration
|
|
202
|
-
*/
|
|
203
117
|
abs() {
|
|
204
|
-
return new
|
|
118
|
+
return new e({
|
|
205
119
|
years: Math.abs(this.years),
|
|
206
120
|
months: Math.abs(this.months),
|
|
207
121
|
weeks: Math.abs(this.weeks),
|
|
@@ -212,40 +126,24 @@ var Duration = class Duration {
|
|
|
212
126
|
milliseconds: Math.abs(this.milliseconds)
|
|
213
127
|
});
|
|
214
128
|
}
|
|
215
|
-
/**
|
|
216
|
-
* String representation
|
|
217
|
-
*/
|
|
218
129
|
toString() {
|
|
219
130
|
return this.toISO();
|
|
220
131
|
}
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Duration Plugin
|
|
224
|
-
*/
|
|
225
|
-
var DurationPlugin = class {
|
|
132
|
+
}, t = class {
|
|
226
133
|
name = "duration";
|
|
227
134
|
version = "1.0.0";
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
TimeGuardClass.prototype.duration = function(other) {
|
|
236
|
-
return Duration.between(this, other);
|
|
237
|
-
};
|
|
238
|
-
TimeGuardClass.Duration = Duration;
|
|
239
|
-
TimeGuardClass.duration = {
|
|
240
|
-
fromISO: (iso) => Duration.fromISO(iso),
|
|
241
|
-
between: (from, to) => Duration.between(from, to),
|
|
242
|
-
fromMilliseconds: (ms) => Duration.fromMilliseconds(ms)
|
|
135
|
+
install(t) {
|
|
136
|
+
t.prototype.duration = function(t) {
|
|
137
|
+
return e.between(this, t);
|
|
138
|
+
}, t.Duration = e, t.duration = {
|
|
139
|
+
fromISO: (t) => e.fromISO(t),
|
|
140
|
+
between: (t, n) => e.between(t, n),
|
|
141
|
+
fromMilliseconds: (t) => e.fromMilliseconds(t)
|
|
243
142
|
};
|
|
244
143
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
var durationPlugin = new DurationPlugin();
|
|
144
|
+
uninstall(e) {
|
|
145
|
+
delete e.prototype.duration, delete e.Duration, delete e.duration;
|
|
146
|
+
}
|
|
147
|
+
}, n = new t();
|
|
250
148
|
//#endregion
|
|
251
|
-
export { Duration, DurationPlugin,
|
|
149
|
+
export { e as Duration, t as DurationPlugin, n as default, n as durationPlugin };
|
|
@@ -43,12 +43,16 @@ export declare class PluginManager {
|
|
|
43
43
|
*/
|
|
44
44
|
static listPlugins(): string[];
|
|
45
45
|
/**
|
|
46
|
-
* Unregister a plugin
|
|
46
|
+
* Unregister a plugin — calls its uninstall() hook (if implemented) to
|
|
47
|
+
* reverse whatever install() did, so re-registering the same plugin
|
|
48
|
+
* later starts from a clean prototype instead of stacking a new patch
|
|
49
|
+
* on top of the old one.
|
|
47
50
|
* @param name - Plugin name
|
|
48
51
|
*/
|
|
49
52
|
static unuse(name: string): boolean;
|
|
50
53
|
/**
|
|
51
|
-
* Clear all plugins
|
|
54
|
+
* Clear all plugins — calls each one's uninstall() hook (if implemented)
|
|
55
|
+
* before forgetting it, for the same reason as unuse().
|
|
52
56
|
*/
|
|
53
57
|
static clear(): void;
|
|
54
58
|
/**
|
|
@@ -11,6 +11,12 @@ export declare class RelativeTimePlugin implements ITimeGuardPlugin {
|
|
|
11
11
|
* Install plugin into TimeGuard
|
|
12
12
|
*/
|
|
13
13
|
install(TimeGuardClass: typeof TimeGuard): void;
|
|
14
|
+
/**
|
|
15
|
+
* Reverse install() — none of fromNow/toNow/humanize pre-exist on
|
|
16
|
+
* TimeGuard, so undoing this is just deleting them, not restoring a
|
|
17
|
+
* prior value.
|
|
18
|
+
*/
|
|
19
|
+
uninstall(TimeGuardClass: typeof TimeGuard): void;
|
|
14
20
|
/**
|
|
15
21
|
* Format relative time
|
|
16
22
|
*/
|
|
@@ -1,166 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperties(exports, {
|
|
3
|
-
__esModule: { value: true },
|
|
4
|
-
[Symbol.toStringTag]: { value: "Module" }
|
|
5
|
-
});
|
|
6
|
-
//#region src/plugins/relative-time/index.ts
|
|
7
|
-
var DEFAULT_THRESHOLDS = [
|
|
8
|
-
{
|
|
9
|
-
l: "s",
|
|
10
|
-
r: 44,
|
|
11
|
-
d: "second"
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
l: "m",
|
|
15
|
-
r: 89
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
l: "mm",
|
|
19
|
-
r: 44,
|
|
20
|
-
d: "minute"
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
l: "h",
|
|
24
|
-
r: 89
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
l: "hh",
|
|
28
|
-
r: 21,
|
|
29
|
-
d: "hour"
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
l: "d",
|
|
33
|
-
r: 35
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
l: "dd",
|
|
37
|
-
r: 25,
|
|
38
|
-
d: "day"
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
l: "M",
|
|
42
|
-
r: 45
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
l: "MM",
|
|
46
|
-
r: 10,
|
|
47
|
-
d: "month"
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
l: "y",
|
|
51
|
-
r: 17
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
l: "yy",
|
|
55
|
-
d: "year"
|
|
56
|
-
}
|
|
57
|
-
];
|
|
58
|
-
var DEFAULT_FORMATS = {
|
|
59
|
-
future: "in %s",
|
|
60
|
-
past: "%s ago",
|
|
61
|
-
s: "a few seconds",
|
|
62
|
-
m: "a minute",
|
|
63
|
-
mm: "%d minutes",
|
|
64
|
-
h: "an hour",
|
|
65
|
-
hh: "%d hours",
|
|
66
|
-
d: "a day",
|
|
67
|
-
dd: "%d days",
|
|
68
|
-
M: "a month",
|
|
69
|
-
MM: "%d months",
|
|
70
|
-
y: "a year",
|
|
71
|
-
yy: "%d years"
|
|
72
|
-
};
|
|
73
|
-
var RelativeTimePlugin = class {
|
|
74
|
-
name = "relative-time";
|
|
75
|
-
version = "1.0.0";
|
|
76
|
-
config;
|
|
77
|
-
formats;
|
|
78
|
-
constructor(config) {
|
|
79
|
-
this.config = {
|
|
80
|
-
thresholds: config?.thresholds || DEFAULT_THRESHOLDS,
|
|
81
|
-
rounding: config?.rounding || Math.round
|
|
82
|
-
};
|
|
83
|
-
this.formats = DEFAULT_FORMATS;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Install plugin into TimeGuard
|
|
87
|
-
*/
|
|
88
|
-
install(TimeGuardClass) {
|
|
89
|
-
const plugin = this;
|
|
90
|
-
/**
|
|
91
|
-
* Get relative time string (e.g., "2 hours ago")
|
|
92
|
-
*/
|
|
93
|
-
TimeGuardClass.prototype.fromNow = function(withoutSuffix) {
|
|
94
|
-
return plugin.formatRelativeTime(this, false, withoutSuffix);
|
|
95
|
-
};
|
|
96
|
-
TimeGuardClass.prototype.toNow = function(withoutSuffix) {
|
|
97
|
-
return plugin.formatRelativeTime(this, true, withoutSuffix);
|
|
98
|
-
};
|
|
99
|
-
TimeGuardClass.prototype.humanize = function(other, withoutSuffix) {
|
|
100
|
-
if (other) return plugin.formatRelativeTime(this, other.isAfter(this), withoutSuffix);
|
|
101
|
-
return plugin.formatRelativeTime(this, false, withoutSuffix);
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Format relative time
|
|
106
|
-
*/
|
|
107
|
-
formatRelativeTime(date, isFuture, withoutSuffix) {
|
|
108
|
-
const diff = date.constructor.now().diff(date, "millisecond");
|
|
109
|
-
const absDiff = Math.abs(diff);
|
|
110
|
-
const actualIsFuture = isFuture ?? !(diff > 0);
|
|
111
|
-
const result = this.getRelativeTimeString(absDiff);
|
|
112
|
-
if (withoutSuffix) return result;
|
|
113
|
-
return (actualIsFuture ? this.formats.future : this.formats.past).replace("%s", result);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Get relative time string based on milliseconds
|
|
117
|
-
*/
|
|
118
|
-
getRelativeTimeString(milliseconds) {
|
|
119
|
-
const thresholds = this.config.thresholds || DEFAULT_THRESHOLDS;
|
|
120
|
-
const rounding = this.config.rounding || Math.round;
|
|
121
|
-
for (let i = 0; i < thresholds.length; i++) {
|
|
122
|
-
const threshold = thresholds[i];
|
|
123
|
-
if ((i + 1 < thresholds.length ? thresholds[i + 1] : null) && threshold.r && milliseconds < threshold.r * 1e3) continue;
|
|
124
|
-
let value;
|
|
125
|
-
if (threshold.d) value = rounding(milliseconds / this.getUnitMilliseconds(threshold.d));
|
|
126
|
-
else value = 1;
|
|
127
|
-
const label = threshold.l;
|
|
128
|
-
const format = this.formats[label] || label;
|
|
129
|
-
if (typeof format === "string") return format.includes("%d") ? format.replace("%d", String(value)) : format;
|
|
130
|
-
return format;
|
|
131
|
-
}
|
|
132
|
-
return `${rounding(milliseconds / 1e3)} seconds`;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Get milliseconds per unit
|
|
136
|
-
*/
|
|
137
|
-
getUnitMilliseconds(unit) {
|
|
138
|
-
return {
|
|
139
|
-
second: 1e3,
|
|
140
|
-
minute: 1e3 * 60,
|
|
141
|
-
hour: 1e3 * 60 * 60,
|
|
142
|
-
day: 1e3 * 60 * 60 * 24,
|
|
143
|
-
month: 1e3 * 60 * 60 * 24 * 30,
|
|
144
|
-
year: 1e3 * 60 * 60 * 24 * 365
|
|
145
|
-
}[unit] || 1;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Set format strings
|
|
149
|
-
*/
|
|
150
|
-
setFormats(formats) {
|
|
151
|
-
Object.assign(this.formats, formats);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Get current formats
|
|
155
|
-
*/
|
|
156
|
-
getFormats() {
|
|
157
|
-
return { ...this.formats };
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
/**
|
|
161
|
-
* Create and export default instance
|
|
162
|
-
*/
|
|
163
|
-
var relative_time_default = new RelativeTimePlugin();
|
|
164
|
-
//#endregion
|
|
165
|
-
exports.RelativeTimePlugin = RelativeTimePlugin;
|
|
166
|
-
exports.default = relative_time_default;
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var e=[{l:`s`,r:44,d:`second`},{l:`m`,r:89},{l:`mm`,r:44,d:`minute`},{l:`h`,r:89},{l:`hh`,r:21,d:`hour`},{l:`d`,r:35},{l:`dd`,r:25,d:`day`},{l:`M`,r:45},{l:`MM`,r:10,d:`month`},{l:`y`,r:17},{l:`yy`,d:`year`}],t={future:`in %s`,past:`%s ago`,s:`a few seconds`,m:`a minute`,mm:`%d minutes`,h:`an hour`,hh:`%d hours`,d:`a day`,dd:`%d days`,M:`a month`,MM:`%d months`,y:`a year`,yy:`%d years`},n=class{name=`relative-time`;version=`1.0.0`;config;formats;constructor(n){this.config={thresholds:n?.thresholds||e,rounding:n?.rounding||Math.round},this.formats=t}install(e){let t=this;e.prototype.fromNow=function(e){return t.formatRelativeTime(this,!1,e)},e.prototype.toNow=function(e){return t.formatRelativeTime(this,!0,e)},e.prototype.humanize=function(e,n){return e?t.formatRelativeTime(this,e.isAfter(this),n):t.formatRelativeTime(this,!1,n)}}uninstall(e){delete e.prototype.fromNow,delete e.prototype.toNow,delete e.prototype.humanize}formatRelativeTime(e,t,n){let r=e.constructor.now().diff(e,`millisecond`),i=Math.abs(r),a=t??!(r>0),o=this.getRelativeTimeString(i);return n?o:(a?this.formats.future:this.formats.past).replace(`%s`,o)}getRelativeTimeString(t){let n=this.config.thresholds||e,r=this.config.rounding||Math.round,i=`second`;for(let e=0;e<n.length;e++){let a=n[e];i=a.d||i;let o=t/this.getUnitMilliseconds(i);if(e===n.length-1||a.r===void 0||o<=a.r){let e=r(o),t=a.l,n=this.formats[t]||t;return typeof n==`string`&&n.includes(`%d`)?n.replace(`%d`,String(e)):n}}return`${r(t/1e3)} seconds`}getUnitMilliseconds(e){return{second:1e3,minute:1e3*60,hour:1e3*60*60,day:1e3*60*60*24,month:1e3*60*60*24*30,year:1e3*60*60*24*365}[e]||1}setFormats(e){Object.assign(this.formats,e)}getFormats(){return{...this.formats}}},r=new n;exports.RelativeTimePlugin=n,exports.default=r;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/*! time-guard v2.
|
|
1
|
+
/*! time-guard v2.9.0 | (c) 2026 Berea-Soft | MIT License | https://github.com/Berea-Soft/time-guard */
|
|
2
2
|
//#region src/plugins/relative-time/index.ts
|
|
3
|
-
var
|
|
3
|
+
var e = [
|
|
4
4
|
{
|
|
5
5
|
l: "s",
|
|
6
6
|
r: 44,
|
|
@@ -50,8 +50,7 @@ var DEFAULT_THRESHOLDS = [
|
|
|
50
50
|
l: "yy",
|
|
51
51
|
d: "year"
|
|
52
52
|
}
|
|
53
|
-
]
|
|
54
|
-
var DEFAULT_FORMATS = {
|
|
53
|
+
], t = {
|
|
55
54
|
future: "in %s",
|
|
56
55
|
past: "%s ago",
|
|
57
56
|
s: "a few seconds",
|
|
@@ -65,72 +64,48 @@ var DEFAULT_FORMATS = {
|
|
|
65
64
|
MM: "%d months",
|
|
66
65
|
y: "a year",
|
|
67
66
|
yy: "%d years"
|
|
68
|
-
}
|
|
69
|
-
var RelativeTimePlugin = class {
|
|
67
|
+
}, n = class {
|
|
70
68
|
name = "relative-time";
|
|
71
69
|
version = "1.0.0";
|
|
72
70
|
config;
|
|
73
71
|
formats;
|
|
74
|
-
constructor(
|
|
72
|
+
constructor(n) {
|
|
75
73
|
this.config = {
|
|
76
|
-
thresholds:
|
|
77
|
-
rounding:
|
|
78
|
-
};
|
|
79
|
-
this.formats = DEFAULT_FORMATS;
|
|
74
|
+
thresholds: n?.thresholds || e,
|
|
75
|
+
rounding: n?.rounding || Math.round
|
|
76
|
+
}, this.formats = t;
|
|
80
77
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
TimeGuardClass.prototype.fromNow = function(withoutSuffix) {
|
|
90
|
-
return plugin.formatRelativeTime(this, false, withoutSuffix);
|
|
91
|
-
};
|
|
92
|
-
TimeGuardClass.prototype.toNow = function(withoutSuffix) {
|
|
93
|
-
return plugin.formatRelativeTime(this, true, withoutSuffix);
|
|
94
|
-
};
|
|
95
|
-
TimeGuardClass.prototype.humanize = function(other, withoutSuffix) {
|
|
96
|
-
if (other) return plugin.formatRelativeTime(this, other.isAfter(this), withoutSuffix);
|
|
97
|
-
return plugin.formatRelativeTime(this, false, withoutSuffix);
|
|
78
|
+
install(e) {
|
|
79
|
+
let t = this;
|
|
80
|
+
e.prototype.fromNow = function(e) {
|
|
81
|
+
return t.formatRelativeTime(this, !1, e);
|
|
82
|
+
}, e.prototype.toNow = function(e) {
|
|
83
|
+
return t.formatRelativeTime(this, !0, e);
|
|
84
|
+
}, e.prototype.humanize = function(e, n) {
|
|
85
|
+
return e ? t.formatRelativeTime(this, e.isAfter(this), n) : t.formatRelativeTime(this, !1, n);
|
|
98
86
|
};
|
|
99
87
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
formatRelativeTime(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const actualIsFuture = isFuture ?? !(diff > 0);
|
|
107
|
-
const result = this.getRelativeTimeString(absDiff);
|
|
108
|
-
if (withoutSuffix) return result;
|
|
109
|
-
return (actualIsFuture ? this.formats.future : this.formats.past).replace("%s", result);
|
|
88
|
+
uninstall(e) {
|
|
89
|
+
delete e.prototype.fromNow, delete e.prototype.toNow, delete e.prototype.humanize;
|
|
90
|
+
}
|
|
91
|
+
formatRelativeTime(e, t, n) {
|
|
92
|
+
let r = e.constructor.now().diff(e, "millisecond"), i = Math.abs(r), a = t ?? !(r > 0), o = this.getRelativeTimeString(i);
|
|
93
|
+
return n ? o : (a ? this.formats.future : this.formats.past).replace("%s", o);
|
|
110
94
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (threshold.d) value = rounding(milliseconds / this.getUnitMilliseconds(threshold.d));
|
|
122
|
-
else value = 1;
|
|
123
|
-
const label = threshold.l;
|
|
124
|
-
const format = this.formats[label] || label;
|
|
125
|
-
if (typeof format === "string") return format.includes("%d") ? format.replace("%d", String(value)) : format;
|
|
126
|
-
return format;
|
|
95
|
+
getRelativeTimeString(t) {
|
|
96
|
+
let n = this.config.thresholds || e, r = this.config.rounding || Math.round, i = "second";
|
|
97
|
+
for (let e = 0; e < n.length; e++) {
|
|
98
|
+
let a = n[e];
|
|
99
|
+
i = a.d || i;
|
|
100
|
+
let o = t / this.getUnitMilliseconds(i);
|
|
101
|
+
if (e === n.length - 1 || a.r === void 0 || o <= a.r) {
|
|
102
|
+
let e = r(o), t = a.l, n = this.formats[t] || t;
|
|
103
|
+
return typeof n == "string" && n.includes("%d") ? n.replace("%d", String(e)) : n;
|
|
104
|
+
}
|
|
127
105
|
}
|
|
128
|
-
return `${
|
|
106
|
+
return `${r(t / 1e3)} seconds`;
|
|
129
107
|
}
|
|
130
|
-
|
|
131
|
-
* Get milliseconds per unit
|
|
132
|
-
*/
|
|
133
|
-
getUnitMilliseconds(unit) {
|
|
108
|
+
getUnitMilliseconds(e) {
|
|
134
109
|
return {
|
|
135
110
|
second: 1e3,
|
|
136
111
|
minute: 1e3 * 60,
|
|
@@ -138,24 +113,14 @@ var RelativeTimePlugin = class {
|
|
|
138
113
|
day: 1e3 * 60 * 60 * 24,
|
|
139
114
|
month: 1e3 * 60 * 60 * 24 * 30,
|
|
140
115
|
year: 1e3 * 60 * 60 * 24 * 365
|
|
141
|
-
}[
|
|
116
|
+
}[e] || 1;
|
|
142
117
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
*/
|
|
146
|
-
setFormats(formats) {
|
|
147
|
-
Object.assign(this.formats, formats);
|
|
118
|
+
setFormats(e) {
|
|
119
|
+
Object.assign(this.formats, e);
|
|
148
120
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Get current formats
|
|
151
|
-
*/
|
|
152
121
|
getFormats() {
|
|
153
122
|
return { ...this.formats };
|
|
154
123
|
}
|
|
155
|
-
};
|
|
156
|
-
/**
|
|
157
|
-
* Create and export default instance
|
|
158
|
-
*/
|
|
159
|
-
var relative_time_default = new RelativeTimePlugin();
|
|
124
|
+
}, r = new n();
|
|
160
125
|
//#endregion
|
|
161
|
-
export { RelativeTimePlugin,
|
|
126
|
+
export { n as RelativeTimePlugin, r as default };
|