Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(utils): improve createTransformer #1054

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/utils/createTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ 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) => {
export function createTransformer(
literal: Literal
): (statement: string, mapping?: { [key: string]: Name | Value }) => string {
return (statement, mapping = {}) =>
Object.keys(mapping).reduce((str, param) => {
const val = mapping?.[param];
return str.replace(
new RegExp(`{${param}}`, 'g'),
Expand Down
52 changes: 36 additions & 16 deletions test/utils/createTransformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,52 @@ describe('utils', () => {
it('should handle string and Name', () => {
const t = createTransformer(createSchemalize(true, true));

expect(
t('CREATE INDEX {string} ON {name} (id);', {
string: 'string',
name: { schema: 'schema', name: 'name' },
})
).toBe('CREATE INDEX "string" ON "schema"."name" (id);');
const actual = t('CREATE INDEX {string} ON {name} (id);', {
string: 'string',
name: { schema: 'schema', name: 'name' },
});

expect(actual).toBe('CREATE INDEX "string" ON "schema"."name" (id);');
});

it('should not escape PgLiteral', () => {
const t = createTransformer(createSchemalize(true, true));

expect(
t('INSERT INTO s (id) VALUES {values};', {
values: new PgLiteral(['s1', 's2'].map((e) => `('${e}')`).join(', ')),
})
).toBe("INSERT INTO s (id) VALUES ('s1'), ('s2');");
const actual = t('INSERT INTO s (id) VALUES {values};', {
values: new PgLiteral(['s1', 's2'].map((e) => `('${e}')`).join(', ')),
});

expect(actual).toBe("INSERT INTO s (id) VALUES ('s1'), ('s2');");
});

it('should use number', () => {
const t = createTransformer(createSchemalize(true, true));

expect(
t('INSERT INTO s (id) VALUES ({values});', {
values: 1,
})
).toBe('INSERT INTO s (id) VALUES (1);');
const actual = t('INSERT INTO s (id) VALUES ({values});', {
values: 1,
});

expect(actual).toBe('INSERT INTO s (id) VALUES (1);');
});

it('should fallback to empty mapping', () => {
const t = createTransformer(createSchemalize(true, true));

const actual = t('INSERT INTO s (id) VALUES ({values});');

expect(actual).toBe('INSERT INTO s (id) VALUES ({values});');
});

it('should fallback to empty string for undefined value', () => {
const t = createTransformer(createSchemalize(true, true));

const actual = t('INSERT INTO s (id) VALUES ({values});', {
// @ts-expect-error: JS-only test
values: undefined,
});

// TODO @Shinigami92 2024-04-03: Should this be an error?
expect(actual).toBe('INSERT INTO s (id) VALUES ();');
});
});
});