All files / rdf uri.ts

100% Statements 28/28
100% Branches 22/22
100% Functions 4/4
100% Lines 28/28

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 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                    7x 7x   7x 1x       7x 2x     7x   7x                   6519x 6519x   6519x 1x       6519x   6519x 6163x       356x     356x 344x   12x                   5x   5x 4x   4x   1x                   18381x   18381x 15298x   3083x      
/**
 * A helper class for working with URI string.
 */
export class Uri {
    /**
     * Get the portion of a URI after the first occurance of '#' or the last occurance of '/'.
     * @param uri A URI.
     * @returns The label portion of the URI.
     */
    static getLocalPart(uri: string): string | undefined {
        let u = uri;
        let n = u.indexOf('?');
 
        if (n > -1) {
            u = uri.substring(0, n);
        }
 
        // If we have namespace URI, return the label of the document or folder.
        if (u.endsWith('/') || u.endsWith('#')) {
            u = u.substring(0, u.length - 1);
        }
 
        let ns = Uri.getNamespaceIri(u);
 
        return ns.length < u.length ? u.replace(ns, "") : undefined;
    }
 
    /**
     * Get the portion of a URI after the first occurance of '#' or the last occurance of '/'.
     * @param uri A URI.
     * @returns The namespace portion of the URI.
     */
    static getNamespaceIri(uri: string): string {
        // Remove any query strings from the URI.
        let u = uri;
        let n = u.indexOf('?');
 
        if (n > -1) {
            u = uri.substring(0, n);
        }
 
        // Find the first occurance of '#' and return the substring up to that point.
        n = u.indexOf('#');
 
        if (n > -1) {
            return u.substring(0, n + 1);
        }
 
        // Find the last occurance of '/' and return the substring up to that point.
        n = u.lastIndexOf('/');
 
        // Only return the substring if it is not the 'http://' or 'https://' protocol.
        if (n > 8) {
            return u.substring(0, n + 1);
        } else {
            return u + "/";
        }
    }
 
    /**
     * Get a transformed version of the URI that can be used as a JSON identifier which only contains letters, numbers and dots.
     * @param uri A URI.
     * @returns A transformed version which only contains letters, numbers and dots.
     */
    static toJsonId(uri: string): string | undefined {
        let u = uri.split('//')[1];
 
        if (u) {
            u = u.replace(/[^a-zA-Z0-9]/g, '.');
 
            return u.endsWith('.') ? u.slice(0, -1) : u;
        } else {
            return undefined;
        }
    }
 
    /**
     * Get a normalized version of the URI which removes any trailing slashes or hashes.
     * @param uri A URI.
     * @returns A normalized URI.
     */
    static getNormalizedUri(uri: string): string {
        let u = uri.split('?')[0];
 
        if (u.endsWith('/') || u.endsWith('#')) {
            return u.substring(0, u.length - 1);
        } else {
            return u;
        }
    }
}