Skip to content

Commit fe0f663

Browse files
Refactor protocol assignment in getUrl method (#322)
Refactor the protocol determination logic to use bitwise operations for extracting protocol index. This change simplifies the code by directly using the lower four bits of the protocol to check for HTTP or HTTPS. Additionally, enhanced the error message formatting for better readability.
1 parent ac1f634 commit fe0f663

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/src/nanotdf/models/ResourceLocator.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,15 @@ export default class ResourceLocator {
106106
* Construct URL from ResourceLocator or throw error
107107
*/
108108
getUrl(): string | never {
109-
let protocol;
110-
if (this.protocol === ProtocolEnum.Http) {
109+
let protocol: string;
110+
// protocolIndex get the first four bits
111+
const protocolIndex: number = this.protocol & 0xf;
112+
if (protocolIndex === ProtocolEnum.Http) {
111113
protocol = 'http';
112-
} else if (this.protocol === ProtocolEnum.Https) {
114+
} else if (protocolIndex === ProtocolEnum.Https) {
113115
protocol = 'https';
114116
} else {
115-
throw new Error(`Cannot create URL from protocol, ${ProtocolEnum[this.protocol]}`);
117+
throw new Error(`Cannot create URL from protocol, "${ProtocolEnum[this.protocol]}"`);
116118
}
117119

118120
return `${protocol}://${this.body}`;

0 commit comments

Comments
 (0)