import URIUtils from "../../utils/URIUtils"; export interface IURI { uriPath: string; hasPathVariable: boolean; } class URI { private uriPath: string; private hasPathVariable: boolean; constructor(data: IURI) { this.uriPath = data.uriPath; this.hasPathVariable = data.hasPathVariable; } public getUriPath(): string { return this.uriPath; } public setUriPath(uriPath: string): void { this.uriPath = uriPath; } public isHasPathVariable(): boolean { return this.hasPathVariable; } public setHasPathVariable(hasPathVariable: boolean): void { this.hasPathVariable = hasPathVariable; } public static getNonTemplatedURI(uriPath: string): URI { return new URI({ uriPath: uriPath, hasPathVariable: false, }); } public static getURI(uriPath: string): URI { let pathSegments: string[] = URIUtils.getPathSegments(uriPath); let isTemplateURI: boolean = false; for (let pathSegment of pathSegments) { if (URIUtils.isPathSegmentTemplate(pathSegment)) { isTemplateURI = true; break; } } return new URI({ uriPath: uriPath, hasPathVariable: isTemplateURI, }); } public equals(object: Object): boolean { if (object == this) { return true; } if (object == null) { return false; } if (!(object instanceof URI)) { return false; } let otherURI: URI = object as URI; if (!this.isHasPathVariable() && !otherURI.isHasPathVariable()) { return this.getUriPath() === otherURI.getUriPath(); } let pathSegments: string[] = URIUtils.getPathSegments(this.getUriPath()); let otherUTIPathSegments: string[] = URIUtils.getPathSegments( otherURI.getUriPath() ); if (pathSegments.length !== otherUTIPathSegments.length) { return false; } for (let idx: number = 0; idx < pathSegments.length; idx++) { let pathSegment: string = pathSegments[idx]; let otherPathSegment: string = otherUTIPathSegments[idx]; if (!URIUtils.arePathSegmentsMatching(pathSegment, otherPathSegment)) { return false; } } return true; } public getSize(): number { return URIUtils.getPathSegments(this.getUriPath()).length; } public getPathSegments(): string[] { return URIUtils.getPathSegments(this.getUriPath()); } } export default URI;