UNPKG

joi-to-typescript

Version:

Convert Joi Schemas to TypeScript interfaces

46 lines (45 loc) 1.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.filterMap = filterMap; exports.toStringLiteral = toStringLiteral; exports.isDescribe = isDescribe; /** * 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 */ 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 */ 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}`; } function isDescribe(x) { if (!x) { return false; } if (x.type) { return true; } return false; }