lincd-jsonld
Version:
Utilities to parse and write JSON-LD for the LINCD library
75 lines • 2.9 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { QuadSet } from 'lincd/collections/QuadSet';
import { Prefix } from 'lincd/utils/Prefix';
import { linkedUtil } from '../package.js';
import * as N3Lib from 'n3';
import { NodeURIMappings } from 'lincd/collections/NodeURIMappings';
import { Datafactory } from 'lincd/Datafactory';
let N3 = class N3 {
static fromQuads(quads, extraContext = {}) {
return new Promise((resolve, reject) => {
let prefixes = { ...extraContext };
Prefix.getPrefixToUriMap().forEach((uri, prefix) => {
prefixes[prefix] = uri;
});
const writer = new N3Lib.Writer({ prefixes });
quads.forEach((quad) => {
writer.addQuad(quad);
});
writer.end((error, result) => {
if (error) {
reject(error);
}
else {
resolve(result);
}
});
});
}
static parse(content, setExplicit = true, nodeMap = new NodeURIMappings(), targetGraph, overwriteData = false) {
return new Promise((resolve, reject) => {
//TODO: add setExplicit
const factory = new Datafactory({
nodeMap,
targetGraph,
overwriteData,
});
const parser = new N3Lib.Parser({
factory,
});
const quads = new QuadSet();
parser.parse(content, (error, quad) => {
if (error) {
console.error('Error parsing quads: ' + error);
reject();
}
else if (quad) {
quads.add(quad);
}
else {
resolve({
quads,
nodemap: nodeMap,
//TODO: do we still need this?
graphs: null,
});
}
});
});
}
};
N3 = __decorate([
linkedUtil
], N3);
export { N3 };
//# sourceMappingURL=N3.js.map