Skip to content

Commit

Permalink
Allow array as default column value
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfranca committed May 5, 2017
1 parent 2f676b3 commit 0390d9b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ const escapeString = (str) => {
return `'${escaped}'`;
};

const parseArray = (arr) => {
const input = arr.map((item) => {
if (Array.isArray(item)) return parseArray(item);
else if (typeof item === 'string') return `"${item}"`;
return item;
});

return `{${input}}`;
};

export const escapeValue = (val) => {
if (val === null) return 'NULL';
if (typeof val === 'boolean') return val.toString();
if (typeof val === 'string') return escapeString(val);
if (typeof val === 'number') return val;
if (Array.isArray(val)) return `'${parseArray(val)}'`;
if (val instanceof PgLiteral) return val.toString();
return '';
};
Expand Down
8 changes: 8 additions & 0 deletions test/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ describe('lib/utils', () => {
assert.equal(escapeValue(value), 'true');
});

it('parse array to string enclosed within curly braces', () => {
const value = [[1], [2]];
const value2 = [['a'], ['b']];

assert.equal(escapeValue(value), '\'{{1},{2}}\'');
assert.equal(escapeValue(value2), '\'{{"a"},{"b"}}\'');
});

it('escape string allowing function tokens', () => {
const value = 'fn(fn2(one, #2), three)';
const value2 = '#no_fn_here';
Expand Down

0 comments on commit 0390d9b

Please sign in to comment.