@bereasoftware/time-guard 2.7.1 → 2.8.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/angular/index.cjs +1 -148
- package/dist/angular/index.es.js +60 -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-Dxn1aRFk.js +1186 -0
- package/dist/core-TqmHKXY1.cjs +1 -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-B1mDDyn_.cjs → locales-3Mw99lQ-.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.cjs +1 -105
- package/dist/plugins/advanced-format.es.js +43 -63
- package/dist/plugins/duration.cjs +1 -258
- package/dist/plugins/duration.es.js +59 -165
- package/dist/plugins/relative-time.cjs +1 -166
- package/dist/plugins/relative-time.es.js +36 -74
- package/dist/qwik/index.cjs +1 -104
- package/dist/qwik/index.es.js +33 -94
- 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 +34 -116
- 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/vue/index.cjs +1 -143
- package/dist/vue/index.es.js +62 -116
- package/package.json +2 -3
- package/dist/chunk-BbtWAPZS.cjs +0 -36
- package/dist/core-BFdo248T.cjs +0 -1892
- package/dist/core-CyBR5PPd.js +0 -1809
- package/dist/locales-By7IK3Ep.js +0 -3514
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
/*! time-guard v2.
|
|
1
|
+
/*! time-guard v2.8.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,48 @@ 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
|
-
static fromISO(iso) {
|
|
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] = n, u = r ? -1 : 1;
|
|
19
|
+
return new e({
|
|
20
|
+
years: parseInt(i || "0", 10) * u,
|
|
21
|
+
months: parseInt(a || "0", 10) * u,
|
|
22
|
+
days: parseInt(o || "0", 10) * u,
|
|
23
|
+
hours: parseInt(s || "0", 10) * u,
|
|
24
|
+
minutes: parseInt(c || "0", 10) * u,
|
|
25
|
+
seconds: parseFloat(l || "0") * u
|
|
41
26
|
});
|
|
42
27
|
}
|
|
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,
|
|
28
|
+
static between(t, n) {
|
|
29
|
+
let r = t.toTemporal(), i = n.toTemporal().since(r);
|
|
30
|
+
return new e({
|
|
31
|
+
years: i.years || 0,
|
|
32
|
+
months: i.months || 0,
|
|
52
33
|
weeks: 0,
|
|
53
|
-
days:
|
|
54
|
-
hours:
|
|
55
|
-
minutes:
|
|
56
|
-
seconds:
|
|
57
|
-
milliseconds:
|
|
34
|
+
days: i.days || 0,
|
|
35
|
+
hours: i.hours || 0,
|
|
36
|
+
minutes: i.minutes || 0,
|
|
37
|
+
seconds: i.seconds || 0,
|
|
38
|
+
milliseconds: i.milliseconds || 0
|
|
58
39
|
});
|
|
59
40
|
}
|
|
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
|
|
41
|
+
static fromMilliseconds(t) {
|
|
42
|
+
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;
|
|
43
|
+
return new e({
|
|
44
|
+
years: i * d,
|
|
45
|
+
months: a * d,
|
|
46
|
+
days: o * d,
|
|
47
|
+
hours: s * d,
|
|
48
|
+
minutes: c * d,
|
|
49
|
+
seconds: l * d,
|
|
50
|
+
milliseconds: u * d
|
|
82
51
|
});
|
|
83
52
|
}
|
|
84
|
-
|
|
85
|
-
* Get duration in specific unit
|
|
86
|
-
*/
|
|
87
|
-
as(unit) {
|
|
53
|
+
as(e) {
|
|
88
54
|
return this.asMilliseconds() / {
|
|
89
55
|
milliseconds: 1,
|
|
90
56
|
seconds: 1e3,
|
|
@@ -94,59 +60,32 @@ var Duration = class Duration {
|
|
|
94
60
|
weeks: 1e3 * 60 * 60 * 24 * 7,
|
|
95
61
|
months: 1e3 * 60 * 60 * 24 * 30,
|
|
96
62
|
years: 1e3 * 60 * 60 * 24 * 365
|
|
97
|
-
}[
|
|
63
|
+
}[e];
|
|
98
64
|
}
|
|
99
|
-
/**
|
|
100
|
-
* Get duration as milliseconds
|
|
101
|
-
*/
|
|
102
65
|
asMilliseconds() {
|
|
103
66
|
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
67
|
}
|
|
105
|
-
/**
|
|
106
|
-
* Get duration as seconds
|
|
107
|
-
*/
|
|
108
68
|
asSeconds() {
|
|
109
69
|
return this.asMilliseconds() / 1e3;
|
|
110
70
|
}
|
|
111
|
-
/**
|
|
112
|
-
* Get duration as minutes
|
|
113
|
-
*/
|
|
114
71
|
asMinutes() {
|
|
115
72
|
return this.asSeconds() / 60;
|
|
116
73
|
}
|
|
117
|
-
/**
|
|
118
|
-
* Get duration as hours
|
|
119
|
-
*/
|
|
120
74
|
asHours() {
|
|
121
75
|
return this.asMinutes() / 60;
|
|
122
76
|
}
|
|
123
|
-
/**
|
|
124
|
-
* Get duration as days
|
|
125
|
-
*/
|
|
126
77
|
asDays() {
|
|
127
78
|
return this.asHours() / 24;
|
|
128
79
|
}
|
|
129
|
-
/**
|
|
130
|
-
* Get duration as weeks
|
|
131
|
-
*/
|
|
132
80
|
asWeeks() {
|
|
133
81
|
return this.asDays() / 7;
|
|
134
82
|
}
|
|
135
|
-
/**
|
|
136
|
-
* Get duration as months
|
|
137
|
-
*/
|
|
138
83
|
asMonths() {
|
|
139
84
|
return this.asDays() / 30;
|
|
140
85
|
}
|
|
141
|
-
/**
|
|
142
|
-
* Get duration as years
|
|
143
|
-
*/
|
|
144
86
|
asYears() {
|
|
145
87
|
return this.asDays() / 365;
|
|
146
88
|
}
|
|
147
|
-
/**
|
|
148
|
-
* Get all components
|
|
149
|
-
*/
|
|
150
89
|
toObject() {
|
|
151
90
|
return {
|
|
152
91
|
years: this.years,
|
|
@@ -159,49 +98,23 @@ var Duration = class Duration {
|
|
|
159
98
|
milliseconds: this.milliseconds
|
|
160
99
|
};
|
|
161
100
|
}
|
|
162
|
-
/**
|
|
163
|
-
* Get ISO 8601 string representation
|
|
164
|
-
* @example "P3Y6M4DT12H30M5S"
|
|
165
|
-
*/
|
|
166
101
|
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
|
-
*/
|
|
102
|
+
let e = "";
|
|
103
|
+
this.years && (e += `${this.years}Y`), this.months && (e += `${this.months}M`), (this.weeks || this.days) && (e += `${this.weeks * 7 + this.days}D`);
|
|
104
|
+
let t = "";
|
|
105
|
+
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}` : ""}`;
|
|
106
|
+
}
|
|
180
107
|
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
|
-
*/
|
|
108
|
+
let e = [];
|
|
109
|
+
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";
|
|
110
|
+
let t = e.join(", ");
|
|
111
|
+
return this.isNegative() ? `-${t}` : t;
|
|
112
|
+
}
|
|
197
113
|
isNegative() {
|
|
198
114
|
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
115
|
}
|
|
200
|
-
/**
|
|
201
|
-
* Get absolute duration
|
|
202
|
-
*/
|
|
203
116
|
abs() {
|
|
204
|
-
return new
|
|
117
|
+
return new e({
|
|
205
118
|
years: Math.abs(this.years),
|
|
206
119
|
months: Math.abs(this.months),
|
|
207
120
|
weeks: Math.abs(this.weeks),
|
|
@@ -212,40 +125,21 @@ var Duration = class Duration {
|
|
|
212
125
|
milliseconds: Math.abs(this.milliseconds)
|
|
213
126
|
});
|
|
214
127
|
}
|
|
215
|
-
/**
|
|
216
|
-
* String representation
|
|
217
|
-
*/
|
|
218
128
|
toString() {
|
|
219
129
|
return this.toISO();
|
|
220
130
|
}
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Duration Plugin
|
|
224
|
-
*/
|
|
225
|
-
var DurationPlugin = class {
|
|
131
|
+
}, t = class {
|
|
226
132
|
name = "duration";
|
|
227
133
|
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)
|
|
134
|
+
install(t) {
|
|
135
|
+
t.prototype.duration = function(t) {
|
|
136
|
+
return e.between(this, t);
|
|
137
|
+
}, t.Duration = e, t.duration = {
|
|
138
|
+
fromISO: (t) => e.fromISO(t),
|
|
139
|
+
between: (t, n) => e.between(t, n),
|
|
140
|
+
fromMilliseconds: (t) => e.fromMilliseconds(t)
|
|
243
141
|
};
|
|
244
142
|
}
|
|
245
|
-
};
|
|
246
|
-
/**
|
|
247
|
-
* Create and export default instance
|
|
248
|
-
*/
|
|
249
|
-
var durationPlugin = new DurationPlugin();
|
|
143
|
+
}, n = new t();
|
|
250
144
|
//#endregion
|
|
251
|
-
export { Duration, DurationPlugin,
|
|
145
|
+
export { e as Duration, t as DurationPlugin, n as default, n as durationPlugin };
|
|
@@ -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)}}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.8.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,45 @@ 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(date, isFuture, withoutSuffix) {
|
|
104
|
-
const diff = date.constructor.now().diff(date, "millisecond");
|
|
105
|
-
const absDiff = Math.abs(diff);
|
|
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
|
+
formatRelativeTime(e, t, n) {
|
|
89
|
+
let r = e.constructor.now().diff(e, "millisecond"), i = Math.abs(r), a = t ?? !(r > 0), o = this.getRelativeTimeString(i);
|
|
90
|
+
return n ? o : (a ? this.formats.future : this.formats.past).replace("%s", o);
|
|
110
91
|
}
|
|
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;
|
|
92
|
+
getRelativeTimeString(t) {
|
|
93
|
+
let n = this.config.thresholds || e, r = this.config.rounding || Math.round, i = "second";
|
|
94
|
+
for (let e = 0; e < n.length; e++) {
|
|
95
|
+
let a = n[e];
|
|
96
|
+
i = a.d || i;
|
|
97
|
+
let o = t / this.getUnitMilliseconds(i);
|
|
98
|
+
if (e === n.length - 1 || a.r === void 0 || o <= a.r) {
|
|
99
|
+
let e = r(o), t = a.l, n = this.formats[t] || t;
|
|
100
|
+
return typeof n == "string" && n.includes("%d") ? n.replace("%d", String(e)) : n;
|
|
101
|
+
}
|
|
127
102
|
}
|
|
128
|
-
return `${
|
|
103
|
+
return `${r(t / 1e3)} seconds`;
|
|
129
104
|
}
|
|
130
|
-
|
|
131
|
-
* Get milliseconds per unit
|
|
132
|
-
*/
|
|
133
|
-
getUnitMilliseconds(unit) {
|
|
105
|
+
getUnitMilliseconds(e) {
|
|
134
106
|
return {
|
|
135
107
|
second: 1e3,
|
|
136
108
|
minute: 1e3 * 60,
|
|
@@ -138,24 +110,14 @@ var RelativeTimePlugin = class {
|
|
|
138
110
|
day: 1e3 * 60 * 60 * 24,
|
|
139
111
|
month: 1e3 * 60 * 60 * 24 * 30,
|
|
140
112
|
year: 1e3 * 60 * 60 * 24 * 365
|
|
141
|
-
}[
|
|
113
|
+
}[e] || 1;
|
|
142
114
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
*/
|
|
146
|
-
setFormats(formats) {
|
|
147
|
-
Object.assign(this.formats, formats);
|
|
115
|
+
setFormats(e) {
|
|
116
|
+
Object.assign(this.formats, e);
|
|
148
117
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Get current formats
|
|
151
|
-
*/
|
|
152
118
|
getFormats() {
|
|
153
119
|
return { ...this.formats };
|
|
154
120
|
}
|
|
155
|
-
};
|
|
156
|
-
/**
|
|
157
|
-
* Create and export default instance
|
|
158
|
-
*/
|
|
159
|
-
var relative_time_default = new RelativeTimePlugin();
|
|
121
|
+
}, r = new n();
|
|
160
122
|
//#endregion
|
|
161
|
-
export { RelativeTimePlugin,
|
|
123
|
+
export { n as RelativeTimePlugin, r as default };
|