UNPKG

@lyra/base

Version:

Lyra plugin containing the base components and roles for a Lyra configuration

26 lines (21 loc) 961 B
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); const VEGAQL_KEYWORDS = ['match', 'in', 'asc', 'desc', 'true', 'false', 'null']; const VALID_FIELD = /^[a-zA-Z_][a-zA-Z0-9_]*$/; const fieldNeedsEscape = exports.fieldNeedsEscape = fieldName => !VALID_FIELD.test(fieldName) || VEGAQL_KEYWORDS.includes(fieldName); const escapeField = exports.escapeField = fieldName => `["${fieldName}"]`; const escapeFirst = fieldName => `@${escapeField(fieldName)}`; const isEmptyArray = v => Array.isArray(v) && v.length === 0; const joinPath = exports.joinPath = pathArray => pathArray.reduce((prev, pathSegment, i) => { if (isEmptyArray(pathSegment)) { return `${prev}[]`; } const isFirst = i === 0; const needsEscape = fieldNeedsEscape(pathSegment); if (needsEscape) { return isFirst ? escapeFirst(pathSegment) : `${prev}${escapeField(pathSegment)}`; } return isFirst ? pathSegment : `${prev}.${pathSegment}`; }, '');