Skip to content

Instantly share code, notes, and snippets.

@YanLobat
Created August 2, 2017 14:53
Show Gist options
  • Save YanLobat/f42338b5acfbaa3054fe5e763d437b76 to your computer and use it in GitHub Desktop.
Save YanLobat/f42338b5acfbaa3054fe5e763d437b76 to your computer and use it in GitHub Desktop.
'use strict';
function embedNodeResolver(jsonLD) {
let IDindex = {};
let suspectedNodes = {};
let markedShapes = [];
jsonLD.shapes.forEach((shape, index) => {
IDindex[shape['@id']] = index; //write down the node to check it in future
let suspectedNode = suspectedNodes[shape['@id']]
if (suspectedNode !== undefined) {
jsonLD.shapes[suspectedNode['shapeIndex']]['property'][suspectedNode['propertyIndex']]['node'] = shape;
markedShapes.push(index);
}
shape.property.forEach((property, i) => {
if (property.node !== undefined) {
let possibleNodeIndex = IDindex[property.node];
// if we already have node for injection
if (possibleNodeIndex !== undefined) {
property.node = jsonLD.shapes[possibleNodeIndex];
markedShapes.push(possibleNodeIndex);
}
else { //or we will have it in future
suspectedNodes[property.node] = {
shapeIndex: index,
propertyIndex: i
};
}
}
});
});
//remove duplicates which are embed already
jsonLD.shapes = jsonLD.shapes.filter((shape, index) => {
if (!markedShapes.includes(index)) {
return shape;
}
});
}
async function importsResolver(jsonLD) {
if (jsonLD.imports === undefined) {
return Promise.resolve(jsonLD);
}
await Promise.all(jsonLD.imports.map(importedItem => {
const json = fetch(importedItem)
.then(response => {
if (response.status == 200) {
return response.json();
}
else {
console.log(response.status);
}
})
.then(importedjson => {
embedNodeResolver(importedjson);
importsResolver(importedjson);
jsonLD['@context'] = Object.assign(jsonLD['@context'], importedjson['@context']);
importedjson.shapes.forEach(shape => {
jsonLD.shapes.push(shape);
});
})
.catch(err => {
console.log(err);
});
}));
}
function getResolvedSchema(jsonLD) {
embedNodeResolver(jsonLD);
return importsResolver(jsonLD);
}
function getPath(path, context) {
const isURL = path.indexOf('http') !== -1;
let shortPath, fullPath, pathArr;
if (isURL) {
pathArr = path.split('#');
if (pathArr.length > 1) {
shortPath = pathArr[1];
fullPath = path;
}
else {
pathArr = path.split('/');
shortPath = pathArr[pathArr.length-1];
fullPath = path;
}
}
else {
pathArr = path.split(':');
if (pathArr.length > 1) {
shortPath = pathArr[1];
fullPath = context[pathArr[0]] + shortPath;
}
else {
shortPath = path;
fullPath = context[path];
}
}
return {shortPath: shortPath, fullPath: fullPath};
}
function getValue(data, schema, path) {
if ((typeof data === 'string') || (typeof schema === 'string')) {
throw new Error('inccorect arguments types');
return;
}
const pathObj = getPath(path);
if (data[pathObj['shortPath']] !== undefined) {
return data[pathObj['shortPath']];
}
else {
return getValueFromEmbedNodes(data, schema, pathObj['shortPath']);
}
}
function getValueFromEmbedNodes(data, schema, shortPath) {
const shapes = schema.shapes;
shapes.forEach(shape => {
searchInShape(data, shape, shortPath);
});
}
function searchInShape(data, shape, shortPath, path) {
shape.property.forEach(property => {
const pathObj = getPath(property.path);
if (pathObj['shortPath'] === shortPath) {
if (!path) {
path = data[pathObj['shortPath']];
return path;
}
else {
return path[shortPath];
}
}
if (property.node) {
if (!path) {
path = data[pathObj['shortPath']];
return searchInShape(data, property.node, shortPath, path);
}
else {
return searchInShape(data, property.node, shortPath, path[pathObj['shortPath']]);
}
}
});
}
function getPropertyMap(schema) {
if (typeof schema === 'string') {
throw new Error('Schema should be object');
}
const map;
let propertyObj;
schema.shapes.forEach(shape => {
getPropertyMapFromShape(shape, map);
});
}
function getPropertyMapFromShape(shape, map) {
shape.property.forEach(property => {
propertyObj = getPath(property.path);
map[propertyObj['shortPath']] = property;
if (property.node) {
getPropertyMapFromShape(property.node, map);
}
});
}
module.exports = { getResolvedSchema, getValue };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment