All files N3GroupedWriter.ts

100% Statements 18/18
100% Branches 1/1
100% Functions 5/5
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39  1x           1x         6x 6x             6x 6x 8x 8x 8x   8x 6x 6x           1x 5x   1x  
import { Quad } from "@rdfjs/types";
import { Prefixes, Writer, WriterOptions, ErrorCallback } from "n3";
import { Writable } from "readable-stream";
 
/**
 * Due to some historic decisions in N3.js it seems impossible to extend the Writer class.
 */
export default class GroupedWriter {
    private _groupCount;
    public writer : Writer;
 
    constructor (outputStream?: Writable | WriterOptions, options?:WriterOptions) {
        this.writer = new Writer(outputStream, options);
        this._groupCount = 0;
    }
 
    /**
     * Add a group of quads and group them using wurtle
     * @param groupedQuads 
     */
    public addGroupedQuads (groupedQuads: Quad[]) : void {
        this.writer._outputStream.write('# @group begin ' + this._groupCount+ '\n');
        for (let i = 0; i < groupedQuads.length; i++) {
            let quad = groupedQuads[i];
            this.writer.addQuad(quad.subject,quad.predicate, quad.object,quad.graph, () => {
                //If this was the last element, close the group now.
                if (groupedQuads.length === i+1) {
                    this.writer._outputStream.write('\n# @group end ' + this._groupCount + '\n');
                    this._groupCount++;
                }
            });
        }
    }
 
    public end (cb: ErrorCallback) : void {
        this.writer.end(cb);
    }
}