@bereasoftware/time-guard 2.6.1 → 2.7.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 +232 -0
- package/README.md +259 -4
- package/dist/angular/index.cjs +148 -0
- package/dist/angular/index.d.ts +2 -0
- package/dist/angular/index.es.js +143 -0
- package/dist/angular.d.ts +59 -0
- package/dist/calendars/index.cjs +357 -2
- package/dist/calendars/index.es.js +99 -68
- package/dist/chunk-Ddh6569R.cjs +36 -0
- package/dist/core-BAd7GYfw.js +1809 -0
- package/dist/core-CafqOI7W.cjs +1892 -0
- package/dist/core.d.ts +224 -0
- package/dist/index.d.ts +9 -0
- package/dist/locales/african.locale.d.ts +7 -0
- package/dist/locales/index.cjs +7 -1
- package/dist/{types/locales → locales}/index.d.ts +2 -2
- package/dist/locales/index.es.js +3 -3
- package/dist/locales/south-american.locale.d.ts +7 -0
- package/dist/locales-C2-lOmC8.cjs +3537 -0
- package/dist/{locales-DSBvgSaw.js → locales-CcuSu4P1.js} +749 -33
- package/dist/native/index.cjs +48 -0
- package/dist/native/index.d.ts +2 -0
- package/dist/native/index.es.js +18 -0
- package/dist/native.d.ts +9 -0
- package/dist/plugins/advanced-format.cjs +105 -2
- package/dist/plugins/advanced-format.es.js +63 -45
- package/dist/plugins/duration.cjs +258 -2
- package/dist/plugins/duration.es.js +165 -61
- package/dist/plugins/relative-time.cjs +166 -2
- package/dist/plugins/relative-time.es.js +74 -37
- package/dist/qwik/index.cjs +104 -0
- package/dist/qwik/index.d.ts +2 -0
- package/dist/qwik/index.es.js +100 -0
- package/dist/qwik.d.ts +32 -0
- package/dist/react/index.cjs +112 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.es.js +103 -0
- package/dist/react.d.ts +46 -0
- package/dist/solid/index.cjs +127 -0
- package/dist/solid/index.d.ts +2 -0
- package/dist/solid/index.es.js +122 -0
- package/dist/solid.d.ts +53 -0
- package/dist/svelte/index.cjs +398 -0
- package/dist/svelte/index.d.ts +2 -0
- package/dist/svelte/index.es.js +393 -0
- package/dist/svelte.d.ts +88 -0
- package/dist/time-guard.cjs +7359 -4
- package/dist/time-guard.es.js +3625 -8085
- package/dist/time-guard.iife.js +13549 -4
- package/dist/time-guard.umd.js +13550 -4
- package/dist/types/index.d.ts +651 -127
- package/dist/vue/index.cjs +143 -0
- package/dist/vue/index.d.ts +2 -0
- package/dist/vue/index.es.js +137 -0
- package/dist/vue.d.ts +35 -0
- package/package.json +107 -27
- package/dist/calendars/index.cjs.map +0 -1
- package/dist/calendars/index.es.js.map +0 -1
- package/dist/locales-DSBvgSaw.js.map +0 -1
- package/dist/locales-ybx7jS8G.cjs +0 -2
- package/dist/locales-ybx7jS8G.cjs.map +0 -1
- package/dist/plugins/advanced-format.cjs.map +0 -1
- package/dist/plugins/advanced-format.es.js.map +0 -1
- package/dist/plugins/duration.cjs.map +0 -1
- package/dist/plugins/duration.es.js.map +0 -1
- package/dist/plugins/relative-time.cjs.map +0 -1
- package/dist/plugins/relative-time.es.js.map +0 -1
- package/dist/time-guard.cjs.map +0 -1
- package/dist/time-guard.es.js.map +0 -1
- package/dist/time-guard.iife.js.map +0 -1
- package/dist/time-guard.umd.js.map +0 -1
- package/dist/types/types/index.d.ts +0 -641
- /package/dist/{types/adapters → adapters}/temporal.adapter.d.ts +0 -0
- /package/dist/{types/calendars → calendars}/calendar.manager.d.ts +0 -0
- /package/dist/{types/calendars → calendars}/index.d.ts +0 -0
- /package/dist/{types/formatters → formatters}/date.formatter.d.ts +0 -0
- /package/dist/{types/locales → locales}/additional.locale.d.ts +0 -0
- /package/dist/{types/locales → locales}/asian.locale.d.ts +0 -0
- /package/dist/{types/locales → locales}/english.locale.d.ts +0 -0
- /package/dist/{types/locales → locales}/european.locale.d.ts +0 -0
- /package/dist/{types/locales → locales}/locale.manager.d.ts +0 -0
- /package/dist/{types/locales → locales}/middle-eastern.locale.d.ts +0 -0
- /package/dist/{types/locales → locales}/nordic.locale.d.ts +0 -0
- /package/dist/{types/locales → locales}/romance.locale.d.ts +0 -0
- /package/dist/{types/locales → locales}/slavic.locale.d.ts +0 -0
- /package/dist/{types/locales → locales}/spanish.locale.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/advanced-format/index.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/advanced-format.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/duration/index.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/duration/types.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/duration.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/index.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/manager.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/relative-time/index.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/relative-time/types.d.ts +0 -0
- /package/dist/{types/plugins → plugins}/relative-time.d.ts +0 -0
- /package/dist/{types/time-guard.d.ts → time-guard.d.ts} +0 -0
- /package/dist/{types/utils → utils}/duration-locale.d.ts +0 -0
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
/*! time-guard v2.
|
|
1
|
+
/*! time-guard v2.7.0 | (c) 2026 Berea-Soft | MIT License | https://github.com/Berea-Soft/time-guard */
|
|
2
2
|
//#region src/plugins/duration/index.ts
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Duration class - represents time span following ISO 8601 standard
|
|
5
|
+
*/
|
|
6
|
+
var Duration = class Duration {
|
|
4
7
|
years = 0;
|
|
5
8
|
months = 0;
|
|
6
9
|
weeks = 0;
|
|
@@ -9,48 +12,79 @@ var e = class e {
|
|
|
9
12
|
minutes = 0;
|
|
10
13
|
seconds = 0;
|
|
11
14
|
milliseconds = 0;
|
|
12
|
-
constructor(
|
|
13
|
-
this.years =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
constructor(input) {
|
|
16
|
+
this.years = input.years || 0;
|
|
17
|
+
this.months = input.months || 0;
|
|
18
|
+
this.weeks = input.weeks || 0;
|
|
19
|
+
this.days = input.days || 0;
|
|
20
|
+
this.hours = input.hours || 0;
|
|
21
|
+
this.minutes = input.minutes || 0;
|
|
22
|
+
this.seconds = input.seconds || 0;
|
|
23
|
+
this.milliseconds = input.milliseconds || 0;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create Duration from ISO 8601 string
|
|
27
|
+
* @example Duration.fromISO("P3Y6M4DT12H30M5S")
|
|
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
|
|
26
41
|
});
|
|
27
42
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Create a Duration between this date and another
|
|
45
|
+
*/
|
|
46
|
+
static between(from, to) {
|
|
47
|
+
const fromDT = from.toTemporal();
|
|
48
|
+
const duration = to.toTemporal().since(fromDT);
|
|
49
|
+
return new Duration({
|
|
50
|
+
years: duration.years || 0,
|
|
51
|
+
months: duration.months || 0,
|
|
33
52
|
weeks: 0,
|
|
34
|
-
days:
|
|
35
|
-
hours:
|
|
36
|
-
minutes:
|
|
37
|
-
seconds:
|
|
38
|
-
milliseconds:
|
|
53
|
+
days: duration.days || 0,
|
|
54
|
+
hours: duration.hours || 0,
|
|
55
|
+
minutes: duration.minutes || 0,
|
|
56
|
+
seconds: duration.seconds || 0,
|
|
57
|
+
milliseconds: duration.milliseconds || 0
|
|
39
58
|
});
|
|
40
59
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Create Duration from milliseconds
|
|
62
|
+
*/
|
|
63
|
+
static fromMilliseconds(ms) {
|
|
64
|
+
const isNegative = ms < 0;
|
|
65
|
+
const absMilhs = Math.abs(ms);
|
|
66
|
+
const years = Math.floor(absMilhs / (1e3 * 60 * 60 * 24 * 365));
|
|
67
|
+
const months = Math.floor(absMilhs % (1e3 * 60 * 60 * 24 * 365) / (1e3 * 60 * 60 * 24 * 30));
|
|
68
|
+
const days = Math.floor(absMilhs % (1e3 * 60 * 60 * 24 * 30) / (1e3 * 60 * 60 * 24));
|
|
69
|
+
const hours = Math.floor(absMilhs % (1e3 * 60 * 60 * 24) / (1e3 * 60 * 60));
|
|
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
|
|
51
82
|
});
|
|
52
83
|
}
|
|
53
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Get duration in specific unit
|
|
86
|
+
*/
|
|
87
|
+
as(unit) {
|
|
54
88
|
return this.asMilliseconds() / {
|
|
55
89
|
milliseconds: 1,
|
|
56
90
|
seconds: 1e3,
|
|
@@ -60,32 +94,59 @@ var e = class e {
|
|
|
60
94
|
weeks: 1e3 * 60 * 60 * 24 * 7,
|
|
61
95
|
months: 1e3 * 60 * 60 * 24 * 30,
|
|
62
96
|
years: 1e3 * 60 * 60 * 24 * 365
|
|
63
|
-
}[
|
|
97
|
+
}[unit];
|
|
64
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Get duration as milliseconds
|
|
101
|
+
*/
|
|
65
102
|
asMilliseconds() {
|
|
66
103
|
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;
|
|
67
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Get duration as seconds
|
|
107
|
+
*/
|
|
68
108
|
asSeconds() {
|
|
69
109
|
return this.asMilliseconds() / 1e3;
|
|
70
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Get duration as minutes
|
|
113
|
+
*/
|
|
71
114
|
asMinutes() {
|
|
72
115
|
return this.asSeconds() / 60;
|
|
73
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Get duration as hours
|
|
119
|
+
*/
|
|
74
120
|
asHours() {
|
|
75
121
|
return this.asMinutes() / 60;
|
|
76
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Get duration as days
|
|
125
|
+
*/
|
|
77
126
|
asDays() {
|
|
78
127
|
return this.asHours() / 24;
|
|
79
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Get duration as weeks
|
|
131
|
+
*/
|
|
80
132
|
asWeeks() {
|
|
81
133
|
return this.asDays() / 7;
|
|
82
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Get duration as months
|
|
137
|
+
*/
|
|
83
138
|
asMonths() {
|
|
84
139
|
return this.asDays() / 30;
|
|
85
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Get duration as years
|
|
143
|
+
*/
|
|
86
144
|
asYears() {
|
|
87
145
|
return this.asDays() / 365;
|
|
88
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Get all components
|
|
149
|
+
*/
|
|
89
150
|
toObject() {
|
|
90
151
|
return {
|
|
91
152
|
years: this.years,
|
|
@@ -98,23 +159,49 @@ var e = class e {
|
|
|
98
159
|
milliseconds: this.milliseconds
|
|
99
160
|
};
|
|
100
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Get ISO 8601 string representation
|
|
164
|
+
* @example "P3Y6M4DT12H30M5S"
|
|
165
|
+
*/
|
|
101
166
|
toISO() {
|
|
102
|
-
let
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
167
|
+
let datePart = "";
|
|
168
|
+
if (this.years) datePart += `${this.years}Y`;
|
|
169
|
+
if (this.months) datePart += `${this.months}M`;
|
|
170
|
+
if (this.weeks || this.days) datePart += `${this.weeks * 7 + this.days}D`;
|
|
171
|
+
let timePart = "";
|
|
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
|
+
*/
|
|
107
180
|
humanize() {
|
|
108
|
-
|
|
109
|
-
if (this.years
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
181
|
+
const parts = [];
|
|
182
|
+
if (this.years) parts.push(`${Math.abs(this.years)} year${Math.abs(this.years) !== 1 ? "s" : ""}`);
|
|
183
|
+
if (this.months) parts.push(`${Math.abs(this.months)} month${Math.abs(this.months) !== 1 ? "s" : ""}`);
|
|
184
|
+
if (this.weeks) parts.push(`${Math.abs(this.weeks)} week${Math.abs(this.weeks) !== 1 ? "s" : ""}`);
|
|
185
|
+
if (this.days) parts.push(`${Math.abs(this.days)} day${Math.abs(this.days) !== 1 ? "s" : ""}`);
|
|
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
|
+
*/
|
|
113
197
|
isNegative() {
|
|
114
198
|
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;
|
|
115
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Get absolute duration
|
|
202
|
+
*/
|
|
116
203
|
abs() {
|
|
117
|
-
return new
|
|
204
|
+
return new Duration({
|
|
118
205
|
years: Math.abs(this.years),
|
|
119
206
|
months: Math.abs(this.months),
|
|
120
207
|
weeks: Math.abs(this.weeks),
|
|
@@ -125,23 +212,40 @@ var e = class e {
|
|
|
125
212
|
milliseconds: Math.abs(this.milliseconds)
|
|
126
213
|
});
|
|
127
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* String representation
|
|
217
|
+
*/
|
|
128
218
|
toString() {
|
|
129
219
|
return this.toISO();
|
|
130
220
|
}
|
|
131
|
-
}
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* Duration Plugin
|
|
224
|
+
*/
|
|
225
|
+
var DurationPlugin = class {
|
|
132
226
|
name = "duration";
|
|
133
227
|
version = "1.0.0";
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
228
|
+
/**
|
|
229
|
+
* Install plugin into TimeGuard
|
|
230
|
+
*/
|
|
231
|
+
install(TimeGuardClass) {
|
|
232
|
+
/**
|
|
233
|
+
* Create a Duration between this date and another
|
|
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)
|
|
141
243
|
};
|
|
142
244
|
}
|
|
143
|
-
}
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* Create and export default instance
|
|
248
|
+
*/
|
|
249
|
+
var durationPlugin = new DurationPlugin();
|
|
144
250
|
//#endregion
|
|
145
|
-
export {
|
|
146
|
-
|
|
147
|
-
//# sourceMappingURL=duration.es.js.map
|
|
251
|
+
export { Duration, DurationPlugin, durationPlugin as default, durationPlugin };
|
|
@@ -1,2 +1,166 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/*! time-guard v2.7.0 | (c) 2026 Berea-Soft | MIT License | https://github.com/Berea-Soft/time-guard */
|
|
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,6 +1,6 @@
|
|
|
1
|
-
/*! time-guard v2.
|
|
1
|
+
/*! time-guard v2.7.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 DEFAULT_THRESHOLDS = [
|
|
4
4
|
{
|
|
5
5
|
l: "s",
|
|
6
6
|
r: 44,
|
|
@@ -50,7 +50,8 @@ var e = [
|
|
|
50
50
|
l: "yy",
|
|
51
51
|
d: "year"
|
|
52
52
|
}
|
|
53
|
-
]
|
|
53
|
+
];
|
|
54
|
+
var DEFAULT_FORMATS = {
|
|
54
55
|
future: "in %s",
|
|
55
56
|
past: "%s ago",
|
|
56
57
|
s: "a few seconds",
|
|
@@ -64,44 +65,72 @@ var e = [
|
|
|
64
65
|
MM: "%d months",
|
|
65
66
|
y: "a year",
|
|
66
67
|
yy: "%d years"
|
|
67
|
-
}
|
|
68
|
+
};
|
|
69
|
+
var RelativeTimePlugin = class {
|
|
68
70
|
name = "relative-time";
|
|
69
71
|
version = "1.0.0";
|
|
70
72
|
config;
|
|
71
73
|
formats;
|
|
72
|
-
constructor(
|
|
74
|
+
constructor(config) {
|
|
73
75
|
this.config = {
|
|
74
|
-
thresholds:
|
|
75
|
-
rounding:
|
|
76
|
-
}
|
|
76
|
+
thresholds: config?.thresholds || DEFAULT_THRESHOLDS,
|
|
77
|
+
rounding: config?.rounding || Math.round
|
|
78
|
+
};
|
|
79
|
+
this.formats = DEFAULT_FORMATS;
|
|
77
80
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Install plugin into TimeGuard
|
|
83
|
+
*/
|
|
84
|
+
install(TimeGuardClass) {
|
|
85
|
+
const plugin = this;
|
|
86
|
+
/**
|
|
87
|
+
* Get relative time string (e.g., "2 hours ago")
|
|
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);
|
|
86
98
|
};
|
|
87
99
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Format relative time
|
|
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);
|
|
91
110
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
111
|
+
/**
|
|
112
|
+
* Get relative time string based on milliseconds
|
|
113
|
+
*/
|
|
114
|
+
getRelativeTimeString(milliseconds) {
|
|
115
|
+
const thresholds = this.config.thresholds || DEFAULT_THRESHOLDS;
|
|
116
|
+
const rounding = this.config.rounding || Math.round;
|
|
117
|
+
for (let i = 0; i < thresholds.length; i++) {
|
|
118
|
+
const threshold = thresholds[i];
|
|
119
|
+
if ((i + 1 < thresholds.length ? thresholds[i + 1] : null) && threshold.r && milliseconds < threshold.r * 1e3) continue;
|
|
120
|
+
let value;
|
|
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;
|
|
101
127
|
}
|
|
102
|
-
return `${
|
|
128
|
+
return `${rounding(milliseconds / 1e3)} seconds`;
|
|
103
129
|
}
|
|
104
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Get milliseconds per unit
|
|
132
|
+
*/
|
|
133
|
+
getUnitMilliseconds(unit) {
|
|
105
134
|
return {
|
|
106
135
|
second: 1e3,
|
|
107
136
|
minute: 1e3 * 60,
|
|
@@ -109,16 +138,24 @@ var e = [
|
|
|
109
138
|
day: 1e3 * 60 * 60 * 24,
|
|
110
139
|
month: 1e3 * 60 * 60 * 24 * 30,
|
|
111
140
|
year: 1e3 * 60 * 60 * 24 * 365
|
|
112
|
-
}[
|
|
141
|
+
}[unit] || 1;
|
|
113
142
|
}
|
|
114
|
-
|
|
115
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Set format strings
|
|
145
|
+
*/
|
|
146
|
+
setFormats(formats) {
|
|
147
|
+
Object.assign(this.formats, formats);
|
|
116
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Get current formats
|
|
151
|
+
*/
|
|
117
152
|
getFormats() {
|
|
118
153
|
return { ...this.formats };
|
|
119
154
|
}
|
|
120
|
-
}
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Create and export default instance
|
|
158
|
+
*/
|
|
159
|
+
var relative_time_default = new RelativeTimePlugin();
|
|
121
160
|
//#endregion
|
|
122
|
-
export {
|
|
123
|
-
|
|
124
|
-
//# sourceMappingURL=relative-time.es.js.map
|
|
161
|
+
export { RelativeTimePlugin, relative_time_default as default };
|