@bbn/bbn 2.0.77 → 2.0.79
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/dt/classes/dt.d.ts
CHANGED
|
@@ -86,7 +86,8 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
|
|
|
86
86
|
get isValid(): boolean;
|
|
87
87
|
format(format?: string): string;
|
|
88
88
|
matchFormat(value: any, format: string): boolean;
|
|
89
|
-
getWeekday(n: 0 | 1 | 2 | 3 | 4 | 5 | 6, mode?: string, locale?: string): string;
|
|
89
|
+
getWeekday(n: 0 | 1 | 2 | 3 | 4 | 5 | 6, mode?: string, locale?: string): string | object;
|
|
90
|
+
getWeekdayIndex: (name: string, locale?: string) => number;
|
|
90
91
|
/**
|
|
91
92
|
* Returns a NEW date that is the next (or previous if past=true)
|
|
92
93
|
* occurrence of the given weekday, starting from this.#value.
|
package/dist/dt/classes/dt.js
CHANGED
|
@@ -11,7 +11,6 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
11
11
|
};
|
|
12
12
|
var _bbnDt_value;
|
|
13
13
|
import { Temporal } from 'temporal-polyfill';
|
|
14
|
-
import { getWeekdayIndex, getWeekday } from '../functions/getWeekday.js';
|
|
15
14
|
import { unitsCorrespondence, units, formatsMap } from '../vars/units.js';
|
|
16
15
|
import _ from '../../_.js';
|
|
17
16
|
import substr from '../../fn/string/substr.js';
|
|
@@ -23,6 +22,28 @@ import camelToCss from '../../fn/string/camelToCss.js';
|
|
|
23
22
|
export class bbnDt {
|
|
24
23
|
constructor(value) {
|
|
25
24
|
_bbnDt_value.set(this, void 0);
|
|
25
|
+
this.getWeekdayIndex = (name, locale) => {
|
|
26
|
+
const loc = locale || bbn.env.lang;
|
|
27
|
+
const input = name.trim().toLowerCase();
|
|
28
|
+
// Build a localized map only once per locale (optional optimization)
|
|
29
|
+
const langs = [loc, ...navigator.languages];
|
|
30
|
+
for (let i = 0; i < langs.length; i++) {
|
|
31
|
+
if (!langs[i]) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const formatter = new Intl.DateTimeFormat(langs[i], { weekday: "long" });
|
|
35
|
+
// Generate localized weekday names for Sun → Sat
|
|
36
|
+
for (let i = 0; i < 7; i++) {
|
|
37
|
+
// 2023-01-01 was Sunday
|
|
38
|
+
const date = new Date(2023, 0, 1 + i);
|
|
39
|
+
const localized = formatter.format(date).toLowerCase();
|
|
40
|
+
if (localized === input) {
|
|
41
|
+
return i; // JS weekday number
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
throw new Error(`Unknown weekday name '${name}' for locale '${loc}'`);
|
|
46
|
+
};
|
|
26
47
|
__classPrivateFieldSet(this, _bbnDt_value, value, "f");
|
|
27
48
|
}
|
|
28
49
|
get value() {
|
|
@@ -216,7 +237,7 @@ export class bbnDt {
|
|
|
216
237
|
if (!(other instanceof bbnDt)) {
|
|
217
238
|
other = bbn.dt(other, null, this.kind);
|
|
218
239
|
}
|
|
219
|
-
return bbnDt.compare(this
|
|
240
|
+
return bbnDt.compare(this, other, unit);
|
|
220
241
|
}
|
|
221
242
|
add(amount, unit) {
|
|
222
243
|
if (!this.value) {
|
|
@@ -477,13 +498,13 @@ export class bbnDt {
|
|
|
477
498
|
}
|
|
478
499
|
get dddd() {
|
|
479
500
|
if ('dayOfWeek' in this.value) {
|
|
480
|
-
return getWeekday(0, 'long');
|
|
501
|
+
return this.getWeekday(0, 'long');
|
|
481
502
|
}
|
|
482
503
|
return undefined;
|
|
483
504
|
}
|
|
484
505
|
get ddd() {
|
|
485
506
|
if ('day' in this.value) {
|
|
486
|
-
return getWeekday(0, 'short');
|
|
507
|
+
return this.getWeekday(0, 'short');
|
|
487
508
|
}
|
|
488
509
|
return undefined;
|
|
489
510
|
}
|
|
@@ -599,7 +620,39 @@ export class bbnDt {
|
|
|
599
620
|
}
|
|
600
621
|
}
|
|
601
622
|
getWeekday(n, mode = 'long', locale) {
|
|
602
|
-
|
|
623
|
+
if (!mode) {
|
|
624
|
+
const letter = this.getWeekday(n, 'narrow', locale);
|
|
625
|
+
const abbr = this.getWeekday(n, 'short', locale);
|
|
626
|
+
const full = this.getWeekday(n, 'long', locale);
|
|
627
|
+
return {
|
|
628
|
+
letter,
|
|
629
|
+
abbr,
|
|
630
|
+
full,
|
|
631
|
+
long: full,
|
|
632
|
+
short: abbr,
|
|
633
|
+
narrow: letter
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
let m;
|
|
637
|
+
if (mode === 'letter') {
|
|
638
|
+
m = 'narrow';
|
|
639
|
+
}
|
|
640
|
+
else if (mode === 'abbr') {
|
|
641
|
+
m = 'short';
|
|
642
|
+
}
|
|
643
|
+
else if (mode === 'full') {
|
|
644
|
+
m = 'long';
|
|
645
|
+
}
|
|
646
|
+
else if (!['long', 'short', 'narrow'].includes(mode)) {
|
|
647
|
+
throw new Error('Invalid mode for getWeekDay: ' + mode + '. Allowed values are "long", "short", "narrow", "letter", "abbr", "full".');
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
m = mode;
|
|
651
|
+
}
|
|
652
|
+
// Create a date with the right weekday
|
|
653
|
+
// 2023-01-01 was a Sunday → base for offset
|
|
654
|
+
const base = new Date(2023, 0, 1 + n);
|
|
655
|
+
return base.toLocaleDateString([locale || bbn.env.lang, ...navigator.languages], { weekday: m });
|
|
603
656
|
}
|
|
604
657
|
/**
|
|
605
658
|
* Returns a NEW date that is the next (or previous if past=true)
|
|
@@ -614,7 +667,7 @@ export class bbnDt {
|
|
|
614
667
|
let targetDay;
|
|
615
668
|
if (typeof weekday === "string") {
|
|
616
669
|
// Use your previously defined reverse method:
|
|
617
|
-
weekday = getWeekdayIndex(weekday, locale);
|
|
670
|
+
weekday = this.getWeekdayIndex(weekday, locale);
|
|
618
671
|
}
|
|
619
672
|
// --- Normalize weekday ---
|
|
620
673
|
if (typeof weekday === "number") {
|
package/package.json
CHANGED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
const getWeekday = (n, mode = 'long', locale) => {
|
|
2
|
-
if (!mode) {
|
|
3
|
-
const letter = getWeekday(n, 'narrow', locale);
|
|
4
|
-
const abbr = getWeekday(n, 'short', locale);
|
|
5
|
-
const full = getWeekday(n, 'long', locale);
|
|
6
|
-
return {
|
|
7
|
-
letter,
|
|
8
|
-
abbr,
|
|
9
|
-
full,
|
|
10
|
-
long: full,
|
|
11
|
-
short: abbr,
|
|
12
|
-
narrow: letter
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
let m;
|
|
16
|
-
if (mode === 'letter') {
|
|
17
|
-
m = 'narrow';
|
|
18
|
-
}
|
|
19
|
-
else if (mode === 'abbr') {
|
|
20
|
-
m = 'short';
|
|
21
|
-
}
|
|
22
|
-
else if (mode === 'full') {
|
|
23
|
-
m = 'long';
|
|
24
|
-
}
|
|
25
|
-
else if (!['long', 'short', 'narrow'].includes(mode)) {
|
|
26
|
-
throw new Error('Invalid mode for getWeekDay: ' + mode + '. Allowed values are "long", "short", "narrow", "letter", "abbr", "full".');
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
m = mode;
|
|
30
|
-
}
|
|
31
|
-
// Create a date with the right weekday
|
|
32
|
-
// 2023-01-01 was a Sunday → base for offset
|
|
33
|
-
const base = new Date(2023, 0, 1 + n);
|
|
34
|
-
return base.toLocaleDateString([locale || bbn.env.lang, ...navigator.languages], { weekday: m });
|
|
35
|
-
};
|
|
36
|
-
const getWeekdayIndex = (name, locale) => {
|
|
37
|
-
const loc = locale || bbn.env.lang;
|
|
38
|
-
const input = name.trim().toLowerCase();
|
|
39
|
-
// Build a localized map only once per locale (optional optimization)
|
|
40
|
-
const langs = [loc, ...navigator.languages];
|
|
41
|
-
for (let i = 0; i < langs.length; i++) {
|
|
42
|
-
if (!langs[i]) {
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
const formatter = new Intl.DateTimeFormat(langs[i], { weekday: "long" });
|
|
46
|
-
// Generate localized weekday names for Sun → Sat
|
|
47
|
-
for (let i = 0; i < 7; i++) {
|
|
48
|
-
// 2023-01-01 was Sunday
|
|
49
|
-
const date = new Date(2023, 0, 1 + i);
|
|
50
|
-
const localized = formatter.format(date).toLowerCase();
|
|
51
|
-
if (localized === input) {
|
|
52
|
-
return i; // JS weekday number
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
throw new Error(`Unknown weekday name '${name}' for locale '${loc}'`);
|
|
57
|
-
};
|
|
58
|
-
export { getWeekday, getWeekdayIndex };
|