@bbn/bbn 2.0.22 → 2.0.23

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
@@ -22,6 +22,11 @@ declare class bbnDateTool {
22
22
  weekdaysLong?: string[];
23
23
  weekdaysShort?: string[];
24
24
  }): Date;
25
+ /**
26
+ * Compare 2 dates with a given precision.
27
+ * @returns -1 if this < other, 0 if equal, 1 if this > other
28
+ */
29
+ static compare(date1: any, date2: any, unit?: string): -1 | 0 | 1;
25
30
  constructor(value: any, inputFormat?: null | String);
26
31
  parse(input: string, format: string): bbnDateTool;
27
32
  matchFormat(value: any, format: string): boolean;
@@ -63,11 +68,6 @@ declare class bbnDateTool {
63
68
  get S(): string;
64
69
  get WW(): string;
65
70
  get isValid(): boolean;
66
- inLeapYear(): boolean;
67
- daysInMonth(): number;
68
- valueOf(): number;
69
- add(value: number, unit?: string): bbnDateTool | null;
70
- subtract(value: number, unit?: string): bbnDateTool;
71
71
  dateFromFormat(value: string, unit: string | null): Date;
72
72
  date(v?: any): string | bbnDateTool;
73
73
  datetime(v?: any): string | bbnDateTool;
@@ -77,11 +77,16 @@ declare class bbnDateTool {
77
77
  format(format?: string): string;
78
78
  unix(ms?: boolean | number): number | bbnDateTool;
79
79
  sql(noTime?: boolean): string;
80
+ inLeapYear(): boolean;
81
+ daysInMonth(): number;
82
+ valueOf(): number;
80
83
  /**
81
84
  * Compare this date to another date with a given precision.
82
85
  * @returns -1 if this < other, 0 if equal, 1 if this > other
83
86
  */
84
87
  compare(date: any, unit?: string): -1 | 0 | 1;
88
+ add(value: number, unit?: string): bbnDateTool | null;
89
+ subtract(value: number, unit?: string): bbnDateTool;
85
90
  isBefore(date: any, unit?: string): Boolean;
86
91
  isAfter(date: any, unit?: string): Boolean;
87
92
  isSame(date: any, unit?: string): Boolean;
package/dist/date.js CHANGED
@@ -779,6 +779,44 @@ class bbnDateTool {
779
779
  }
780
780
  return date;
781
781
  }
782
+ /**
783
+ * Compare 2 dates with a given precision.
784
+ * @returns -1 if this < other, 0 if equal, 1 if this > other
785
+ */
786
+ static compare(date1, date2, unit = '') {
787
+ var _a;
788
+ const d1 = date1 instanceof bbnDateTool ? date1 : new bbnDateTool(date1);
789
+ const d2 = date2 instanceof bbnDateTool ? date2 : new bbnDateTool(date2);
790
+ const realUnit = unitsCorrespondence[unit] || null;
791
+ // If no unit or unknown unit, fall back to timestamp comparison
792
+ if (!realUnit) {
793
+ if (d1.mtst < d2.mtst) {
794
+ return -1;
795
+ }
796
+ if (d1.mtst > d2.mtst) {
797
+ return 1;
798
+ }
799
+ return 0;
800
+ }
801
+ const order = ['y', 'm', 'd', 'h', 'i', 's'];
802
+ // Compare step by step until the requested precision
803
+ for (const u of order) {
804
+ const key = (_a = getRow(units, un => un[0] === u)) === null || _a === void 0 ? void 0 : _a[1];
805
+ const a = d2[key]();
806
+ const b = d1[key]();
807
+ if (a < b) {
808
+ return -1;
809
+ }
810
+ if (a > b) {
811
+ return 1;
812
+ }
813
+ // Stop when we've reached the desired unit
814
+ if (u === realUnit) {
815
+ break;
816
+ }
817
+ }
818
+ return 0;
819
+ }
782
820
  constructor(value, inputFormat = null) {
783
821
  _bbnDateTool_value.set(this, void 0);
784
822
  _bbnDateTool_isDuration.set(this, false);
@@ -1038,52 +1076,6 @@ class bbnDateTool {
1038
1076
  get isValid() {
1039
1077
  return __classPrivateFieldGet(this, _bbnDateTool_value, "f") !== undefined;
1040
1078
  }
1041
- inLeapYear() {
1042
- if (this.isValid) {
1043
- const year = __classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear();
1044
- return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
1045
- }
1046
- return false;
1047
- }
1048
- daysInMonth() {
1049
- if (this.isValid) {
1050
- switch (__classPrivateFieldGet(this, _bbnDateTool_value, "f").getMonth()) {
1051
- case 1:
1052
- if (__classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear() % 4 === 0) {
1053
- return 29;
1054
- }
1055
- else {
1056
- return 28;
1057
- }
1058
- case 0:
1059
- case 3:
1060
- case 5:
1061
- case 8:
1062
- case 10:
1063
- return 30;
1064
- default:
1065
- return 31;
1066
- }
1067
- }
1068
- }
1069
- valueOf() {
1070
- return this.mtst;
1071
- }
1072
- add(value, unit = 'd') {
1073
- const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
1074
- if (unitsCorrespondence[unit]) {
1075
- const realUnit = unitsCorrespondence[unit];
1076
- const suffix = realUnit === 'y' ? 'FullYear' : unitsMap[realUnit];
1077
- const getter = 'get' + suffix;
1078
- const setter = 'set' + suffix;
1079
- date[setter](date[getter]() + value);
1080
- return new bbnDateTool(date);
1081
- }
1082
- return null;
1083
- }
1084
- subtract(value, unit) {
1085
- return this.add(-value, unit);
1086
- }
1087
1079
  dateFromFormat(value, unit) {
1088
1080
  const d = new Date();
1089
1081
  return d;
@@ -1187,42 +1179,58 @@ class bbnDateTool {
1187
1179
  ? this.format('YYYY-MM-DD')
1188
1180
  : this.format('YYYY-MM-DD HH:II:SS');
1189
1181
  }
1182
+ inLeapYear() {
1183
+ if (this.isValid) {
1184
+ const year = __classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear();
1185
+ return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
1186
+ }
1187
+ return false;
1188
+ }
1189
+ daysInMonth() {
1190
+ if (this.isValid) {
1191
+ switch (__classPrivateFieldGet(this, _bbnDateTool_value, "f").getMonth()) {
1192
+ case 1:
1193
+ if (__classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear() % 4 === 0) {
1194
+ return 29;
1195
+ }
1196
+ else {
1197
+ return 28;
1198
+ }
1199
+ case 0:
1200
+ case 3:
1201
+ case 5:
1202
+ case 8:
1203
+ case 10:
1204
+ return 30;
1205
+ default:
1206
+ return 31;
1207
+ }
1208
+ }
1209
+ }
1210
+ valueOf() {
1211
+ return this.mtst;
1212
+ }
1190
1213
  /**
1191
1214
  * Compare this date to another date with a given precision.
1192
1215
  * @returns -1 if this < other, 0 if equal, 1 if this > other
1193
1216
  */
1194
1217
  compare(date, unit = '') {
1195
- var _a;
1196
- const d = date instanceof bbnDateTool ? date : new bbnDateTool(date);
1197
- const realUnit = unitsCorrespondence[unit] || null;
1198
- // If no unit or unknown unit, fall back to timestamp comparison
1199
- if (!realUnit) {
1200
- if (this.mtst < d.mtst) {
1201
- return -1;
1202
- }
1203
- if (this.mtst > d.mtst) {
1204
- return 1;
1205
- }
1206
- return 0;
1207
- }
1208
- const order = ['y', 'm', 'd', 'h', 'i', 's'];
1209
- // Compare step by step until the requested precision
1210
- for (const u of order) {
1211
- const key = (_a = getRow(units, un => un[0] === u)) === null || _a === void 0 ? void 0 : _a[1];
1212
- const a = this[key]();
1213
- const b = d[key]();
1214
- if (a < b) {
1215
- return -1;
1216
- }
1217
- if (a > b) {
1218
- return 1;
1219
- }
1220
- // Stop when we've reached the desired unit
1221
- if (u === realUnit) {
1222
- break;
1223
- }
1218
+ return bbnDateTool.compare(this, date, unit);
1219
+ }
1220
+ add(value, unit = 'd') {
1221
+ const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
1222
+ if (unitsCorrespondence[unit]) {
1223
+ const realUnit = unitsCorrespondence[unit];
1224
+ const suffix = realUnit === 'y' ? 'FullYear' : unitsMap[realUnit];
1225
+ const getter = 'get' + suffix;
1226
+ const setter = 'set' + suffix;
1227
+ date[setter](date[getter]() + value);
1228
+ return new bbnDateTool(date);
1224
1229
  }
1225
- return 0;
1230
+ return null;
1231
+ }
1232
+ subtract(value, unit) {
1233
+ return this.add(-value, unit);
1226
1234
  }
1227
1235
  isBefore(date, unit = '') {
1228
1236
  return this.compare(date, unit) === -1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.22",
3
+ "version": "2.0.23",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",