@bbn/bbn 2.0.78 → 2.0.80

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.
@@ -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.
@@ -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() {
@@ -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
- return getWeekday(n, mode, locale);
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/dist/dt.js CHANGED
@@ -1,4 +1,11 @@
1
+ import { Temporal } from 'temporal-polyfill';
1
2
  import bbnDtDateTime from './dt/classes/dateTime.js';
3
+ import bbnDtDate from './dt/classes/date.js';
4
+ import bbnDtTime from './dt/classes/zoned.js';
5
+ import bbnDtYearMonth from './dt/classes/yearMonth.js';
6
+ import bbnDtMonthDay from './dt/classes/monthDay.js';
7
+ import bbnDtZoned from './dt/classes/zoned.js';
8
+ import bbnDtDuration from './dt/classes/duration.js';
2
9
  import _ from './_.js';
3
10
  import parse from './dt/functions/parse.js';
4
11
  import guessFormat from './dt/functions/guessFormat.js';
@@ -220,6 +227,27 @@ const dt = (value, inputFormat = null, cls = 'auto') => {
220
227
  const d = value;
221
228
  return new bbnDtDateTime(d.getFullYear(), d.getMonth() + 1, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
222
229
  }
230
+ else if (value instanceof Temporal.PlainDateTime) {
231
+ return new bbnDtDateTime(value);
232
+ }
233
+ else if (value instanceof Temporal.PlainDate) {
234
+ return new bbnDtDate(value);
235
+ }
236
+ else if (value instanceof Temporal.PlainTime) {
237
+ return new bbnDtTime(value);
238
+ }
239
+ else if (value instanceof Temporal.PlainYearMonth) {
240
+ return new bbnDtYearMonth(value);
241
+ }
242
+ else if (value instanceof Temporal.PlainMonthDay) {
243
+ return new bbnDtMonthDay(value);
244
+ }
245
+ else if (value instanceof Temporal.ZonedDateTime) {
246
+ return new bbnDtZoned(value);
247
+ }
248
+ else if (value instanceof Temporal.Duration) {
249
+ return new bbnDtDuration(value);
250
+ }
223
251
  else {
224
252
  return { isValid: false };
225
253
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.78",
3
+ "version": "2.0.80",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,3 +0,0 @@
1
- declare const getWeekday: (n: 0 | 1 | 2 | 3 | 4 | 5 | 6, mode?: string, locale?: string) => any;
2
- declare const getWeekdayIndex: (name: string, locale?: string) => number;
3
- export { getWeekday, getWeekdayIndex };
@@ -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 };