Add condition input

make-only-subtitle-default
HaveAGitGat 2 years ago
parent 3266446c46
commit e83408fb3c

@ -32,19 +32,24 @@ const details = () => ({
'Enter a comma separated list of values to check for.', 'Enter a comma separated list of values to check for.',
}, },
{ {
name: 'exactMatch', name: 'condition',
type: 'boolean', type: 'string',
defaultValue: true, defaultValue: '==',
inputUI: { inputUI: {
type: 'dropdown', type: 'dropdown',
options: [ options: [
'false', '==',
'true', '!=',
'>',
'>=',
'<',
'<=',
'includes',
'not includes',
], ],
}, },
tooltip: tooltip:
'Specify true if the property value must be an exact match,' 'Specify the condition to use when comparing the property value to the input value.',
+ ' false if the property value must contain the value.',
}, },
{ {
name: 'continueIfPropertyFound', name: 'continueIfPropertyFound',
@ -65,7 +70,7 @@ const details = () => ({
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const plugin = (file, librarySettings, inputs, otherArguments) => { const plugin = (file, librarySettings, inputs, otherArguments) => {
const { strHasValue } = require('../methods/utils'); const { conditionMet } = require('../methods/utils');
const lib = require('../methods/lib')(); const lib = require('../methods/lib')();
// eslint-disable-next-line no-unused-vars,no-param-reassign // eslint-disable-next-line no-unused-vars,no-param-reassign
inputs = lib.loadDefaultValues(inputs, details); inputs = lib.loadDefaultValues(inputs, details);
@ -86,10 +91,16 @@ const plugin = (file, librarySettings, inputs, otherArguments) => {
return response; return response;
} }
// legacy
if (inputs.exactMatch === false && inputs.condition === '==') {
// eslint-disable-next-line no-param-reassign
inputs.condition = 'includes';
}
const propertyValues = inputs.propertyValues.trim().split(','); const propertyValues = inputs.propertyValues.trim().split(',');
try { try {
const fileContainsProperty = strHasValue(propertyValues, file[propertyName], inputs.exactMatch); const fileContainsProperty = conditionMet(propertyValues, file[propertyName], inputs.condition);
const message = `File property ${propertyName} of ${file[propertyName]}` const message = `File property ${propertyName} of ${file[propertyName]}`
+ ` being one of ${propertyValues.join(',')} has`; + ` being one of ${propertyValues.join(',')} has`;

@ -18,6 +18,63 @@ const strHasValue = (inputsArr, value, exactMatch) => {
return contains; return contains;
}; };
const conditionMet = (inputsArr, value, condition) => {
for (let j = 0; j < inputsArr.length; j += 1) {
try {
switch (condition) {
case '==':
if (inputsArr[j] === String(value)) {
return true;
}
break;
case '!=':
if (inputsArr[j] !== String(value)) {
return true;
}
break;
case '>':
if (inputsArr[j] > Number(value)) {
return true;
}
break;
case '>=':
if (inputsArr[j] >= Number(value)) {
return true;
}
break;
case '<':
if (inputsArr[j] < Number(value)) {
return true;
}
break;
case '<=':
if (inputsArr[j] <= Number(value)) {
return true;
}
break;
case 'includes':
if (String(value).includes(inputsArr[j])) {
return true;
}
break;
case 'not includes':
if (!String(value).includes(inputsArr[j])) {
return true;
}
break;
default:
}
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
}
}
return false;
};
module.exports = { module.exports = {
strHasValue, strHasValue,
conditionMet,
}; };

Loading…
Cancel
Save