Skip to content

Commit

Permalink
feat(builtins): move common functions into helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Dec 12, 2024
1 parent a993de0 commit 10212e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 208 deletions.
113 changes: 10 additions & 103 deletions src/if-run/builtins/csv-import/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
/* eslint-disable eqeqeq */
import {readFile} from 'fs/promises';
import axios from 'axios';
import {z} from 'zod';
import {parse} from 'csv-parse/sync';

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';

const {
FILE_FETCH_FAILED,
FILE_READ_FAILED,
MISSING_CSV_COLUMN,
MISSING_CONFIG,
} = STRINGS;

const {
FetchingFileError,
ReadFileError,
MissingCSVColumnError,
ConfigError,
CSVParseError,
} = ERRORS;
import {
fieldAccessor,
nanifyEmptyValues,
parseCSVFile,
retrieveFile,
} from '../util/helpers';

const {MISSING_CONFIG} = STRINGS;

const {ConfigError} = ERRORS;

export const CSVImport = PluginFactory({
configValidation: (config: ConfigParams) => {
Expand Down Expand Up @@ -59,74 +52,6 @@ export const CSVImport = PluginFactory({
},
});

/**
* Checks if given string is URL.
*/
const isURL = (filepath: string) => {
try {
new URL(filepath);
return true;
} catch (error) {
return false;
}
};

/**
* Checks if given `filepath` is url, then tries to fetch it.
* Otherwise tries to read file.
*/
const retrieveFile = async (filepath: string) => {
if (isURL(filepath)) {
const {data} = await axios.get(filepath).catch(error => {
throw new FetchingFileError(
FILE_FETCH_FAILED(filepath, error.response.message)
);
});

return data;
}

return readFile(filepath).catch(error => {
throw new ReadFileError(FILE_READ_FAILED(filepath, error));
});
};

/**
* Checks if value is invalid: `undefined`, `null` or an empty string, then sets `nan` instead.
*/
const setNanValue = (value: any) =>
value == null || value === '' ? 'nan' : value;

/**
* Converts empty values to `nan`.
*/
const nanifyEmptyValues = (object: any) => {
if (typeof object === 'object') {
const keys = Object.keys(object);

keys.forEach(key => {
const value = object[key];
object[key] = setNanValue(value);
});

return object;
}

return setNanValue(object);
};

/**
* If `field` is missing from `object`, then reject with error.
* Otherwise nanify empty values and return data.
*/
const fieldAccessor = (field: string, object: any) => {
if (!(`${field}` in object)) {
throw new MissingCSVColumnError(MISSING_CSV_COLUMN(field));
}

return nanifyEmptyValues(object[field]);
};

/**
* 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 ().
Expand Down Expand Up @@ -166,21 +91,3 @@ const filterOutput = (
[output]: fieldAccessor(output, dataFromCSV),
};
};

/**
* Parses CSV file.
*/
const parseCSVFile = (file: string | Buffer) => {
try {
const parsedCSV: any[] = parse(file, {
columns: true,
skip_empty_lines: true,
cast: true,
});

return parsedCSV;
} catch (error: any) {
console.error(error);
throw new CSVParseError(error);
}
};
114 changes: 9 additions & 105 deletions src/if-run/builtins/csv-lookup/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
/* eslint-disable eqeqeq */
import {readFile} from 'fs/promises';
import axios from 'axios';
import {z} from 'zod';
import {parse} from 'csv-parse/sync';

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/helpers';

const {
FILE_FETCH_FAILED,
FILE_READ_FAILED,
MISSING_CSV_COLUMN,
MISSING_CONFIG,
NO_QUERY_DATA,
} = STRINGS;

const {
FetchingFileError,
ReadFileError,
MissingCSVColumnError,
QueryDataNotFoundError,
ConfigError,
CSVParseError,
} = ERRORS;
const {MISSING_CONFIG, NO_QUERY_DATA} = STRINGS;

const {QueryDataNotFoundError, ConfigError} = ERRORS;

export const CSVLookup = PluginFactory({
configValidation: (config: ConfigParams) => {
Expand Down Expand Up @@ -78,74 +68,6 @@ export const CSVLookup = PluginFactory({
},
});

/**
* Checks if given string is URL.
*/
const isURL = (filepath: string) => {
try {
new URL(filepath);
return true;
} catch (error) {
return false;
}
};

/**
* Checks if given `filepath` is url, then tries to fetch it.
* Otherwise tries to read file.
*/
const retrieveFile = async (filepath: string) => {
if (isURL(filepath)) {
const {data} = await axios.get(filepath).catch(error => {
throw new FetchingFileError(
FILE_FETCH_FAILED(filepath, error.response.message)
);
});

return data;
}

return readFile(filepath).catch(error => {
throw new ReadFileError(FILE_READ_FAILED(filepath, error));
});
};

/**
* Checks if value is invalid: `undefined`, `null` or an empty string, then sets `nan` instead.
*/
const setNanValue = (value: any) =>
value == null || value === '' ? 'nan' : value;

/**
* Converts empty values to `nan`.
*/
const nanifyEmptyValues = (object: any) => {
if (typeof object === 'object') {
const keys = Object.keys(object);

keys.forEach(key => {
const value = object[key];
object[key] = setNanValue(value);
});

return object;
}

return setNanValue(object);
};

/**
* If `field` is missing from `object`, then reject with error.
* Otherwise nanify empty values and return data.
*/
const fieldAccessor = (field: string, object: any) => {
if (!(`${field}` in object)) {
throw new MissingCSVColumnError(MISSING_CSV_COLUMN(field));
}

return nanifyEmptyValues(object[field]);
};

/**
* 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 ().
Expand Down Expand Up @@ -207,21 +129,3 @@ const withCriteria = (queryData: Record<string, any>) => (csvRecord: any) => {

return ifMatchesCriteria.every(value => value === true);
};

/**
* Parses CSV file.
*/
const parseCSVFile = (file: string | Buffer) => {
try {
const parsedCSV: any[] = parse(file, {
columns: true,
skip_empty_lines: true,
cast: true,
});

return parsedCSV;
} catch (error: any) {
console.error(error);
throw new CSVParseError(error);
}
};

0 comments on commit 10212e6

Please sign in to comment.