@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/_.d.ts +2 -1
- package/dist/_.js +2 -2
- package/dist/bbn.js +1 -1
- package/dist/bbn.js.map +1 -1
- package/dist/date.d.ts +2 -1
- package/dist/date.js +14 -8
- package/package.json +1 -1
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(
|
|
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
|
-
|
|
413
|
-
const
|
|
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
|
-
|
|
422
|
-
|
|
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(
|
|
425
|
-
const absDiff = Math.abs(
|
|
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 =
|
|
441
|
+
let diff = now - target;
|
|
436
442
|
if (abs) {
|
|
437
443
|
diff = Math.abs(diff);
|
|
438
444
|
}
|