@bbn/bbn 1.0.449 → 1.0.451

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
@@ -34,8 +34,9 @@ declare class bbnDateTool {
34
34
  isSame(date: any, unit?: string): Boolean;
35
35
  isAfterOrSame(date: any, unit?: string): Boolean;
36
36
  isBeforeOrSame(date: any, unit?: string): Boolean;
37
+ fromNow(unit?: string): string;
37
38
  fromDate(date: any, unit?: string): string;
38
- guessUnit(value: number): string | null;
39
+ guessUnit(valueInMs: number): string | null;
39
40
  diff(date: any, unit?: string, abs?: boolean): number;
40
41
  calendar(format: string): string;
41
42
  get daysInMonth(): Number | 0;
package/dist/date.js CHANGED
@@ -10,6 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _bbnDateTool_value, _bbnDateTool_daysInMonth;
13
+ import _ from './_.js';
13
14
  import each from './fn/loop/each.js';
14
15
  import substr from './fn/string/substr.js';
15
16
  import isNumber from './fn/type/isNumber.js';
@@ -409,20 +410,25 @@ class bbnDateTool {
409
410
  isBeforeOrSame(date, unit = '') {
410
411
  return [-1, 0].includes(this.compare(date, unit));
411
412
  }
412
- fromDate(date, unit = '') {
413
- const diff = this.diff(date, unit);
413
+ fromNow(unit = '') {
414
+ const date = new Date();
415
+ const chosenUnit = unitsCorrespondence[unit] || this.guessUnit(this.diff(date));
416
+ const diff = this.diff(date, chosenUnit);
414
417
  const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" });
415
- const chosenUnit = unitsCorrespondence[unit] || this.guessUnit(diff) || 's';
416
418
  // FORCED UNIT MODE
417
419
  const match = bbn.fn.getRow(units, d => d[0] === chosenUnit);
418
420
  if (!match) {
419
421
  throw new Error('Invalid unit for fromDate: ' + unit);
420
422
  }
421
- const [u, rtfUnit, ms] = match;
422
- return rtf.format(diff, rtfUnit);
423
+ return rtf.format(diff, match[1]);
424
+ }
425
+ fromDate(date, unit = '') {
426
+ const chosenUnit = unitsCorrespondence[unit] || this.guessUnit(this.diff(date));
427
+ const diff = this.diff(date, chosenUnit);
428
+ return diff > 0 ? _('%d %s before', diff, unit) : (diff < 0 ? _('%d %s after', -diff, unit) : _("The same %s", unit));
423
429
  }
424
- guessUnit(value) {
425
- const absDiff = Math.abs(value);
430
+ guessUnit(valueInMs) {
431
+ const absDiff = Math.abs(valueInMs);
426
432
  for (const [shortUnit, rtfUnit, ms] of units) {
427
433
  if ((absDiff >= ms) || (rtfUnit === "second")) {
428
434
  return shortUnit;
@@ -432,7 +438,7 @@ class bbnDateTool {
432
438
  diff(date, unit = '', abs = false) {
433
439
  const target = (date instanceof bbnDateTool) ? date.mtst : new Date(date).getTime();
434
440
  const now = this.mtst;
435
- let diff = target - now;
441
+ let diff = now - target;
436
442
  if (abs) {
437
443
  diff = Math.abs(diff);
438
444
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.449",
3
+ "version": "1.0.451",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",