-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(utils): restructure utils (#1053)
- Loading branch information
1 parent
aacf8db
commit 51ffdeb
Showing
21 changed files
with
491 additions
and
437 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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); | ||
} | ||
} |
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,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); | ||
}; | ||
} |
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,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); | ||
} |
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,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 ''; | ||
} |
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,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); | ||
} |
Oops, something went wrong.