@bbn/bbn 2.0.105 → 2.0.107

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.
@@ -7,6 +7,7 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
7
7
  readonly __bbnNoData = true;
8
8
  readonly __isBbnDt = true;
9
9
  constructor(value?: TValue);
10
+ setValid(isValid: boolean): void;
10
11
  get value(): TValue | undefined;
11
12
  get hasFullDate(): boolean;
12
13
  /** System time zone ID (e.g. "Europe/Rome") */
@@ -86,7 +87,6 @@ export declare abstract class bbnDt<TValue extends bbnDtTemporal> {
86
87
  get S(): string;
87
88
  get WW(): string;
88
89
  get W(): string;
89
- get isValid(): boolean;
90
90
  format(format?: string): string;
91
91
  matchFormat(value: any, format: string): boolean;
92
92
  getWeekday(n: 0 | 1 | 2 | 3 | 4 | 5 | 6, mode?: string, locale?: string): string | object;
@@ -46,8 +46,20 @@ export class bbnDt {
46
46
  }
47
47
  throw new Error(`Unknown weekday name '${name}' for locale '${loc}'`);
48
48
  };
49
+ Object.defineProperty(this, 'isValid', {
50
+ value: false,
51
+ writable: false,
52
+ configurable: true
53
+ });
49
54
  __classPrivateFieldSet(this, _bbnDt_value, value, "f");
50
55
  }
56
+ setValid(isValid) {
57
+ Object.defineProperty(this, 'isValid', {
58
+ value: isValid,
59
+ writable: false,
60
+ configurable: false
61
+ });
62
+ }
51
63
  get value() {
52
64
  return __classPrivateFieldGet(this, _bbnDt_value, "f");
53
65
  }
@@ -612,9 +624,6 @@ export class bbnDt {
612
624
  }
613
625
  return undefined;
614
626
  }
