Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 5x 5x 5x 5x 5x 5x 5x 5x 8x 1x 7x 7x 7x 7x 20x 20x 7x 7x 6x 6x 6x 6x 5x 6x 6x 11x | import {z} from 'zod';
import {PluginFactory} from '@grnsft/if-core/interfaces';
import {ConfigParams, PluginParams} from '@grnsft/if-core/types';
import {ERRORS} from '@grnsft/if-core/utils';
import {validate} from '../../../common/util/validations';
import {STRINGS} from '../../config';
const {ConfigError} = ERRORS;
const {MISSING_CONFIG} = STRINGS;
export const Subtract = PluginFactory({
configValidation: (config: ConfigParams) => {
if (!config || !Object.keys(config)?.length) {
throw new ConfigError(MISSING_CONFIG);
}
const configSchema = z.object({
'input-parameters': z.array(z.string()),
'output-parameter': z.string().min(1),
});
return validate<z.infer<typeof configSchema>>(configSchema, config);
},
inputValidation: (input: PluginParams, config: ConfigParams) => {
const inputParameters = config['input-parameters'];
const inputData = inputParameters.reduce(
(acc: {[x: string]: any}, param: string | number) => {
acc[param] = input[param];
return acc;
},
{} as Record<string, number>
);
const validationSchema = z.record(z.string(), z.number());
return validate(validationSchema, inputData);
},
implementation: async (inputs: PluginParams[], config: ConfigParams) => {
const {
'input-parameters': inputParameters,
'output-parameter': outputParameter,
} = config;
return inputs.map(input => {
const calculatedResult = calculateDiff(input, inputParameters);
return {
...input,
[outputParameter]: calculatedResult,
};
});
},
allowArithmeticExpressions: [],
});
/**
* Calculates the diff between the 1st item in the inputs nad the rest of the items
*/
const calculateDiff = (input: PluginParams, inputParameters: string[]) => {
const [firstItem, ...restItems] = inputParameters;
return restItems.reduce(
(accumulator, metricToSubtract) => accumulator - input[metricToSubtract],
input[firstItem] // Starting accumulator with the value of the first item
);
};
|