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 73 74 75 76 77 78 79 80 81 | 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 4x 5x 3x 3x 5x 5x 5x 5x 2x 3x 3x 5x | import {z} from 'zod';
import {ERRORS} from '@grnsft/if-core/utils';
import {ExecutePlugin, PluginParams, ConfigParams} from '@grnsft/if-core/types';
import {validate, allDefined} from '../../util/validations';
import {STRINGS} from '../../config';
const {MissingInputDataError} = ERRORS;
const {MISSING_FUNCTIONAL_UNIT_CONFIG, MISSING_FUNCTIONAL_UNIT_INPUT} = STRINGS;
export const Sci = (globalConfig: ConfigParams): ExecutePlugin => {
const metadata = {
kind: 'execute',
};
/**
* Validates node and gloabl configs.
*/
const validateConfig = (config?: ConfigParams) => {
const schema = z
.object({
'functional-unit': z.string(),
})
.refine(data => data['functional-unit'], {
message: MISSING_FUNCTIONAL_UNIT_CONFIG,
});
return validate<z.infer<typeof schema>>(schema, config);
};
/**
* Calculate the total emissions for a list of inputs.
*/
const execute = (inputs: PluginParams[]): PluginParams[] =>
inputs.map(input => {
const safeInput = validateInput(input);
const sci =
safeInput['carbon'] > 0
? safeInput['carbon'] / input[globalConfig['functional-unit']]
: 0;
return {
...input,
sci,
};
});
/**
* Checks for fields in input.
*/
const validateInput = (input: PluginParams) => {
const message = `'carbon' and ${globalConfig['functional-unit']} should be present in your input data.`;
const validatedConfig = validateConfig(globalConfig);
if (
!(
validatedConfig['functional-unit'] in input &&
input[validatedConfig['functional-unit']] > 0
)
) {
throw new MissingInputDataError(MISSING_FUNCTIONAL_UNIT_INPUT);
}
const schema = z
.object({
carbon: z.number().gte(0),
duration: z.number().gte(1),
})
.refine(allDefined, {message});
return validate<z.infer<typeof schema>>(schema, input);
};
return {
metadata,
execute,
};
};
|