All files / languages prefixed-name-resolver.ts

100% Statements 16/16
100% Branches 10/10
100% Functions 4/4
100% Lines 16/16

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                  141x   141x           141x               3854x   3854x 1319x   1319x     3854x                   5935x   5935x 1316x   1316x   1316x     5935x             178x 178x      
import { Uri } from '@faubulous/mentor-rdf';
import { getIriFromPrefixedName, PrefixMap } from '@src/utilities';
 
/**
 * Resolves the prefixed names of a document to IRIs while its index maps are built. A
 * document repeats its prefixed names many times; each name is resolved once, so all of
 * its occurrences share one IRI string, which the index maps hash only once.
 */
export class PrefixedNameResolver {
	private readonly _iris = new Map<string, string | undefined>();
 
	private readonly _normalizedIris = new Map<string, string | undefined>();
 
	/**
	 * @param _namespaces The namespace IRIs by prefix. Call {@link clear} when a prefix is
	 * declared, as it changes the IRIs of the names that follow.
	 */
	constructor(private readonly _namespaces: PrefixMap) { }
 
	/**
	 * Returns the IRI of a prefixed name.
	 * @param name The prefixed name, e.g. `ex:Thing`.
	 * @returns The IRI, or `undefined` if the prefix is not declared.
	 */
	getIri(name: string): string | undefined {
		let iri = this._iris.get(name);
 
		if (iri === undefined && !this._iris.has(name)) {
			iri = getIriFromPrefixedName(this._namespaces, name);
 
			this._iris.set(name, iri);
		}
 
		return iri;
	}
 
	/**
	 * Returns the IRI of a prefixed name without a trailing slash or hash, so that it is
	 * comparable with the output of `vscode.Uri.toString()`.
	 * @param name The prefixed name, e.g. `ex:Thing`.
	 * @returns The normalized IRI, or `undefined` if the prefix is not declared.
	 */
	getNormalizedIri(name: string): string | undefined {
		let iri = this._normalizedIris.get(name);
 
		if (iri === undefined && !this._normalizedIris.has(name)) {
			const resolvedIri = this.getIri(name);
 
			iri = resolvedIri ? Uri.getNormalizedUri(resolvedIri) : undefined;
 
			this._normalizedIris.set(name, iri);
		}
 
		return iri;
	}
 
	/**
	 * Forgets the resolved names, e.g. after a prefix was declared.
	 */
	clear(): void {
		this._iris.clear();
		this._normalizedIris.clear();
	}
}