wurtle
Version:
Utilities to parse grouped quads from the N3-family of serializations based on parser directives
80 lines • 3.58 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 __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// **N3GroupedStreamParser** parses a text stream into a quad stream.
var readable_stream_1 = require("readable-stream");
var events_1 = require("events");
var N3GroupedParser_1 = __importDefault(require("./N3GroupedParser"));
var GroupedStreamParser = /** @class */ (function (_super) {
__extends(GroupedStreamParser, _super);
function GroupedStreamParser(options) {
var _this = _super.call(this, { decodeStrings: true }) || this;
_this._readableState.objectMode = true;
if (!options)
options = {};
options.comments = true;
// Set up parser with dummy stream to obtain `data` and `end` callbacks
var parser = new N3GroupedParser_1.default(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 currently active group.
// If no active group is currently in place, it is going to emit the quad in their own group as it appears.
onGroupedQuads: function (error, quadGroup) {
if (error) {
_this.emit('error', error);
}
else if (quadGroup) {
_this.push(quadGroup);
}
},
// Emit prefixes through the `prefix` event
onPrefix: function (prefix, iri) { _this.emit('prefix', prefix, iri); },
onComment: function (comment) { _this.emit('comment', comment); },
});
// Implement Transform methods through parser callbacks
_this._transform = function (chunk, encoding, done) { onData(chunk); done(); };
_this._flush = function (done) { onEnd(); done(); };
return _this;
}
// ### Parses a stream of strings
GroupedStreamParser.prototype.import = function (stream) {
var _this = this;
stream.on('data', function (chunk) { _this.write(chunk); });
stream.on('end', function () { _this.end(); });
stream.on('error', function (error) { _this.emit('error', error); });
return this;
};
return GroupedStreamParser;
}(readable_stream_1.Transform));
exports.default = GroupedStreamParser;
//# sourceMappingURL=N3GroupedStreamParser.js.map