@bbn/bbn 1.0.438 → 1.0.439

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
@@ -1,24 +1,37 @@
1
1
  declare class bbnDateTool {
2
2
  #private;
3
3
  constructor(value: any, inputFormat?: null | String);
4
- get year(): Number;
4
+ toString(): number;
5
+ get year(): number;
5
6
  get month(): number;
6
7
  get day(): number;
7
8
  get hours(): number;
8
9
  get minutes(): number;
9
10
  get seconds(): number;
11
+ get tst(): number;
12
+ get mtst(): number;
10
13
  get YYYY(): string;
11
14
  get YY(): string;
12
15
  get MM(): string;
13
16
  get DD(): string;
14
- add(value: number, unit: string | null): bbnDateTool;
17
+ get HH(): string;
18
+ get mm(): string;
19
+ get ss(): string;
20
+ add(value: number, unit?: string): bbnDateTool | null;
15
21
  sub(value: number, unit: string | null): bbnDateTool;
16
22
  dateFromFormat(value: string, unit: string | null): Date;
17
- format(format: string): string;
23
+ format(format?: string): string;
18
24
  unix(ms?: boolean): number;
25
+ /**
26
+ * Compare this date to another date with a given precision.
27
+ * @returns -1 if this < other, 0 if equal, 1 if this > other
28
+ */
29
+ compare(date: any, unit?: string): -1 | 0 | 1;
19
30
  isBefore(date: any, unit?: string): Boolean;
20
31
  isAfter(date: any, unit?: string): Boolean;
21
32
  isSame(date: any, unit?: string): Boolean;
33
+ isAfterOrSame(date: any, unit?: string): Boolean;
34
+ isBeforeOrSame(date: any, unit?: string): Boolean;
22
35
  calendar(format: string): string;
23
36
  get daysInMonth(): Number | 0;
24
37
  }
package/dist/date.js CHANGED
@@ -14,19 +14,63 @@ import each from './fn/loop/each.js';
14
14
  import substr from './fn/string/substr.js';
15
15
  import isNumber from './fn/type/isNumber.js';
16
16
  import isDate from './fn/type/isDate.js';
