fix: escape . in regexes

This commit is contained in:
diced 2022-09-28 20:45:50 -07:00
parent b460da74dd
commit 78a6f3122d
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1
3 changed files with 29 additions and 28 deletions

View file

@ -1,4 +1,5 @@
{
"editor.tabSize": 2,
"files.eol": "\n"
"files.eol": "\n",
"typescript.tsdk": "node_modules/typescript/lib"
}

View file

@ -5,15 +5,15 @@ export function parse(str: string, image: Image, user: User) {
if (!str) return null;
return str
.replace(/{user.admin}/gi, user.administrator ? 'yes' : 'no')
.replace(/{user.id}/gi, user.id.toString())
.replace(/{user.name}/gi, user.username)
.replace(/{image.id}/gi, image.id.toString())
.replace(/{image.mime}/gi, image.mimetype)
.replace(/{image.file}/gi, image.file)
.replace(/{image.created_at.full_string}/gi, image.created_at.toLocaleString())
.replace(/{image.created_at.time_string}/gi, image.created_at.toLocaleTimeString())
.replace(/{image.created_at.date_string}/gi, image.created_at.toLocaleDateString());
.replace(/{user\.admin}/gi, user.administrator ? 'yes' : 'no')
.replace(/{user\.id}/gi, user.id.toString())
.replace(/{user\.name}/gi, user.username)
.replace(/{image\.id}/gi, image.id.toString())
.replace(/{image\.mime}/gi, image.mimetype)
.replace(/{image\.file}/gi, image.file)
.replace(/{image\.created_at.full_string}/gi, image.created_at.toLocaleString())
.replace(/{image\.created_at.time_string}/gi, image.created_at.toLocaleTimeString())
.replace(/{image\.created_at.date_string}/gi, image.created_at.toLocaleDateString());
}
export function bytesToRead(bytes: number) {
@ -41,7 +41,7 @@ export const units = {
export function relativeTime(to: Date, from: Date = new Date()) {
const time = new Date(to.getTime() - from.getTime());
const rtf = new Intl.RelativeTimeFormat('en', { style: 'long' });
for (const unit in units) {
@ -54,10 +54,10 @@ export function relativeTime(to: Date, from: Date = new Date()) {
export function humanTime(string: StringValue | string): Date {
try {
const mil = ms(string as StringValue);
if (typeof mil !== 'number') return null;
if (typeof mil !== 'number') return null;
if (isNaN(mil)) return null;
if (!mil) return null;
return new Date(Date.now() + mil);
} catch (_) {
return null;

View file

@ -10,26 +10,26 @@ function parse(str: string, args: Args) {
if (!str) return null;
str = str
.replace(/{user.admin}/gi, args[0].administrator ? 'yes' : 'no')
.replace(/{user.id}/gi, args[0].id.toString())
.replace(/{user.name}/gi, args[0].username)
.replace(/{user\.admin}/gi, args[0].administrator ? 'yes' : 'no')
.replace(/{user\.id}/gi, args[0].id.toString())
.replace(/{user\.name}/gi, args[0].username)
.replace(/{link}/gi, args[3]);
if (args[1]) str = str
.replace(/{file.id}/gi, args[1].id.toString())
.replace(/{file.mime}/gi, args[1].mimetype)
.replace(/{file.file}/gi, args[1].file)
.replace(/{file.created_at.full_string}/gi, args[1].created_at.toLocaleString())
.replace(/{file.created_at.time_string}/gi, args[1].created_at.toLocaleTimeString())
.replace(/{file.created_at.date_string}/gi, args[1].created_at.toLocaleDateString());
.replace(/{file\.id}/gi, args[1].id.toString())
.replace(/{file\.mime}/gi, args[1].mimetype)
.replace(/{file\.file}/gi, args[1].file)
.replace(/{file\.created_at.full_string}/gi, args[1].created_at.toLocaleString())
.replace(/{file\.created_at.time_string}/gi, args[1].created_at.toLocaleTimeString())
.replace(/{file\.created_at.date_string}/gi, args[1].created_at.toLocaleDateString());
if (args[2]) str = str
.replace(/{url.id}/gi, args[2].id.toString())
.replace(/{url.vanity}/gi, args[2].vanity ? args[2].vanity : 'none')
.replace(/{url.destination}/gi, args[2].destination)
.replace(/{url.created_at.full_string}/gi, args[2].created_at.toLocaleString())
.replace(/{url.created_at.time_string}/gi, args[2].created_at.toLocaleTimeString())
.replace(/{url.created_at.date_string}/gi, args[2].created_at.toLocaleDateString());
.replace(/{url\.id}/gi, args[2].id.toString())
.replace(/{url\.vanity}/gi, args[2].vanity ? args[2].vanity : 'none')
.replace(/{url\.destination}/gi, args[2].destination)
.replace(/{url\.created_at.full_string}/gi, args[2].created_at.toLocaleString())
.replace(/{url\.created_at.time_string}/gi, args[2].created_at.toLocaleTimeString())
.replace(/{url\.created_at.date_string}/gi, args[2].created_at.toLocaleDateString());
return str;
}