feat(formatter): add number action with math operation transformer (#1264)

* feat(formatter): Add number action with math operation transformer

* chore: Use different folders for list transform options of formatter
This commit is contained in:
Ömer Faruk Aydın
2023-09-05 13:02:43 +02:00
committed by GitHub
parent 18cef5f3bd
commit c9f292e252
17 changed files with 175 additions and 13 deletions

View File

@@ -1,3 +1,4 @@
import text from './text';
import numbers from './numbers';
export default [text];
export default [text, numbers];

View File

@@ -0,0 +1,52 @@
import defineAction from '../../../../helpers/define-action';
import performMathOperation from './transformers/perform-math-operation';
const transformers = {
performMathOperation,
};
export default defineAction({
name: 'Numbers',
key: 'numbers',
description:
'Transform numbers to perform math operations, generate random numbers, format numbers, and much more.',
arguments: [
{
label: 'Transform',
key: 'transform',
type: 'dropdown' as const,
required: true,
variables: true,
options: [
{ label: 'Perform Math Operation', value: 'performMathOperation' },
],
additionalFields: {
type: 'query',
name: 'getDynamicFields',
arguments: [
{
name: 'key',
value: 'listTransformOptions',
},
{
name: 'parameters.transform',
value: '{parameters.transform}',
},
],
},
},
],
async run($) {
const transformerName = $.step.parameters
.transform as keyof typeof transformers;
const output = transformers[transformerName]($);
$.setActionItem({
raw: {
output,
},
});
},
});

View File

@@ -0,0 +1,23 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import { add, divide, multiply, subtract } from 'lodash';
const mathOperation = ($: IGlobalVariable) => {
const mathOperation = $.step.parameters.mathOperation as string;
const values = ($.step.parameters.values as IJSONObject[]).map((value) =>
Number(value.input)
) as number[];
if (mathOperation === 'add') {
return values.reduce((acc, curr) => add(acc, curr), 0);
} else if (mathOperation === 'divide') {
return values.reduce((acc, curr) => divide(acc, curr));
} else if (mathOperation === 'makeNegative') {
return values.map((value) => -value);
} else if (mathOperation === 'multiply') {
return values.reduce((acc, curr) => multiply(acc, curr), 1);
} else if (mathOperation === 'subtract') {
return values.reduce((acc, curr) => subtract(acc, curr));
}
};
export default mathOperation;

View File

@@ -1,5 +1,6 @@
import capitalize from './transformers/capitalize';
import defineAction from '../../../../helpers/define-action';
import capitalize from './transformers/capitalize';
import extractEmailAddress from './transformers/extract-email-address';
import extractNumber from './transformers/extract-number';
import htmlToMarkdown from './transformers/html-to-markdown';
@@ -34,7 +35,6 @@ export default defineAction({
key: 'transform',
type: 'dropdown' as const,
required: true,
description: 'Pick a channel to send the message to.',
variables: true,
options: [
{ label: 'Capitalize', value: 'capitalize' },

View File

@@ -1,14 +1,15 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import capitalize from './options/capitalize';
import extractEmailAddress from './options/extract-email-address';
import extractNumber from './options/extract-number';
import htmlToMarkdown from './options/html-to-markdown';
import lowercase from './options/lowercase';
import markdownToHtml from './options/markdown-to-html';
import pluralize from './options/pluralize';
import replace from './options/replace';
import trimWhitespace from './options/trim-whitespace';
import useDefaultValue from './options/use-default-value';
import capitalize from './text/capitalize';
import extractEmailAddress from './text/extract-email-address';
import extractNumber from './text/extract-number';
import htmlToMarkdown from './text/html-to-markdown';
import lowercase from './text/lowercase';
import markdownToHtml from './text/markdown-to-html';
import pluralize from './text/pluralize';
import replace from './text/replace';
import trimWhitespace from './text/trim-whitespace';
import useDefaultValue from './text/use-default-value';
import performMathOperation from './numbers/perform-math-operation';
const options: IJSONObject = {
capitalize,
@@ -21,6 +22,7 @@ const options: IJSONObject = {
replace,
trimWhitespace,
useDefaultValue,
performMathOperation,
};
export default {

View File

@@ -0,0 +1,36 @@
const performMathOperation = [
{
label: 'Math Operation',
key: 'mathOperation',
type: 'dropdown' as const,
required: true,
description: 'The math operation to perform.',
variables: true,
options: [
{ label: 'Add', value: 'add' },
{ label: 'Divide', value: 'divide' },
{ label: 'Make Negative', value: 'makeNegative' },
{ label: 'Multiply', value: 'multiply' },
{ label: 'Subtract', value: 'subtract' },
],
},
{
label: 'Values',
key: 'values',
type: 'dynamic' as const,
required: false,
description: 'Add or remove numbers as needed.',
fields: [
{
label: 'Input',
key: 'input',
type: 'string' as const,
required: true,
description: 'The number to perform the math operation on.',
variables: true,
},
],
},
];
export default performMathOperation;

View File

@@ -13,10 +13,58 @@ export const GET_DYNAMIC_FIELDS = gql`
required
description
variables
dependsOn
value
options {
label
value
}
source {
type
name
arguments {
name
value
}
}
additionalFields {
type
name
arguments {
name
value
}
}
fields {
label
key
type
required
description
variables
value
dependsOn
options {
label
value
}
source {
type
name
arguments {
name
value
}
}
additionalFields {
type
name
arguments {
name
value
}
}
}
}
}
`;