UNPKG

@coat/cli

Version:

TODO: See #3

38 lines (37 loc) 1.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatPropertyPath = formatPropertyPath; var _chalk = _interopRequireDefault(require("chalk")); var _unquotedPropertyValidator = _interopRequireDefault(require("unquoted-property-validator")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Formats a property path to be displayed as a validation issue. * * @param propertyPath The property path that should be formatted * @returns The formatted property path */ function formatPropertyPath(propertyPath) { if (!propertyPath.length) { return ""; } const [head, ...tail] = propertyPath; const tailParts = tail.reduce((acc, prop) => { // Check whether the current property must be quoted // or accessed via square brackets ([]) const propResult = (0, _unquotedPropertyValidator.default)(prop.toString()); if (propResult.needsBrackets) { if (propResult.needsQuotes) { acc.push(`[${propResult.quotedValue}]`); } else { acc.push(`[${prop}]`); } } else { acc.push(`.${prop}`); } return acc; }, []); const pathResult = [head, ...tailParts].join(""); return (0, _chalk.default)`{green ${pathResult}:}`; }