UNPKG

joi-to-typescript

Version:

Convert Joi Schemas to TypeScript interfaces

41 lines (40 loc) 1.04 kB
/** * Applies the mapper over each element in the list. * If the mapper returns undefined it will not show up in the result * * @param list - array to filter + map * @param mapper - mapper func to apply to map */ export function filterMap(list, mapper) { return list.reduce((res, val) => { const mappedVal = mapper(val); if (mappedVal !== undefined) { res.push(mappedVal); } return res; }, []); } /** * Escape value so that it can be go into single quoted string literal. * @param value */ export function toStringLiteral(value, doublequoteEscape) { const escapeChar = doublequoteEscape ? '"' : "'"; value = value.replace(/\\/g, '\\\\'); if (doublequoteEscape) { value = value.replace(/"/g, '\\"'); } else { value = value.replace(/'/g, "\\'"); } return `${escapeChar}${value}${escapeChar}`; } export function isDescribe(x) { if (!x) { return false; } if (x.type) { return true; } return false; }