@bbn/bbn 1.0.447 → 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;
@@ -16,8 +16,9 @@ declare class bbnDateTool {
16
16
  get MM(): string;
17
17
  get DD(): string;
18
18
  get HH(): string;
19
- get mm(): string;
20
- get ss(): string;
19
+ get II(): string;
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,57 +94,102 @@ 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
- 'n': 'Minutes',
112
+ 'i': 'Minutes',
103
113
  's': 'Seconds'
104
114
  };
105
115
  const formatsMap = {
106
116
  'y': 'YYYY',
107
117
  'm': 'MM',
108
118
  'd': 'DD',
119
+ 'w': 'WW',
109
120
  'h': 'HH',
110
- 'n': 'mm',
111
- 's': 'ss'
121
+ 'i': 'II',
122
+ 's': 'SS'
112
123
  };
113
124
  const unitsCorrespondence = {
125
+ 'years': 'y',
126
+ 'year': 'y',
127
+ 'YEARS': 'y',
128
+ 'YEAR': 'y',
129
+ 'Years': 'y',
130
+ 'Year': 'y',
114
131
  'YYYY': 'y',
115
132
  'YY': 'y',
116
133
  'yyyy': 'y',
117
134
  'yy': 'y',
118
- 'year': 'y',
119
135
  'Y': 'y',
120
136
  'y': 'y',
137
+ 'months': 'm',
138
+ 'month': 'm',
139
+ 'Months': 'm',
140
+ 'Month': 'm',
141
+ 'MONTHS': 'm',
142
+ 'MONTH': 'm',
121
143
  'MM': 'm',
122
144
  'M': 'm',
123
- 'month': 'm',
124
- 'mm': 'm',
125
145
  'm': 'm',
126
- 'DD': 'd',
146
+ 'days': 'd',
127
147
  'day': 'd',
148
+ 'Days': 'd',
149
+ 'Day': 'd',
150
+ 'DAYS': 'd',
151
+ 'DAY': 'd',
152
+ 'DD': 'd',
128
153
  'D': 'd',
129
154
  'dd': 'd',
130
155
  'd': 'd',
156
+ 'hours': 'h',
157
+ 'hour': 'h',
158
+ 'Hours': 'h',
159
+ 'Hour': 'h',
160
+ 'HOURS': 'h',
161
+ 'HOUR': 'h',
131
162
  'HH': 'h',
132
163
  'hr': 'h',
133
- 'hour': 'h',
134
164
  'H': 'h',
135
165
  'hh': 'h',
136
166
  'h': 'h',
137
- 'NN': 'n',
138
- 'nn': 'n',
139
- 'mn': 'n',
140
- 'minute': 'n',
141
- 'min': 'n',
142
- 'n': 'n',
167
+ 'minutes': 'i',
168
+ 'minute': 'i',
169
+ 'Minutes': 'i',
170
+ 'Minute': 'i',
171
+ 'MINUTES': 'i',
172
+ 'MINUTE': 'i',
173
+ 'II': 'i',
174
+ 'ii': 'i',
175
+ 'mn': 'i',
176
+ 'mm': 'i',
177
+ 'min': 'i',
178
+ 'n': 'i',
143
179
  'SS': 's',
144
180
  'ss': 's',
181
+ 'seconds': 's',
182
+ 'second': 's',
183
+ 'Seconds': 's',
184
+ 'Second': 's',
185
+ 'SECONDS': 's',
186
+ 'SECOND': 's',
145
187
  'sec': 's',
146
188
  's': 's',
147
189
  'S': 's',
190
+ 'WW': 'w',
191
+ 'W': 'w',
192
+ 'w': 'w'
148
193
  };
149
194
  class bbnDateTool {
150
195
  constructor(value, inputFormat = null) {
@@ -198,7 +243,7 @@ class bbnDateTool {
198
243
  }
199
244
  }
200
245
  toString() {
201
- return __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? __classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime() : 0;
246
+ return __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? this.format() : '';
202
247
  }
203
248
  get year() {
204
249
  return __classPrivateFieldGet(this, _bbnDateTool_value, "f").getFullYear();
@@ -242,12 +287,17 @@ class bbnDateTool {
242
287
  get HH() {
243
288
  return this.hours < 10 ? '0' + this.hours.toString() : this.hours.toString();
244
289
  }
245
- get mm() {
290
+ get II() {
246
291
  return this.minutes < 10 ? '0' + this.minutes.toString() : this.minutes.toString();
247
292
  }
248
- get ss() {
293
+ get SS() {
249
294
  return this.seconds < 10 ? '0' + this.seconds.toString() : this.seconds.toString();
250
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
+ }
251
301
  add(value, unit = 'd') {
252
302
  const date = __classPrivateFieldGet(this, _bbnDateTool_value, "f") ? new Date(__classPrivateFieldGet(this, _bbnDateTool_value, "f").getTime()) : new Date();
253
303
  if (unitsCorrespondence[unit]) {
@@ -267,12 +317,25 @@ class bbnDateTool {
267
317
  const d = new Date();
268
318
  return d;
269
319
  }
270
- format(format = 'y-m-d h:n:s') {
320
+ format(format = 'y-m-d h:i:s') {
271
321
  let str = '';
272
322
  if (format) {
273
- const reg = new RegExp('(' + Object.keys(unitsCorrespondence).join('|') + ')', 'g');
323
+ const reg = new RegExp('(\[|\]|' + Object.keys(unitsCorrespondence).join('|') + ')', 'g');
324
+ let opened = 0;
274
325
  const parts = format.split(reg);
275
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
+ }
276
339
  if (part in unitsCorrespondence) {
277
340
  const suffix = formatsMap[unitsCorrespondence[part]];
278
341
  str += this[suffix];
@@ -312,7 +375,7 @@ class bbnDateTool {
312
375
  }
313
376
  return 0;
314
377
  }
315
- const order = ['y', 'm', 'd', 'h', 'n', 's'];
378
+ const order = ['y', 'm', 'd', 'h', 'i', 's'];
316
379
  // Compare step by step until the requested precision
317
380
  for (const u of order) {
318
381
  const key = unitsMap[u].toLowerCase();
@@ -346,6 +409,44 @@ class bbnDateTool {
346
409
  isBeforeOrSame(date, unit = '') {
347
410
  return [-1, 0].includes(this.compare(date, unit));
348
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
+ }
349
450
  calendar(format) {
350
451
  let str = '';
351
452
  if (format) {
@@ -1,5 +1,4 @@
1
- import date from './date.js';
2
- import dayjs from 'dayjs';
1
+ import date from '../../date.js';
3
2
  /**
4
3
  * Returns a date with SQL format.
5
4
  *
@@ -21,7 +20,7 @@ import dayjs from 'dayjs';
21
20
  export default function dateSQL(v, dayOnly) {
22
21
  let value = date(v);
23
22
  if (value) {
24
- return dayjs(value).format('YYYY-MM-DD' + (dayOnly ? '' : ' HH:mm:ss'));
23
+ return value.format('YYYY-MM-DD' + (dayOnly ? '' : ' HH:mm:ss'));
25
24
  }
26
25
  }
27
26
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.447",
3
+ "version": "1.0.449",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",