chronal 0.0.10 → 0.0.12
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/esm/core/rtf.d.ts +2 -0
- package/esm/core/rtf.d.ts.map +1 -0
- package/esm/core/rtf.js +10 -0
- package/esm/lib/from-now.d.ts.map +1 -1
- package/esm/lib/from-now.js +9 -3
- package/esm/lib/to-now.d.ts.map +1 -1
- package/esm/lib/to-now.js +9 -3
- package/package.json +1 -1
- package/script/core/rtf.d.ts +2 -0
- package/script/core/rtf.d.ts.map +1 -0
- package/script/core/rtf.js +13 -0
- package/script/lib/from-now.d.ts.map +1 -1
- package/script/lib/from-now.js +9 -3
- package/script/lib/to-now.d.ts.map +1 -1
- package/script/lib/to-now.js +9 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rtf.d.ts","sourceRoot":"","sources":["../../src/core/rtf.ts"],"names":[],"mappings":"AAEA,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,2BAQhE"}
|
package/esm/core/rtf.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const rtfCache = new Map();
|
|
2
|
+
export function getRTF(locale, numeric) {
|
|
3
|
+
const key = `${locale}|${numeric}`;
|
|
4
|
+
let rtf = rtfCache.get(key);
|
|
5
|
+
if (!rtf) {
|
|
6
|
+
rtf = new Intl.RelativeTimeFormat(locale, { numeric });
|
|
7
|
+
rtfCache.set(key, rtf);
|
|
8
|
+
}
|
|
9
|
+
return rtf;
|
|
10
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"from-now.d.ts","sourceRoot":"","sources":["../../src/lib/from-now.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"from-now.d.ts","sourceRoot":"","sources":["../../src/lib/from-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAyC1E"}
|
package/esm/lib/from-now.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getRTF } from "../core/rtf.js";
|
|
1
2
|
import { config } from "./config.js";
|
|
2
3
|
/**
|
|
3
4
|
* Returns a string representing how long ago the date was from now.
|
|
@@ -25,21 +26,26 @@ export function fromNow(date, locale = config.locale) {
|
|
|
25
26
|
const minutes = Math.floor(seconds / 60);
|
|
26
27
|
const hours = Math.floor(minutes / 60);
|
|
27
28
|
const days = Math.floor(hours / 24);
|
|
29
|
+
const weeks = Math.floor(days / 7);
|
|
28
30
|
const months = Math.floor(days / 30);
|
|
29
31
|
const years = Math.floor(days / 365);
|
|
30
|
-
|
|
32
|
+
// Use auto for "now", always for everything else
|
|
31
33
|
if (seconds < 60) {
|
|
32
|
-
return
|
|
34
|
+
return getRTF(locale, "auto").format(0, "second");
|
|
33
35
|
}
|
|
36
|
+
const rtf = getRTF(locale, "always");
|
|
34
37
|
if (minutes < 60) {
|
|
35
38
|
return rtf.format(diff < 0 ? minutes : -minutes, "minute");
|
|
36
39
|
}
|
|
37
40
|
if (hours < 24) {
|
|
38
41
|
return rtf.format(diff < 0 ? hours : -hours, "hour");
|
|
39
42
|
}
|
|
40
|
-
if (days <
|
|
43
|
+
if (days < 7) {
|
|
41
44
|
return rtf.format(diff < 0 ? days : -days, "day");
|
|
42
45
|
}
|
|
46
|
+
if (days < 30) {
|
|
47
|
+
return rtf.format(diff < 0 ? weeks : -weeks, "week");
|
|
48
|
+
}
|
|
43
49
|
if (months < 12) {
|
|
44
50
|
return rtf.format(diff < 0 ? months : -months, "month");
|
|
45
51
|
}
|
package/esm/lib/to-now.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAyCxE"}
|
package/esm/lib/to-now.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getRTF } from "../core/rtf.js";
|
|
1
2
|
import { config } from "./config.js";
|
|
2
3
|
/**
|
|
3
4
|
* Returns a string representing the time from now to the date.
|
|
@@ -26,21 +27,26 @@ export function toNow(date, locale = config.locale) {
|
|
|
26
27
|
const minutes = Math.floor(seconds / 60);
|
|
27
28
|
const hours = Math.floor(minutes / 60);
|
|
28
29
|
const days = Math.floor(hours / 24);
|
|
30
|
+
const weeks = Math.floor(days / 7);
|
|
29
31
|
const months = Math.floor(days / 30);
|
|
30
32
|
const years = Math.floor(days / 365);
|
|
31
|
-
|
|
33
|
+
// Use auto for "now", always for everything else
|
|
32
34
|
if (seconds < 60) {
|
|
33
|
-
return
|
|
35
|
+
return getRTF(locale, "auto").format(0, "second");
|
|
34
36
|
}
|
|
37
|
+
const rtf = getRTF(locale, "always");
|
|
35
38
|
if (minutes < 60) {
|
|
36
39
|
return rtf.format(diff > 0 ? minutes : -minutes, "minute");
|
|
37
40
|
}
|
|
38
41
|
if (hours < 24) {
|
|
39
42
|
return rtf.format(diff > 0 ? hours : -hours, "hour");
|
|
40
43
|
}
|
|
41
|
-
if (days <
|
|
44
|
+
if (days < 7) {
|
|
42
45
|
return rtf.format(diff > 0 ? days : -days, "day");
|
|
43
46
|
}
|
|
47
|
+
if (days < 30) {
|
|
48
|
+
return rtf.format(diff > 0 ? weeks : -weeks, "week");
|
|
49
|
+
}
|
|
44
50
|
if (months < 12) {
|
|
45
51
|
return rtf.format(diff > 0 ? months : -months, "month");
|
|
46
52
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rtf.d.ts","sourceRoot":"","sources":["../../src/core/rtf.ts"],"names":[],"mappings":"AAEA,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,2BAQhE"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRTF = getRTF;
|
|
4
|
+
const rtfCache = new Map();
|
|
5
|
+
function getRTF(locale, numeric) {
|
|
6
|
+
const key = `${locale}|${numeric}`;
|
|
7
|
+
let rtf = rtfCache.get(key);
|
|
8
|
+
if (!rtf) {
|
|
9
|
+
rtf = new Intl.RelativeTimeFormat(locale, { numeric });
|
|
10
|
+
rtfCache.set(key, rtf);
|
|
11
|
+
}
|
|
12
|
+
return rtf;
|
|
13
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"from-now.d.ts","sourceRoot":"","sources":["../../src/lib/from-now.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"from-now.d.ts","sourceRoot":"","sources":["../../src/lib/from-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAyC1E"}
|
package/script/lib/from-now.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fromNow = fromNow;
|
|
4
|
+
const rtf_js_1 = require("../core/rtf.js");
|
|
4
5
|
const config_js_1 = require("./config.js");
|
|
5
6
|
/**
|
|
6
7
|
* Returns a string representing how long ago the date was from now.
|
|
@@ -28,21 +29,26 @@ function fromNow(date, locale = config_js_1.config.locale) {
|
|
|
28
29
|
const minutes = Math.floor(seconds / 60);
|
|
29
30
|
const hours = Math.floor(minutes / 60);
|
|
30
31
|
const days = Math.floor(hours / 24);
|
|
32
|
+
const weeks = Math.floor(days / 7);
|
|
31
33
|
const months = Math.floor(days / 30);
|
|
32
34
|
const years = Math.floor(days / 365);
|
|
33
|
-
|
|
35
|
+
// Use auto for "now", always for everything else
|
|
34
36
|
if (seconds < 60) {
|
|
35
|
-
return
|
|
37
|
+
return (0, rtf_js_1.getRTF)(locale, "auto").format(0, "second");
|
|
36
38
|
}
|
|
39
|
+
const rtf = (0, rtf_js_1.getRTF)(locale, "always");
|
|
37
40
|
if (minutes < 60) {
|
|
38
41
|
return rtf.format(diff < 0 ? minutes : -minutes, "minute");
|
|
39
42
|
}
|
|
40
43
|
if (hours < 24) {
|
|
41
44
|
return rtf.format(diff < 0 ? hours : -hours, "hour");
|
|
42
45
|
}
|
|
43
|
-
if (days <
|
|
46
|
+
if (days < 7) {
|
|
44
47
|
return rtf.format(diff < 0 ? days : -days, "day");
|
|
45
48
|
}
|
|
49
|
+
if (days < 30) {
|
|
50
|
+
return rtf.format(diff < 0 ? weeks : -weeks, "week");
|
|
51
|
+
}
|
|
46
52
|
if (months < 12) {
|
|
47
53
|
return rtf.format(diff < 0 ? months : -months, "month");
|
|
48
54
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"to-now.d.ts","sourceRoot":"","sources":["../../src/lib/to-now.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,MAAsB,GAAG,MAAM,CAyCxE"}
|
package/script/lib/to-now.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toNow = toNow;
|
|
4
|
+
const rtf_js_1 = require("../core/rtf.js");
|
|
4
5
|
const config_js_1 = require("./config.js");
|
|
5
6
|
/**
|
|
6
7
|
* Returns a string representing the time from now to the date.
|
|
@@ -29,21 +30,26 @@ function toNow(date, locale = config_js_1.config.locale) {
|
|
|
29
30
|
const minutes = Math.floor(seconds / 60);
|
|
30
31
|
const hours = Math.floor(minutes / 60);
|
|
31
32
|
const days = Math.floor(hours / 24);
|
|
33
|
+
const weeks = Math.floor(days / 7);
|
|
32
34
|
const months = Math.floor(days / 30);
|
|
33
35
|
const years = Math.floor(days / 365);
|
|
34
|
-
|
|
36
|
+
// Use auto for "now", always for everything else
|
|
35
37
|
if (seconds < 60) {
|
|
36
|
-
return
|
|
38
|
+
return (0, rtf_js_1.getRTF)(locale, "auto").format(0, "second");
|
|
37
39
|
}
|
|
40
|
+
const rtf = (0, rtf_js_1.getRTF)(locale, "always");
|
|
38
41
|
if (minutes < 60) {
|
|
39
42
|
return rtf.format(diff > 0 ? minutes : -minutes, "minute");
|
|
40
43
|
}
|
|
41
44
|
if (hours < 24) {
|
|
42
45
|
return rtf.format(diff > 0 ? hours : -hours, "hour");
|
|
43
46
|
}
|
|
44
|
-
if (days <
|
|
47
|
+
if (days < 7) {
|
|
45
48
|
return rtf.format(diff > 0 ? days : -days, "day");
|
|
46
49
|
}
|
|
50
|
+
if (days < 30) {
|
|
51
|
+
return rtf.format(diff > 0 ? weeks : -weeks, "week");
|
|
52
|
+
}
|
|
47
53
|
if (months < 12) {
|
|
48
54
|
return rtf.format(diff > 0 ? months : -months, "month");
|
|
49
55
|
}
|