@bbn/bbn 1.0.438 → 1.0.440
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/bbn.js +1 -1
- package/dist/bbn.js.map +1 -1
- package/dist/date.d.ts +16 -3
- package/dist/date.js +265 -33
- package/package.json +1 -1
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
@@ -9,24 +9,147 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _bbnDateTool_value, _bbnDateTool_daysInMonth
|
|
12
|
+
var _bbnDateTool_value, _bbnDateTool_daysInMonth;
|
|
13
13
|
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
|
-
|
|
17
|
+
const patterns = [
|
|
18
|
+
// MariaDB DATETIME "YYYY-MM-DD HH:MM:SS"
|
|
19
|
+
{
|
|
20
|
+
name: 'mariadb-datetime',
|
|
21
|
+
re: /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2}):(\d{2})$/,
|
|
22
|
+
map: m => ({
|
|
23
|
+
year: +m[1],
|
|
24
|
+
month: +m[2],
|
|
25
|
+
day: +m[3],
|
|
26
|
+
hours: +m[4],
|
|
27
|
+
minutes: +m[5],
|
|
28
|
+
seconds: +m[6],
|
|
29
|
+
})
|
|
30
|
+
},
|
|
31
|
+
// MariaDB DATETIME without seconds "YYYY-MM-DD HH:MM"
|
|
32
|
+
{
|
|
33
|
+
name: 'mariadb-datetime-no-sec',
|
|
34
|
+
re: /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})$/,
|
|
35
|
+
map: m => ({
|
|
36
|
+
year: +m[1],
|
|
37
|
+
month: +m[2],
|
|
38
|
+
day: +m[3],
|
|
39
|
+
hours: +m[4],
|
|
40
|
+
minutes: +m[5],
|
|
41
|
+
seconds: 0,
|
|
42
|
+
})
|
|
43
|
+
},
|
|
44
|
+
// MariaDB DATE "YYYY-MM-DD"
|
|
45
|
+
{
|
|
46
|
+
name: 'mariadb-date',
|
|
47
|
+
re: /^(\d{4})-(\d{2})-(\d{2})$/,
|
|
48
|
+
map: m => ({
|
|
49
|
+
year: +m[1],
|
|
50
|
+
month: +m[2],
|
|
51
|
+
day: +m[3],
|
|
52
|
+
hours: 0,
|
|
53
|
+
minutes: 0,
|
|
54
|
+
seconds: 0,
|
|
55
|
+
})
|
|
56
|
+
},
|
|
57
|
+
// ISO / JS-style "YYYY-MM-DDTHH:MM[:SS][.sss][Z or ±HH:MM]"
|
|
58
|
+
{
|
|
59
|
+
name: 'iso-datetime',
|
|
60
|
+
re: /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2}))?(?:\.\d+)?(?:Z|[+\-]\d{2}:?\d{2})?$/,
|
|
61
|
+
map: m => ({
|
|
62
|
+
year: +m[1],
|
|
63
|
+
month: +m[2],
|
|
64
|
+
day: +m[3],
|
|
65
|
+
hours: +m[4],
|
|
66
|
+
minutes: +m[5],
|
|
67
|
+
seconds: m[6] !== undefined ? +m[6] : 0,
|
|
68
|
+
})
|
|
69
|
+
},
|
|
70
|
+
// Simple slash date "YYYY/MM/DD"
|
|
71
|
+
{
|
|
72
|
+
name: 'slash-date',
|
|
73
|
+
re: /^(\d{2})\/(\d{2})\/(\d{4})$/,
|
|
74
|
+
map: m => ({
|
|
75
|
+
year: +m[3],
|
|
76
|
+
month: +m[2],
|
|
77
|
+
day: +m[1],
|
|
78
|
+
hours: 0,
|
|
79
|
+
minutes: 0,
|
|
80
|
+
seconds: 0,
|
|
81
|
+
})
|
|
82
|
+
},
|
|
83
|
+
// Slash datetime "YYYY/MM/DD HH:MM:SS"
|
|
84
|
+
{
|
|
85
|
+
name: 'slash-datetime',
|
|
86
|
+
re: /^(\d{2})\/(\d{2})\/(\d{4})[ T](\d{2}):(\d{2}):(\d{2})$/,
|
|
87
|
+
map: m => ({
|
|
88
|
+
year: +m[3],
|
|
89
|
+
month: +m[2],
|
|
90
|
+
day: +m[1],
|
|
91
|
+
hours: +m[4],
|
|
92
|
+
minutes: +m[5],
|
|
93
|
+
seconds: +m[6],
|
|
94
|
+
})
|
|
95
|
+
},
|
|
96
|
+
];
|
|
97
|
+
const unitsMap = {
|
|
98
|
+
'y': 'FullYear',
|
|
99
|
+
'm': 'Month',
|
|
100
|
+
'd': 'Date',
|
|
101
|
+
'h': 'Hours',
|
|
102
|
+
'n': 'Minutes',
|
|
103
|
+
's': 'Seconds'
|
|
104
|
+
};
|
|
105
|
+
const unitsCorrespondence = {
|
|
106
|
+
'YYYY': 'y',
|
|
107
|
+
'YY': 'y',
|
|
108
|
+
'yyyy': 'y',
|
|
109
|
+
'yy': 'y',
|
|
110
|
+
'year': 'y',
|
|
111
|
+
'Y': 'y',
|
|
112
|
+
'y': 'y',
|
|
113
|
+
'MM': 'm',
|
|
114
|
+
'M': 'm',
|
|
115
|
+
'month': 'm',
|
|
116
|
+
'mm': 'm',
|
|
117
|
+
'm': 'm',
|
|
118
|
+
'DD': 'd',
|
|
119
|
+
'day': 'd',
|
|
120
|
+
'D': 'd',
|
|
121
|
+
'dd': 'd',
|
|
122
|
+
'd': 'd',
|
|
123
|
+
'HH': 'h',
|
|
124
|
+
'hr': 'h',
|
|
125
|
+
'hour': 'h',
|
|
126
|
+
'H': 'h',
|
|
127
|
+
'hh': 'h',
|
|
128
|
+
'h': 'h',
|
|
129
|
+
'NN': 'n',
|
|
130
|
+
'nn': 'n',
|
|
131
|
+
'mn': 'n',
|
|
132
|
+
'minute': 'n',
|
|
133
|
+
'min': 'n',
|
|
134
|
+
'n': 'n',
|
|
135
|
+
'SS': 's',
|
|
136
|
+
'ss': 's',
|
|
137
|
+
'sec': 's',
|
|
138
|
+
's': 's',
|
|
139
|
+
'S': 's',
|
|
140
|
+
};
|
|
18
141
|
class bbnDateTool {
|
|
19
142
|
constructor(value, inputFormat = null) {
|
|
20
143
|
_bbnDateTool_value.set(this, void 0);
|
|
21
144
|
_bbnDateTool_daysInMonth.set(this, undefined);
|
|
22
|
-
|
|
23
|
-
|
|
145
|
+
if (!value) {
|
|
146
|
+
__classPrivateFieldSet(this, _bbnDateTool_value, new Date(), "f");
|
|
147
|
+
}
|
|
148
|
+
else if (inputFormat) {
|
|
149
|
+
__classPrivateFieldSet(this, _bbnDateTool_value, new Date(), "f");
|
|
24
150
|
}
|
|
25
151
|
else {
|
|
26
152
|
let t = typeof value;
|
|
27
|
-
if (value === undefined) {
|
|
28
|
-
__classPrivateFieldSet(this, _bbnDateTool_value, new Date(), "f");
|
|
29
|
-
}
|
|
30
153
|
if (t === 'number' || (isNumber(value) && value !== '')) {
|
|
31
154
|
if ((value < 5000) && isNumber(inputFormat)) {
|
|
32
155
|
value = Array.from(arguments);
|
|
@@ -39,11 +162,15 @@ class bbnDateTool {
|
|
|
39
162
|
}
|
|
40
163
|
}
|
|
41
164
|
if (t === 'string') {
|
|
42
|
-
|
|
43
|
-
|
|
165
|
+
for (const p of patterns) {
|
|
166
|
+
const m = value.match(p.re);
|
|
167
|
+
if (m) {
|
|
168
|
+
const { year, month, day, hours, minutes, seconds } = p.map(m);
|
|
169
|
+
__classPrivateFieldSet(this, _bbnDateTool_value, new Date(year, month - 1, day, hours, minutes, seconds, 0), "f");
|
|
170
|
+
}
|
|
44
171
|
}
|
|
45
|
-
|
|
46
|
-
|
|
172
|
+
if (!__classPrivateFieldGet(this, _bbnDateTool_value, "f")) {
|
|
173
|
+
throw new Error('Invalid date string format: ' + value);
|
|
47
174
|
}
|
|
48
175
|
}
|
|
49
176
|
else if (isDate(value)) {
|
|
@@ -62,6 +189,9 @@ class bbnDateTool {
|
|
|
62
189
|
});
|
|
63
190
|
}
|
|
64
191
|
}
|
|
192
|
+
toString() {
|
|
193
|
+
return __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? __classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime() : 0;
|
|
194
|
+
}
|
|
65
195
|
get year() {
|
|
66
196
|
return __classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear();
|
|
67
197
|
}
|
|
@@ -80,6 +210,12 @@ class bbnDateTool {
|
|
|
80
210
|
get seconds() {
|
|
81
211
|
return __classPrivateFieldGet(this, _bbnDateTool_value, "f").getSeconds();
|
|
82
212
|
}
|
|
213
|
+
get tst() {
|
|
214
|
+
return Math.ceil(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime() / 1000);
|
|
215
|
+
}
|
|
216
|
+
get mtst() {
|
|
217
|
+
return __classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime();
|
|
218
|
+
}
|
|
83
219
|
get YYYY() {
|
|
84
220
|
return this.year.toString();
|
|
85
221
|
}
|
|
@@ -92,25 +228,43 @@ class bbnDateTool {
|
|
|
92
228
|
get DD() {
|
|
93
229
|
return this.day < 10 ? '0' + this.day.toString() : this.day.toString();
|
|
94
230
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
231
|
+
get HH() {
|
|
232
|
+
return this.hours < 10 ? '0' + this.hours.toString() : this.hours.toString();
|
|
233
|
+
}
|
|
234
|
+
get mm() {
|
|
235
|
+
return this.minutes < 10 ? '0' + this.minutes.toString() : this.minutes.toString();
|
|
236
|
+
}
|
|
237
|
+
get ss() {
|
|
238
|
+
return this.seconds < 10 ? '0' + this.seconds.toString() : this.seconds.toString();
|
|
239
|
+
}
|
|
240
|
+
add(value, unit = 'd') {
|
|
241
|
+
const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
|
|
242
|
+
if (unitsCorrespondence[unit]) {
|
|
243
|
+
const realUnit = unitsCorrespondence[unit];
|
|
244
|
+
const suffix = unitsMap[realUnit];
|
|
245
|
+
const getter = 'get' + suffix;
|
|
246
|
+
const setter = 'set' + suffix;
|
|
247
|
+
date[setter](date[getter]() + value);
|
|
248
|
+
return new bbnDateTool(date);
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
98
251
|
}
|
|
99
252
|
sub(value, unit) {
|
|
100
|
-
|
|
101
|
-
return new bbnDateTool(newValue);
|
|
253
|
+
return this.add(-value, unit);
|
|
102
254
|
}
|
|
103
255
|
dateFromFormat(value, unit) {
|
|
104
256
|
const d = new Date();
|
|
105
257
|
return d;
|
|
106
258
|
}
|
|
107
|
-
format(format) {
|
|
259
|
+
format(format = '') {
|
|
260
|
+
const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
|
|
108
261
|
let str = '';
|
|
109
262
|
if (format) {
|
|
110
|
-
const parts = format.split(/(
|
|
263
|
+
const parts = format.split('/(' + Object.keys(unitsCorrespondence).join('|') + ')/');
|
|
111
264
|
each(parts, (part) => {
|
|
112
|
-
if (part in
|
|
113
|
-
|
|
265
|
+
if (part in unitsCorrespondence) {
|
|
266
|
+
const suffix = unitsMap[unitsCorrespondence[part]];
|
|
267
|
+
str += date['get' + suffix]();
|
|
114
268
|
}
|
|
115
269
|
else {
|
|
116
270
|
str += part;
|
|
@@ -130,24 +284,102 @@ class bbnDateTool {
|
|
|
130
284
|
}
|
|
131
285
|
return 0;
|
|
132
286
|
}
|
|
133
|
-
|
|
287
|
+
/**
|
|
288
|
+
* Compare this date to another date with a given precision.
|
|
289
|
+
* @returns -1 if this < other, 0 if equal, 1 if this > other
|
|
290
|
+
*/
|
|
291
|
+
compare(date, unit = '') {
|
|
134
292
|
const d = new bbnDateTool(date);
|
|
135
|
-
|
|
293
|
+
const realUnit = unitsCorrespondence[unit] || null;
|
|
294
|
+
// If no unit or unknown unit, fall back to timestamp comparison
|
|
295
|
+
if (!realUnit) {
|
|
296
|
+
if (this.mtst < d.mtst) {
|
|
297
|
+
return -1;
|
|
298
|
+
}
|
|
299
|
+
if (this.mtst > d.mtst) {
|
|
300
|
+
return 1;
|
|
301
|
+
}
|
|
302
|
+
return 0;
|
|
136
303
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
304
|
+
const order = ['y', 'm', 'd', 'h', 'n', 's'];
|
|
305
|
+
// Compare step by step until the requested precision
|
|
306
|
+
for (const u of order) {
|
|
307
|
+
const key = unitsMap[u];
|
|
308
|
+
const a = this[key];
|
|
309
|
+
const b = d[key];
|
|
310
|
+
if (a < b) {
|
|
311
|
+
return -1;
|
|
312
|
+
}
|
|
313
|
+
if (a > b) {
|
|
314
|
+
return 1;
|
|
315
|
+
}
|
|
316
|
+
// Stop when we've reached the desired unit
|
|
317
|
+
if (u === realUnit) {
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
142
320
|
}
|
|
143
|
-
return
|
|
321
|
+
return 0;
|
|
144
322
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
323
|
+
isBefore(date, unit = '') {
|
|
324
|
+
return this.compare(date, unit) === -1;
|
|
325
|
+
}
|
|
326
|
+
isAfter(date, unit = 'day') {
|
|
327
|
+
return this.compare(date, unit) === 1;
|
|
328
|
+
}
|
|
329
|
+
isSame(date, unit = 'day') {
|
|
330
|
+
return this.compare(date, unit) === 0;
|
|
331
|
+
}
|
|
332
|
+
isAfterOrSame(date, unit = '') {
|
|
333
|
+
return [0, 1].includes(this.compare(date, unit));
|
|
334
|
+
}
|
|
335
|
+
isBeforeOrSame(date, unit = '') {
|
|
336
|
+
return [-1, 0].includes(this.compare(date, unit));
|
|
337
|
+
}
|
|
338
|
+
/*
|
|
339
|
+
const d = new bbnDateTool(date);
|
|
340
|
+
if (unitsCorrespondence[unit]) {
|
|
341
|
+
const realUnit = unitsCorrespondence[unit];
|
|
342
|
+
switch (realUnit) {
|
|
343
|
+
case 'y':
|
|
344
|
+
return this.year < d.year;
|
|
345
|
+
case 'm':
|
|
346
|
+
return (this.year < d.year) || (this.year === d.year && this.month < d.month);
|
|
347
|
+
case 'd':
|
|
348
|
+
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);
|
|
349
|
+
case 'h':
|
|
350
|
+
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);
|
|
351
|
+
case 'n':
|
|
352
|
+
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);
|
|
353
|
+
case 's':
|
|
354
|
+
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
355
|
}
|
|
149
|
-
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return this.mtst < d.mtst;
|
|
150
359
|
}
|
|
360
|
+
|
|
361
|
+
isBeforeOrSame(date: any, unit: string = ''): Boolean {
|
|
362
|
+
const d = new bbnDateTool(date);
|
|
363
|
+
if (unitsCorrespondence[unit]) {
|
|
364
|
+
const realUnit = unitsCorrespondence[unit];
|
|
365
|
+
switch (realUnit) {
|
|
366
|
+
case 'y':
|
|
367
|
+
return this.year <= d.year;
|
|
368
|
+
case 'm':
|
|
369
|
+
return (this.year <= d.year) || (this.year === d.year && this.month <= d.month);
|
|
370
|
+
case 'd':
|
|
371
|
+
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);
|
|
372
|
+
case 'h':
|
|
373
|
+
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);
|
|
374
|
+
case 'n':
|
|
375
|
+
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);
|
|
376
|
+
case 's':
|
|
377
|
+
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);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return this.mtst < d.mtst;
|
|
382
|
+
}*/
|
|
151
383
|
calendar(format) {
|
|
152
384
|
let str = '';
|
|
153
385
|
if (format) {
|
|
@@ -183,7 +415,7 @@ class bbnDateTool {
|
|
|
183
415
|
return __classPrivateFieldGet(this, _bbnDateTool_daysInMonth, "f");
|
|
184
416
|
}
|
|
185
417
|
}
|
|
186
|
-
_bbnDateTool_value = new WeakMap(), _bbnDateTool_daysInMonth = new WeakMap()
|
|
418
|
+
_bbnDateTool_value = new WeakMap(), _bbnDateTool_daysInMonth = new WeakMap();
|
|
187
419
|
function generatorFunction(value, inputFormat = null) {
|
|
188
420
|
return new bbnDateTool(value, inputFormat);
|
|
189
421
|
}
|