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.',
},
{
name: 'exactMatch',
type: 'boolean',
defaultValue: true,
name: 'condition',
type: 'string',
defaultValue: '==',
inputUI: {
type: 'dropdown',
options: [
'false',
'true',
'==',
'!=',
'>',
'>=',
'<',
'<=',
'includes',
'not includes',
],
},
tooltip:
'Specify true if the property value must be an exact match,'
+ ' false if the property value must contain the value.',
'Specify the condition to use when comparing the property value to the input value.',
},
{
name: 'continueIfPropertyFound',
@ -65,7 +70,7 @@ const details = () => ({
// eslint-disable-next-line no-unused-vars
const plugin = (file, librarySettings, inputs, otherArguments) => {
const { strHasValue } = require('../methods/utils');
const { conditionMet } = require('../methods/utils');
const lib = require('../methods/lib')();
// eslint-disable-next-line no-unused-vars,no-param-reassign
inputs = lib.loadDefaultValues(inputs, details);
@ -86,10 +91,16 @@ const plugin = (file, librarySettings, inputs, otherArguments) => {
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(',');
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]}`
+ ` being one of ${propertyValues.join(',')} has`;

@ -18,6 +18,63 @@ const strHasValue = (inputsArr, value, exactMatch) => {
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 = {
strHasValue,
conditionMet,
};

Loading…
Cancel
Save