@bbn/bbn 2.0.64 → 2.0.65
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/dist/bbn.js +1 -1
- package/dist/bbn.js.map +1 -1
- package/dist/dt/classes/date.d.ts +2 -3
- package/dist/dt/classes/date.js +0 -1
- package/dist/dt/classes/dateTime.d.ts +2 -3
- package/dist/dt/classes/dateTime.js +0 -1
- package/dist/dt/classes/monthDay.d.ts +2 -3
- package/dist/dt/classes/monthDay.js +0 -1
- package/dist/dt/classes/time.d.ts +0 -1
- package/dist/dt/classes/time.js +0 -1
- package/dist/dt/classes/yearMonth.d.ts +2 -3
- package/dist/dt/classes/yearMonth.js +0 -1
- package/dist/dt/classes/zoned.d.ts +2 -3
- package/dist/dt/classes/zoned.js +0 -1
- package/dist/dt/functions/parse.d.ts +1 -7
- package/dist/dt/vars/types.d.ts +2 -2
- package/dist/dt.d.ts +1 -2
- package/package.json +1 -1
- package/dist/dt/classes/dt.d.ts +0 -70
- package/dist/dt/classes/dt.js +0 -450
- package/dist/dt/classes/duration.d.ts +0 -39
- package/dist/dt/classes/duration.js +0 -185
- package/dist/dt/functions/getWeekday.d.ts +0 -3
- package/dist/dt/functions/getWeekday.js +0 -58
- package/dist/dt/vars/units.d.ts +0 -11
- package/dist/dt/vars/units.js +0 -109
package/dist/dt/classes/dt.js
DELETED
|
@@ -1,450 +0,0 @@
|
|
|
1
|
-
import { Temporal } from 'temporal-polyfill';
|
|
2
|
-
import substr from '../../fn/string/substr.js';
|
|
3
|
-
import { getWeekdayIndex, getWeekday } from '../functions/getWeekday.js';
|
|
4
|
-
import { unitsCorrespondence, formatsMap } from '../vars/units.js';
|
|
5
|
-
import each from '../../fn/loop/each.js';
|
|
6
|
-
import isPrimitive from '../../fn/type/isPrimitive.js';
|
|
7
|
-
import bbnDtDuration from './duration.js';
|
|
8
|
-
import parse from '../functions/parse.js';
|
|
9
|
-
import camelToCss from '../../fn/string/camelToCss.js';
|
|
10
|
-
export class bbnDt {
|
|
11
|
-
static compare(a, b, unit) {
|
|
12
|
-
if (!a || !b) {
|
|
13
|
-
throw new TypeError('Both arguments must be Temporal values');
|
|
14
|
-
}
|
|
15
|
-
if (a.constructor !== b.constructor) {
|
|
16
|
-
throw new TypeError('Cannot compare different Temporal types');
|
|
17
|
-
}
|
|
18
|
-
const Ctor = a.constructor;
|
|
19
|
-
// No unit → delegate to the built-in static compare
|
|
20
|
-
if (unit === undefined) {
|
|
21
|
-
if (typeof Ctor.compare !== 'function') {
|
|
22
|
-
throw new TypeError('This Temporal type has no static compare');
|
|
23
|
-
}
|
|
24
|
-
return Ctor.compare(a, b); // -1, 0, 1
|
|
25
|
-
}
|
|
26
|
-
// With unit → use .until() and let Temporal validate the unit
|
|
27
|
-
if (typeof a.until !== 'function') {
|
|
28
|
-
throw new TypeError('This Temporal type does not support until/since');
|
|
29
|
-
}
|
|
30
|
-
// If `unit` is invalid for this Temporal type, this will throw RangeError
|
|
31
|
-
const diff = a.until(b, { largestUnit: unit });
|
|
32
|
-
return diff.sign; // -1 / 0 / 1
|
|
33
|
-
}
|
|
34
|
-
static parse(input, format, cls = 'auto', locale) {
|
|
35
|
-
return parse(input, format, cls, locale);
|
|
36
|
-
}
|
|
37
|
-
parse(input, format) {
|
|
38
|
-
return bbnDt.parse(input, format, camelToCss(this.kind));
|
|
39
|
-
}
|
|
40
|
-
compare(other, unit) {
|
|
41
|
-
if (!(other instanceof bbnDt)) {
|
|
42
|
-
other = bbn.dt(other, this.kind);
|
|
43
|
-
}
|
|
44
|
-
return bbnDt.compare(this.value, other.value, unit);
|
|
45
|
-
}
|
|
46
|
-
add(amount, unit) {
|
|
47
|
-
if (!this.value) {
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
if (this.value instanceof Temporal.PlainMonthDay || typeof this.value.add !== 'function') {
|
|
51
|
-
throw new TypeError('This Temporal type does not support add/subtract');
|
|
52
|
-
}
|
|
53
|
-
if (amount instanceof bbnDtDuration) {
|
|
54
|
-
const d = this.value.add(amount.value);
|
|
55
|
-
return new this.constructor(d);
|
|
56
|
-
}
|
|
57
|
-
else if (typeof amount === 'object') {
|
|
58
|
-
const dur = new bbnDtDuration(amount);
|
|
59
|
-
const d = this.value.add(dur.value);
|
|
60
|
-
return new this.constructor(d);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
if (!unit) {
|
|
64
|
-
throw new Error('Unit must be specified when adding a number');
|
|
65
|
-
}
|
|
66
|
-
const dur = bbnDtDuration.fromUnit(amount, unit);
|
|
67
|
-
const d = this.value.add(dur.value);
|
|
68
|
-
return new this.constructor(d);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
subtract(amount, unit) {
|
|
72
|
-
if (!this.value) {
|
|
73
|
-
return undefined;
|
|
74
|
-
}
|
|
75
|
-
if (this.value instanceof Temporal.PlainMonthDay || typeof this.value.subtract !== 'function') {
|
|
76
|
-
throw new TypeError('This Temporal type does not support add/subtract');
|
|
77
|
-
}
|
|
78
|
-
if (amount instanceof bbnDtDuration) {
|
|
79
|
-
const d = this.value.subtract(amount.value);
|
|
80
|
-
return new this.constructor(d);
|
|
81
|
-
}
|
|
82
|
-
else if (typeof amount === 'object') {
|
|
83
|
-
const dur = new bbnDtDuration(amount);
|
|
84
|
-
const d = this.value.subtract(dur.value);
|
|
85
|
-
return new this.constructor(d);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
if (!unit) {
|
|
89
|
-
throw new Error('Unit must be specified when adding a number');
|
|
90
|
-
}
|
|
91
|
-
const dur = bbnDtDuration.fromUnit(amount, unit);
|
|
92
|
-
const d = this.value.subtract(dur.value);
|
|
93
|
-
return new this.constructor(d);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
isBefore(other) {
|
|
97
|
-
return this.compare(other) < 0;
|
|
98
|
-
}
|
|
99
|
-
isAfter(other) {
|
|
100
|
-
return this.compare(other) > 0;
|
|
101
|
-
}
|
|
102
|
-
isSame(other) {
|
|
103
|
-
return this.compare(other) === 0;
|
|
104
|
-
}
|
|
105
|
-
equals(other) {
|
|
106
|
-
return this.isSame(other);
|
|
107
|
-
}
|
|
108
|
-
// ---- Serialization ----
|
|
109
|
-
toJSON() {
|
|
110
|
-
return {
|
|
111
|
-
kind: this.kind,
|
|
112
|
-
value: String(this.value)
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
toString() {
|
|
116
|
-
return String(this.value);
|
|
117
|
-
}
|
|
118
|
-
year(v) {
|
|
119
|
-
if (!this.value) {
|
|
120
|
-
return undefined;
|
|
121
|
-
}
|
|
122
|
-
if (!('year' in this.value)) {
|
|
123
|
-
throw new Error('year() is not supported for this type');
|
|
124
|
-
}
|
|
125
|
-
if ((v !== undefined) && !(v instanceof Event)) {
|
|
126
|
-
const d = this.value.with({ year: v });
|
|
127
|
-
return new this.constructor(d);
|
|
128
|
-
}
|
|
129
|
-
return this.value.year;
|
|
130
|
-
}
|
|
131
|
-
month(v) {
|
|
132
|
-
if (!this.value) {
|
|
133
|
-
return undefined;
|
|
134
|
-
}
|
|
135
|
-
if (!('month' in this.value)) {
|
|
136
|
-
throw new Error('month() is not supported for this type');
|
|
137
|
-
}
|
|
138
|
-
if ((v !== undefined) && !(v instanceof Event)) {
|
|
139
|
-
const d = this.value.with({ month: v });
|
|
140
|
-
return new this.constructor(d);
|
|
141
|
-
}
|
|
142
|
-
return this.value.month;
|
|
143
|
-
}
|
|
144
|
-
day(v) {
|
|
145
|
-
if (!this.value) {
|
|
146
|
-
return undefined;
|
|
147
|
-
}
|
|
148
|
-
if (!('day' in this.value)) {
|
|
149
|
-
throw new Error('day() is not supported for this type');
|
|
150
|
-
}
|
|
151
|
-
if ((v !== undefined) && !(v instanceof Event)) {
|
|
152
|
-
const d = this.value.with({ day: v });
|
|
153
|
-
return new this.constructor(d);
|
|
154
|
-
}
|
|
155
|
-
return this.value.day;
|
|
156
|
-
}
|
|
157
|
-
hour(v) {
|
|
158
|
-
if (!this.value) {
|
|
159
|
-
return undefined;
|
|
160
|
-
}
|
|
161
|
-
if (!('hour' in this.value)) {
|
|
162
|
-
throw new Error('hour() is not supported for this type');
|
|
163
|
-
}
|
|
164
|
-
if ((v !== undefined) && !(v instanceof Event)) {
|
|
165
|
-
const d = this.value.with({ hour: v });
|
|
166
|
-
return new this.constructor(d);
|
|
167
|
-
}
|
|
168
|
-
return this.value.hour;
|
|
169
|
-
}
|
|
170
|
-
minute(v) {
|
|
171
|
-
if (!this.value) {
|
|
172
|
-
return undefined;
|
|
173
|
-
}
|
|
174
|
-
if (!('minute' in this.value)) {
|
|
175
|
-
throw new Error('minute() is not supported for this type');
|
|
176
|
-
}
|
|
177
|
-
if ((v !== undefined) && !(v instanceof Event)) {
|
|
178
|
-
const d = this.value.with({ minute: v });
|
|
179
|
-
return new this.constructor(d);
|
|
180
|
-
}
|
|
181
|
-
return this.value.minute;
|
|
182
|
-
}
|
|
183
|
-
second(v) {
|
|
184
|
-
if (!this.value) {
|
|
185
|
-
return undefined;
|
|
186
|
-
}
|
|
187
|
-
if (!('second' in this.value)) {
|
|
188
|
-
throw new Error('second() is not supported for this type');
|
|
189
|
-
}
|
|
190
|
-
if ((v !== undefined) && !(v instanceof Event)) {
|
|
191
|
-
const d = this.value.with({ second: v });
|
|
192
|
-
return new this.constructor(d);
|
|
193
|
-
}
|
|
194
|
-
return this.value.second;
|
|
195
|
-
}
|
|
196
|
-
weekday(v, past) {
|
|
197
|
-
if (!this.value) {
|
|
198
|
-
return undefined;
|
|
199
|
-
}
|
|
200
|
-
if (!('dayOfWeek' in this.value)) {
|
|
201
|
-
throw new Error('weekday() is not supported for this type');
|
|
202
|
-
}
|
|
203
|
-
if ((v !== undefined) && !(v instanceof Event)) {
|
|
204
|
-
return this.setWeekday(v, past);
|
|
205
|
-
}
|
|
206
|
-
return this.value.dayOfWeek;
|
|
207
|
-
}
|
|
208
|
-
date(v) {
|
|
209
|
-
if (!this.value) {
|
|
210
|
-
return undefined;
|
|
211
|
-
}
|
|
212
|
-
if (!('year' in this.value) || !('month' in this.value) || !('day' in this.value)) {
|
|
213
|
-
throw new Error('date() is not supported for this type');
|
|
214
|
-
}
|
|
215
|
-
return this.parse(v, 'Y-m-d');
|
|
216
|
-
}
|
|
217
|
-
datetime(v) {
|
|
218
|
-
if (0 in arguments && (v !== undefined) && !(v instanceof Event)) {
|
|
219
|
-
return this.parse(v, 'Y-m-d H:i:s');
|
|
220
|
-
}
|
|
221
|
-
return this.format('Y-m-d H:i:s');
|
|
222
|
-
}
|
|
223
|
-
time(v) {
|
|
224
|
-
if (0 in arguments && (v !== undefined) && !(v instanceof Event)) {
|
|
225
|
-
return this.parse(v, 'H:i:s');
|
|
226
|
-
}
|
|
227
|
-
return this.format('H:i:s');
|
|
228
|
-
}
|
|
229
|
-
week() {
|
|
230
|
-
if (!this.value) {
|
|
231
|
-
return undefined;
|
|
232
|
-
}
|
|
233
|
-
if (!('weekOfYear' in this.value)) {
|
|
234
|
-
throw new Error('week() is not supported for this type');
|
|
235
|
-
}
|
|
236
|
-
return this.value.weekOfYear;
|
|
237
|
-
}
|
|
238
|
-
get YYYY() {
|
|
239
|
-
if ('year' in this.value) {
|
|
240
|
-
return this.year().toString();
|
|
241
|
-
}
|
|
242
|
-
return undefined;
|
|
243
|
-
}
|
|
244
|
-
get YY() {
|
|
245
|
-
if ('year' in this.value) {
|
|
246
|
-
return substr(this.year().toString(), 2, 2);
|
|
247
|
-
}
|
|
248
|
-
return undefined;
|
|
249
|
-
}
|
|
250
|
-
get MMMM() {
|
|
251
|
-
if ('month' in this.value) {
|
|
252
|
-
return this.format('MMMM');
|
|
253
|
-
}
|
|
254
|
-
return undefined;
|
|
255
|
-
}
|
|
256
|
-
get MMM() {
|
|
257
|
-
if ('month' in this.value) {
|
|
258
|
-
return this.format('MMM');
|
|
259
|
-
}
|
|
260
|
-
return undefined;
|
|
261
|
-
}
|
|
262
|
-
get MM() {
|
|
263
|
-
if ('month' in this.value) {
|
|
264
|
-
const m = parseInt(this.month().toString());
|
|
265
|
-
return m < 10 ? '0' + m.toString() : m.toString();
|
|
266
|
-
}
|
|
267
|
-
return undefined;
|
|
268
|
-
}
|
|
269
|
-
get M() {
|
|
270
|
-
if ('month' in this.value) {
|
|
271
|
-
return this.month().toString();
|
|
272
|
-
}
|
|
273
|
-
return undefined;
|
|
274
|
-
}
|
|
275
|
-
get EE() {
|
|
276
|
-
if ('dayOfWeek' in this.value) {
|
|
277
|
-
return this.value.dayOfWeek.toString();
|
|
278
|
-
}
|
|
279
|
-
return undefined;
|
|
280
|
-
}
|
|
281
|
-
get DD() {
|
|
282
|
-
if ('day' in this.value) {
|
|
283
|
-
const d = parseInt(this.day().toString());
|
|
284
|
-
return d < 10 ? '0' + d.toString() : d.toString();
|
|
285
|
-
}
|
|
286
|
-
return undefined;
|
|
287
|
-
}
|
|
288
|
-
get d() {
|
|
289
|
-
if ('day' in this.value) {
|
|
290
|
-
return this.day().toString();
|
|
291
|
-
}
|
|
292
|
-
return undefined;
|
|
293
|
-
}
|
|
294
|
-
get dddd() {
|
|
295
|
-
if ('dayOfWeek' in this.value) {
|
|
296
|
-
return getWeekday(0, 'long');
|
|
297
|
-
}
|
|
298
|
-
return undefined;
|
|
299
|
-
}
|
|
300
|
-
get ddd() {
|
|
301
|
-
if ('day' in this.value) {
|
|
302
|
-
return getWeekday(0, 'short');
|
|
303
|
-
}
|
|
304
|
-
return undefined;
|
|
305
|
-
}
|
|
306
|
-
get D() {
|
|
307
|
-
if ('day' in this.value) {
|
|
308
|
-
return this.day().toString();
|
|
309
|
-
}
|
|
310
|
-
return undefined;
|
|
311
|
-
}
|
|
312
|
-
get HH() {
|
|
313
|
-
if ('hour' in this.value) {
|
|
314
|
-
const h = parseInt(this.hour().toString());
|
|
315
|
-
return h < 10 ? '0' + h.toString() : h.toString();
|
|
316
|
-
}
|
|
317
|
-
return undefined;
|
|
318
|
-
}
|
|
319
|
-
get H() {
|
|
320
|
-
if ('hour' in this.value) {
|
|
321
|
-
return this.hour().toString();
|
|
322
|
-
}
|
|
323
|
-
return undefined;
|
|
324
|
-
}
|
|
325
|
-
get II() {
|
|
326
|
-
if ('minute' in this.value) {
|
|
327
|
-
const i = parseInt(this.minute().toString());
|
|
328
|
-
return i < 10 ? '0' + i.toString() : i.toString();
|
|
329
|
-
}
|
|
330
|
-
return undefined;
|
|
331
|
-
}
|
|
332
|
-
get mm() {
|
|
333
|
-
if ('minute' in this.value) {
|
|
334
|
-
const i = parseInt(this.minute().toString());
|
|
335
|
-
return i < 10 ? '0' + i.toString() : i.toString();
|
|
336
|
-
}
|
|
337
|
-
return undefined;
|
|
338
|
-
}
|
|
339
|
-
get I() {
|
|
340
|
-
if ('minute' in this.value) {
|
|
341
|
-
return this.minute().toString();
|
|
342
|
-
}
|
|
343
|
-
return undefined;
|
|
344
|
-
}
|
|
345
|
-
get SS() {
|
|
346
|
-
if ('second' in this.value) {
|
|
347
|
-
const s = parseInt(this.second().toString());
|
|
348
|
-
return s < 10 ? '0' + s.toString() : s.toString();
|
|
349
|
-
}
|
|
350
|
-
return undefined;
|
|
351
|
-
}
|
|
352
|
-
get S() {
|
|
353
|
-
if ('second' in this.value) {
|
|
354
|
-
return this.second().toString();
|
|
355
|
-
}
|
|
356
|
-
return undefined;
|
|
357
|
-
}
|
|
358
|
-
get WW() {
|
|
359
|
-
if ('weekOfYear' in this.value) {
|
|
360
|
-
return this.value.weekOfYear.toString().padStart(2, '0');
|
|
361
|
-
}
|
|
362
|
-
return undefined;
|
|
363
|
-
}
|
|
364
|
-
get W() {
|
|
365
|
-
if ('weekOfYear' in this.value) {
|
|
366
|
-
return this.value.weekOfYear.toString();
|
|
367
|
-
}
|
|
368
|
-
return undefined;
|
|
369
|
-
}
|
|
370
|
-
format(format = 'YYYY-MM-DD HH:II:SS') {
|
|
371
|
-
let str = '';
|
|
372
|
-
if (format) {
|
|
373
|
-
const reg = new RegExp('(\[|\]|' + Object.keys(unitsCorrespondence).join('|') + ')', 'g');
|
|
374
|
-
let opened = 0;
|
|
375
|
-
const parts = format.split(reg);
|
|
376
|
-
each(parts, (part) => {
|
|
377
|
-
if (part === '[') {
|
|
378
|
-
opened++;
|
|
379
|
-
return;
|
|
380
|
-
}
|
|
381
|
-
else if (part === ']') {
|
|
382
|
-
opened--;
|
|
383
|
-
return;
|
|
384
|
-
}
|
|
385
|
-
if (opened > 0) {
|
|
386
|
-
str += part;
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
if (part in unitsCorrespondence) {
|
|
390
|
-
if (part in this && isPrimitive(this[part])) {
|
|
391
|
-
str += this[part];
|
|
392
|
-
}
|
|
393
|
-
else {
|
|
394
|
-
const suffix = formatsMap[unitsCorrespondence[part]];
|
|
395
|
-
str += this[suffix];
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
str += part;
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
return str;
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* Returns a NEW date that is the next (or previous if past=true)
|
|
407
|
-
* occurrence of the given weekday, starting from this.#value.
|
|
408
|
-
*
|
|
409
|
-
* @param {number|string} weekday - Weekday index (0=Sunday…6=Saturday)
|
|
410
|
-
* or a localized weekday name.
|
|
411
|
-
* @param {boolean} past - If true → return previous occurrence instead of next.
|
|
412
|
-
* @param {string} [locale] - Optional locale for weekday names.
|
|
413
|
-
*/
|
|
414
|
-
setWeekday(weekday, past = false, locale) {
|
|
415
|
-
let targetDay;
|
|
416
|
-
if (typeof weekday === "string") {
|
|
417
|
-
// Use your previously defined reverse method:
|
|
418
|
-
weekday = getWeekdayIndex(weekday, locale);
|
|
419
|
-
}
|
|
420
|
-
// --- Normalize weekday ---
|
|
421
|
-
if (typeof weekday === "number") {
|
|
422
|
-
if (weekday < 0 || weekday > 6) {
|
|
423
|
-
throw new RangeError("weekday number must be between 0 and 6");
|
|
424
|
-
}
|
|
425
|
-
targetDay = weekday;
|
|
426
|
-
}
|
|
427
|
-
else {
|
|
428
|
-
throw new TypeError("weekday must be a number (0–6) or a string");
|
|
429
|
-
}
|
|
430
|
-
const currentDay = this.weekday(); // JS weekday (0–6)
|
|
431
|
-
let diff;
|
|
432
|
-
if (!past) {
|
|
433
|
-
// ---------- NEXT occurrence ----------
|
|
434
|
-
diff = (targetDay - currentDay + 7) % 7;
|
|
435
|
-
if (diff === 0) {
|
|
436
|
-
diff = 7; // next week if same day
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
else {
|
|
440
|
-
// ---------- PREVIOUS occurrence ----------
|
|
441
|
-
diff = (currentDay - targetDay + 7) % 7;
|
|
442
|
-
if (diff === 0) {
|
|
443
|
-
diff = 7; // previous week if same day
|
|
444
|
-
}
|
|
445
|
-
diff = -diff;
|
|
446
|
-
}
|
|
447
|
-
return this.add(diff, 'd');
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
export default bbnDt;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { Temporal } from 'temporal-polyfill';
|
|
2
|
-
export default class bbnDtDuration {
|
|
3
|
-
#private;
|
|
4
|
-
static fromUnit(value: number, unit: string): bbnDtDuration;
|
|
5
|
-
constructor(y: Temporal.Duration | number | object, m?: number, d?: number, h?: number, i?: number, s?: number, ms?: number, unit?: string);
|
|
6
|
-
get value(): Temporal.Duration;
|
|
7
|
-
years(remaining?: boolean): number;
|
|
8
|
-
months(remaining?: boolean): number;
|
|
9
|
-
weeks(remaining?: boolean): number;
|
|
10
|
-
days(remaining?: boolean): number;
|
|
11
|
-
hours(remaining?: boolean): number;
|
|
12
|
-
minutes(remaining?: boolean): number;
|
|
13
|
-
seconds(remaining?: boolean): number;
|
|
14
|
-
toJSON(): {
|
|
15
|
-
years: number;
|
|
16
|
-
months: number;
|
|
17
|
-
days: number;
|
|
18
|
-
hours: number;
|
|
19
|
-
minutes: number;
|
|
20
|
-
seconds: number;
|
|
21
|
-
milliseconds: number;
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Returns the full duration expressed as X (float), like Day.js.
|
|
25
|
-
*/
|
|
26
|
-
asYears(): number;
|
|
27
|
-
asMonths(): number;
|
|
28
|
-
asWeeks(): number;
|
|
29
|
-
asDays(): number;
|
|
30
|
-
asHours(): number;
|
|
31
|
-
asMinutes(): number;
|
|
32
|
-
asSeconds(): number;
|
|
33
|
-
/**
|
|
34
|
-
* Add any unit (or instance default).
|
|
35
|
-
*/
|
|
36
|
-
add(value: number, unit?: string): bbnDtDuration;
|
|
37
|
-
subtract(value: number, unit?: string): bbnDtDuration;
|
|
38
|
-
toMilliseconds(): number;
|
|
39
|
-
}
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _bbnDtDuration_instances, _bbnDtDuration_value, _bbnDtDuration_unit, _bbnDtDuration_getUnitValue;
|
|
13
|
-
import { Temporal } from 'temporal-polyfill';
|
|
14
|
-
import { units, unitsCorrespondence } from '../vars/units.js';
|
|
15
|
-
import getRow from '../../fn/object/getRow.js';
|
|
16
|
-
const DURATION_RELATIVE_TO = Temporal.ZonedDateTime.from('1970-01-01T00:00Z[UTC]');
|
|
17
|
-
class bbnDtDuration {
|
|
18
|
-
static fromUnit(value, unit) {
|
|
19
|
-
const realUnit = unitsCorrespondence[unit] || 'ms';
|
|
20
|
-
if (!realUnit) {
|
|
21
|
-
throw new Error('Invalid unit for duration: ' + unit);
|
|
22
|
-
}
|
|
23
|
-
const ctx = [
|
|
24
|
-
realUnit === 'y' ? value : 0,
|
|
25
|
-
realUnit === 'm' ? value : 0,
|
|
26
|
-
realUnit === 'd' ? value : 0,
|
|
27
|
-
realUnit === 'h' ? value : 0,
|
|
28
|
-
realUnit === 'i' ? value : 0,
|
|
29
|
-
realUnit === 's' ? value : 0,
|
|
30
|
-
['y', 'm', 'd', 'h', 'i', 's'].includes(realUnit) ? 0 : value
|
|
31
|
-
];
|
|
32
|
-
const dur = new Temporal.Duration(...ctx);
|
|
33
|
-
return new bbnDtDuration(dur, 0, 0, 0, 0, 0, 0, realUnit);
|
|
34
|
-
}
|
|
35
|
-
constructor(y, m, d, h, i, s, ms, unit) {
|
|
36
|
-
_bbnDtDuration_instances.add(this);
|
|
37
|
-
_bbnDtDuration_value.set(this, void 0);
|
|
38
|
-
_bbnDtDuration_unit.set(this, void 0);
|
|
39
|
-
if (y instanceof Temporal.Duration) {
|
|
40
|
-
__classPrivateFieldSet(this, _bbnDtDuration_value, y, "f");
|
|
41
|
-
}
|
|
42
|
-
else if (typeof y === 'object') {
|
|
43
|
-
__classPrivateFieldSet(this, _bbnDtDuration_value, new Temporal.Duration(y.years || 0, y.months || 0, y.weeks || 0, y.days || 0, y.hours || 0, y.minutes || 0, y.seconds || 0, y.milliseconds || 0, 0, 0), "f");
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
__classPrivateFieldSet(this, _bbnDtDuration_value, new Temporal.Duration(y || 0, m || 0, d || 0, h || 0, i || 0, s || 0, ms || 0, 0, 0), "f");
|
|
47
|
-
}
|
|
48
|
-
const realUnit = unitsCorrespondence[unit || ''] || unit;
|
|
49
|
-
__classPrivateFieldSet(this, _bbnDtDuration_unit, realUnit || 'ms', "f");
|
|
50
|
-
const row = getRow(units, (a) => a[0] === realUnit);
|
|
51
|
-
if (!row) {
|
|
52
|
-
throw new Error('Invalid unit for duration: ' + realUnit);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
get value() {
|
|
56
|
-
return __classPrivateFieldGet(this, _bbnDtDuration_value, "f");
|
|
57
|
-
}
|
|
58
|
-
// -----------------------
|
|
59
|
-
// Public getters
|
|
60
|
-
// -----------------------
|
|
61
|
-
years(remaining = false) { return __classPrivateFieldGet(this, _bbnDtDuration_instances, "m", _bbnDtDuration_getUnitValue).call(this, 'year', remaining); }
|
|
62
|
-
months(remaining = false) { return __classPrivateFieldGet(this, _bbnDtDuration_instances, "m", _bbnDtDuration_getUnitValue).call(this, 'month', remaining); }
|
|
63
|
-
weeks(remaining = false) { return __classPrivateFieldGet(this, _bbnDtDuration_instances, "m", _bbnDtDuration_getUnitValue).call(this, 'week', remaining); }
|
|
64
|
-
days(remaining = false) { return __classPrivateFieldGet(this, _bbnDtDuration_instances, "m", _bbnDtDuration_getUnitValue).call(this, 'day', remaining); }
|
|
65
|
-
hours(remaining = false) { return __classPrivateFieldGet(this, _bbnDtDuration_instances, "m", _bbnDtDuration_getUnitValue).call(this, 'hour', remaining); }
|
|
66
|
-
minutes(remaining = false) { return __classPrivateFieldGet(this, _bbnDtDuration_instances, "m", _bbnDtDuration_getUnitValue).call(this, 'minute', remaining); }
|
|
67
|
-
seconds(remaining = false) { return __classPrivateFieldGet(this, _bbnDtDuration_instances, "m", _bbnDtDuration_getUnitValue).call(this, 'second', remaining); }
|
|
68
|
-
// -----------------------
|
|
69
|
-
// Day.js style
|
|
70
|
-
// "asX" conversions
|
|
71
|
-
// -----------------------
|
|
72
|
-
toJSON() {
|
|
73
|
-
return {
|
|
74
|
-
years: this.years(true),
|
|
75
|
-
months: this.months(true),
|
|
76
|
-
days: this.days(true),
|
|
77
|
-
hours: this.hours(true),
|
|
78
|
-
minutes: this.minutes(true),
|
|
79
|
-
seconds: this.seconds(true),
|
|
80
|
-
milliseconds: this.toMilliseconds()
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Returns the full duration expressed as X (float), like Day.js.
|
|
85
|
-
*/
|
|
86
|
-
asYears() {
|
|
87
|
-
return __classPrivateFieldGet(this, _bbnDtDuration_value, "f").total({
|
|
88
|
-
unit: 'year',
|
|
89
|
-
relativeTo: DURATION_RELATIVE_TO
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
asMonths() {
|
|
93
|
-
return __classPrivateFieldGet(this, _bbnDtDuration_value, "f").total({
|
|
94
|
-
unit: 'month',
|
|
95
|
-
relativeTo: DURATION_RELATIVE_TO
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
asWeeks() {
|
|
99
|
-
return __classPrivateFieldGet(this, _bbnDtDuration_value, "f").total({
|
|
100
|
-
unit: 'week',
|
|
101
|
-
relativeTo: DURATION_RELATIVE_TO
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
asDays() {
|
|
105
|
-
return __classPrivateFieldGet(this, _bbnDtDuration_value, "f").total({
|
|
106
|
-
unit: 'day',
|
|
107
|
-
relativeTo: DURATION_RELATIVE_TO
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
asHours() {
|
|
111
|
-
return __classPrivateFieldGet(this, _bbnDtDuration_value, "f").total({
|
|
112
|
-
unit: 'hour',
|
|
113
|
-
relativeTo: DURATION_RELATIVE_TO
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
asMinutes() {
|
|
117
|
-
return __classPrivateFieldGet(this, _bbnDtDuration_value, "f").total({
|
|
118
|
-
unit: 'minute',
|
|
119
|
-
relativeTo: DURATION_RELATIVE_TO
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
asSeconds() {
|
|
123
|
-
return __classPrivateFieldGet(this, _bbnDtDuration_value, "f").total({
|
|
124
|
-
unit: 'second',
|
|
125
|
-
relativeTo: DURATION_RELATIVE_TO
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Add any unit (or instance default).
|
|
130
|
-
*/
|
|
131
|
-
add(value, unit) {
|
|
132
|
-
const targetUnit = unit
|
|
133
|
-
? (unitsCorrespondence[unit] || unit)
|
|
134
|
-
: __classPrivateFieldGet(this, _bbnDtDuration_unit, "f");
|
|
135
|
-
// Map to Temporal.DurationLike field name, e.g. 'year' → 'years'
|
|
136
|
-
const field = (targetUnit + 's');
|
|
137
|
-
if (!['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds', 'milliseconds'].includes(field)) {
|
|
138
|
-
throw new Error('Invalid unit for duration: ' + (unit !== null && unit !== void 0 ? unit : targetUnit));
|
|
139
|
-
}
|
|
140
|
-
const delta = { [field]: value };
|
|
141
|
-
const newDuration = __classPrivateFieldGet(this, _bbnDtDuration_value, "f").add(delta);
|
|
142
|
-
// Adapt this constructor call to however you now construct your duration:
|
|
143
|
-
return new bbnDtDuration(newDuration, undefined, undefined, undefined, undefined, undefined, undefined, __classPrivateFieldGet(this, _bbnDtDuration_unit, "f"));
|
|
144
|
-
}
|
|
145
|
-
subtract(value, unit) {
|
|
146
|
-
return this.add(-value, unit);
|
|
147
|
-
}
|
|
148
|
-
toMilliseconds() {
|
|
149
|
-
const d = __classPrivateFieldGet(this, _bbnDtDuration_value, "f");
|
|
150
|
-
// If there are no calendar units, we can let Temporal do it directly:
|
|
151
|
-
if (!d.years && !d.months && !d.weeks && !d.days) {
|
|
152
|
-
return d.total({ unit: 'millisecond' });
|
|
153
|
-
}
|
|
154
|
-
// Otherwise we must supply a relativeTo (same as in asX)
|
|
155
|
-
return d.total({
|
|
156
|
-
unit: 'millisecond',
|
|
157
|
-
relativeTo: DURATION_RELATIVE_TO
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
_bbnDtDuration_value = new WeakMap(), _bbnDtDuration_unit = new WeakMap(), _bbnDtDuration_instances = new WeakSet(), _bbnDtDuration_getUnitValue = function _bbnDtDuration_getUnitValue(name, remaining) {
|
|
162
|
-
const d = __classPrivateFieldGet(this, _bbnDtDuration_value, "f"); // Temporal.Duration
|
|
163
|
-
if (remaining) {
|
|
164
|
-
switch (name) {
|
|
165
|
-
case 'year': return d.years;
|
|
166
|
-
case 'month': return d.months;
|
|
167
|
-
case 'week': return d.weeks;
|
|
168
|
-
case 'day': return d.days;
|
|
169
|
-
case 'hour': return d.hours;
|
|
170
|
-
case 'minute': return d.minutes;
|
|
171
|
-
case 'second':
|
|
172
|
-
// seconds component only; sub-second parts go to milliseconds in toJSON()
|
|
173
|
-
return d.seconds;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
// Total units: use Duration.total()
|
|
177
|
-
const total = d.total({
|
|
178
|
-
unit: name,
|
|
179
|
-
relativeTo: DURATION_RELATIVE_TO
|
|
180
|
-
});
|
|
181
|
-
// Keep same semantics as old code (Math.floor on totals)
|
|
182
|
-
return Math.floor(total);
|
|
183
|
-
};
|
|
184
|
-
export default bbnDtDuration;
|
|
185
|
-
;
|