From 33522fcdc6caa27f601200f8dfd27e85e21fdfb5 Mon Sep 17 00:00:00 2001 From: Stefano Del Prete Date: Sat, 18 Jan 2025 01:59:40 +0100 Subject: [PATCH] fix: conditional modifiers fix (#667) * should be signed * remove boolean modifiers that cna be replaced by istrue/isfalse * added exists check on string, removed isfalse check on boolean since it the same as istrue but inverting the true/false strings * add ::exists conditional modifier to date since {file.deletesAt} can be null * Fix parser regex before this fix, here `{file.createdAt::locale::it-IT,Europe/Rome}{file.id}` it would matcg `it-IT,Europe/Rome}{file.id` as locale timezone here `{file.name::exists["yes"||"no"]}{file.originalName::exists["yes"||"no"]}` it would match `yes"||"no"]}{file.originalName::exists["yes"||"no` as `mod_check_true` group * Forgot `\` before first `{` in the regex * add conditional mods to originalName too, fix regex not matching when true/false string had "\n", fix "::exists" matching on string when they were "null" --------- Co-authored-by: dicedtomato <35403473+diced@users.noreply.github.com> --- src/lib/parser/index.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/lib/parser/index.ts b/src/lib/parser/index.ts index a185d90c..39e5f0b9 100755 --- a/src/lib/parser/index.ts +++ b/src/lib/parser/index.ts @@ -25,7 +25,6 @@ export type ParseValue = { export function parseString(str: string, value: ParseValue) { if (!str) return null; - str = str.replace(/\\n/g, '\n'); const replacer = (key: string, value: unknown) => { if (key === 'password' || key === 'avatar') return '***'; @@ -76,7 +75,14 @@ export function parseString(str: string, value: ParseValue) { const decoded = decodeURIComponent(escape(getV[matches.groups.prop as keyof ParseValue['file']])); str = replaceCharsFromString( str, - modifier(matches.groups.mod || 'string', decoded), + modifier( + matches.groups.mod || 'string', + decoded, + matches.groups.mod_tzlocale ?? undefined, + matches.groups.mod_check_true ?? undefined, + matches.groups.mod_check_false ?? undefined, + value, + ), index, re.lastIndex, ); @@ -114,7 +120,7 @@ export function parseString(str: string, value: ParseValue) { re.lastIndex = index; } - return str; + return str.replace(/\\n/g, '\n'); } function modifier( @@ -224,12 +230,12 @@ function modifier( return `{unknown_str_modifier(${mod})}`; if (_value) { - return value + return value != 'null' && value ? parseString(check_true, _value) || check_true : parseString(check_false, _value) || check_false; } - return value ? check_true : check_false; + return value != 'null' && value ? check_true : check_false; } case mod.startsWith('='): { if (typeof check_true !== 'string' || typeof check_false !== 'string') @@ -416,17 +422,13 @@ function modifier( if ( typeof check_false == 'string' && - ['>', '>=', '=', '<=', '<', '~', '$', '^'].some((modif) => mod.startsWith(modif)) + (['>', '>=', '=', '<=', '<', '~', '$', '^'].some((modif) => mod.startsWith(modif)) || + ['istrue', 'exists'].includes(mod)) ) { if (_value) return parseString(check_false, _value) || check_false; return check_false; } - if (typeof check_false == 'string' && ['istrue', 'isfalse', 'exists'].includes(mod)) { - if (_value) return parseString(check_false, _value) || check_false; - return check_false; - } - return `{unknown_modifier(${mod})}`; }