export const qpabbr = (value: string|null, max: number|undefined=undefined) => {
if (value) {
let v = value.split(" ").map((k) => k[0]);
let r = v.slice(0,max).join("").toUpperCase();
return r;
} else return "";
}
export const qpcode = (text: string) => {
let r = text
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/\n/g, "
")
.replace(/\[g\]/g, '')
.replace(/\[\/g\]/g, "")
.replace(/\[i\]/g, '')
.replace(/\[\/i\]/g, "")
.replace(/\[s\]/g, '')
.replace(/\[\/s\]/g, "")
.replace(/\[b\]/g, '')
.replace(/\[\/b\]/g, "")
.replace(/\[ag\]/g, '
')
.replace(/\[\/ag\]/g, "
")
.replace(/\[ac\]/g, '')
.replace(/\[\/ac\]/g, "
")
.replace(/\[ad\]/g, '')
.replace(/\[\/ad\]/g, "
")
.replace(/\[c=#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\]/g, '')
.replace(/\[c=([a-zA-Z0-9_]+)\]/g, '')
.replace(/\[\/c\]/g, "")
.replace(/\[url=([^<>[\]]+)\]/g, '')
.replace(/\[\/url\]/g, "")
.replace(/\[urlo=([^<>[\]]+)\]/g, '')
.replace(/\[\/urlo\]/g, "")
.replace(/\[img=([^<>[\]]+)\]/g, '')
.replace(/\[ico=([a-z- ]+)\]/g, '');
return r;
}
export const qpdate = (value: string|null=null, lang: string="fr", tz: string="America/Toronto", show: string="full") => {
let d = !!value ? new Date(value) : new Date(new Date().toString());
let o: Intl.DateTimeFormatOptions = {
timeZone: tz,
hour12: false,
year: (["full","date"].includes(show) ? "numeric" : undefined),
month: (["full","date"].includes(show) ? "long" : undefined),
day: (["full","date"].includes(show) ? "numeric" : undefined),
hour: (["full","time"].includes(show) ? "2-digit" : undefined),
minute: (["full","time"].includes(show) ? "2-digit" : undefined)
};
return d.toLocaleString(lang, o);
}
export const qprhex = () => {
return `#${Math.floor((Math.random() * 0xffffff)).toString(16)}`;
}
export const qpslug = (value: string|number) => {
return value.toString()
.normalize("NFD")
.toLowerCase()
.trim()
.replace(/\s+/g, "-")
.replace(/ð/g, "d")
.replace(/ß/g, "ss")
.replace(/§/g, "ss")
.replace(/ł/g, "l")
.replace(/ø/g, "o")
.replace(/æ/g, "ae")
.replace(/œ/g, "oe")
.replace(/ĸ/g, "k")
.replace(/µ/g, "u")
.replace(/[^\w\-]+/g, "")
.replace(/\_/g, "-")
.replace(/\-\-+/g, "-")
.replace(/\-$/g, "");
}
export const qpunit = (num: number, digits: number = 2) => {
const l = [
{ v: 1, u: "" },
{ v: 1e3, u: "k" },
{ v: 1e6, u: "M" },
{ v: 1e9, u: "G" },
{ v: 1e12, u: "T" },
{ v: 1e15, u: "P" },
{ v: 1e18, u: "E" }
];
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
var i = l.slice().reverse().find(function (i) {
return num >= i.v;
});
return i ? (num / i.v).toFixed(digits).replace(rx, "$1") + i.u : "0";
}
const qpfilters = {
qpabbr,
qpcode,
qpdate,
qprhex,
qpslug,
qpunit
};
export default qpfilters;