UNPKG

lincd-jsonld

Version:

Utilities to parse and write JSON-LD for the LINCD library

120 lines 5.21 kB
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 { Graph, Literal, NamedNode } from 'lincd/models'; import { linkedUtil } from '../package.js'; let NodeFactory = class NodeFactory { static getOrCreateFromAny(resource) { if (typeof resource === 'string') { if (this.isURI(resource)) { return NamedNode.getOrCreate(resource); } else { return new Literal(resource); } } else if (typeof resource === 'number') { return new Literal(resource.toString()); } else if (resource instanceof Map) { return this.createFromMap(resource); } else if (typeof resource === 'boolean') { //can't use xsd.boolean from xsd ontology here because of circular references var boolean = NamedNode.getOrCreate('http://www.w3.org/2001/XMLSchema#boolean'); return new Literal(resource.toString(), boolean); } } static getFromToString(string) { if (Literal.isLiteralString(string)) { return new Literal(string); } else { return NamedNode.getNamedNode(string); } } static getOrCreateNamedNodeFromAny(resource) { if (typeof resource === 'string' && this.isURI(resource)) { return NamedNode.getOrCreate(resource); } else if (resource instanceof Map) { return this.createFromMap(resource); } else if (resource instanceof NamedNode) { return resource; } } static createFromMap(propertyMap) { var res = NamedNode.create(); for (let [key, value] of propertyMap) { res.set(this.getOrCreateNamedNodeFromAny(key), this.getOrCreateFromAny(value)); } return res; } static graphFromSPARQL(uriString, bnodes) { var firstChar = uriString.substr(0, 1); if (firstChar == '<') { return Graph.getOrCreate(uriString.substr(1, uriString.length - 2)); } else if (firstChar == '_') { //hmmmm a blanknode as a graph, we can't really do both at the moment //maybe a BlanknodeGraph Object? implementing IBlankNode? throw new Error('blanknodes as graphs are not implemented yet'); //return new Graph(uriString); } } static uriResourceFromSPARQL(uriString, bnodes) { var firstChar = uriString.substr(0, 1); if (firstChar == '<') { return this.createNamedNodeFromSparql(uriString, bnodes); } else if (firstChar == '_') { return bnodes.getOrCreateBlankNode(uriString); //return new BlankNode(uriString); } } static literalResourceFromSPARQL(literalString) { return Literal.fromString(literalString); } static resourceFromSPARQL(resourceString, bnodes) { var firstChar = resourceString.substr(0, 1); if (firstChar == '"') { return this.literalResourceFromSPARQL(resourceString); } else if (firstChar == '_') { return bnodes.getOrCreateBlankNode(resourceString); } else if (firstChar == '<') { return this.createNamedNodeFromSparql(resourceString, bnodes); } throw new Error('Invalid format, this is not a node'); } /** * returns true if string is a valid URI * requires a http:// https:// ftp:// or other prefix with :// to be part of the string (so www.test.com is not valid) * @param string */ static isURI(string) { //combining https://www.regextester.com/94092 and https://stackoverflow.com/a/163684/977206 //this might be good too if we need something stricter / better: https://raw.githubusercontent.com/jhermsmeier/uri.regex/master/pattern.js return /\w+:\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/.test(string); } static createNamedNodeFromSparql(uriString, bnodes) { //TODO: refactor this class, rename bnodes to node map and simplify using a more general bnodes.getResource(potentialTmpOrBlanknodeUri) return bnodes.getOrCreateNamedNode(uriString.substr(1, uriString.length - 2)); // return NamedNode.getOrCreate(uriString.substr(1,uriString.length-2)); } }; NodeFactory = __decorate([ linkedUtil ], NodeFactory); export { NodeFactory }; //# sourceMappingURL=NodeFactory.js.map