Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function _pluckDidNode(did, target, didDocument) {
// flatten to isolate target
jsonld.documentLoader = testLoader.documentLoader.bind(testLoader);
const flattened = await jsonld.flatten(didDocument);
// filter out non-DID nodes and find target
let found = false;
const filtered = [];
for(const node of flattened) {
const id = node['@id'];
if(id === target) {
filtered.push(node);
found = true;
break;
}
}
// target not found
if(!found) {
const err = new Error('Not Found');
err.httpStatusCode = 404;
err.status = 404;
return Observable_1.Observable.fromPromise(new Promise(function (resolve, reject) {
jsonld.flatten(obj, context, function (err, data) {
// console.log('flatten data: bla bla bla: ' + JSON.stringify(data, null, 2));
resolve(data);
});
}));
};
});
jsonld.expand(doc)
.then((res) => {
log(res);
});
jsonld.expand(doc, {keepFreeFloatingNodes: false})
.then((res) => {
log(res);
});
/**
* flatten() test
*/
jsonld.flatten(doc, context, (err, res) => {
log(res);
});
jsonld.flatten(doc, context, {expandContext: context}, (err, res) => {
log(res);
});
jsonld.flatten(doc, context)
.then((res) => {
log(res);
});
jsonld.flatten(doc, context, {base: baseUrl})
.then((res) => {
log(res);
});
if (arguments.length === 3) {
callback = opts;
opts = {};
} else {
opts = clone(opts);
}
//flatten the doc to get all the @id and generate opts.reservedIds
var tdoc = clone(doc);
var ctx;
if (tdoc['@context'] === SchemaOrgIo.contextUrl) {
tdoc['@context'] = SchemaOrgIo.context()['@context']; //offline
ctx = tdoc['@context'];
}
jsonld.flatten(tdoc, ctx || tdoc['@context'], function(err, fdoc){
if (err) return callback(err);
var ioIds = fdoc['@graph']
.filter(function(x){
return x['@id'] && x['@id'].split(':')[0] === 'ldr';
})
.map(function(x){
return x['@id'].split(':')[1]
});
opts.reservedIds = opts.reservedIds || {};
ioIds.forEach(function(id){ opts.reservedIds[id] = true; });
//get namespace
if (doc['@id']) {
opts.namespace = opts.namespace || this.namespace(doc['@id']);
return async () =>
findEntity(
(await expand(
await flatten(await Promise.all(thunks.map(t => t())), null)
)) as Array,
url
)!;
}
function _next(err, pdoc) {
if (err) return callback(err);
//reverse @context transfo
if (ctx && pdoc['@context'] === ctxUrl) {
pdoc['@context'] = ctx;
}
callback(null, pdoc);
};
if (opts.normalize) {
jsonld.normalize(doc, {format: 'application/nquads'}, _next);
} else if (opts.profile === 'flattened') {
jsonld.flatten(doc, doc['@context'], _next);
} else if (opts.profile === 'expanded') {
jsonld.expand(doc, {expandContext: doc['@context']}, _next);
} else {
jsonld.compact(doc, doc['@context'], _next);
}
}.bind(this));
};
export default function jsonldParser (str, kb, base, callback) {
const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType')
? base.value
: base
return jsonld
.flatten(JSON.parse(str), null, { base: baseString })
.then((flattened) => flattened.reduce((store, flatResource) => {
const id = flatResource['@id']
? kb.rdfFactory.namedNode(flatResource['@id'])
: kb.rdfFactory.blankNode()
for (const property of Object.keys(flatResource)) {
if (property === '@id') {
continue
}
const value = flatResource[property]
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
kb.addStatement(kb.rdfFactory.quad(id, kb.rdfFactory.namedNode(property), jsonldObjectToTerm(kb, value[i])))
}
} else {
GraphManager.prototype.load_jsonld = function (jd) {
var self = this;
jsonld.flatten(jd, function (error, robjectds) {
self._load_flattened(error, jd, robjectds);
});
};
return async () =>
findEntity(
(await expand(
await flatten(await Promise.all(thunks.map(t => t())), null)
)) as Array,
url
)!;
}