enhance array parser
This commit is contained in:
parent
7521e2494b
commit
f2f380787e
@ -1,3 +1,5 @@
|
||||
var arrayParser = require(__dirname + "/arrayParser.js");
|
||||
|
||||
//parses PostgreSQL server formatted date strings into javascript date objects
|
||||
var parseDate = function(isoDate) {
|
||||
//TODO this could do w/ a refactor
|
||||
@ -51,24 +53,22 @@ var parseBool = function(val) {
|
||||
}
|
||||
|
||||
var parseIntegerArray = function(val) {
|
||||
return JSON.parse(val.replace("{","[").replace("}","]"));
|
||||
if(!val) return null;
|
||||
var p = arrayParser.create(val, function(entry){
|
||||
if(entry != null)
|
||||
entry = parseInt(entry);
|
||||
|
||||
return entry;
|
||||
});
|
||||
|
||||
return p.parse();
|
||||
};
|
||||
|
||||
var parseStringArray = function(val) {
|
||||
if (!val) return null;
|
||||
if (val[0] !== '{' || val[val.length-1] !== '}')
|
||||
throw "Not postgresql array! (" + arrStr + ")";
|
||||
|
||||
var x = val.substring(1, val.length - 1);
|
||||
if (x === '') return [];
|
||||
x = x.match(/(NULL|[^,]+|"((?:.|\n|\r)*?)(?!\\)"|\{((?:.|\n|\r)*?(?!\\)\}) (,|$))/mg);
|
||||
if (x === null) throw "Not postgre array";
|
||||
return x.map(function (el) {
|
||||
if (el === 'NULL') return null;
|
||||
if (el[0] === '{') return arguments.callee(el);
|
||||
if (el[0] === '\"') return el.substring(1, el.length - 1).replace(/\\(.)/g, '$1');
|
||||
return el;
|
||||
});
|
||||
if(!val) return null;
|
||||
|
||||
var p = arrayParser.create(val);
|
||||
return p.parse();
|
||||
};
|
||||
|
||||
|
||||
|
@ -26,6 +26,101 @@ test('parsing array results', function() {
|
||||
pg.end();
|
||||
}))
|
||||
})
|
||||
|
||||
test('empty array', function(){
|
||||
client.query("SELECT '{}'::text[] as names", assert.success(function(result) {
|
||||
var names = result.rows[0].names;
|
||||
assert.lengthIs(names, 0);
|
||||
pg.end();
|
||||
}))
|
||||
})
|
||||
|
||||
test('element containing comma', function(){
|
||||
client.query("SELECT '{\"joe,bob\",jim}'::text[] as names", assert.success(function(result) {
|
||||
var names = result.rows[0].names;
|
||||
assert.lengthIs(names, 2);
|
||||
assert.equal(names[0], 'joe,bob');
|
||||
assert.equal(names[1], 'jim');
|
||||
pg.end();
|
||||
}))
|
||||
})
|
||||
|
||||
test('bracket in quotes', function(){
|
||||
client.query("SELECT '{\"{\",\"}\"}'::text[] as names", assert.success(function(result) {
|
||||
var names = result.rows[0].names;
|
||||
assert.lengthIs(names, 2);
|
||||
assert.equal(names[0], '{');
|
||||
assert.equal(names[1], '}');
|
||||
pg.end();
|
||||
}))
|
||||
})
|
||||
|
||||
test('null value', function(){
|
||||
client.query("SELECT '{joe,null,bob}'::text[] as names", assert.success(function(result) {
|
||||
var names = result.rows[0].names;
|
||||
assert.lengthIs(names, 3);
|
||||
assert.equal(names[0], 'joe');
|
||||
assert.equal(names[1], null);
|
||||
assert.equal(names[2], 'bob');
|
||||
pg.end();
|
||||
}))
|
||||
})
|
||||
|
||||
test('element containing quote char', function(){
|
||||
client.query("SELECT '{\"joe''\",jim'',\"bob\\\\\"\"}'::text[] as names", assert.success(function(result) {
|
||||
var names = result.rows[0].names;
|
||||
assert.lengthIs(names, 3);
|
||||
assert.equal(names[0], 'joe\'');
|
||||
assert.equal(names[1], 'jim\'');
|
||||
assert.equal(names[2], 'bob"');
|
||||
pg.end();
|
||||
}))
|
||||
})
|
||||
|
||||
test('nested array', function(){
|
||||
client.query("SELECT '{{1,joe},{2,bob}}'::text[] as names", assert.success(function(result) {
|
||||
var names = result.rows[0].names;
|
||||
assert.lengthIs(names, 2);
|
||||
|
||||
assert.lengthIs(names[0], 2);
|
||||
assert.equal(names[0][0], '1');
|
||||
assert.equal(names[0][1], 'joe');
|
||||
|
||||
assert.lengthIs(names[1], 2);
|
||||
assert.equal(names[1][0], '2');
|
||||
assert.equal(names[1][1], 'bob');
|
||||
|
||||
pg.end();
|
||||
}))
|
||||
})
|
||||
|
||||
test('integer array', function(){
|
||||
client.query("SELECT '{1,2,3}'::integer[] as names", assert.success(function(result) {
|
||||
var names = result.rows[0].names;
|
||||
assert.lengthIs(names, 3);
|
||||
assert.equal(names[0], 1);
|
||||
assert.equal(names[1], 2);
|
||||
assert.equal(names[2], 3);
|
||||
pg.end();
|
||||
}))
|
||||
})
|
||||
|
||||
test('integer nested array', function(){
|
||||
client.query("SELECT '{{1,100},{2,100},{3,100}}'::integer[] as names", assert.success(function(result) {
|
||||
var names = result.rows[0].names;
|
||||
assert.lengthIs(names, 3);
|
||||
assert.equal(names[0][0], 1);
|
||||
assert.equal(names[0][1], 100);
|
||||
|
||||
assert.equal(names[1][0], 2);
|
||||
assert.equal(names[1][1], 100);
|
||||
|
||||
assert.equal(names[2][0], 3);
|
||||
assert.equal(names[2][1], 100);
|
||||
pg.end();
|
||||
}))
|
||||
})
|
||||
|
||||
}))
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user