Skip to content

Commit

Permalink
Separate functions into files
Browse files Browse the repository at this point in the history
  • Loading branch information
c032 committed Feb 8, 2024
1 parent 9bc6383 commit f3e770d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 56 deletions.
104 changes: 52 additions & 52 deletions src/lib.ts → src/connection-uri-object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseKeywordValueConnectionString } from "./parser";
import { KeyValuePair } from "./parser";

interface ConnectionUriObject {
userspec?: {
Expand All @@ -16,57 +16,9 @@ interface ConnectionUriObject {
}[];
}

function connectionUriObjectToString(uriObject: ConnectionUriObject): string {
let userspec: string = "";
if (uriObject.userspec) {
const { user, password } = uriObject.userspec;
userspec += user;
if (password) {
userspec += `:${encodeURIComponent(password)}`;
}

userspec += "@";
}

let hostspec: string = "";
if (uriObject.hostspec) {
const { host, port } = uriObject.hostspec;
if (host) {
hostspec += host;
}
if (typeof port !== "undefined") {
hostspec += `:${port}`;
}
}

let dbname: string = "";
if (typeof uriObject.dbname !== "undefined") {
dbname += `/${encodeURIComponent(uriObject.dbname)}`;
}

let paramspec: string = "";
if (uriObject.paramspec) {
paramspec += "?";

paramspec += uriObject.paramspec
.map((item) => {
const { name, value } = item;
return `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
})
.join("&");
}

return `postgresql://${userspec}${hostspec}${dbname}${paramspec}`;
}

export function keywordValueToUri(kvConnectionString: string): string {
if (kvConnectionString === "") {
return "postgresql://";
}

const pairs = parseKeywordValueConnectionString(kvConnectionString);

function pairsToConnectionUriObject(pairs: KeyValuePair[]): ConnectionUriObject {
const uriObject: ConnectionUriObject = {};

pairs.forEach((pair) => {
const { key, value } = pair;
switch (key) {
Expand Down Expand Up @@ -123,7 +75,55 @@ export function keywordValueToUri(kvConnectionString: string): string {
}
});

const connectionStringUri = connectionUriObjectToString(uriObject);
return uriObject;
}

function connectionUriObjectToString(uriObject: ConnectionUriObject): string {
let userspec: string = "";
if (uriObject.userspec) {
const { user, password } = uriObject.userspec;
userspec += user;
if (password) {
userspec += `:${encodeURIComponent(password)}`;
}

userspec += "@";
}

let hostspec: string = "";
if (uriObject.hostspec) {
const { host, port } = uriObject.hostspec;
if (host) {
hostspec += host;
}
if (typeof port !== "undefined") {
hostspec += `:${port}`;
}
}

let dbname: string = "";
if (typeof uriObject.dbname !== "undefined") {
dbname += `/${encodeURIComponent(uriObject.dbname)}`;
}

let paramspec: string = "";
if (uriObject.paramspec) {
paramspec += "?";

paramspec += uriObject.paramspec
.map((item) => {
const { name, value } = item;
return `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
})
.join("&");
}

return `postgresql://${userspec}${hostspec}${dbname}${paramspec}`;
}

export function pairsToConnectionString(pairs: KeyValuePair[]): string {
const connectionUriObject = pairsToConnectionUriObject(pairs);
const connectionStringUri = connectionUriObjectToString(connectionUriObject);

return connectionStringUri;
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./lib";
export { keywordValueToUri } from "./keyword-value";
4 changes: 2 additions & 2 deletions src/lib.test.ts → src/keyword-value.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { strict as assert } from "node:assert";
import { describe, it } from "node:test";

import { keywordValueToUri } from "./lib";
import { keywordValueToUri } from "./keyword-value";

const TEST_CASES: Record<string, string> = {
"": "postgresql://",
Expand All @@ -12,7 +12,7 @@ const TEST_CASES: Record<string, string> = {
"postgresql://user_name:correct%20horse%20battery%20staple@postgresql/database_name?sslmode=disable",
};

describe("lib", () => {
describe("keyword-value", () => {
describe("keywordValueToUri", () => {
Object.entries(TEST_CASES).forEach(([input, expectedOutput]) => {
it(`converts ${JSON.stringify(input)} as ${JSON.stringify(expectedOutput)}`, () => {
Expand Down
13 changes: 13 additions & 0 deletions src/keyword-value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { parseKeywordValueConnectionString } from "./parser";
import { pairsToConnectionString } from "./connection-uri-object";

export function keywordValueToUri(kvConnectionString: string): string {
if (kvConnectionString === "") {
return "postgresql://";
}

const pairs = parseKeywordValueConnectionString(kvConnectionString);
const connectionStringUri = pairsToConnectionString(pairs);

return connectionStringUri;
}
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface KeyValuePair {
export interface KeyValuePair {
key: string;
value: string;
}
Expand Down

0 comments on commit f3e770d

Please sign in to comment.