properly prepare complex arrays

`arrayString` duplicated too much of `prepareValue`'s logic, and so
didn't receive bugfixes for handling dates with timestamps. Defer to
`prepareValue` whenever possible.

This change enforces double-quote escaping of all array elements,
regardless of whether escaping is necessary. This has the side-effect of
properly escaping JSON arrays.
This commit is contained in:
Nikhil Benesch 2014-03-30 18:07:03 -04:00
parent 364cf4b3ca
commit c41eedc3e0
2 changed files with 19 additions and 8 deletions

View File

@ -22,10 +22,7 @@ function arrayString(val) {
if(i > 0) {
result = result + ',';
}
if(val[i] instanceof Date) {
result = result + JSON.stringify(val[i]);
}
else if(typeof val[i] === 'undefined') {
if(val[i] === null || typeof val[i] === 'undefined') {
result = result + 'NULL';
}
else if(Array.isArray(val[i])) {
@ -33,8 +30,7 @@ function arrayString(val) {
}
else
{
result = result +
(val[i] === null ? 'NULL' : JSON.stringify(val[i]));
result = result + JSON.stringify(prepareValue(val[i]));
}
}
result = result + '}';

View File

@ -95,9 +95,24 @@ test('prepareValue: string prepared properly', function() {
assert.strictEqual(out, 'big bad wolf');
});
test('prepareValue: array prepared properly', function() {
test('prepareValue: simple array prepared properly', function() {
var out = utils.prepareValue([1, null, 3, undefined, [5, 6, "squ,awk"]]);
assert.strictEqual(out, '{1,NULL,3,NULL,{5,6,"squ,awk"}}');
assert.strictEqual(out, '{"1",NULL,"3",NULL,{"5","6","squ,awk"}}');
});
test('prepareValue: complex array prepared properly', function() {
var out = utils.prepareValue([{ x: 42 }, { y: 84 }]);
assert.strictEqual(out, '{"{\\"x\\":42}","{\\"y\\":84}"}');
});
test('prepareValue: date array prepared properly', function() {
helper.setTimezoneOffset(-330);
var date = new Date(2014, 1, 1, 11, 11, 1, 7);
var out = utils.prepareValue([date]);
assert.strictEqual(out, '{"2014-02-01T11:11:01.007+05:30"}');
helper.resetTimezoneOffset();
});
test('prepareValue: arbitrary objects prepared properly', function() {