@bbn/bbn 1.0.459 → 1.0.461

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?: 'long' | 'short' | 'narrow', locale?: string): String;
46
+ getWeekDay(n: 0 | 1 | 2 | 3 | 4 | 5 | 6, mode?: string, locale?: string): any;
47
+ copy(): Date;
48
+ /**
49
+ * Returns a NEW bbnDateTool at the start of the given unit.
50
+ * Units: year, month, week, day, hour, minute, second
51
+ */
52
+ startOf(unit?: string): bbnDateTool;
53
+ /**
54
+ * Returns a NEW bbnDateTool at the end of the given unit.
55
+ * Units: year, month, week, day, hour, minute, second
56
+ */
57
+ endOf(unit?: string): bbnDateTool;
47
58
  get daysInMonth(): number;
48
59
  }
49
60
  declare function generatorFunction(value: any, inputFormat?: null | String): bbnDateTool;
package/dist/date.js CHANGED
@@ -496,10 +496,129 @@ class bbnDateTool {
496
496
  return str;
497
497
  }
498
498
  getWeekDay(n, mode = 'long', locale) {
499
+ if (!mode) {
500
+ const letter = this.getWeekDay(n, 'narrow', locale);
501
+ const abbr = this.getWeekDay(n, 'short', locale);
502
+ const full = this.getWeekDay(n, 'long', locale);
503
+ return {
504
+ letter,
505
+ abbr,
506
+ full,
507
+ long: full,
508
+ short: abbr,
509
+ narrow: letter
510
+ };
511
+ }
512
+ let m;
513
+ if (mode === 'letter') {
514
+ m = 'narrow';
515
+ }
516
+ else if (mode === 'abbr') {
517
+ m = 'short';
518
+ }
519
+ else if (mode === 'full') {
520
+ m = 'long';
521
+ }
522
+ else if (!['long', 'short', 'narrow'].includes(mode)) {
523
+ throw new Error('Invalid mode for getWeekDay: ' + mode + '. Allowed values are "long", "short", "narrow", "letter", "abbr", "full".');
524
+ }
525
+ else {
526
+ m = mode;
527
+ }
499
528
  // Create a date with the right weekday
500
529
  // 2023-01-01 was a Sunday → base for offset
501
530
  const base = new Date(2023, 0, 1 + n);
502
- return base.toLocaleDateString([bbn.env.lang, ...navigator.languages], { weekday: mode });
531
+ return base.toLocaleDateString([locale || bbn.env.lang, ...navigator.languages], { weekday: m });
532
+ }
533
+ copy() {
534
+ return new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime());
535
+ }
536
+ /**
537
+ * Returns a NEW bbnDateTool at the start of the given unit.
538
+ * Units: year, month, week, day, hour, minute, second
539
+ */
540
+ startOf(unit = "d") {
541
+ const u = unitsCorrespondence[unit];
542
+ if (!u) {
543
+ throw new Error('Invalid unit for startOf: ' + unit);
544
+ }
545
+ let d;
546
+ switch (u) {
547
+ case "y":
548
+ d = new Date(this.year, 0, 1, 0, 0, 0, 0);
549
+ break;
550
+ case "m":
551
+ d = new Date(this.year, this.month - 1, 1, 0, 0, 0, 0);
552
+ break;
553
+ case "w": {
554
+ // Week starting Monday:
555
+ // JS getDay(): 0 (Sun) .. 6 (Sat)
556
+ const current = new Date(this.year, this.month - 1, this.day, this.hours, this.minutes, this.seconds, 0);
557
+ const wd = current.getDay(); // 0..6
558
+ const diffToMonday = (wd + 6) % 7; // 0 for Monday, 6 for Sunday
559
+ d = new Date(current.getFullYear(), current.getMonth(), current.getDate() - diffToMonday, 0, 0, 0, 0);
560
+ break;
561
+ }
562
+ case "d":
563
+ d = new Date(this.year, this.month - 1, this.day, 0, 0, 0, 0);
564
+ break;
565
+ case "h":
566
+ d = new Date(this.year, this.month - 1, this.day, this.hours, 0, 0, 0);
567
+ break;
568
+ case "i":
569
+ d = new Date(this.year, this.month - 1, this.day, this.hours, this.minutes, 0, 0);
570
+ break;
571
+ case "s":
572
+ d = new Date(this.year, this.month - 1, this.day, this.hours, this.minutes, this.seconds, 0);
573
+ break;
574
+ default:
575
+ throw new Error('Invalid unit for startOf: ' + unit);
576
+ }
577
+ return new bbnDateTool(d);
578
+ }
579
+ /**
580
+ * Returns a NEW bbnDateTool at the end of the given unit.
581
+ * Units: year, month, week, day, hour, minute, second
582
+ */
583
+ endOf(unit = "d") {
584
+ const u = unitsCorrespondence[unit];
585
+ if (!u) {
586
+ throw new Error('Invalid unit for endOf: ' + unit);
587
+ }
588
+ let d;
589
+ switch (u) {
590
+ case "y":
591
+ // Dec 31, 23:59:59.999
592
+ d = new Date(this.year, 11, 31, 23, 59, 59, 999);
593
+ break;
594
+ case "m":
595
+ // Day 0 of next month = last day of this month
596
+ d = new Date(this.year, this.month, 0, 23, 59, 59, 999);
597
+ break;
598
+ case "w": {
599
+ // End of week (starting Monday) = startOf('week') + 6 days, at 23:59:59.999
600
+ const start = this.startOf("w");
601
+ const base = new Date(start.year, start.month - 1, start.day, 23, 59, 59, 999);
602
+ base.setDate(base.getDate() + 6);
603
+ d = base;
604
+ break;
605
+ }
606
+ case "day":
607
+ d = new Date(this.year, this.month - 1, this.day, 23, 59, 59, 999);
608
+ break;
609
+ case "hour":
610
+ d = new Date(this.year, this.month - 1, this.day, this.hours, 59, 59, 999);
611
+ break;
612
+ case "minute":
613
+ d = new Date(this.year, this.month - 1, this.day, this.hours, this.minutes, 59, 999);
614
+ break;
615
+ case "second":
616
+ d = new Date(this.year, this.month - 1, this.day, this.hours, this.minutes, this.seconds, 999);
617
+ break;
618
+ default:
619
+ d = new Date(this.mtst);
620
+ }
621
+ return new bbnDateTool(d);
503
622
  }
504
623
  get daysInMonth() {
505
624
  if (this.isValid && !__classPrivateFieldGet(this, _bbnDateTool_daysInMonth, "f")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.459",
3
+ "version": "1.0.461",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",