Skip to content

Commit 62fd254

Browse files
deepshekhardasljharb
andcommitted
[Fix] stringify: serialize Date values when a filter is provided
[Tests] `stringify`: pin default Date handling through a `filter` function Cover a nested Date, `serializeDate` after a filter, a Date inside an `arrayFormat: 'comma'` array (previously stringified as locale text), a Date produced by the filter, the array-filter form, and an invalid Date throwing like the no-filter path. Also wrap the over-long lines. Co-authored-by: deepshekhardas <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
1 parent 8859c37 commit 62fd254

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎lib/stringify.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ var stringify = function stringify(
110110
}
111111
}
112112

113-
if (typeof filter === 'function') {
114-
obj = filter(prefix, obj);
115-
} else if (obj instanceof Date) {
113+
obj = typeof filter === 'function' ? filter(prefix, obj) : obj;
114+
115+
if (obj instanceof Date) {
116116
obj = serializeDate(obj);
117117
} else if (generateArrayPrefix === 'comma' && isArray(obj)) {
118118
obj = utils.maybeMap(obj, function (value) {

‎test/stringify.js‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,66 @@ test('stringify()', function (t) {
11171117
st.end();
11181118
});
11191119

1120+
t.test('serializes Date values unless filter transforms them', function (st) {
1121+
var obj = {
1122+
a: new Date(Date.UTC(2023, 2, 7, 9, 2, 41, 260)),
1123+
b: [new Date(Date.UTC(2023, 2, 7, 9, 2, 41, 260))]
1124+
};
1125+
var iso = '2023-03-07T09%3A02%3A41.260Z';
1126+
st.equal(
1127+
qs.stringify(obj, { filter: function (prefix, value) { return value; } }),
1128+
'a=' + iso + '&b%5B0%5D=' + iso,
1129+
'Dates returned unchanged by a filter are serialized'
1130+
);
1131+
st.equal(
1132+
qs.stringify({ a: new Date(0) }, { filter: function (prefix, value) { return value instanceof Date ? 'kept' : value; } }),
1133+
'a=kept',
1134+
'a filter that transforms a Date wins over default serialization'
1135+
);
1136+
st.end();
1137+
});
1138+
1139+
t.test('applies default Date handling to whatever a filter function returns', function (st) {
1140+
var date = new Date(Date.UTC(2023, 2, 7, 9, 2, 41, 260));
1141+
var iso = '2023-03-07T09%3A02%3A41.260Z';
1142+
var identity = function (prefix, value) { return value; };
1143+
1144+
st.equal(
1145+
qs.stringify({ a: { b: date } }, { filter: identity }),
1146+
'a%5Bb%5D=' + iso,
1147+
'a nested Date is serialized through a filter'
1148+
);
1149+
st.equal(
1150+
qs.stringify(
1151+
{ a: date },
1152+
{ filter: identity, serializeDate: function (d) { return String(d.getTime()); } }
1153+
),
1154+
'a=1678179761260',
1155+
'`serializeDate` is honored after a filter'
1156+
);
1157+
st.equal(
1158+
qs.stringify({ a: [date, 'x'] }, { filter: identity, arrayFormat: 'comma' }),
1159+
'a=' + iso + '%2Cx',
1160+
'Dates inside a comma array are serialized through a filter, not stringified as locale text'
1161+
);
1162+
st.equal(
1163+
qs.stringify({ a: 'x' }, { filter: function (prefix, value) { return prefix === 'a' ? date : value; } }),
1164+
'a=' + iso,
1165+
'a Date produced by the filter is serialized'
1166+
);
1167+
st.equal(
1168+
qs.stringify({ a: date, b: 1 }, { filter: ['a'] }),
1169+
'a=' + iso,
1170+
'an array filter still serializes Dates'
1171+
);
1172+
st['throws'](
1173+
function () { qs.stringify({ a: new Date(NaN) }, { filter: identity }); },
1174+
RangeError,
1175+
'an invalid Date through a filter throws like the no-filter path'
1176+
);
1177+
st.end();
1178+
});
1179+
11201180
t.test('can disable uri encoding', function (st) {
11211181
st.equal(qs.stringify({ a: 'b' }, { encode: false }), 'a=b');
11221182
st.equal(qs.stringify({ a: { b: 'c' } }, { encode: false }), 'a[b]=c');

0 commit comments

Comments
 (0)