Skip to content

Commit e42cdb3

Browse files
committed
Clearer ternary statement
1 parent 4d6d58b commit e42cdb3

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/mongo_client_options.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,23 @@ const HOSTS_RX = new RegExp(
103103
'(?<protocol>mongodb(?:\\+srv|)):\\/\\/(?:(?<username>[^:]*)(?::(?<password>[^@]*))?@)?(?<hosts>[^\\/?]*)(?<rest>.*)'
104104
);
105105

106-
export function parseURI(uri: string): { srv: boolean; url: URL; hosts: string[] } {
106+
function parseURI(uri: string): { srv: boolean; url: URL; hosts: string[] } {
107107
const match = uri.match(HOSTS_RX);
108108
if (!match) {
109109
throw new MongoParseError(`Invalid connection string ${uri}`);
110110
}
111-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
112-
//@ts-expect-error
113-
const { protocol, username, password, hosts, rest } = match.groups;
111+
112+
const protocol = match.groups?.protocol;
113+
const username = match.groups?.username;
114+
const password = match.groups?.password;
115+
const hosts = match.groups?.hosts;
116+
const rest = match.groups?.rest;
117+
114118
if (!protocol || !hosts) {
115119
throw new MongoParseError('Invalid connection string, protocol and host(s) required');
116120
}
117-
const authString = `${username ? `${password ? `${username}:${password}` : username}` : ''}`;
121+
122+
const authString = username ? (password ? `${username}:${password}` : `${username}`) : '';
118123
return {
119124
srv: protocol.includes('srv'),
120125
url: new URL(`${protocol.toLowerCase()}://${authString}@dummyHostname${rest}`),

0 commit comments

Comments
 (0)