|
|
@ -219,17 +219,8 @@ class RelativeTimeFormatter {
|
|
|
|
* Default locale and options for Relative Time Formatter
|
|
|
|
* Default locale and options for Relative Time Formatter
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
constructor() {
|
|
|
|
this.locale = currentLocale();
|
|
|
|
|
|
|
|
this.options = { numeric: "auto" };
|
|
|
|
this.options = { numeric: "auto" };
|
|
|
|
this.rtf = new Intl.RelativeTimeFormat(this.locale, this.options);
|
|
|
|
this.instance = new Intl.RelativeTimeFormat(currentLocale(), this.options);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Method to get the singleton instance
|
|
|
|
|
|
|
|
* @returns {object} Intl.RelativeTimeFormat instance
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
getInstance() {
|
|
|
|
|
|
|
|
return this.rtf;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -238,8 +229,7 @@ class RelativeTimeFormatter {
|
|
|
|
* @returns {void} No return value.
|
|
|
|
* @returns {void} No return value.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
updateLocale(locale) {
|
|
|
|
updateLocale(locale) {
|
|
|
|
this.locale = locale;
|
|
|
|
this.instance = new Intl.RelativeTimeFormat(locale, this.options);
|
|
|
|
this.rtf = new Intl.RelativeTimeFormat(this.locale, this.options);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -255,24 +245,24 @@ class RelativeTimeFormatter {
|
|
|
|
const parts = [];
|
|
|
|
const parts = [];
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Build the formatted string from parts
|
|
|
|
* Build the formatted string from parts
|
|
|
|
|
|
|
|
* 1. Get the relative time formatted parts from the instance.
|
|
|
|
|
|
|
|
* 2. Filter out the relevant parts literal (unit of time) or integer (value).
|
|
|
|
|
|
|
|
* 3. Map out the required values.
|
|
|
|
* @param {number} value Receives value in seconds.
|
|
|
|
* @param {number} value Receives value in seconds.
|
|
|
|
* @param {string} unitOfTime Expected unit of time after conversion.
|
|
|
|
* @param {string} unitOfTime Expected unit of time after conversion.
|
|
|
|
* @returns {void}
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
const toFormattedPart = (value, unitOfTime) => {
|
|
|
|
const toFormattedPart = (value, unitOfTime) => {
|
|
|
|
const res = this.getInstance().formatToParts(value, unitOfTime);
|
|
|
|
const partsArray = this.instance.formatToParts(value, unitOfTime);
|
|
|
|
let formattedString = res
|
|
|
|
const filteredParts = partsArray
|
|
|
|
.map((part, _idx) => {
|
|
|
|
.filter(
|
|
|
|
if (
|
|
|
|
(part, index) =>
|
|
|
|
(part.type === "literal" || part.type === "integer") &&
|
|
|
|
(part.type === "literal" || part.type === "integer") &&
|
|
|
|
_idx > 0
|
|
|
|
index > 0
|
|
|
|
) {
|
|
|
|
)
|
|
|
|
return part.value;
|
|
|
|
.map((part) => part.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
const formattedString = filteredParts.join("").trim();
|
|
|
|
})
|
|
|
|
|
|
|
|
.join("");
|
|
|
|
|
|
|
|
formattedString = formattedString.trim();
|
|
|
|
|
|
|
|
parts.push(formattedString);
|
|
|
|
parts.push(formattedString);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -289,14 +279,12 @@ class RelativeTimeFormatter {
|
|
|
|
toFormattedPart(secs, "second");
|
|
|
|
toFormattedPart(secs, "second");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const result =
|
|
|
|
if (parts.length > 0) {
|
|
|
|
parts.length > 0
|
|
|
|
return `${parts.join(" ")}`;
|
|
|
|
? `${parts.join(" ")}`
|
|
|
|
}
|
|
|
|
: this.getInstance().format(0, "second"); // Handle case for 0 seconds
|
|
|
|
return this.instance.format(0, "second"); // Handle case for 0 seconds
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const rtf = new RelativeTimeFormatter();
|
|
|
|
export const relativeTimeFormatter = new RelativeTimeFormatter();
|
|
|
|
|
|
|
|
|
|
|
|