all files / sparql/ graph-pattern.js

96.63% Statements 86/89
91.95% Branches 80/87
100% Functions 15/15
89.66% Lines 26/29
12 statements, 3 functions, 17 branches Ignored     
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                              15× 15× 15× 15× 15× 16×                         29×   29× 29×                                                                       33×                 16×                   22× 22× 36×   35×     22× 22×    
'use strict';
 
import Triple from './triple';
 
export default class GraphPattern {
    /**
     * The GraphPattern represents a list of Triples, Filters and Queries
     *
     * @class GraphPattern
     * @constructor
     * @param {Object|Array} elements - Initial item(s) for the GraphPattern
     * @param {Boolean} optional - Set the OPTIONAL flag (for use within GroupGraphPatterns)
     * @param {Boolean} union - Set the UNION flag (for use within GroupGraphPatterns)
     * @param {Boolean} allowedTypes - Override the default allowed types (Triple, Filter and Query)
     */
    constructor(elements, optional = false, union = false, allowedTypes = ['Triple', 'Filter', 'Query']) {
        this.clear();
        this._allowedTypes = allowedTypes;
        this._optional = optional;
        this._union = union;
        if (Array.isArray(elements)) {
            for (let element of elements) {
                this.addElement(element);
            }
        } else Eif (elements) {
            this.addElement(elements);
        }
    }
 
    /**
     * Adds an element to the pattern
     *
     * @method addElement
     * @param {Object} element - Single item to add
     * @param {number} atIndex - Optional index for the added element (default is end of list)
     */
    addElement(element, atIndex = -1) {
        if (typeof element === 'string') {
            element = new Triple(element);
        }
        Eif (this._allowedTypes.indexOf(element.constructor.name) > -1) {
            this._elements.splice(atIndex < 0 ? this.countElements() : atIndex, 0, element);
        } else {
            throw new Error(`TypeError: Element of type ${element.constructor.name || typeof element} is not allowed for this block.`);
        }
    }
 
    /**
     * Removes one or more elements from the pattern
     *
     * @method removeElements
     * @param {number} atIndex - Index of item to remove (default is first)
     * @param {number} count - Number of elements to remove (default is 1)
     */
    removeElements(atIndex = 0, count = 1) {
        Eif (atIndex >= 0 && atIndex + count <= this.countElements()) {
            this._elements.splice(atIndex, count);
        } else {
            throw new Error('OutOfBounds: Cannot remove elements from block, index and/or count out of bounds.');
        }
    }
 
    /**
     * Retrieves the elements from the pattern
     *
     * @method getElements
     * @returns {Array}
     */
    getElements() {
        return this._elements;
    }
 
    /**
     * Get the element count
     *
     * @method countElements
     * @returns {Number}
     */
    countElements() {
        return this._elements.length;
    }
 
    /**
     * Clear the pattern
     *
     * @method clear
     */
    clear() {
        this._elements = [];
    }
 
    /**
     * Retrieves the SPARQL string representation of the current instance
     *
     * @method toString
     * @returns {String}
     */
    toString() {
        var result = `${this._optional ? 'OPTIONAL ' : ''}${this._union ? 'UNION ' : ''}{ `;
        for (let element of this._elements) {
            if (element.constructor.name === 'Query') {
                result += `{ ${element.toString(true)} } `;
            } else {
                result += `${element.toString()}${this._elements.length > 1 && element instanceof Triple ? ' . ' : ' '}`;
            }
        }
        result += '}';
        return result;
    }
}