schema-fun
Version:
JSON schema tools
129 lines • 3.89 kB
JavaScript
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 propertyMatch = tail.match(/^([^.[\]]+)((\[|\.)(.*))?$/);
if (propertyMatch) {
pathArray.push(propertyMatch[1]);
if (propertyMatch[3] == '[') {
tail = propertyMatch[2];
}
else {
tail = propertyMatch[4] || '';
}
}
else {
const arrayElementMatch = tail.match(/^\[([0-9]+|\*)\]((\[|\.)(.*))?/);
if (arrayElementMatch) {
if (arrayElementMatch[1] == '*') {
pathArray.push(-1);
}
else {
pathArray.push(parseInt(arrayElementMatch[1]));
}
if (arrayElementMatch[3] == '[') {
tail = arrayElementMatch[2];
}
else {
tail = arrayElementMatch[4] || '';
}
}
else {
throw new Error(`Malformed dotted/bracketed path: ${path}`);
}
}
}
return pathArray;
}
export function pathDepth(path) {
return propertyPathToArray(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 propertyPathToArray(path) {
if (_.isString(path)) {
return dottedPathToArray(path);
}
else {
return path;
}
}
export function propertyPathToDottedPath(path) {
if (_.isArray(path)) {
return arrayToDottedPath(path);
}
else {
return path;
}
}
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