Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(Angular.js): toKeyValue is not serializing null values
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Kurz <jkurz25@gmail.com>
  • Loading branch information
jkurz authored and joshkurz committed Apr 3, 2014
1 parent f9eb324 commit 0c5e1dd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,14 +1085,16 @@ function parseKeyValue(/**string*/keyValue) {
function toKeyValue(obj) {
var parts = [];
forEach(obj, function(value, key) {
if (isArray(value)) {
forEach(value, function(arrayValue) {
parts.push(encodeUriQuery(key, true) +
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
});
} else {
parts.push(encodeUriQuery(key, true) +
(value === true ? '' : '=' + encodeUriQuery(value, true)));
if(value !== null){
if (isArray(value)) {
forEach(value, function(arrayValue) {
parts.push(encodeUriQuery(key, true) +
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
});
} else {
parts.push(encodeUriQuery(key, true) +
(value === true ? '' : '=' + encodeUriQuery(value, true)));
}
}
});
return parts.length ? parts.join('&') : '';
Expand Down
6 changes: 5 additions & 1 deletion test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,11 @@ describe('angular', function() {
expect(toKeyValue({key: [323,'value',true]})).toEqual('key=323&key=value&key');
expect(toKeyValue({key: [323,'value',true, 1234]})).
toEqual('key=323&key=value&key&key=1234');
});
});

it('should not serialize null values', function() {
expect(toKeyValue({nullKey: null, key: 'value'})).toEqual('key=value');
});
});


Expand Down

0 comments on commit 0c5e1dd

Please sign in to comment.