wurtle
Version:
Utilities to parse grouped quads from the N3-family of serializations based on parser directives
87 lines • 3.67 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var n3_1 = require("n3");
var events_1 = require("events");
var N3GroupedStreamParser = /** @class */ (function (_super) {
__extends(N3GroupedStreamParser, _super);
/**
*
* @param options Mind you need to have comments enabled when passing parseroptions
*/
function N3GroupedStreamParser(options) {
var _this = _super.call(this, options ? options : { comments: true }) || this;
_this._groups = new Map();
// Set up parser with dummy stream to obtain `data` and `end` callbacks
var parser = new n3_1.Parser(options);
var onData;
var onEnd;
var input = new events_1.EventEmitter();
input.on = function (event, callback) {
switch (event) {
case 'data':
onData = callback;
break;
case 'end':
onEnd = callback;
break;
}
return _this;
};
parser.parse(input, {
// Handle quads by pushing them in each active group. If no active group is currently
onQuad: function (error, quad) {
if (error) {
_this.emit('error', error);
}
else if (quad) {
if (_this._groups.size > 0) {
_this._groups.forEach(function (group) {
group.push(quad);
});
}
else {
// push a 1 quad group
_this.push([quad]);
}
}
},
// Emit prefixes through the `prefix` event
onPrefix: function (prefix, iri) { _this.emit('prefix', prefix, iri); },
onComment: function (comment) {
// Parse comment and try to find a start pragma
var foundBegin = comment.match(/\s*@group\s+begin\s+(.*)\s*/);
if (foundBegin && foundBegin[1]) {
_this._groups.set(foundBegin[1], []);
}
// Parse comment
var foundEnd = comment.match(/\s*@group\s+end\s+(.*)\s*/);
if (foundEnd && foundEnd[1]) {
_this.push(_this._groups.get(foundEnd[1]));
_this._groups.delete(foundEnd[1]);
}
},
});
// Implement Transform methods through parser callbacks
_this._transform = function (chunk, encoding, done) { onData(chunk); done(); };
_this._flush = function (done) { onEnd(); done(); };
return _this;
}
return N3GroupedStreamParser;
}(n3_1.StreamParser));
exports.default = N3GroupedStreamParser;
//# sourceMappingURL=N3GroupedStreamParser-WONTWORK.js.map