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 1x 6x 6x 6x 5x 6x 3x 6x 2x 6x 6x 6x 1x 5x | import {z} from 'zod';
import {ERRORS} from '@grnsft/if-core/utils';
import {ConfigParams, PluginParams} from '@grnsft/if-core/types';
import {validate} from '../../../common/util/validations';
import {STRINGS} from '../../config';
import {PluginFactory} from '@grnsft/if-core/interfaces';
const {MissingInputDataError, ConfigError, RegexMismatchError} = ERRORS;
const {MISSING_CONFIG, MISSING_INPUT_DATA, REGEX_MISMATCH} = STRINGS;
export const Regex = PluginFactory({
configValidation: (config: ConfigParams) => {
if (!config || !Object.keys(config)?.length) {
throw new ConfigError(MISSING_CONFIG);
}
const schema = z.object({
parameter: z.string().min(1),
match: z.string().min(1),
output: z.string(),
});
return validate<z.infer<typeof schema>>(schema, config);
},
inputValidation: (input: PluginParams, config: ConfigParams) => {
const parameter = config['parameter'];
if (!input[parameter]) {
throw new MissingInputDataError(MISSING_INPUT_DATA(parameter));
}
return input;
},
implementation: async (inputs, config) => {
const {parameter: parameter, match, output} = config;
return inputs.map(input => ({
...input,
[output]: extractMatching(input, parameter, match),
}));
},
});
/**
* Extracts a substring from the given input parameter that matches the provided regular expression pattern.
*/
const extractMatching = (
input: PluginParams,
parameter: string,
match: string
) => {
if (!match.startsWith('/')) {
match = '/' + match;
}
if (!match.endsWith('/g') && !match.endsWith('/')) {
match += '/';
}
const regex = eval(match);
const matchedItems = input[parameter].match(regex);
if (!matchedItems || matchedItems.length === 0) {
throw new RegexMismatchError(REGEX_MISMATCH(input[parameter], match));
}
return matchedItems.join(' ');
};
|