UNPKG

idea-toolbox

Version:
182 lines (181 loc) 7.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loopNumericEnumValues = exports.loopNumericEnumKeys = exports.getStringEnumKeyByValue = exports.loopStringEnumValues = exports.loopStringEnumKeys = exports.mdToHtml = exports.isEmpty = exports.joinArraysOnKeys = exports.cleanStr = exports.nowISODateString = exports.toISODate = exports.toISODateString = exports.nowISOString = exports.toISOString = void 0; const isEmail_1 = __importDefault(require("validator/lib/isEmail")); const isMobilePhone_1 = __importDefault(require("validator/lib/isMobilePhone")); const isURL_1 = __importDefault(require("validator/lib/isURL")); const isFQDN_1 = __importDefault(require("validator/lib/isFQDN")); const isDate_1 = __importDefault(require("validator/lib/isDate")); const marked_1 = require("marked"); // // Utilities (static) functions, to support IDEA's projects. // /** * Get the ISO string version of a date in the format `YYYY-MM-DDTHH:mm:ss.sssZ`. * The timezone is always UTC, as denoted by the suffix `Z`. */ const toISOString = (input) => { if (!input) return null; const date = input instanceof Date ? input : new Date(input); return date.toISOString(); }; exports.toISOString = toISOString; /** * Get the current date and time in the format `YYYY-MM-DDTHH:mm:ss.sssZ`. * The timezone is always UTC, as denoted by the suffix `Z`. */ const nowISOString = () => (0, exports.toISOString)(new Date()); exports.nowISOString = nowISOString; /** * Get the ISO string version of a date in the format `YYYY-MM-DD`. * It doesn't say anything about the timezone. */ const toISODateString = (input) => { if (!input) return null; /// it forces the internal time to 12.00 to make the date timezone-resistant const dateResistantToTimeZones = new Date(input); dateResistantToTimeZones.setHours(12, 0, 0, 0); return dateResistantToTimeZones.toISOString().slice(0, 10); }; exports.toISODateString = toISODateString; /** * @deprecated use toISODateString instead */ exports.toISODate = exports.toISODateString; /** * Get the current date in the format `YYYY-MM-DD`. * It doesn't say anything about the timezone. */ const nowISODateString = () => (0, exports.toISODateString)(new Date()); exports.nowISODateString = nowISODateString; /** * Clean a string to use it within filenames and so. * @param str the string to clean * @param separator optional, separator char * @returns cleaned string */ const cleanStr = (str, separator) => (str ?? '').toLowerCase().replace(/[^a-zA-Z0-9]+/g, separator ?? ''); exports.cleanStr = cleanStr; /** * Join two arrays by a common column, selecting which data to extract. * @param mainTable the main array * @param lookupTable the lookup array * @param mainKey mainTable's column for the join condition * @param lookupKey lookupTable's column for the join condition * @param selectFunction defines which * attributes we want to retain in the joined array; null values generated by the * function are ignored * @returns the joined array */ const joinArraysOnKeys = (mainTable, lookupTable, mainKey, lookupKey, selectFunction) => { const lookupIndex = []; const output = []; for (const row of lookupTable) lookupIndex[row[lookupKey]] = row; for (const row of mainTable) { const lookupTableRow = lookupIndex[row[mainKey]]; const outputElement = selectFunction(row, lookupTableRow); if (outputElement) output.push(outputElement); } return output; }; exports.joinArraysOnKeys = joinArraysOnKeys; /** * Check if a field (/variable) is empty or invalid, based on its type. * If the type isn't passed as a parameter, it will be auto-detected. * @param field the field to check * @param fieldType set to force a type check * @returns return if the field is empty/invalid or not */ const isEmpty = (field, fieldType) => { if (field === null || field === undefined) return true; const type = fieldType ?? typeof field; if (!type) return true; try { switch (type) { case 'string': return !field.trim().length; case 'number': return field === 0; case 'positiveNumber': return field <= 0; case 'boolean': return !field; case 'object': if (field instanceof Array) return field.filter(i => i).length <= 0; else if (field instanceof Set) return field.size <= 0; else if (field instanceof Date) return !(0, isDate_1.default)(field.toISOString().slice(0, 10)); else return Object.keys(field).length <= 0; case 'date': return !(0, isDate_1.default)(new Date(field).toISOString().slice(0, 10)); case 'email': return !(0, isEmail_1.default)(field); case 'phone': return !(0, isMobilePhone_1.default)(field, 'any'); case 'url': return !(0, isURL_1.default)(field); case 'domain': return !(0, isFQDN_1.default)(field, { require_tld: false }); default: return true; } } catch (_) { return true; } }; exports.isEmpty = isEmpty; /** * Convert a markdown string to HTML. */ const mdToHtml = (mdString) => !mdString ? '' : (0, marked_1.marked)(mdString, { gfm: true, breaks: true }); exports.mdToHtml = mdToHtml; // // DEPRECATED // /** * @deprecated Get an array to iterate containing the keys of a string enum. */ const loopStringEnumKeys = (theEnum) => Object.keys(theEnum); exports.loopStringEnumKeys = loopStringEnumKeys; /** * @deprecated Get an array to iterate containing the values of a string enum. */ const loopStringEnumValues = (theEnum) => Object.keys(theEnum).map(key => theEnum[key]); exports.loopStringEnumValues = loopStringEnumValues; /** * @deprecated Get an array to iterate containing the keys of a numeric enum. */ const getStringEnumKeyByValue = (theEnum, value) => { const el = Object.keys(theEnum) .map(key => ({ value: theEnum[key], key })) .find(x => x.value === value); return el ? el.key : null; }; exports.getStringEnumKeyByValue = getStringEnumKeyByValue; /** * @deprecated Get an array to iterate containing the keys of a numeric enum. */ const loopNumericEnumKeys = (theEnum) => Object.keys(theEnum) .filter(key => !isNaN(Number(key))) .map(c => Number(c)); exports.loopNumericEnumKeys = loopNumericEnumKeys; /** * @deprecated Get an array to iterate containing the values of a numeric enum. */ const loopNumericEnumValues = (theEnum) => Object.keys(theEnum) .filter(key => !isNaN(Number(theEnum[key]))) .map(c => String(c)); exports.loopNumericEnumValues = loopNumericEnumValues;