-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from dhensby/pulls/create-string
feat: add connection string builder
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
type ValidDataTypes = string | boolean | number | null | undefined | { toString(): string }; | ||
|
||
function isQuoted(val: string): boolean { | ||
if (val[0] !== '{') { | ||
return false; | ||
} | ||
for (let i = 1; i < val.length; i++) { | ||
if (val[i] === '}') { | ||
if (i + 1 === val.length) { | ||
// if last char, then it's quoted properly | ||
return true; | ||
} else if (val[i + 1] !== '}') { | ||
// the next char is no a `}` so there is no valid escaping here | ||
return false; | ||
} else { | ||
// we are seeing an escaped `}`, so skip ahead | ||
i++; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function needsQuotes(val: string): boolean { | ||
return !isQuoted(val) && !!val.match(/\[|]|{|}|\|\(|\)|,|;|\?|\*|=|!|@/)?.length; | ||
} | ||
|
||
function encodeTuple(key: string, value: ValidDataTypes): [string, string] { | ||
if (value === null || value === undefined) { | ||
return [key, '']; | ||
} | ||
switch (typeof value) { | ||
case 'boolean': | ||
return [key, value ? 'Yes' : 'No']; | ||
default: { | ||
const strVal = (value as { toString(): string }).toString(); | ||
if (needsQuotes(strVal)) { | ||
return [key, `{${strVal.replace(/}/g, '}}')}}`]; | ||
} | ||
return [key, strVal]; | ||
} | ||
} | ||
} | ||
|
||
export function buildConnectionString(data: Record<string, ValidDataTypes>): string { | ||
return Object.entries(data).map(([key, value]) => { | ||
return encodeTuple(key.trim(), value).join('='); | ||
}).join(';'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { buildConnectionString } from '../../src/builder'; | ||
import { expect } from 'chai'; | ||
|
||
describe('builder', () => { | ||
it('builds a connection string', () => { | ||
const built = buildConnectionString({ | ||
strkey: 'value', | ||
booltrue: true, | ||
boolfalse: false, | ||
numkey: 123, | ||
needsquote: 'a}test', | ||
quotedquote: '{quoted}}value}', | ||
quoted: '{regular value}', | ||
badlyquoted: '{no closing quote', | ||
badclose: '{close}early', | ||
nullkey: null, | ||
undefinedkey: undefined, | ||
}); | ||
expect(built).to.equal('strkey=value;booltrue=Yes;boolfalse=No;numkey=123;needsquote={a}}test};quotedquote={quoted}}value};quoted={regular value};badlyquoted={{no closing quote};badclose={{close}}early};nullkey=;undefinedkey='); | ||
}); | ||
}); |