build: switch from yarn to npm to manage js dependencies and move js contents to root
yarn v1 is being deprecated and starts to have some issues Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
79
src/filters/datetime.ts
Normal file
79
src/filters/datetime.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { i18n } from "../utils/i18n";
|
||||
|
||||
function parseDateTime(value: string): Date {
|
||||
return new Date(value);
|
||||
}
|
||||
|
||||
function formatDateString(value: string): string {
|
||||
return parseDateTime(value).toLocaleString(locale(), {
|
||||
weekday: "long",
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
});
|
||||
}
|
||||
|
||||
function formatTimeString(value: string, timeZone?: string): string {
|
||||
return parseDateTime(value).toLocaleTimeString(locale(), {
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
timeZone,
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: These can be removed in favor of dateStyle/timeStyle when those two have sufficient support
|
||||
// https://caniuse.com/mdn-javascript_builtins_intl_datetimeformat_datetimeformat_datestyle
|
||||
const LONG_DATE_FORMAT_OPTIONS: any = {
|
||||
weekday: undefined,
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
hour: undefined,
|
||||
minute: undefined,
|
||||
};
|
||||
|
||||
const LONG_TIME_FORMAT_OPTIONS: any = {
|
||||
weekday: "long",
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
};
|
||||
|
||||
const SHORT_DATE_FORMAT_OPTIONS: any = {
|
||||
weekday: undefined,
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
hour: undefined,
|
||||
minute: undefined,
|
||||
};
|
||||
|
||||
const SHORT_TIME_FORMAT_OPTIONS: any = {
|
||||
weekday: "short",
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
};
|
||||
|
||||
function formatDateTimeString(
|
||||
value: string,
|
||||
timeZone: string | null | undefined = undefined,
|
||||
showTime = true,
|
||||
dateFormat = "long"
|
||||
): string {
|
||||
const isLongFormat = dateFormat === "long";
|
||||
let options = isLongFormat
|
||||
? LONG_DATE_FORMAT_OPTIONS
|
||||
: SHORT_DATE_FORMAT_OPTIONS;
|
||||
if (showTime) {
|
||||
options = {
|
||||
...options,
|
||||
...(isLongFormat ? LONG_TIME_FORMAT_OPTIONS : SHORT_TIME_FORMAT_OPTIONS),
|
||||
timeZone: timeZone || undefined,
|
||||
};
|
||||
}
|
||||
const format = new Intl.DateTimeFormat(locale(), options);
|
||||
return format.format(parseDateTime(value));
|
||||
}
|
||||
|
||||
const locale = () => i18n.global.locale.replace("_", "-");
|
||||
|
||||
export { formatDateString, formatTimeString, formatDateTimeString };
|
||||
16
src/filters/index.ts
Normal file
16
src/filters/index.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import nl2br from "@/filters/utils";
|
||||
import {
|
||||
formatDateString,
|
||||
formatTimeString,
|
||||
formatDateTimeString,
|
||||
} from "./datetime";
|
||||
|
||||
export default {
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
install(vue: any): void {
|
||||
vue.filter("formatDateString", formatDateString);
|
||||
vue.filter("formatTimeString", formatTimeString);
|
||||
vue.filter("formatDateTimeString", formatDateTimeString);
|
||||
vue.filter("nl2br", nl2br);
|
||||
},
|
||||
};
|
||||
9
src/filters/utils.ts
Normal file
9
src/filters/utils.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* New Line to <br>
|
||||
*
|
||||
* @param {string} str Input text
|
||||
* @return {string} Filtered text
|
||||
*/
|
||||
export default function nl2br(str: string): string {
|
||||
return `${str}`.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, "$1<br>");
|
||||
}
|
||||
Reference in New Issue
Block a user