All files / utilities uri.ts

100% Statements 23/23
95% Branches 19/20
100% Functions 5/5
100% Lines 23/23

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            6x 1x     5x   5x 4x   4x   1x                     16x   16x 10x     6x   6x 5x   1x                   5x 2x   3x                 10x   10x                 3x   3x 2x   1x    
/**
 * Get a transformed version of the URI that can be used as a JSON identifier which only contains letters, numbers and dots.
 * @param iri A URI.
 * @returns A transformed version which only contains letters, numbers and dots.
 */
export function toJsonId(iri: string): string | undefined {
	if (!iri) {
		return iri;
	}
 
	let u = iri.split('//')[1];
 
	if (u) {
		u = u.replace(/[^a-zA-Z0-9]/g, '.');
 
		return u.endsWith('.') ? u.slice(0, -1) : u;
	} else {
		return undefined;
	}
}
 
/**
 * Get the IRI from a node ID in the form of `<http://example.com/resource>`.
 * If the node ID does not contain angle brackets, it is returned as is.
 * @param id A node ID.
 * @returns The IRI corresponding to the node ID.
 */
export function getIriFromNodeId(id: string): string {
	const n = id.lastIndexOf('<');
 
	if (n === -1) {
		return id;
	}
 
	const m = id.lastIndexOf('>');
 
	if (n < m) {
		return id.substring(n + 1, m);
	} else {
		return id;
	}
}
 
/**
 * Get the local part and query from an IRI string.
 * @param iri An IRI string.
 * @returns The local part and query of the IRI, or the IRI itself if it does not contain a local part or query.
 */
export function getLocalPartAndQuery(iri: string): string {
	if (iri.includes('#')) {
		return iri.split('#').pop() || iri;
	} else {
		return iri.split('/').pop() || iri;
	}
}
/**
 * Get the file name from a URI string.
 * @param uri A URI string.
 * @returns The file name, or the URI itself if it does not contain a file name.
 */
export function getFileName(uri: string): string {
	const parts = uri.split('/');
 
	return parts.length > 0 ? parts[parts.length - 1] : uri;
}
 
/**
 * Get the folder path from a URI string.
 * @param uri A URI string.
 * @returns The folder path, or the URI itself if it does not contain a path.
 */
export function getPath(uri: string): string {
	const parts = uri.split('/');
 
	if (parts.length > 1) {
		return parts.slice(0, -1).join('/');
	} else {
		return uri;
	}
}