schyma
Version:
JSON Schemas Visualizer React component
138 lines • 4.31 kB
JavaScript
import { __awaiter } from "tslib";
import refRes from '@json-schema-tools/reference-resolver';
const memoizedNameFromRef = {};
export function resolveRef(ref, schema) {
return __awaiter(this, void 0, void 0, function* () {
const resolver = yield refRes.resolve(ref, schema);
return resolver;
});
}
export function nameFromRef(ref) {
if (memoizedNameFromRef[ref]) {
return memoizedNameFromRef[ref];
}
const match = ref.match(/[^/]+$/);
const name = match ? match[0].split('.')[0] : '';
memoizedNameFromRef[ref] = name;
return name;
}
export function propMerge(schema, label) {
let mergedProps = {};
const { properties, patternProperties, additionalProperties, items, oneOf, allOf, anyOf, not } = schema;
const combinedOf = oneOf || anyOf || not;
if (patternProperties) {
Object.assign(mergedProps, patternProperties);
}
if (properties) {
Object.assign(mergedProps, properties);
}
if (additionalProperties || items) {
const arrWithObject = additionalProperties || items;
if (arrWithObject.oneOf || arrWithObject.allOf || arrWithObject.anyOf) {
const items = arrWithObject.oneOf || arrWithObject.anyOf || arrWithObject.not;
const props = arrayToProps(items, label);
Object.assign(mergedProps, props);
}
if (arrWithObject.$ref) {
const name = nameFromRef(arrWithObject.$ref);
mergedProps[name] = arrWithObject;
}
}
// handling allOf case seperatly
if (allOf) {
let propObj = {};
for (let i = 0; i < allOf.length; i++) {
if (allOf[i].type === 'object') {
if (allOf[i].properties) {
propObj = Object.assign(Object.assign({}, propObj), allOf[i].properties);
}
else {
propObj[allOf[i].type] = allOf[i];
}
}
else if (allOf[i].$ref) {
const name = nameFromRef(allOf[i].$ref);
propObj[name] = allOf[i];
}
else {
propObj[allOf[i].type] = allOf[i];
}
}
Object.assign(mergedProps, propObj);
}
if (combinedOf) {
const props = arrayToProps(combinedOf, label);
Object.assign(mergedProps, props);
}
return mergedProps;
}
export function arrayToProps(props, label) {
const propObj = {};
for (let i = 0; i < props.length; i++) {
if (props[i].$ref) {
const name = nameFromRef(props[i].$ref);
propObj[name] = props[i];
}
else {
if (props[i].type === 'object') {
const objectName = props[i].title || `${label}Object`;
propObj[objectName] = props[i];
}
else {
propObj[props[i].type] = props[i];
}
}
}
return propObj;
}
export function removeElementsByParent(nodes, id) {
const result = nodes.filter((node) => {
const relations = node.data.relations;
if (relations) {
if (relations.hasOwnProperty(id)) {
return;
}
else {
return node;
}
}
return node;
});
return result;
}
export function removeEdgesByParent(edges, id) {
const result = edges.filter((edge) => {
if (edge.source === id) {
return false;
}
return true;
});
return result;
}
export function retrieveObj(theObject, key) {
var result = null;
if (theObject instanceof Array) {
for (var i = 0; i < theObject.length; i++) {
result = retrieveObj(theObject[i], key);
if (result) {
break;
}
}
}
else {
for (var prop in theObject) {
if (prop == key) {
return theObject[prop];
}
if (theObject[prop] instanceof Object ||
theObject[prop] instanceof Array) {
result = retrieveObj(theObject[prop], key);
if (result) {
break;
}
}
}
}
return result;
}
//# sourceMappingURL=reusables.js.map