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

Enable string functions and arrays as default column values #82

Merged
merged 2 commits into from
May 10, 2017
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
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const escapeValue = (val) => {
if (typeof val === 'boolean') return val.toString();
if (typeof val === 'string') return `'${escape(val)}'`;
if (typeof val === 'number') return val;
if (Array.isArray(val)) return `ARRAY[${val.map(escapeValue).join(',').replace(/ARRAY/g, '')}]`;
if (val instanceof PgLiteral) return val.toString();
return '';
};
Expand Down
50 changes: 50 additions & 0 deletions test/utils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import assert from 'assert';
import { escapeValue, PgLiteral } from '../lib/utils';

describe('lib/utils', () => {
describe('.escapeValue', () => {
it('parse null to \'NULL\'', () => {
const value = null;

assert.equal(escapeValue(value), 'NULL');
});

it('parse boolean to string', () => {
const value = true;

assert.equal(escapeValue(value), 'true');
});

it('escape string', () => {
const value = '#escape_me';

assert.equal(escapeValue(value), '\'%23escape_me\'');
});

it('keep number as is', () => {
const value = 77.7;

assert.equal(escapeValue(value), 77.7);
});

it('parse array to ARRAY constructor syntax string', () => {
const value = [[1], [2]];
const value2 = [['a'], ['b']];

assert.equal(escapeValue(value), 'ARRAY[[1],[2]]');
assert.equal(escapeValue(value2), 'ARRAY[[\'a\'],[\'b\']]');
});

it('parse PgLiteral to unescaped string', () => {
const value = PgLiteral.create('@l|<e');

assert.equal(escapeValue(value), '@l|<e');
});

it('parse unexpected type to empty string', () => {
const value = undefined;

assert.equal(escapeValue(value), '');
});
});
});