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 committed Nov 22, 2014
1 parent 764fa86 commit 39d94c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -1062,14 +1062,16 @@ function parseKeyValue(/**string*/keyValue) {
function toKeyValue(obj) {
var parts = [];
forEach(obj, function(value, key) {
if (isArray(value)) {
forEach(value, function(arrayValue) {
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) +
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
});
} else {
parts.push(encodeUriQuery(key, true) +
(value === true ? '' : '=' + encodeUriQuery(value, true)));
(value === true ? '' : '=' + encodeUriQuery(value, true)));
}
}
});
return parts.length ? parts.join('&') : '';
Expand Down
8 changes: 8 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ describe('angular', function() {
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');
});

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


Expand Down

0 comments on commit 39d94c7

Please sign in to comment.