link-rdflib
Version:
an RDF library for node.js, patched for speed.
212 lines (171 loc) • 6.93 kB
JavaScript
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
var ClassOrder = require('./class-order');
var Term = require('./term');
/**
* @class NamedNode
* @extends Term
*/
var NamedNode =
/*#__PURE__*/
function (_Term) {
_inherits(NamedNode, _Term);
/**
* @constructor
* @param iri {String}
* @param ln {String|undefined} The term, if applicable
*/
function NamedNode(iri) {
var _this;
var ln = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
_classCallCheck(this, NamedNode);
_this = _possibleConstructorReturn(this, _getPrototypeOf(NamedNode).call(this));
_this.termType = NamedNode.termType;
_this.term = undefined;
if (iri && iri.termType === NamedNode.termType) {
// param is a named node
iri = iri.value;
}
if (!iri) {
throw new Error('Missing IRI for NamedNode');
}
if (!iri.includes(':')) {
throw new Error('NamedNode IRI "' + iri + '" must be absolute.');
}
if (iri.includes(' ')) {
var message = 'Error: NamedNode IRI "' + iri + '" must not contain unencoded spaces.';
throw new Error(message);
}
var existing = NamedNode.nsMap[iri];
if (existing) {
return _possibleConstructorReturn(_this, existing);
}
_this.value = iri;
NamedNode.mem(_assertThisInitialized(_assertThisInitialized(_this)), ln);
return _this;
}
/**
* Returns an $rdf node for the containing directory, ending in slash.
*/
_createClass(NamedNode, [{
key: "dir",
value: function dir() {
var str = this.uri.split('#')[0];
var p = str.slice(0, -1).lastIndexOf('/');
var q = str.indexOf('//');
if (q >= 0 && p < q + 2 || p < 0) return null;
return NamedNode.find(str.slice(0, p + 1));
}
/**
* Returns an NN for the whole web site, ending in slash.
* Contrast with the "origin" which does NOT have a trailing slash
*/
}, {
key: "site",
value: function site() {
var str = this.uri.split('#')[0];
var p = str.indexOf('//');
if (p < 0) throw new Error('This URI does not have a web site part (origin)');
var q = str.indexOf('/', p + 2);
if (q < 0) throw new Error('This URI does not have a web site part. (origin)');
return NamedNode.find(str.slice(0, q + 1));
}
}, {
key: "doc",
value: function doc() {
if (this.uri.indexOf('#') < 0) {
return this;
} else {
return NamedNode.find(this.uri.split('#')[0]);
}
}
}, {
key: "generateString",
value: function generateString() {
return '<' + this.uri + '>';
}
/**
* Legacy getter and setter alias, node.uri
*/
}, {
key: "uri",
get: function get() {
return this.value;
},
set: function set(uri) {
this.value = uri;
}
/**
* Retrieve or create a NamedNode by its IRI
* @param iri {string} The IRI of the blank node
* @param ln {string} Property accessor-friendly string representation.
* @return {NamedNode} The resolved or created NamedNode
*/
}], [{
key: "find",
value: function find(iri) {
var ln = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
if (iri && iri.termType) {
iri = iri.value;
}
var fromMap = Term.nsMap[iri];
if (fromMap !== undefined) {
return fromMap;
}
return new NamedNode(iri, ln);
}
/**
* Retrieve a NamedNode by its store index
* @param sI {integer} The store index of the NamedNode
* @return {NamedNode | undefined}
*/
}, {
key: "findByStoreIndex",
value: function findByStoreIndex(sI) {
var term = Term.termMap[sI];
if (!term) {
return undefined;
}
if (term.termType === "NamedNode") {
return term;
}
return undefined;
}
/**
* Assigns an index number and adds a NamedNode instance to the indices
* @private
* @param nn {NamedNode} The NamedNode instance to register
* @param ln? {string} Property accessor-friendly string representation.
* @return {NamedNode} The updated NamedNode instance
*/
}, {
key: "mem",
value: function mem(nn, ln) {
if (nn.sI) {
throw new Error("NamedNode ".concat(nn, " already registered"));
}
nn.sI = ++Term.termIndex;
nn.term = ln;
Term.termMap[nn.sI] = Term.nsMap[nn.value] = nn;
return nn;
}
}, {
key: "fromValue",
value: function fromValue(value) {
return NamedNode.find(value);
}
}]);
return NamedNode;
}(Term);
NamedNode.termType = 'NamedNode';
NamedNode.prototype.classOrder = ClassOrder['NamedNode'];
NamedNode.prototype.isVar = 0;
module.exports = NamedNode;
;