From e83408fb3cb2a7734d618d0cb55962ec85909770 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Fri, 28 Jul 2023 06:50:15 +0100 Subject: [PATCH] Add condition input --- ...arr_Plugin_00td_filter_by_file_property.js | 29 +++++++--- methods/utils.js | 57 +++++++++++++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/Community/Tdarr_Plugin_00td_filter_by_file_property.js b/Community/Tdarr_Plugin_00td_filter_by_file_property.js index 35d5216..b0a57db 100644 --- a/Community/Tdarr_Plugin_00td_filter_by_file_property.js +++ b/Community/Tdarr_Plugin_00td_filter_by_file_property.js @@ -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`; diff --git a/methods/utils.js b/methods/utils.js index 11dfdc1..17f8d89 100644 --- a/methods/utils.js +++ b/methods/utils.js @@ -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, };