@bbn/bbn 1.0.486 → 1.0.487

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
@@ -33,8 +33,7 @@ declare class bbnDateTool {
33
33
  get daysInMonth(): number;
34
34
  valueOf(): number;
35
35
  add(value: number, unit?: string): bbnDateTool | null;
36
- sub(value: number, unit: string | null): bbnDateTool;
37
- subtract(value: number, unit: string | null): bbnDateTool;
36
+ subtract(value: number, unit?: string): bbnDateTool;
38
37
  dateFromFormat(value: string, unit: string | null): Date;
39
38
  date(): string;
40
39
  datetime(): string;
@@ -82,6 +81,7 @@ declare class bbnDateTool {
82
81
  * Units: year, month, week, day, hour, minute, second
83
82
  */
84
83
  endOf(unit?: string): bbnDateTool;
84
+ duration(num: number, unit?: string): any;
85
85
  }
86
86
  declare function generatorFunction(value: any, inputFormat?: null | String): bbnDateTool;
87
87
  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);
@@ -430,9 +539,6 @@ class bbnDateTool {
430
539
  }
431
540
  return null;
432
541
  }
433
- sub(value, unit) {
434
- return this.add(-value, unit);
435
- }
436
542
  subtract(value, unit) {
437
543
  return this.add(-value, unit);
438
544
  }
@@ -814,8 +920,11 @@ class bbnDateTool {
814
920
  }
815
921
  return new bbnDateTool(d);
816
922
  }
923
+ duration(num, unit = 's') {
924
+ return new bbnDateDuration(num, unit);
925
+ }
817
926
  }
818
- _bbnDateTool_value = new WeakMap(), _bbnDateTool_daysInMonth = new WeakMap();
927
+ _bbnDateTool_value = new WeakMap(), _bbnDateTool_daysInMonth = new WeakMap(), _bbnDateTool_isDuration = new WeakMap();
819
928
  function generatorFunction(value, inputFormat = null) {
820
929
  if (value instanceof bbnDateTool) {
821
930
  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.487",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",