@bbn/bbn 1.0.486 → 1.0.488

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
@@ -23,18 +23,22 @@ declare class bbnDateTool {
23
23
  get MMMM(): string;
24
24
  get MMM(): string;
25
25
  get MM(): string;
26
+ get M(): string;
26
27
  get EE(): string;
27
28
  get DD(): string;
29
+ get D(): string;
28
30
  get HH(): string;
31
+ get H(): string;
29
32
  get II(): string;
33
+ get I(): string;
30
34
  get SS(): string;
35
+ get S(): string;
31
36
  get WW(): string;
32
37
  get isValid(): boolean;
33
38
  get daysInMonth(): number;
34
39
  valueOf(): number;
35
40
  add(value: number, unit?: string): bbnDateTool | null;
36
- sub(value: number, unit: string | null): bbnDateTool;
37
- subtract(value: number, unit: string | null): bbnDateTool;
41
+ subtract(value: number, unit?: string): bbnDateTool;
38
42
  dateFromFormat(value: string, unit: string | null): Date;
39
43
  date(): string;
40
44
  datetime(): string;
@@ -82,6 +86,7 @@ declare class bbnDateTool {
82
86
  * Units: year, month, week, day, hour, minute, second
83
87
  */
84
88
  endOf(unit?: string): bbnDateTool;
89
+ duration(num: number, unit?: string): any;
85
90
  }
86
91
  declare function generatorFunction(value: any, inputFormat?: null | String): bbnDateTool;
87
92
  export default generatorFunction;
package/dist/date.js CHANGED
@@ -9,7 +9,7 @@ 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 _bbnDateDuration_instances, _bbnDateDuration_durationMs, _bbnDateDuration_unit, _bbnDateDuration_getUnitRowByName, _bbnDateDuration_getUnitValue, _bbnDateTool_value, _bbnDateTool_daysInMonth, _bbnDateTool_isDuration;
13
13
  import _ from './_.js';
14
14
  import each from './fn/loop/each.js';
15
15
  import substr from './fn/string/substr.js';
@@ -204,18 +204,127 @@ const unitsCorrespondence = {
204
204
  'W': 'w',
205
205
  'w': 'w'
206
206
  };
207
+ class bbnDateDuration {
208
+ constructor(len, unit, fromMs = false) {
209
+ _bbnDateDuration_instances.add(this);
210
+ _bbnDateDuration_durationMs.set(this, 0);
211
+ _bbnDateDuration_unit.set(this, '');
212
+ const realUnit = unitsCorrespondence[unit] || unit;
213
+ if (!realUnit) {
214
+ throw new Error('Invalid unit for duration: ' + unit);
215
+ }
216
+ __classPrivateFieldSet(this, _bbnDateDuration_unit, realUnit, "f");
217
+ const row = bbn.fn.getRow(units, d => d[0] === realUnit);
218
+ if (!row) {
219
+ throw new Error('Invalid unit for duration: ' + realUnit);
220
+ }
221
+ const msPerUnit = row[2];
222
+ __classPrivateFieldSet(this, _bbnDateDuration_durationMs, fromMs ? len : len * msPerUnit, "f");
223
+ }
224
+ // -----------------------
225
+ // Public getters
226
+ // -----------------------
227
+ years(remaining = false) { return __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitValue).call(this, 'year', remaining); }
228
+ months(remaining = false) { return __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitValue).call(this, 'month', remaining); }
229
+ weeks(remaining = false) { return __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitValue).call(this, 'week', remaining); }
230
+ days(remaining = false) { return __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitValue).call(this, 'day', remaining); }
231
+ hours(remaining = false) { return __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitValue).call(this, 'hour', remaining); }
232
+ minutes(remaining = false) { return __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitValue).call(this, 'minute', remaining); }
233
+ seconds(remaining = false) { return __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitValue).call(this, 'second', remaining); }
234
+ // -----------------------
235
+ // Day.js style
236
+ // "asX" conversions
237
+ // -----------------------
238
+ /**
239
+ * Returns the full duration expressed as X (float), like Day.js.
240
+ */
241
+ asYears() {
242
+ const [, , ms] = __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitRowByName).call(this, 'year');
243
+ return __classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f") / ms;
244
+ }
245
+ asMonths() {
246
+ const [, , ms] = __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitRowByName).call(this, 'month');
247
+ return __classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f") / ms;
248
+ }
249
+ asWeeks() {
250
+ const [, , ms] = __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitRowByName).call(this, 'week');
251
+ return __classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f") / ms;
252
+ }
253
+ asDays() {
254
+ const [, , ms] = __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitRowByName).call(this, 'day');
255
+ return __classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f") / ms;
256
+ }
257
+ asHours() {
258
+ const [, , ms] = __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitRowByName).call(this, 'hour');
259
+ return __classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f") / ms;
260
+ }
261
+ asMinutes() {
262
+ const [, , ms] = __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitRowByName).call(this, 'minute');
263
+ return __classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f") / ms;
264
+ }
265
+ asSeconds() {
266
+ const [, , ms] = __classPrivateFieldGet(this, _bbnDateDuration_instances, "m", _bbnDateDuration_getUnitRowByName).call(this, 'second');
267
+ return __classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f") / ms;
268
+ }
269
+ /**
270
+ * Add any unit (or instance default).
271
+ */
272
+ add(value, unit) {
273
+ const targetUnit = unit
274
+ ? (unitsCorrespondence[unit] || unit)
275
+ : __classPrivateFieldGet(this, _bbnDateDuration_unit, "f");
276
+ const row = bbn.fn.getRow(units, d => d[0] === targetUnit);
277
+ if (!row) {
278
+ throw new Error('Invalid unit for duration: ' + (unit !== null && unit !== void 0 ? unit : targetUnit));
279
+ }
280
+ return new bbnDateDuration(__classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f") + value * row[2], __classPrivateFieldGet(this, _bbnDateDuration_unit, "f"), true);
281
+ }
282
+ subtract(value, unit) {
283
+ return this.add(-value, unit);
284
+ }
285
+ toMilliseconds() {
286
+ return __classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f");
287
+ }
288
+ }
289
+ _bbnDateDuration_durationMs = new WeakMap(), _bbnDateDuration_unit = new WeakMap(), _bbnDateDuration_instances = new WeakSet(), _bbnDateDuration_getUnitRowByName = function _bbnDateDuration_getUnitRowByName(name) {
290
+ const row = bbn.fn.getRow(units, d => d[1] === name);
291
+ if (!row) {
292
+ throw new Error('Unit name not found: ' + name);
293
+ }
294
+ return row;
295
+ }, _bbnDateDuration_getUnitValue = function _bbnDateDuration_getUnitValue(name, remaining) {
296
+ const index = units.findIndex(([, n]) => n === name);
297
+ if (index === -1) {
298
+ throw new Error('Unit not found: ' + name);
299
+ }
300
+ const unitMs = units[index][2];
301
+ // Total units
302
+ if (!remaining) {
303
+ return Math.floor(__classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f") / unitMs);
304
+ }
305
+ // Remaining units
306
+ let remainingMs = __classPrivateFieldGet(this, _bbnDateDuration_durationMs, "f");
307
+ for (let i = 0; i < index; i++) {
308
+ const [, , msHigher] = units[i];
309
+ const amount = Math.floor(remainingMs / msHigher);
310
+ remainingMs -= amount * msHigher;
311
+ }
312
+ return Math.floor(remainingMs / unitMs);
313
+ };
207
314
  class bbnDateTool {
208
315
  constructor(value, inputFormat = null) {
209
316
  _bbnDateTool_value.set(this, void 0);
210
317
  _bbnDateTool_daysInMonth.set(this, 0);
318
+ _bbnDateTool_isDuration.set(this, false);
319
+ let t = typeof value;
211
320
  if (!value) {
212
321
  __classPrivateFieldSet(this, _bbnDateTool_value, new Date(), "f");
213
322
  }
214
323
  else if (inputFormat) {
324
+ /** @todo read the date from the input format */
215
325
  __classPrivateFieldSet(this, _bbnDateTool_value, new Date(), "f");
216
326
  }
217
327
  else {
218
- let t = typeof value;
219
328
  if (t === 'number' || (isNumber(value) && value !== '')) {
220
329
  if ((value < 5000) && isNumber(inputFormat)) {
221
330
  value = Array.from(arguments);
@@ -362,6 +471,9 @@ class bbnDateTool {
362
471
  const m = parseInt(this.month().toString());
363
472
  return m < 10 ? '0' + m.toString() : m.toString();
364
473
  }
474
+ get M() {
475
+ return this.month().toString();
476
+ }
365
477
  get EE() {
366
478
  return this.weekday().toString();
367
479
  }
@@ -369,18 +481,30 @@ class bbnDateTool {
369
481
  const d = parseInt(this.day().toString());
370
482
  return d < 10 ? '0' + d.toString() : d.toString();
371
483
  }
484
+ get D() {
485
+ return this.day().toString();
486
+ }
372
487
  get HH() {
373
488
  const h = parseInt(this.hours().toString());
374
489
  return h < 10 ? '0' + h.toString() : h.toString();
375
490
  }
491
+ get H() {
492
+ return this.hours().toString();
493
+ }
376
494
  get II() {
377
495
  const i = parseInt(this.minutes().toString());
378
496
  return i < 10 ? '0' + i.toString() : i.toString();
379
497
  }
498
+ get I() {
499
+ return this.minutes().toString();
500
+ }
380
501
  get SS() {
381
502
  const s = parseInt(this.seconds().toString());
382
503
  return s < 10 ? '0' + s.toString() : s.toString();
383
504
  }
505
+ get S() {
506
+ return this.seconds().toString();
507
+ }
384
508
  get WW() {
385
509
  const y = parseInt(this.year().toString());
386
510
  const firstDayOfYear = new Date(y, 0, 1);
@@ -430,9 +554,6 @@ class bbnDateTool {
430
554
  }
431
555
  return null;
432
556
  }
433
- sub(value, unit) {
434
- return this.add(-value, unit);
435
- }
436
557
  subtract(value, unit) {
437
558
  return this.add(-value, unit);
438
559
  }
@@ -814,8 +935,11 @@ class bbnDateTool {
814
935
  }
815
936
  return new bbnDateTool(d);
816
937
  }
938
+ duration(num, unit = 's') {
939
+ return new bbnDateDuration(num, unit);
940
+ }
817
941
  }
818
- _bbnDateTool_value = new WeakMap(), _bbnDateTool_daysInMonth = new WeakMap();
942
+ _bbnDateTool_value = new WeakMap(), _bbnDateTool_daysInMonth = new WeakMap(), _bbnDateTool_isDuration = new WeakMap();
819
943
  function generatorFunction(value, inputFormat = null) {
820
944
  if (value instanceof bbnDateTool) {
821
945
  return value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.486",
3
+ "version": "1.0.488",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",