Skip to content

Commit c41eedc

Browse files
committed
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.
1 parent 364cf4b commit c41eedc

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

lib/utils.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@ function arrayString(val) {
2222
if(i > 0) {
2323
result = result + ',';
2424
}
25-
if(val[i] instanceof Date) {
26-
result = result + JSON.stringify(val[i]);
27-
}
28-
else if(typeof val[i] === 'undefined') {
25+
if(val[i] === null || typeof val[i] === 'undefined') {
2926
result = result + 'NULL';
3027
}
3128
else if(Array.isArray(val[i])) {
3229
result = result + arrayString(val[i]);
3330
}
3431
else
3532
{
36-
result = result +
37-
(val[i] === null ? 'NULL' : JSON.stringify(val[i]));
33+
result = result + JSON.stringify(prepareValue(val[i]));
3834
}
3935
}
4036
result = result + '}';

test/unit/utils-tests.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,24 @@ test('prepareValue: string prepared properly', function() {
9595
assert.strictEqual(out, 'big bad wolf');
9696
});
9797

98-
test('prepareValue: array prepared properly', function() {
98+
test('prepareValue: simple array prepared properly', function() {
9999
var out = utils.prepareValue([1, null, 3, undefined, [5, 6, "squ,awk"]]);
100-
assert.strictEqual(out, '{1,NULL,3,NULL,{5,6,"squ,awk"}}');
100+
assert.strictEqual(out, '{"1",NULL,"3",NULL,{"5","6","squ,awk"}}');
101+
});
102+
103+
test('prepareValue: complex array prepared properly', function() {
104+
var out = utils.prepareValue([{ x: 42 }, { y: 84 }]);
105+
assert.strictEqual(out, '{"{\\"x\\":42}","{\\"y\\":84}"}');
106+
});
107+
108+
test('prepareValue: date array prepared properly', function() {
109+
helper.setTimezoneOffset(-330);
110+
111+
var date = new Date(2014, 1, 1, 11, 11, 1, 7);
112+
var out = utils.prepareValue([date]);
113+
assert.strictEqual(out, '{"2014-02-01T11:11:01.007+05:30"}');
114+
115+
helper.resetTimezoneOffset();
101116
});
102117

103118
test('prepareValue: arbitrary objects prepared properly', function() {

0 commit comments

Comments
 (0)