Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var toTriples = function (input, graph, cb) {
var rval = null;
// normalize input
jsonld.normalize(input, {}, function (err, normalized) {
if (err)
cb(err);
else {
var parseTerm = function (term) {
if (term.type === 'blank node') {
return {'blank': term.value};
} else if (term.type === 'IRI') {
return {'token': 'uri', 'value': term.value};
} else if (term.type === 'literal') {
if (term.language != null) {
return {'literal': '"' + term.value + '"@' + term.language};
} else if (term.datatype !== null) {
return {'literal': '"' + term.value + '"^^<' + term.datatype + ">"};
} else {
return {'literal': '"' + term.value + '"'};
jsonld.normalize(jsonldExpected, opts, function (err, norm1) {
if (err) {
return callback(err);
}
jsonld.normalize(jsonldGot, opts, function (err, norm2) {
if (err) {
return callback(err);
}
try {
assert.deepEqual(norm1, norm2);
return callback();
} catch (e) {
return callback(e);
}
});
});
}
* normalize() test
*/
jsonld.normalize(doc, (err, res) => {
log(res);
});
jsonld.normalize(doc, {algorithm: 'URDNA2015', expansion: false}, (err, res) => {
log(res);
});
jsonld.normalize(doc)
.then((res) => {
log(res);
});
jsonld.normalize(doc, {expansion: false})
.then((res) => {
log(res);
});
/**
* fromRDF() test
*/
jsonld.fromRDF(docNQuads, (err, res) => {
log(res);
});
jsonld.fromRDF(docNQuads, {format: 'application/n-quads'}, (err, res) => {
log(res);
});
jsonld.fromRDF(docRDF)
log(res);
});
jsonld.frame(doc, frame, {requireAll: true})
.then((res) => {
log(res);
});
/**
* normalize() test
*/
jsonld.normalize(doc, (err, res) => {
log(res);
});
jsonld.normalize(doc, {algorithm: 'URDNA2015', expansion: false}, (err, res) => {
log(res);
});
jsonld.normalize(doc)
.then((res) => {
log(res);
});
jsonld.normalize(doc, {expansion: false})
.then((res) => {
log(res);
});
/**
* fromRDF() test
*/
function assertEqualRdf (jsonldExpected, jsonldGot, options, callback) {
var opts = {
base: options.base,
format: 'application/nquads'
};
jsonld.normalize(jsonldExpected, opts, function (err, norm1) {
if (err) {
return callback(err);
}
jsonld.normalize(jsonldGot, opts, function (err, norm2) {
if (err) {
return callback(err);
}
try {
assert.deepEqual(norm1, norm2);
return callback();
} catch (e) {
return callback(e);
}
});
});
}
return callback(this._error(doc, resp.statusCode));
}
var ctxUrl = this.url('');
//check if the server could satisfy the option and if so return
if ( ((opts.profile === 'expanded') && Array.isArray(doc)) ||
((opts.profile === 'flattened') && ('@context' in doc) && ('@graph' in doc)) ||
((opts.profile === 'compacted') && ('@context' in doc))
) {
if (opts.normalize) {
if (doc['@context'] === SchemaOrgIo.contextUrl) {
doc['@context'] = ctxUrl;
}
jsonld.normalize(doc, {format: 'application/nquads'}, callback);
} else {
callback(null, doc);
}
return;
}
//the server could not satisfy the option we suppose we got a JSON doc
var ctxUrlFromLink;
if (resp.headers.link){
var links = jsonld.parseLinkHeader(resp.headers.link);
if ('http://www.w3.org/ns/json-ld#context' in links){
ctxUrlFromLink = links['http://www.w3.org/ns/json-ld#context'].target;
};
}
if (!ctxUrlFromLink && !doc['@context']){
function(err,doc) {
if (err) {
callback(err);
return;
}
jsonld.normalize(doc, function(err,doc) {
if (err) {
callback(err);
return;
}
doc = doc['@default'];
var object;
for (var n = 0, l = doc.length; n < l; n++) {
var triple = doc[n];
var subject = triple.subject.value;
var predicate = triple.predicate.value;
object = triple.object;
if (object.type === 'IRI' || object.type === 'blank node') {
object = object.value;
} else if (object.type === 'literal') {
var val = '"' + object.value + '"';
if (object.language)
doc['@context'] = ctxUrl;
}
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));
};