mirror of
https://github.com/diced/zipline.git
synced 2025-04-11 23:31:17 -05:00
feat: exists conditional modifier to date, fix parser regex (#666)
* 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
This commit is contained in:
parent
f68d670600
commit
7ea8207c30
1 changed files with 30 additions and 18 deletions
|
@ -50,7 +50,7 @@ export function parseString(str: string, value: ParseValue) {
|
|||
};
|
||||
|
||||
const re =
|
||||
/\{(?<type>file|url|user|debug|link|metricsUser|metricsZipline)\.(?<prop>\w+)(::(?<mod>(\w+|<|<=|=|>=|>|\^|\$|~|\/)+))?((::(?<mod_tzlocale>\S+))|(?<mod_check>\[(?<mod_check_true>".*")\|\|(?<mod_check_false>".*")\]))?\}/gi;
|
||||
/\{(?<type>file|url|user|debug|link|metricsUser|metricsZipline)\.(?<prop>\w+)(::(?<mod>(\w+|<|<=|=|>=|>|\^|\$|~|\/)+))?((::(?<mod_tzlocale>\S+?))|(?<mod_check>\[(?<mod_check_true>".*?")\|\|(?<mod_check_false>".*?")\]))?\}/gi;
|
||||
let matches: RegExpMatchArray | null;
|
||||
|
||||
while ((matches = re.exec(str))) {
|
||||
|
@ -155,37 +155,49 @@ function modifier(
|
|||
}
|
||||
}
|
||||
|
||||
switch (mod) {
|
||||
case 'locale':
|
||||
switch (true) {
|
||||
case mod == 'locale':
|
||||
return value.toLocaleString(...args);
|
||||
case 'time':
|
||||
case mod == 'time':
|
||||
return value.toLocaleTimeString(...args);
|
||||
case 'date':
|
||||
case mod == 'date':
|
||||
return value.toLocaleDateString(...args);
|
||||
case 'unix':
|
||||
case mod == 'unix':
|
||||
return Math.floor(value.getTime() / 1000).toString();
|
||||
case 'iso':
|
||||
case mod == 'iso':
|
||||
return value.toISOString();
|
||||
case 'utc':
|
||||
case mod == 'utc':
|
||||
return value.toUTCString();
|
||||
case 'year':
|
||||
case mod == 'year':
|
||||
return value.getFullYear().toString();
|
||||
case 'month':
|
||||
case mod == 'month':
|
||||
return (value.getMonth() + 1).toString();
|
||||
case 'day':
|
||||
case mod == 'day':
|
||||
return value.getDate().toString();
|
||||
case 'hour':
|
||||
case mod == 'hour':
|
||||
return value.getHours().toString();
|
||||
case 'minute':
|
||||
case mod == 'minute':
|
||||
return value.getMinutes().toString();
|
||||
case 'second':
|
||||
case mod == 'second':
|
||||
return value.getSeconds().toString();
|
||||
case 'string':
|
||||
case mod == 'string':
|
||||
return value.toString();
|
||||
case 'ampm':
|
||||
case mod == 'ampm':
|
||||
return value.getHours() < 12 ? 'am' : 'pm';
|
||||
case 'AMPM':
|
||||
case mod == 'AMPM':
|
||||
return value.getHours() < 12 ? 'AM' : 'PM';
|
||||
case mod == 'exists': {
|
||||
if (typeof check_true !== 'string' || typeof check_false !== 'string')
|
||||
return `{unknown_date_modifier(${mod})}`;
|
||||
|
||||
if (_value) {
|
||||
return value
|
||||
? parseString(check_true, _value) || check_true
|
||||
: parseString(check_false, _value) || check_false;
|
||||
}
|
||||
|
||||
return value ? check_true : check_false;
|
||||
}
|
||||
default:
|
||||
return `{unknown_date_modifier(${mod})}`;
|
||||
}
|
||||
|
@ -207,7 +219,7 @@ function modifier(
|
|||
return toHex(value);
|
||||
case mod == 'string':
|
||||
return value;
|
||||
case mod.startsWith('exists'): {
|
||||
case mod == 'exists': {
|
||||
if (typeof check_true !== 'string' || typeof check_false !== 'string')
|
||||
return `{unknown_str_modifier(${mod})}`;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue