UNPKG

docorm

Version:

Persistence layer with ORM features for JSON documents

100 lines 3.06 kB
import _ from 'lodash'; function hasKey(obj, key) { return key in obj; } export function jsonPathToPropertyPath(jsonPath) { // TODO First ensure that the JSON path begins with $. and does not contain any complex elements. return jsonPath.substring(2); } export function dottedPathToArray(path) { const pathArray = []; let tail = path; while (tail.length > 0) { const match = tail.match(/^([^\[]*)\[([0-9]+|\*)\]\.?(.*)$/); if (!match) { pathArray.push(tail); tail = ''; } else { if (match[1] != null) { pathArray.push(match[1]); } if (match[2] != null) { if (match[2] == '*') { pathArray.push(-1); } else { pathArray.push(parseInt(match[1])); } } tail = match[3] || ''; } } return pathArray; } export function pathDepth(path) { return dottedPathToArray(path).length; } export function arrayToDottedPath(pathArray) { if (pathArray.length == 0) { return ''; } const firstElement = pathArray[0]; return [ firstElement, ...pathArray.slice(1).map((pathElement) => _.isNumber(pathElement) ? `[${pathElement == -1 ? '*' : pathElement}]` : `.${pathElement}`) ].join(''); } export function shortenPath(path, numComponentsToTrim) { const pathArray = dottedPathToArray(path); if (numComponentsToTrim > pathArray.length) { throw 'Error'; } else { const shortenedPathArray = pathArray.slice(0, -numComponentsToTrim); return arrayToDottedPath(shortenedPathArray); } } export function tailPath(path, numComponentsToTrim) { const pathArray = dottedPathToArray(path); if (numComponentsToTrim > pathArray.length) { throw 'Error'; } else { const pathTailArray = pathArray.slice(-numComponentsToTrim); return arrayToDottedPath(pathTailArray); } } export function mapPaths(x, transformPath, visited = []) { if (x == null) { return x; } if (_.isArray(x)) { return x.map((element) => mapPaths(element, transformPath)); } if (!_.isObject(x)) { return x; } visited.push(x); const mappedObject = {}; for (const key in x) { //if (Object.prototype.hasOwnProperty.call(object, key)) { if (hasKey(x, key)) { const value = x[key]; if (key == 'path' && _.isString(value)) { const { path, additionalOptions } = transformPath(value); mappedObject[key] = path; if (additionalOptions) { _.assign(mappedObject, additionalOptions); } } else { if (!visited.includes(value)) { mappedObject[key] = mapPaths(value, transformPath, visited); } } } } return mappedObject; } //# sourceMappingURL=paths.js.map