@bbn/bbn 1.0.448 → 1.0.449

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,7 +1,7 @@
1
1
  declare class bbnDateTool {
2
2
  #private;
3
3
  constructor(value: any, inputFormat?: null | String);
4
- toString(): number;
4
+ toString(): string;
5
5
  get year(): number;
6
6
  get month(): number;
7
7
  get day(): number;
@@ -18,6 +18,7 @@ declare class bbnDateTool {
18
18
  get HH(): string;
19
19
  get II(): string;
20
20
  get SS(): string;
21
+ get WW(): string;
21
22
  add(value: number, unit?: string): bbnDateTool | null;
22
23
  sub(value: number, unit: string | null): bbnDateTool;
23
24
  dateFromFormat(value: string, unit: string | null): Date;
@@ -33,6 +34,9 @@ declare class bbnDateTool {
33
34
  isSame(date: any, unit?: string): Boolean;
34
35
  isAfterOrSame(date: any, unit?: string): Boolean;
35
36
  isBeforeOrSame(date: any, unit?: string): Boolean;
37
+ fromDate(date: any, unit?: string): string;
38
+ guessUnit(value: number): string | null;
39
+ diff(date: any, unit?: string, abs?: boolean): number;
36
40
  calendar(format: string): string;
37
41
  get daysInMonth(): Number | 0;
38
42
  }
package/dist/date.js CHANGED
@@ -94,10 +94,20 @@ const patterns = [
94
94
  })
95
95
  },
96
96
  ];
97
+ const units = [
98
+ ['y', "year", 365 * 24 * 60 * 60 * 1000],
99
+ ['m', "month", 30 * 24 * 60 * 60 * 1000],
100
+ ['w', "week", 7 * 24 * 60 * 60 * 1000],
101
+ ['d', "day", 24 * 60 * 60 * 1000],
102
+ ['h', "hour", 60 * 60 * 1000],
103
+ ['i', "minute", 60 * 1000],
104
+ ['s', "second", 1000]
105
+ ];
97
106
  const unitsMap = {
98
107
  'y': 'Year',
99
108
  'm': 'Month',
100
109
  'd': 'Date',
110
+ 'w': 'Week',
101
111
  'h': 'Hours',
102
112
  'i': 'Minutes',
103
113
  's': 'Seconds'
@@ -106,6 +116,7 @@ const formatsMap = {
106
116
  'y': 'YYYY',
107
117
  'm': 'MM',
108
118
  'd': 'DD',
119
+ 'w': 'WW',
109
120
  'h': 'HH',
110
121
  'i': 'II',
111
122
  's': 'SS'
@@ -176,6 +187,9 @@ const unitsCorrespondence = {
176
187
  'sec': 's',
177
188
  's': 's',
178
189
  'S': 's',
190
+ 'WW': 'w',
191
+ 'W': 'w',
192
+ 'w': 'w'
179
193
  };
180
194
  class bbnDateTool {
181
195
  constructor(value, inputFormat = null) {
@@ -229,7 +243,7 @@ class bbnDateTool {
229
243
  }
230
244
  }
231
245
  toString() {
232
- return __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? __classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime() : 0;
246
+ return __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? this.format() : '';
233
247
  }
234
248
  get year() {
235
249
  return __classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear();
@@ -279,6 +293,11 @@ class bbnDateTool {
279
293
  get SS() {
280
294
  return this.seconds < 10 ? '0' + this.seconds.toString() : this.seconds.toString();
281
295
  }
296
+ get WW() {
297
+ const firstDayOfYear = new Date(this.year, 0, 1);
298
+ const pastDaysOfYear = (__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime() - firstDayOfYear.getTime()) / 86400000;
299
+ return String(Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7)).padStart(2, '0');
300
+ }
282
301
  add(value, unit = 'd') {
283
302
  const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
284
303
  if (unitsCorrespondence[unit]) {
@@ -301,9 +320,22 @@ class bbnDateTool {
301
320
  format(format = 'y-m-d h:i:s') {
302
321
  let str = '';
303
322
  if (format) {
304
- const reg = new RegExp('(' + Object.keys(unitsCorrespondence).join('|') + ')', 'g');
323
+ const reg = new RegExp('(\[|\]|' + Object.keys(unitsCorrespondence).join('|') + ')', 'g');
324
+ let opened = 0;
305
325
  const parts = format.split(reg);
306
326
  each(parts, (part) => {
327
+ if (part === '[') {
328
+ opened++;
329
+ return;
330
+ }
331
+ else if (part === ']') {
332
+ opened--;
333
+ return;
334
+ }
335
+ if (opened > 0) {
336
+ str += part;
337
+ return;
338
+ }
307
339
  if (part in unitsCorrespondence) {
308
340
  const suffix = formatsMap[unitsCorrespondence[part]];
309
341
  str += this[suffix];
@@ -377,6 +409,44 @@ class bbnDateTool {
377
409
  isBeforeOrSame(date, unit = '') {
378
410
  return [-1, 0].includes(this.compare(date, unit));
379
411
  }
412
+ fromDate(date, unit = '') {
413
+ const diff = this.diff(date, unit);
414
+ const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" });
415
+ const chosenUnit = unitsCorrespondence[unit] || this.guessUnit(diff) || 's';
416
+ // FORCED UNIT MODE
417
+ const match = bbn.fn.getRow(units, d => d[0] === chosenUnit);
418
+ if (!match) {
419
+ throw new Error('Invalid unit for fromDate: ' + unit);
420
+ }
421
+ const [u, rtfUnit, ms] = match;
422
+ return rtf.format(diff, rtfUnit);
423
+ }
424
+ guessUnit(value) {
425
+ const absDiff = Math.abs(value);
426
+ for (const [shortUnit, rtfUnit, ms] of units) {
427
+ if ((absDiff >= ms) || (rtfUnit === "second")) {
428
+ return shortUnit;
429
+ }
430
+ }
431
+ }
432
+ diff(date, unit = '', abs = false) {
433
+ const target = (date instanceof bbnDateTool) ? date.mtst : new Date(date).getTime();
434
+ const now = this.mtst;
435
+ let diff = target - now;
436
+ if (abs) {
437
+ diff = Math.abs(diff);
438
+ }
439
+ if (!unit) {
440
+ return diff;
441
+ }
442
+ const realUnit = unitsCorrespondence[unit];
443
+ const match = bbn.fn.getRow(units, d => d[0] === realUnit);
444
+ if (!match) {
445
+ throw new Error('Invalid unit for diff: ' + unit);
446
+ }
447
+ const [u, rtfUnit, ms] = match;
448
+ return Math.round(diff / ms);
449
+ }
380
450
  calendar(format) {
381
451
  let str = '';
382
452
  if (format) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.448",
3
+ "version": "1.0.449",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",