Skip to content

Commit

Permalink
clean up logic when flattening options object
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Feb 9, 2022
1 parent 0a89c73 commit fb3bc3f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
29 changes: 12 additions & 17 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ function getUint(name: string, value: unknown): number {
return parsedValue;
}


function toArray<T>(value: T): T[];
function toArray<T>(value: T[]): T[];
function toArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value];
}

function toRecord(value: string): Record<string, any> {
const record = Object.create(null);
const keyValuePairs = value.split(',');
Expand Down Expand Up @@ -324,23 +331,11 @@ export function parseOptions(
]);

for (const key of allKeys) {
const values = [];
if (objectOptions.has(key)) {
const options = Array.isArray(objectOptions.get(key))
? objectOptions.get(key)
: [objectOptions.get(key)];
values.push(...options);
}
if (urlOptions.has(key)) {
const options = urlOptions.get(key) ?? [];
values.push(...options);
}
if (DEFAULT_OPTIONS.has(key)) {
const options = Array.isArray(DEFAULT_OPTIONS.get(key))
? DEFAULT_OPTIONS.get(key)
: [DEFAULT_OPTIONS.get(key)];
values.push(...options);
}
const values = [objectOptions, urlOptions, DEFAULT_OPTIONS].flatMap(optionsObject => {
const options = optionsObject.get(key) ?? [];
return toArray(options);
});

allOptions.set(key, values);
}

Expand Down
6 changes: 2 additions & 4 deletions test/unit/assorted/uri_options.spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ describe('URI option spec tests', function () {
'tlsDisableCertificateRevocationCheck can be set to true',
'tlsDisableCertificateRevocationCheck can be set to false',
'tlsDisableOCSPEndpointCheck can be set to true',
'tlsDisableOCSPEndpointCheck can be set to false',

// TODO(NODE-3813): read preference tag issue: parsing rack:1 as rack:true
'Valid read preference options are parsed correctly'
'tlsDisableOCSPEndpointCheck can be set to false'
];

const testsThatDoNotThrowOnWarn = [
Expand Down Expand Up @@ -60,6 +57,7 @@ describe('URI option spec tests', function () {

for (const test of suite.tests) {
it(`${test.description}`, function () {
console.error(test);
if (skipTests.includes(test.description)) {
return this.skip();
}
Expand Down

0 comments on commit fb3bc3f

Please sign in to comment.