UNPKG

wurtle

Version:

Utilities to parse grouped quads from the N3-family of serializations based on parser directives

75 lines 3.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var n3_1 = require("n3"); var GroupedParser = /** @class */ (function () { function GroupedParser(options) { this._parser = new n3_1.Parser(options); } /** * */ GroupedParser.prototype.parse = function (input, groupCallback) { // The second parameter accepts an object { onGrouped: ..., onPrefix: ..., onComment: ...} // As a second and third parameter it still accepts a separate quadCallback and prefixCallback for backward compatibility as well var onGroupedQuads, onPrefix, onComment; if (groupCallback && (groupCallback.onGroupedQuads !== undefined || groupCallback.onComment !== undefined || groupCallback.onPrefix !== undefined)) { onGroupedQuads = groupCallback.onGroupedQuads; onPrefix = groupCallback.onPrefix; onComment = groupCallback.onComment; } else if (groupCallback) { onGroupedQuads = groupCallback; onComment = function () { }; } var groups = new Map(); this._parser.parse(input, { onQuad: function (error, quad) { if (error) { onGroupedQuads(error); } else if (quad) { groups.forEach(function (group) { group.push(quad); }); // If however there were no groups... if (groups.size === 0) { // Output a single quad group onGroupedQuads(null, [quad]); } } else { // This is the end, just output the remaining groups if they weren’t closed groups.forEach(function (group, key) { onGroupedQuads(null, group); groups.delete(key); }); onGroupedQuads(null, null); } }, onComment: function (comment) { // Parse comment and try to find a start pragma var foundBegin = comment.match(/\s*@group begin\s*(.*)\s*/); // When begin is found multiple times, it is going to reset the current set of quads. if (foundBegin && foundBegin[1] !== undefined) { // If the group is named, then add it to the map groups.set(foundBegin[1], []); } // Parse comment to find an end -- a second time the group is closed without opening it is just ignored var foundEnd = comment.match(/\s*@group end\s*(.*)\s*/); if (foundEnd && foundEnd[1] !== undefined) { if (groups.has(foundEnd[1])) { onGroupedQuads(null, groups.get(foundEnd[1])); groups.delete(foundEnd[1]); } } onComment(comment); }, onPrefix: onPrefix }); }; return GroupedParser; }()); exports.default = GroupedParser; //# sourceMappingURL=N3GroupedParser.js.map