Skip to content

Commit

Permalink
chore(utils): restructure utils (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Apr 3, 2024
1 parent aacf8db commit 51ffdeb
Show file tree
Hide file tree
Showing 21 changed files with 491 additions and 437 deletions.
251 changes: 0 additions & 251 deletions src/utils.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/utils/StringIdGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// credits to https://stackoverflow.com/a/12504061/4790644
export class StringIdGenerator {
private ids: number[] = [0];

constructor(private readonly chars = 'abcdefghijklmnopqrstuvwxyz') {}

next(): string {
const idsChars = this.ids.map((id) => this.chars[id]);
this.increment();
return idsChars.join('');
}

private increment(): void {
for (let i = this.ids.length - 1; i >= 0; i -= 1) {
this.ids[i] += 1;
if (this.ids[i] < this.chars.length) {
return;
}

this.ids[i] = 0;
}

this.ids.unshift(0);
}
}
22 changes: 22 additions & 0 deletions src/utils/createSchemalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import decamelize from 'decamelize';
import { identity, quote } from '.';
import type { Name } from '../operations/generalTypes';

export function createSchemalize(
shouldDecamelize: boolean,
shouldQuote: boolean
): (v: Name) => string {
const transform = [
shouldDecamelize ? decamelize : identity,
shouldQuote ? quote : identity,
].reduce((acc, fn) => (fn === identity ? acc : (x: string) => acc(fn(x))));

return (v) => {
if (typeof v === 'object') {
const { schema, name } = v;
return (schema ? `${transform(schema)}.` : '') + transform(name);
}

return transform(v);
};
}
22 changes: 22 additions & 0 deletions src/utils/createTransformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { escapeValue } from '.';
import type { Name, Value } from '../operations/generalTypes';
import type { Literal } from '../types';

export function createTransformer(literal: Literal) {
return (
statement: string,
mapping?: { [key: string]: Name | Value }
): string =>
Object.keys(mapping || {}).reduce((str: string, param) => {
const val = mapping?.[param];
return str.replace(
new RegExp(`{${param}}`, 'g'),
val === undefined
? ''
: typeof val === 'string' ||
(typeof val === 'object' && val !== null && 'name' in val)
? literal(val)
: String(escapeValue(val))
);
}, statement);
}
40 changes: 40 additions & 0 deletions src/utils/escapeValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { isPgLiteral, StringIdGenerator } from '.';
import type { Value } from '../operations/generalTypes';

export function escapeValue(val: Value): string | number {
if (val === null) {
return 'NULL';
}

if (typeof val === 'boolean') {
return val.toString();
}

if (typeof val === 'string') {
let dollars: string;
const ids = new StringIdGenerator();
let index: string;

do {
index = ids.next();
dollars = `$pg${index}$`;
} while (val.includes(dollars));

return `${dollars}${val}${dollars}`;
}

if (typeof val === 'number') {
return val;
}

if (Array.isArray(val)) {
const arrayStr = val.map(escapeValue).join(',').replace(/ARRAY/g, '');
return `ARRAY[${arrayStr}]`;
}

if (isPgLiteral(val)) {
return val.value;
}

return '';
}
10 changes: 10 additions & 0 deletions src/utils/formatLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function formatLines(
lines: string[],
replace = ' ',
separator = ','
): string {
return lines
.map((line) => line.replace(/(?:\r\n|\r|\n)+/g, ' '))
.join(`${separator}\n`)
.replace(/^/gm, replace);
}
Loading

0 comments on commit 51ffdeb

Please sign in to comment.