@bbn/bbn 1.0.462 → 1.0.464

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/date.d.ts CHANGED
@@ -43,7 +43,18 @@ declare class bbnDateTool {
43
43
  guessUnit(valueInMs: number): string | null;
44
44
  diff(date: any, unit?: string, abs?: boolean): number;
45
45
  calendar(format: string): string;
46
- getWeekDay(n: 0 | 1 | 2 | 3 | 4 | 5 | 6, mode?: string, locale?: string): any;
46
+ getWeekday(n: 0 | 1 | 2 | 3 | 4 | 5 | 6, mode?: string, locale?: string): any;
47
+ getWeekdayIndex(name: string, locale?: string): number;
48
+ /**
49
+ * Returns a NEW date that is the next (or previous if past=true)
50
+ * occurrence of the given weekday, starting from this.#value.
51
+ *
52
+ * @param {number|string} weekday - Weekday index (0=Sunday…6=Saturday)
53
+ * or a localized weekday name.
54
+ * @param {boolean} past - If true → return previous occurrence instead of next.
55
+ * @param {string} [locale] - Optional locale for weekday names.
56
+ */
57
+ setWeekday(weekday: any, past: boolean, locale: any): bbnDateTool;
47
58
  copy(): Date;
48
59
  /**
49
60
  * Returns a NEW bbnDateTool at the start of the given unit.
package/dist/date.js CHANGED
@@ -496,11 +496,11 @@ class bbnDateTool {
496
496
  }
497
497
  return str;
498
498
  }
499
- getWeekDay(n, mode = 'long', locale) {
499
+ getWeekday(n, mode = 'long', locale) {
500
500
  if (!mode) {
501
- const letter = this.getWeekDay(n, 'narrow', locale);
502
- const abbr = this.getWeekDay(n, 'short', locale);
503
- const full = this.getWeekDay(n, 'long', locale);
501
+ const letter = this.getWeekday(n, 'narrow', locale);
502
+ const abbr = this.getWeekday(n, 'short', locale);
503
+ const full = this.getWeekday(n, 'long', locale);
504
504
  return {
505
505
  letter,
506
506
  abbr,
@@ -531,6 +531,76 @@ class bbnDateTool {
531
531
  const base = new Date(2023, 0, 1 + n);
532
532
  return base.toLocaleDateString([locale || bbn.env.lang, ...navigator.languages], { weekday: m });
533
533
  }
534
+ getWeekdayIndex(name, locale) {
535
+ const loc = locale || bbn.env.lang;
536
+ const input = name.trim().toLowerCase();
537
+ // Build a localized map only once per locale (optional optimization)
538
+ const langs = [loc, ...navigator.languages];
539
+ for (let i = 0; i < langs.length; i++) {
540
+ if (!langs[i]) {
541
+ continue;
542
+ }
543
+ const formatter = new Intl.DateTimeFormat(langs[i], { weekday: "long" });
544
+ // Generate localized weekday names for Sun → Sat
545
+ for (let i = 0; i < 7; i++) {
546
+ // 2023-01-01 was Sunday
547
+ const date = new Date(2023, 0, 1 + i);
548
+ const localized = formatter.format(date).toLowerCase();
549
+ if (localized === input) {
550
+ return i; // JS weekday number
551
+ }
552
+ }
553
+ }
554
+ throw new Error(`Unknown weekday name '${name}' for locale '${loc}'`);
555
+ }
556
+ /**
557
+ * Returns a NEW date that is the next (or previous if past=true)
558
+ * occurrence of the given weekday, starting from this.#value.
559
+ *
560
+ * @param {number|string} weekday - Weekday index (0=Sunday…6=Saturday)
561
+ * or a localized weekday name.
562
+ * @param {boolean} past - If true → return previous occurrence instead of next.
563
+ * @param {string} [locale] - Optional locale for weekday names.
564
+ */
565
+ setWeekday(weekday, past = false, locale) {
566
+ let targetDay;
567
+ if (typeof weekday === "string") {
568
+ // Use your previously defined reverse method:
569
+ weekday = this.getWeekdayIndex(weekday, locale);
570
+ }
571
+ // --- Normalize weekday ---
572
+ if (typeof weekday === "number") {
573
+ if (weekday < 0 || weekday > 6) {
574
+ throw new RangeError("weekday number must be between 0 and 6");
575
+ }
576
+ targetDay = weekday;
577
+ }
578
+ else {
579
+ throw new TypeError("weekday must be a number (0–6) or a string");
580
+ }
581
+ const currentDay = this.weekday; // JS weekday (0–6)
582
+ let diff;
583
+ if (!past) {
584
+ // ---------- NEXT occurrence ----------
585
+ diff = (targetDay - currentDay + 7) % 7;
586
+ if (diff === 0) {
587
+ diff = 7; // next week if same day
588
+ }
589
+ }
590
+ else {
591
+ // ---------- PREVIOUS occurrence ----------
592
+ diff = (currentDay - targetDay + 7) % 7;
593
+ if (diff === 0) {
594
+ diff = 7; // previous week if same day
595
+ }
596
+ diff = -diff;
597
+ }
598
+ const d = this.copy();
599
+ d.setDate(d.getDate() + diff);
600
+ // Rebuild object keeping hours/min/sec from original
601
+ d.setHours(this.hours, this.minutes, this.seconds, 0);
602
+ return new bbnDateTool(d);
603
+ }
534
604
  copy() {
535
605
  return new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime());
536
606
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.462",
3
+ "version": "1.0.464",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",