615
- get isValid() {
616
- return this.value !== undefined;
617
- }
618
627
  format(format = 'YYYY-MM-DD HH:II:SS') {
619
628
  let str = '';
620
629
  if (format) {
@@ -3,6 +3,7 @@ export default class bbnDtDuration {
3
3
  #private;
4
4
  static fromUnit(value: number, unit: string): bbnDtDuration;
5
5
  constructor(y: Temporal.Duration | number | object, m?: number, w?: number, d?: number, h?: number, i?: number, s?: number, ms?: number, unit?: string);
6
+ setValid(isValid: boolean): void;
6
7
  get value(): Temporal.Duration;
7
8
  years(remaining?: boolean): number;
8
9
  months(remaining?: boolean): number;
@@ -52,6 +52,18 @@ class bbnDtDuration {
52
52
  if (!row) {
53
53
  //throw new Error('Invalid unit for duration: ' + realUnit);
54
54
  }
55
+ Object.defineProperty(this, 'isValid', {
56
+ value: false,
57
+ writable: false,
58
+ configurable: true
59
+ });
60
+ }
61
+ setValid(isValid) {
62
+ Object.defineProperty(this, 'isValid', {
63
+ value: isValid,
64
+ writable: false,
65
+ configurable: false
66
+ });
55
67
  }
56
68
  get value() {
57
69
  return __classPrivateFieldGet(this, _bbnDtDuration_value, "f");
@@ -1,4 +1,4 @@
1
- export default function parse(input: string, format: string | string[], cls?: 'auto' | 'zoned' | 'dateTime' | 'date' | 'time' | 'yearMonth' | 'monthDay', locale?: {
1
+ export default function parse(input: string, format: string | string[], cls?: 'auto' | 'zoned' | 'dateTime' | 'date' | 'time' | 'yearMonth' | 'monthDay', force?: boolean, locale?: {
2
2
  monthsLong?: string[];
3
3
  monthsShort?: string[];
4
4
  weekdaysLong?: string[];
@@ -13,7 +13,7 @@ const lc = function (str, localeCode) {
13
13
  return str.toLowerCase();
14
14
  }
15
15
  };
16
- export default function parse(input, format, cls = 'auto', locale) {
16
+ export default function parse(input, format, cls = 'auto', force, locale) {
17
17
  var _a, _b, _c, _d, _e, _f, _g;
18
18
  buildLocaleFromIntl();
19
19
  const TemporalAny = globalThis.Temporal;
@@ -21,6 +21,7 @@ export default function parse(input, format, cls = 'auto', locale) {
21
21
  throw new Error('Temporal API is required (load @js-temporal/polyfill)');
22
22
  }
23
23
  const T = TemporalAny;
24
+ let isValid = true;
24
25
  if (!locale) {
25
26
  locale = ((_a = bbn === null || bbn === void 0 ? void 0 : bbn.dt) === null || _a === void 0 ? void 0 : _a.locales) || {};
26
27
  }
@@ -527,9 +528,17 @@ export default function parse(input, format, cls = 'auto', locale) {
527
528
  }
528
529
  }
529
530
  const fullRegex = new RegExp('^' + pattern + '$');
530
- const match = fullRegex.exec(input);
531
+ let match = fullRegex.exec(input);
531
532
  if (!match) {
532
- throw new Error(`Date string "${input}" does not match format "${fmt}"`);
533
+ if (force) {
534
+ const inputDate = new bbnDtDateTime(1970, 1, 1, 0, 0, 0, 0);
535
+ input = inputDate.format(fmt);
536
+ isValid = false;
537
+ match = fullRegex.exec(input);
538
+ }
539
+ else {
540
+ throw new Error(`Date string "${input}" does not match format "${fmt}"`);
541
+ }
533
542
  }
534
543
  for (let idx = 1; idx < match.length; idx++) {
535
544
  const value = match[idx];
@@ -562,6 +571,7 @@ export default function parse(input, format, cls = 'auto', locale) {
562
571
  const hasMonthDayOnly = !ctx.hasYear && ctx.hasMonth && ctx.hasDay;
563
572
  const hasTime = ctx.hasHour || ctx.hasMinute || ctx.hasSecond || ctx.hasMs;
564
573
  const hasZone = ctx.timeZone != null || ctx.offsetMinutes != null;
574
+ let dtObj;
565
575
  // ---------- 1) If timezone (Z or z) → Zoned ----------
566
576
  if (isClsZoned || (hasZone && isClsAuto)) {
567
577
  let pdt;
@@ -574,40 +584,48 @@ export default function parse(input, format, cls = 'auto', locale) {
574
584
  if (ctx.timeZone) {
575
585
  const tz = T.TimeZone.from(ctx.timeZone);
576
586
  const zdt = pdt.toZonedDateTime(tz);
577
- return new bbnDtZoned(zdt);
587
+ dtObj = new bbnDtZoned(zdt);
588
+ }
589
+ else {
590
+ const utcMs = Date.UTC(ctx.year, ctx.month - 1, ctx.day, ctx.hour, ctx.minute, ctx.second, ctx.ms);
591
+ const epochMs = utcMs - ((_a = ctx.offsetMinutes) !== null && _a !== void 0 ? _a : 0) * 60000;
592
+ dtObj = new bbnDtZoned(T.Instant.fromEpochMilliseconds(epochMs).toZonedDateTimeISO(T.Now.timeZoneId()));
578
593
  }
579
- const utcMs = Date.UTC(ctx.year, ctx.month - 1, ctx.day, ctx.hour, ctx.minute, ctx.second, ctx.ms);
580
- const epochMs = utcMs - ((_a = ctx.offsetMinutes) !== null && _a !== void 0 ? _a : 0) * 60000;
581
- return new bbnDtZoned(T.Instant.fromEpochMilliseconds(epochMs).toZonedDateTimeISO(T.Now.timeZoneId()));
582
594
  }
583
595
  // ---------- 2) No timezone: decide which Plain* type ----------
584
- if (isClsDateTime || (isClsAuto && hasDate && hasTime)) {
596
+ else if (isClsDateTime || (isClsAuto && hasDate && hasTime)) {
585
597
  if (!hasFullDate) {
586
598
  throw new Error('PlainDateTime requires year, month and day');
587
599
  }
588
600
  const d = new T.PlainDateTime(ctx.year, ctx.month, ctx.day, ctx.hour, ctx.minute, ctx.second, ctx.ms * 1000000);
589
- return new bbnDtDateTime(d);
601
+ dtObj = new bbnDtDateTime(d);
590
602
  }
591
- if (isClsDate || isClsYearMonth || isClsMonthDay || (isClsAuto && hasDate && !hasTime)) {
603
+ else if (isClsDate || isClsYearMonth || isClsMonthDay || (isClsAuto && hasDate && !hasTime)) {
592
604
  if (isClsDate || (hasFullDate && isClsAuto)) {
593
605
  const d = new T.PlainDate(ctx.year, ctx.month, ctx.day);
594
- return new bbnDtDate(d);
606
+ dtObj = new bbnDtDate(d);
595
607
  }
596
- if (isClsYearMonth || (hasYearMonthOnly && isClsAuto)) {
608
+ else if (isClsYearMonth || (hasYearMonthOnly && isClsAuto)) {
597
609
  const d = new T.PlainYearMonth(ctx.year, ctx.month);
598
- return new bbnDtYearMonth(d);
610
+ dtObj = new bbnDtYearMonth(d);
599
611
  }
600
- if (isClsMonthDay || (hasMonthDayOnly && isClsAuto)) {
612
+ else if (isClsMonthDay || (hasMonthDayOnly && isClsAuto)) {
601
613
  const d = new T.PlainMonthDay(ctx.month, ctx.day, 1972);
602
- return new bbnDtMonthDay(d);
614
+ dtObj = new bbnDtMonthDay(d);
615
+ }
616
+ else {
617
+ throw new Error('Not enough date components for a known Temporal type');
603
618
  }
604
- throw new Error('Not enough date components for a known Temporal type');
605
619
  }
606
- if (isClsTime || (isClsAuto && !hasDate && hasTime)) {
620
+ else if (isClsTime || (isClsAuto && !hasDate && hasTime)) {
607
621
  const d = new T.PlainTime(ctx.hour, ctx.minute, ctx.second, ctx.ms * 1000000);
608
- return new bbnDtTime(d);
622
+ dtObj = new bbnDtTime(d);
623
+ }
624
+ else {
625
+ throw new Error('No date or time information found in input');
609
626
  }
610
- throw new Error('No date or time information found in input');
627
+ dtObj.setValid(isValid);
628
+ return dtObj;
611
629
  }
612
630
  if (Array.isArray(format)) {
613
631
  let lastError = null;
package/dist/dt.js CHANGED
@@ -179,7 +179,7 @@ const dt = (value, inputFormat = null, cls = 'auto') => {
179
179
  if (inputFormat) {
180
180
  let parsed;
181
181
  try {
182
- parsed = parse(value, inputFormat, cls);
182
+ parsed = parse(value, inputFormat, cls, true);
183
183
  return parsed;
184
184
  }
185
185
  catch (e) { }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.105",
3
+ "version": "2.0.107",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",