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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | 4x 4x 4x 4x 4x 4x 4x 4x 16x 1x 15x 15x 15x 15x 12x 11x 11x 11x 11x 33x 33x 11x 11x 1x 10x 4x 10x 10x 1x 1x 3x 1x 9x 7x 2x 2x 3x 3x 2x 5x 5x 2x 11x 22x 66x 52x | /* eslint-disable eqeqeq */
import {z} from 'zod';
import {ConfigParams, PluginParams} from '@grnsft/if-core/types';
import {PluginFactory} from '@grnsft/if-core/interfaces';
import {ERRORS, validate} from '@grnsft/if-core/utils';
import {STRINGS} from '../../config';
import {
fieldAccessor,
nanifyEmptyValues,
parseCSVFile,
retrieveFile,
} from '../util/csv-helpers';
const {MISSING_CONFIG, NO_QUERY_DATA} = STRINGS;
const {QueryDataNotFoundError, ConfigError} = ERRORS;
export const CSVLookup = PluginFactory({
configValidation: (config: ConfigParams) => {
if (!config || !Object.keys(config)?.length) {
throw new ConfigError(MISSING_CONFIG);
}
const configSchema = z.object({
filepath: z.string(),
query: z.record(z.string(), z.string()),
output: z
.string()
.or(z.array(z.string()))
.or(z.array(z.array(z.string()))),
});
return validate<z.infer<typeof configSchema>>(configSchema, config);
},
implementation: async (inputs: PluginParams[], config: ConfigParams) => {
/**
* 1. Tries to retrieve given file (with url or local path).
* 2. Parses given CSV.
* 3. Filters requested information from CSV.
*/
const {filepath, query, output} = config;
const file = await retrieveFile(filepath);
const parsedCSV = parseCSVFile(file);
return inputs.map(input => {
/** Collects query values from input. */
const queryData: any = {};
const queryKeys = Object.keys(query);
queryKeys.forEach(queryKey => {
const queryValue = query[queryKey];
queryData[queryKey] = input[queryValue];
});
/** Gets related data from CSV. */
const relatedData = parsedCSV.find(withCriteria(queryData));
if (!relatedData) {
throw new QueryDataNotFoundError(NO_QUERY_DATA);
}
return {
...input,
...filterOutput(relatedData, {output, query}),
};
});
},
});
/**
* 1. If output is anything, then removes query data from csv record to escape duplicates.
* 2. Otherwise checks if it's a miltidimensional array, then grabs multiple fields ().
* 3. If not, then returns single field.
* 4. In case if it's string, then
*/
const filterOutput = (
dataFromCSV: any,
params: {
output: string | string[] | string[][];
query: Record<string, any>;
}
) => {
const {output, query} = params;
if (output === '*') {
const keys = Object.keys(query);
keys.forEach(key => {
delete dataFromCSV[key];
});
return nanifyEmptyValues(dataFromCSV);
}
if (Array.isArray(output)) {
/** Check if it's a multidimensional array. */
if (Array.isArray(output[0])) {
const result: any = {};
output.forEach(outputField => {
/** Check if there is no renaming request, then export as is */
const outputTitle = outputField[1] || outputField[0];
result[outputTitle] = fieldAccessor(outputField[0], dataFromCSV);
});
return result;
}
const outputTitle = output[1] || output[0];
return {
[outputTitle as string]: fieldAccessor(output[0], dataFromCSV),
};
}
return {
[output]: fieldAccessor(output, dataFromCSV),
};
};
/**
* Asserts CSV record with query data.
*/
const withCriteria = (queryData: Record<string, any>) => (csvRecord: any) => {
const ifMatchesCriteria = Object.keys(queryData).map(
(key: string) => csvRecord[key] == queryData[key]
);
return ifMatchesCriteria.every(value => value === true);
};
|