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:
@@ -1,3 +1,4 @@
|
|||||||
import text from './text';
|
import text from './text';
|
||||||
|
import numbers from './numbers';
|
||||||
|
|
||||||
export default [text];
|
export default [text, numbers];
|
||||||
|
52
packages/backend/src/apps/formatter/actions/numbers/index.ts
Normal file
52
packages/backend/src/apps/formatter/actions/numbers/index.ts
Normal 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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
@@ -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;
|
@@ -1,5 +1,6 @@
|
|||||||
import capitalize from './transformers/capitalize';
|
|
||||||
import defineAction from '../../../../helpers/define-action';
|
import defineAction from '../../../../helpers/define-action';
|
||||||
|
|
||||||
|
import capitalize from './transformers/capitalize';
|
||||||
import extractEmailAddress from './transformers/extract-email-address';
|
import extractEmailAddress from './transformers/extract-email-address';
|
||||||
import extractNumber from './transformers/extract-number';
|
import extractNumber from './transformers/extract-number';
|
||||||
import htmlToMarkdown from './transformers/html-to-markdown';
|
import htmlToMarkdown from './transformers/html-to-markdown';
|
||||||
@@ -34,7 +35,6 @@ export default defineAction({
|
|||||||
key: 'transform',
|
key: 'transform',
|
||||||
type: 'dropdown' as const,
|
type: 'dropdown' as const,
|
||||||
required: true,
|
required: true,
|
||||||
description: 'Pick a channel to send the message to.',
|
|
||||||
variables: true,
|
variables: true,
|
||||||
options: [
|
options: [
|
||||||
{ label: 'Capitalize', value: 'capitalize' },
|
{ label: 'Capitalize', value: 'capitalize' },
|
||||||
|
@@ -1,14 +1,15 @@
|
|||||||
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
|
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
|
||||||
import capitalize from './options/capitalize';
|
import capitalize from './text/capitalize';
|
||||||
import extractEmailAddress from './options/extract-email-address';
|
import extractEmailAddress from './text/extract-email-address';
|
||||||
import extractNumber from './options/extract-number';
|
import extractNumber from './text/extract-number';
|
||||||
import htmlToMarkdown from './options/html-to-markdown';
|
import htmlToMarkdown from './text/html-to-markdown';
|
||||||
import lowercase from './options/lowercase';
|
import lowercase from './text/lowercase';
|
||||||
import markdownToHtml from './options/markdown-to-html';
|
import markdownToHtml from './text/markdown-to-html';
|
||||||
import pluralize from './options/pluralize';
|
import pluralize from './text/pluralize';
|
||||||
import replace from './options/replace';
|
import replace from './text/replace';
|
||||||
import trimWhitespace from './options/trim-whitespace';
|
import trimWhitespace from './text/trim-whitespace';
|
||||||
import useDefaultValue from './options/use-default-value';
|
import useDefaultValue from './text/use-default-value';
|
||||||
|
import performMathOperation from './numbers/perform-math-operation';
|
||||||
|
|
||||||
const options: IJSONObject = {
|
const options: IJSONObject = {
|
||||||
capitalize,
|
capitalize,
|
||||||
@@ -21,6 +22,7 @@ const options: IJSONObject = {
|
|||||||
replace,
|
replace,
|
||||||
trimWhitespace,
|
trimWhitespace,
|
||||||
useDefaultValue,
|
useDefaultValue,
|
||||||
|
performMathOperation,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
@@ -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;
|
@@ -13,10 +13,58 @@ export const GET_DYNAMIC_FIELDS = gql`
|
|||||||
required
|
required
|
||||||
description
|
description
|
||||||
variables
|
variables
|
||||||
|
dependsOn
|
||||||
|
value
|
||||||
options {
|
options {
|
||||||
label
|
label
|
||||||
value
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
Reference in New Issue
Block a user