-
Notifications
You must be signed in to change notification settings - Fork 210
feat(flows): add arithmeticFlowVariable plugin #871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HaveAGitGat
merged 17 commits into
HaveAGitGat:master
from
kalbasit:arithmetic-flow-variable
Oct 15, 2025
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f72f2e4
feat(flows): add arithmeticFlowVariable plugin
kalbasit 8a4a4e3
Merge branch 'master' into arithmetic-flow-variable
HaveAGitGat 07f9e35
style: run lint fixes
HaveAGitGat 8e37cbb
Merge branch 'arithmetic-flow-variable' of https://github.com/kalbasi…
HaveAGitGat a0dad59
chore: compile plugin locally
HaveAGitGat 6204f6b
test: add more validation and testing
HaveAGitGat 45cb199
feat: make possible to use on any flow plugin variable on args object
HaveAGitGat f152af5
test: update test data based on updated interface
HaveAGitGat c144d27
test: add 2 more edge case tests
HaveAGitGat 0f28cad
chore: catch case where path does not exist
HaveAGitGat e344e16
style: remove eslint rule ignore
HaveAGitGat 5e4036a
style: use templating
HaveAGitGat 94d2cfa
Merge branch 'master' into arithmetic-flow-variable
HaveAGitGat 04323de
chore: update icon
HaveAGitGat 5e67d30
Merge branch 'arithmetic-flow-variable' of https://github.com/kalbasi…
HaveAGitGat 0a5e405
chore: use variableTemplate for logging
HaveAGitGat a1d9afb
chore: update build
HaveAGitGat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
FlowPlugins/CommunityFlowPlugins/tools/arithmeticFlowVariable/1.0.0/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.plugin = exports.details = void 0; | ||
| var details = function () { return ({ | ||
| name: 'Arithmetic Flow Variable', | ||
| description: 'Apply an arithmetic calculation on a flow variable.', | ||
| style: { | ||
| borderColor: 'green', | ||
| }, | ||
| tags: '', | ||
| isStartPlugin: false, | ||
| pType: '', | ||
| requiresVersion: '2.11.01', | ||
| sidebarPosition: 1, | ||
| icon: '', | ||
| inputs: [ | ||
| { | ||
| label: 'Variable', | ||
| name: 'variable', | ||
| type: 'string', | ||
| defaultValue: '', | ||
| inputUI: { | ||
| type: 'text', | ||
| }, | ||
| tooltip: "Specify the name of an existing flow variable containing a numeric value to perform arithmetic on.\n The variable must exist and contain a valid number.\n\n \\nExample\\n\n {{{args.variables.user.transcodeStage}}}\n ", | ||
| }, | ||
| { | ||
| label: 'Operation', | ||
| name: 'operation', | ||
| type: 'string', | ||
| defaultValue: '', | ||
| inputUI: { | ||
| type: 'dropdown', | ||
| options: ['+', '-', '*', '/'], | ||
| }, | ||
| tooltip: "Select the arithmetic operation to apply to the variable.\n\n + : Add the quantity to the variable\n - : Subtract the quantity from the variable\n * : Multiply the variable by the quantity\n / : Divide the variable by the quantity (quantity cannot be 0)", | ||
| }, | ||
| { | ||
| label: 'Quantity', | ||
| name: 'quantity', | ||
| type: 'string', | ||
| defaultValue: '', | ||
| inputUI: { | ||
| type: 'text', | ||
| }, | ||
| tooltip: "Specify the numeric value to use in the arithmetic operation.\n Must be a valid number. Cannot be 0 when using division.\n\n \\nExample\\n\n 1\n\n \\nExample\\n\n 5.5\n\n \\nExample\\n\n -10\n ", | ||
| }, | ||
| ], | ||
| outputs: [ | ||
| { | ||
| number: 1, | ||
| tooltip: 'Continue to next plugin', | ||
| }, | ||
| ], | ||
| }); }; | ||
| exports.details = details; | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| var plugin = function (args) { | ||
| var lib = require('../../../../../methods/lib')(); | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign | ||
| args.inputs = lib.loadDefaultValues(args.inputs, details); | ||
| // Get original plugin inputs from thisPlugin.inputsDB | ||
| var inputsOriginal = args.thisPlugin.inputsDB; | ||
| // Extract variable path from the original template string | ||
| var variableTemplate = String(inputsOriginal === null || inputsOriginal === void 0 ? void 0 : inputsOriginal.variable).trim(); | ||
| var variableMatch = variableTemplate.match(/\{\{\{(?:args|baseArgs)\.([\w.]+)\}\}\}/); | ||
| if (!variableMatch) { | ||
| throw new Error("Variable template \"".concat(variableTemplate, "\" is invalid. Expected format: {{{args.path.to.variable}}}")); | ||
| } | ||
| var variablePath = variableMatch[1]; // e.g., "variables.user.existingVar" | ||
| var pathParts = variablePath.split('.'); | ||
| var variableName = pathParts[pathParts.length - 1]; // Last part for display purposes | ||
| var variable = String(args.inputs.variable).trim(); | ||
| var operation = String(args.inputs.operation).trim(); | ||
| var quantity = parseFloat(String(args.inputs.quantity).trim()); | ||
| var value = parseFloat(variable); | ||
| // Validate numeric values | ||
| if (Number.isNaN(quantity)) { | ||
| throw new Error("Quantity \"".concat(args.inputs.quantity, "\" is not a valid number")); | ||
| } | ||
| if (Number.isNaN(value)) { | ||
| throw new Error("Variable \"".concat(variableName, "\" is not a valid number")); | ||
| } | ||
| // Check for division by zero | ||
| if (operation === '/' && quantity === 0) { | ||
| throw new Error('Division by zero is not allowed'); | ||
| } | ||
| args.jobLog("Applying the operation ".concat(operation, " ").concat(quantity, " to ").concat(variableName, " of value ").concat(value)); | ||
| // Helper to set value at dynamic path | ||
| var setValueAtPath = function (obj, path, val) { | ||
| var current = obj; | ||
| for (var i = 0; i < path.length - 1; i += 1) { | ||
| // @ts-expect-error dynamic path | ||
| if (current[path[i]] === undefined || current[path[i]] === null) { | ||
| throw new Error("Path \"".concat(path.slice(0, i + 1).join('.'), "\" does not exist in args object")); | ||
| } | ||
| // @ts-expect-error dynamic path | ||
| current = current[path[i]]; | ||
| } | ||
| // @ts-expect-error dynamic path | ||
| current[path[path.length - 1]] = val; // eslint-disable-line no-param-reassign | ||
| }; | ||
| var result; | ||
| switch (operation) { | ||
| case '+': | ||
| result = value + quantity; | ||
| setValueAtPath(args, pathParts, String(result)); | ||
| break; | ||
| case '-': | ||
| result = value - quantity; | ||
| setValueAtPath(args, pathParts, String(result)); | ||
| break; | ||
| case '*': | ||
| result = value * quantity; | ||
| setValueAtPath(args, pathParts, String(result)); | ||
| break; | ||
| case '/': | ||
| result = value / quantity; | ||
| setValueAtPath(args, pathParts, String(result)); | ||
| break; | ||
| default: | ||
| throw new Error("The operation ".concat(operation, " is invalid")); | ||
| } | ||
| return { | ||
| outputFileObj: args.inputFileObj, | ||
| outputNumber: 1, | ||
| variables: args.variables, | ||
| }; | ||
| }; | ||
| exports.plugin = plugin; |
170 changes: 170 additions & 0 deletions
170
FlowPluginsTs/CommunityFlowPlugins/tools/arithmeticFlowVariable/1.0.0/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import { | ||
| IpluginDetails, | ||
| IpluginInputArgs, | ||
| IpluginOutputArgs, | ||
| } from '../../../../FlowHelpers/1.0.0/interfaces/interfaces'; | ||
|
|
||
| const details = (): IpluginDetails => ({ | ||
| name: 'Arithmetic Flow Variable', | ||
| description: 'Apply an arithmetic calculation on a flow variable.', | ||
| style: { | ||
| borderColor: 'green', | ||
| }, | ||
| tags: '', | ||
| isStartPlugin: false, | ||
| pType: '', | ||
| requiresVersion: '2.11.01', | ||
| sidebarPosition: 1, | ||
| icon: '', | ||
| inputs: [ | ||
| { | ||
| label: 'Variable', | ||
| name: 'variable', | ||
| type: 'string', | ||
| defaultValue: '', | ||
| inputUI: { | ||
| type: 'text', | ||
| }, | ||
| tooltip: `Specify the name of an existing flow variable containing a numeric value to perform arithmetic on. | ||
| The variable must exist and contain a valid number. | ||
|
|
||
| \\nExample\\n | ||
| {{{args.variables.user.transcodeStage}}} | ||
| `, | ||
| }, | ||
| { | ||
| label: 'Operation', | ||
| name: 'operation', | ||
| type: 'string', | ||
| defaultValue: '', | ||
| inputUI: { | ||
| type: 'dropdown', | ||
| options: ['+', '-', '*', '/'], | ||
| }, | ||
| tooltip: `Select the arithmetic operation to apply to the variable. | ||
|
|
||
| + : Add the quantity to the variable | ||
| - : Subtract the quantity from the variable | ||
| * : Multiply the variable by the quantity | ||
| / : Divide the variable by the quantity (quantity cannot be 0)`, | ||
| }, | ||
| { | ||
| label: 'Quantity', | ||
| name: 'quantity', | ||
| type: 'string', | ||
| defaultValue: '', | ||
| inputUI: { | ||
| type: 'text', | ||
| }, | ||
| tooltip: `Specify the numeric value to use in the arithmetic operation. | ||
| Must be a valid number. Cannot be 0 when using division. | ||
|
|
||
| \\nExample\\n | ||
| 1 | ||
|
|
||
| \\nExample\\n | ||
| 5.5 | ||
|
|
||
| \\nExample\\n | ||
| -10 | ||
| `, | ||
| }, | ||
| ], | ||
| outputs: [ | ||
| { | ||
| number: 1, | ||
| tooltip: 'Continue to next plugin', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const plugin = (args: IpluginInputArgs): IpluginOutputArgs => { | ||
| const lib = require('../../../../../methods/lib')(); | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign | ||
| args.inputs = lib.loadDefaultValues(args.inputs, details); | ||
|
|
||
| // Get original plugin inputs from thisPlugin.inputsDB | ||
| const inputsOriginal = args.thisPlugin.inputsDB; | ||
|
|
||
| // Extract variable path from the original template string | ||
| const variableTemplate = String(inputsOriginal?.variable).trim(); | ||
| const variableMatch = variableTemplate.match(/\{\{\{(?:args|baseArgs)\.([\w.]+)\}\}\}/); | ||
|
|
||
| if (!variableMatch) { | ||
| throw new Error(`Variable template "${variableTemplate}" is invalid. Expected format: {{{args.path.to.variable}}}`); | ||
| } | ||
|
|
||
| const variablePath = variableMatch[1]; // e.g., "variables.user.existingVar" | ||
| const pathParts = variablePath.split('.'); | ||
| const variableName = pathParts[pathParts.length - 1]; // Last part for display purposes | ||
|
|
||
| const variable = String(args.inputs.variable).trim(); | ||
| const operation = String(args.inputs.operation).trim(); | ||
| const quantity = parseFloat(String(args.inputs.quantity).trim()); | ||
|
|
||
| const value = parseFloat(variable); | ||
|
|
||
| // Validate numeric values | ||
| if (Number.isNaN(quantity)) { | ||
| throw new Error(`Quantity "${args.inputs.quantity}" is not a valid number`); | ||
| } | ||
|
|
||
| if (Number.isNaN(value)) { | ||
| throw new Error(`Variable "${variableName}" is not a valid number`); | ||
| } | ||
|
|
||
| // Check for division by zero | ||
| if (operation === '/' && quantity === 0) { | ||
| throw new Error('Division by zero is not allowed'); | ||
| } | ||
|
|
||
| args.jobLog( | ||
| `Applying the operation ${operation} ${quantity} to ${variableName} of value ${value}`, | ||
| ); | ||
|
|
||
| // Helper to set value at dynamic path | ||
| const setValueAtPath = (obj: IpluginInputArgs, path: string[], val: string) => { | ||
| let current = obj; | ||
| for (let i = 0; i < path.length - 1; i += 1) { | ||
| // @ts-expect-error dynamic path | ||
| if (current[path[i]] === undefined || current[path[i]] === null) { | ||
| throw new Error(`Path "${path.slice(0, i + 1).join('.')}" does not exist in args object`); | ||
| } | ||
| // @ts-expect-error dynamic path | ||
| current = current[path[i]]; | ||
| } | ||
|
|
||
| // @ts-expect-error dynamic path | ||
| current[path[path.length - 1]] = val; // eslint-disable-line no-param-reassign | ||
| }; | ||
|
|
||
| let result: number; | ||
| switch (operation) { | ||
| case '+': | ||
| result = value + quantity; | ||
| setValueAtPath(args, pathParts, String(result)); | ||
| break; | ||
| case '-': | ||
| result = value - quantity; | ||
| setValueAtPath(args, pathParts, String(result)); | ||
| break; | ||
| case '*': | ||
| result = value * quantity; | ||
| setValueAtPath(args, pathParts, String(result)); | ||
| break; | ||
| case '/': | ||
| result = value / quantity; | ||
| setValueAtPath(args, pathParts, String(result)); | ||
| break; | ||
| default: | ||
| throw new Error(`The operation ${operation} is invalid`); | ||
| } | ||
|
|
||
| return { | ||
| outputFileObj: args.inputFileObj, | ||
| outputNumber: 1, | ||
| variables: args.variables, | ||
| }; | ||
| }; | ||
| export { details, plugin }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,6 +103,7 @@ describe('basicVideoOrAudio Plugin', () => { | |
| }, | ||
| }, | ||
| }, | ||
| thisPlugin: {} | ||
| } as IpluginInputArgs; | ||
| }); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.