all files / sparql/ prefix.js

97.37% Statements 37/38
83.33% Branches 20/24
100% Functions 8/8
90% Lines 9/10
2 statements, 5 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                                                           
'use strict';
 
import PrefixIndex from './prefix-index';
 
export default class Prefix {
    /**
     * Represents a single PREFIX component within a query
     *
     * @class Prefix
     * @constructor
     * @param {String} value - Either a full PREFIX string for a single prefix or a shortcode to be looked up in PrefixIndex
     * @param {Object} prefixIndex - Optional override for the default PrefixIndex
     */
    constructor(value, prefixIndex = null) {
        Eif (prefixIndex === null) {
            prefixIndex = PrefixIndex;
        }
        if (value.indexOf(':') === -1) {
            Eif (prefixIndex.hasOwnProperty(value)) {
                this.value = `${value}: <${prefixIndex[value]}>`;
            } else {
                throw Error(`No prefix found in PrefixIndex matching: ${value}`);
            }
        } else {
            this.value = value.replace(/^PREFIX /, '');
        }
    }
 
    /**
     * Retrieves the SPARQL string representation of the current instance.
     *
     * @method toString
     * @returns {String}
     */
    toString() {
        return `PREFIX ${this.value}`;
    }
}