17
- import getProperty from './fn/object/getProperty.js';
17
+ const unitsMap = {
18
+ 'y': 'FullYear',
19
+ 'm': 'Month',
20
+ 'd': 'Date',
21
+ 'h': 'Hours',
22
+ 'n': 'Minutes',
23
+ 's': 'Seconds'
24
+ };
25
+ const unitsCorrespondence = {
26
+ 'YYYY': 'y',
27
+ 'YY': 'y',
28
+ 'yyyy': 'y',
29
+ 'yy': 'y',
30
+ 'year': 'y',
31
+ 'Y': 'y',
32
+ 'y': 'y',
33
+ 'MM': 'm',
34
+ 'M': 'm',
35
+ 'month': 'm',
36
+ 'mm': 'm',
37
+ 'm': 'm',
38
+ 'DD': 'd',
39
+ 'day': 'd',
40
+ 'D': 'd',
41
+ 'dd': 'd',
42
+ 'd': 'd',
43
+ 'HH': 'h',
44
+ 'hr': 'h',
45
+ 'hour': 'h',
46
+ 'H': 'h',
47
+ 'hh': 'h',
48
+ 'h': 'h',
49
+ 'NN': 'n',
50
+ 'nn': 'n',
51
+ 'mn': 'n',
52
+ 'minute': 'n',
53
+ 'min': 'n',
54
+ 'n': 'n',
55
+ 'SS': 's',
56
+ 'ss': 's',
57
+ 'sec': 's',
58
+ 's': 's',
59
+ 'S': 's',
60
+ };
18
61
  class bbnDateTool {
19
62
  constructor(value, inputFormat = null) {
20
63
  _bbnDateTool_value.set(this, void 0);
21
64
  _bbnDateTool_daysInMonth.set(this, undefined);
22
65
  _bbnDateTool_parts.set(this, undefined);
23
- if (inputFormat) {
66
+ if (!value) {
67
+ __classPrivateFieldSet(this, _bbnDateTool_value, new Date(), "f");
68
+ }
69
+ else if (inputFormat) {
70
+ __classPrivateFieldSet(this, _bbnDateTool_value, new Date(), "f");
24
71
  }
25
72
  else {
26
73
  let t = typeof value;
27
- if (value === undefined) {
28
- __classPrivateFieldSet(this, _bbnDateTool_value, new Date(), "f");
29
- }
30
74
  if (t === 'number' || (isNumber(value) && value !== '')) {
31
75
  if ((value < 5000) && isNumber(inputFormat)) {
32
76
  value = Array.from(arguments);
@@ -62,6 +106,9 @@ class bbnDateTool {
62
106
  });
63
107
  }
64
108
  }
109
+ toString() {
110
+ return __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? __classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime() : 0;
111
+ }
65
112
  get year() {
66
113
  return __classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear();
67
114
  }
@@ -80,6 +127,12 @@ class bbnDateTool {
80
127
  get seconds() {
81
128
  return __classPrivateFieldGet(this, _bbnDateTool_value, "f").getSeconds();
82
129
  }
130
+ get tst() {
131
+ return Math.ceil(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime() / 1000);
132
+ }
133
+ get mtst() {
134
+ return __classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime();
135
+ }
83
136
  get YYYY() {
84
137
  return this.year.toString();
85
138
  }
@@ -92,25 +145,43 @@ class bbnDateTool {
92
145
  get DD() {
93
146
  return this.day < 10 ? '0' + this.day.toString() : this.day.toString();
94
147
  }
95
- add(value, unit) {
96
- let newValue;
97
- return new bbnDateTool(newValue);
148
+ get HH() {
149
+ return this.hours < 10 ? '0' + this.hours.toString() : this.hours.toString();
150
+ }
151
+ get mm() {
152
+ return this.minutes < 10 ? '0' + this.minutes.toString() : this.minutes.toString();
153
+ }
154
+ get ss() {
155
+ return this.seconds < 10 ? '0' + this.seconds.toString() : this.seconds.toString();
156
+ }
157
+ add(value, unit = 'd') {
158
+ const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
159
+ if (unitsCorrespondence[unit]) {
160
+ const realUnit = unitsCorrespondence[unit];
161
+ const suffix = unitsMap[realUnit];
162
+ const getter = 'get' + suffix;
163
+ const setter = 'set' + suffix;
164
+ date[setter](date[getter]() + value);
165
+ return new bbnDateTool(date);
166
+ }
167
+ return null;
98
168
  }
99
169
  sub(value, unit) {
100
- let newValue;
101
- return new bbnDateTool(newValue);
170
+ return this.add(-value, unit);
102
171
  }
103
172
  dateFromFormat(value, unit) {
104
173
  const d = new Date();
105
174
  return d;
106
175
  }
107
- format(format) {
176
+ format(format = '') {
177
+ const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
108
178
  let str = '';
109
179
  if (format) {
110
- const parts = format.split(/(YYYY|YY|MM|DD|HH|mm|ss)/);
180
+ const parts = format.split('/(' + Object.keys(unitsCorrespondence).join('|') + ')/');
111
181
  each(parts, (part) => {
112
- if (part in this) {
113
- str += getProperty(this, part);
182
+ if (part in unitsCorrespondence) {
183
+ const suffix = unitsMap[unitsCorrespondence[part]];
184
+ str += date['get' + suffix]();
114
185
  }
115
186
  else {
116
187
  str += part;
@@ -130,24 +201,102 @@ class bbnDateTool {
130
201
  }
131
202
  return 0;
132
203
  }
133
- isBefore(date, unit = '') {
204
+ /**
205
+ * Compare this date to another date with a given precision.
206
+ * @returns -1 if this < other, 0 if equal, 1 if this > other
207
+ */
208
+ compare(date, unit = '') {
134
209
  const d = new bbnDateTool(date);
135
- if (unit === 'd') {
210
+ const realUnit = unitsCorrespondence[unit] || null;
211
+ // If no unit or unknown unit, fall back to timestamp comparison
212
+ if (!realUnit) {
213
+ if (this.mtst < d.mtst) {
214
+ return -1;
215
+ }
216
+ if (this.mtst > d.mtst) {
217
+ return 1;
218
+ }
219
+ return 0;
136
220
  }
137
- return false;
138
- }
139
- isAfter(date, unit = '') {
140
- const d = new bbnDateTool(date);
141
- if (unit === 'd') {
221
+ const order = ['y', 'm', 'd', 'h', 'n', 's'];
222
+ // Compare step by step until the requested precision
223
+ for (const u of order) {
224
+ const key = unitsMap[u];
225
+ const a = this[key];
226
+ const b = d[key];
227
+ if (a < b) {
228
+ return -1;
229
+ }
230
+ if (a > b) {
231
+ return 1;
232
+ }
233
+ // Stop when we've reached the desired unit
234
+ if (u === realUnit) {
235
+ break;
236
+ }
142
237
  }
143
- return false;
238
+ return 0;
144
239
  }
145
- isSame(date, unit = '') {
146
- const d = new bbnDateTool(date);
147
- if (unit === 'd') {
240
+ isBefore(date, unit = '') {
241
+ return this.compare(date, unit) === -1;
242
+ }
243
+ isAfter(date, unit = 'day') {
244
+ return this.compare(date, unit) === 1;
245
+ }
246
+ isSame(date, unit = 'day') {
247
+ return this.compare(date, unit) === 0;
248
+ }
249
+ isAfterOrSame(date, unit = '') {
250
+ return [0, 1].includes(this.compare(date, unit));
251
+ }
252
+ isBeforeOrSame(date, unit = '') {
253
+ return [-1, 0].includes(this.compare(date, unit));
254
+ }
255
+ /*
256
+ const d = new bbnDateTool(date);
257
+ if (unitsCorrespondence[unit]) {
258
+ const realUnit = unitsCorrespondence[unit];
259
+ switch (realUnit) {
260
+ case 'y':
261
+ return this.year < d.year;
262
+ case 'm':
263
+ return (this.year < d.year) || (this.year === d.year && this.month < d.month);
264
+ case 'd':
265
+ return (this.year < d.year) || (this.year === d.year && this.month < d.month) || (this.year === d.year && this.month === d.month && this.day < d.day);
266
+ case 'h':
267
+ return (this.year < d.year) || (this.year === d.year && this.month < d.month) || (this.year === d.year && this.month === d.month && this.day < d.day) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours < d.hours);
268
+ case 'n':
269
+ return (this.year < d.year) || (this.year === d.year && this.month < d.month) || (this.year === d.year && this.month === d.month && this.day < d.day) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours < d.hours) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours === d.hours && this.minutes < d.minutes);
270
+ case 's':
271
+ return (this.year < d.year) || (this.year === d.year && this.month < d.month) || (this.year === d.year && this.month === d.month && this.day < d.day) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours < d.hours) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours === d.hours && this.minutes < d.minutes) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours === d.hours && this.minutes === d.minutes && this.seconds < d.seconds);
148
272
  }
149
- return false;
273
+ }
274
+
275
+ return this.mtst < d.mtst;
150
276
  }
277
+
278
+ isBeforeOrSame(date: any, unit: string = ''): Boolean {
279
+ const d = new bbnDateTool(date);
280
+ if (unitsCorrespondence[unit]) {
281
+ const realUnit = unitsCorrespondence[unit];
282
+ switch (realUnit) {
283
+ case 'y':
284
+ return this.year <= d.year;
285
+ case 'm':
286
+ return (this.year <= d.year) || (this.year === d.year && this.month <= d.month);
287
+ case 'd':
288
+ return (this.year <= d.year) || (this.year === d.year && this.month <= d.month) || (this.year === d.year && this.month === d.month && this.day <= d.day);
289
+ case 'h':
290
+ return (this.year <= d.year) || (this.year === d.year && this.month <= d.month) || (this.year === d.year && this.month === d.month && this.day <= d.day) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours <= d.hours);
291
+ case 'n':
292
+ return (this.year <= d.year) || (this.year === d.year && this.month <= d.month) || (this.year === d.year && this.month === d.month && this.day <= d.day) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours <= d.hours) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours === d.hours && this.minutes <= d.minutes);
293
+ case 's':
294
+ return (this.year <= d.year) || (this.year === d.year && this.month <= d.month) || (this.year === d.year && this.month === d.month && this.day <= d.day) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours <= d.hours) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours === d.hours && this.minutes <= d.minutes) || (this.year === d.year && this.month === d.month && this.day === d.day && this.hours === d.hours && this.minutes === d.minutes && this.seconds <= d.seconds);
295
+ }
296
+ }
297
+
298
+ return this.mtst < d.mtst;
299
+ }*/
151
300
  calendar(format) {
152
301
  let str = '';
153
302
  if (format) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.438",
3
+ "version": "1.0.439",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",