Support negated pattern for `*[pP]ropsToMatch` values in scriptlets

This commit is contained in:
Raymond Hill 2023-08-08 12:20:03 -04:00
parent d6ab05531c
commit 4f082a96bc
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 12 additions and 5 deletions

View File

@ -66,13 +66,14 @@ function safeSelf() {
if ( pattern === '' ) { if ( pattern === '' ) {
return { matchAll: true }; return { matchAll: true };
} }
const expect = (options.canNegate && pattern.startsWith('!') === false); const expect = (options.canNegate === true && pattern.startsWith('!') === false);
if ( expect === false ) { if ( expect === false ) {
pattern = pattern.slice(1); pattern = pattern.slice(1);
} }
const match = /^\/(.+)\/([gimsu]*)$/.exec(pattern); const match = /^\/(.+)\/([gimsu]*)$/.exec(pattern);
if ( match !== null ) { if ( match !== null ) {
return { return {
pattern,
re: new this.RegExp( re: new this.RegExp(
match[1], match[1],
match[2] || options.flags match[2] || options.flags
@ -81,6 +82,7 @@ function safeSelf() {
}; };
} }
return { return {
pattern,
re: new this.RegExp(pattern.replace( re: new this.RegExp(pattern.replace(
/[.*+?^${}()|[\]\\]/g, '\\$&'), /[.*+?^${}()|[\]\\]/g, '\\$&'),
options.flags options.flags
@ -882,13 +884,14 @@ function parsePropertiesToMatch(propsToMatch, implicit = '') {
const safe = safeSelf(); const safe = safeSelf();
const needles = new Map(); const needles = new Map();
if ( propsToMatch === undefined || propsToMatch === '' ) { return needles; } if ( propsToMatch === undefined || propsToMatch === '' ) { return needles; }
const options = { canNegate: true };
for ( const needle of propsToMatch.split(/\s+/) ) { for ( const needle of propsToMatch.split(/\s+/) ) {
const [ prop, pattern ] = needle.split(':'); const [ prop, pattern ] = needle.split(':');
if ( prop === '' ) { continue; } if ( prop === '' ) { continue; }
if ( pattern !== undefined ) { if ( pattern !== undefined ) {
needles.set(prop, { pattern, re: safe.patternToRegex(pattern) }); needles.set(prop, safe.initPattern(pattern, options));
} else if ( implicit !== '' ) { } else if ( implicit !== '' ) {
needles.set(implicit, { pattern: prop, re: safe.patternToRegex(prop) }); needles.set(implicit, safe.initPattern(prop, options));
} }
} }
return needles; return needles;
@ -899,6 +902,9 @@ function parsePropertiesToMatch(propsToMatch, implicit = '') {
builtinScriptlets.push({ builtinScriptlets.push({
name: 'match-object-properties.fn', name: 'match-object-properties.fn',
fn: matchObjectProperties, fn: matchObjectProperties,
dependencies: [
'safe-self.fn',
],
}); });
function matchObjectProperties(propNeedles, ...objs) { function matchObjectProperties(propNeedles, ...objs) {
if ( matchObjectProperties.extractProperties === undefined ) { if ( matchObjectProperties.extractProperties === undefined ) {
@ -910,6 +916,7 @@ function matchObjectProperties(propNeedles, ...objs) {
} }
}; };
} }
const safe = safeSelf();
const haystack = {}; const haystack = {};
const props = Array.from(propNeedles.keys()); const props = Array.from(propNeedles.keys());
for ( const obj of objs ) { for ( const obj of objs ) {
@ -922,9 +929,9 @@ function matchObjectProperties(propNeedles, ...objs) {
if ( typeof value !== 'string' ) { if ( typeof value !== 'string' ) {
try { value = JSON.stringify(value); } try { value = JSON.stringify(value); }
catch(ex) { } catch(ex) { }
}
if ( typeof value !== 'string' ) { continue; } if ( typeof value !== 'string' ) { continue; }
if ( details.re.test(value) ) { continue; } }
if ( safe.testPattern(details, value) ) { continue; }
return false; return false;
} }
return true; return true;