From 39d94c78d371d12b3f25fc51ac3e1f3b48aa8471 Mon Sep 17 00:00:00 2001 From: Josh Kurz Date: Thu, 3 Apr 2014 00:59:21 -0400 Subject: [PATCH] fix(Angular.js): toKeyValue is not serializing null values Signed-off-by: Josh Kurz --- src/Angular.js | 16 +++++++++------- test/AngularSpec.js | 8 ++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index aab712d3e1ee..46e146dcccbc 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -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('&') : ''; diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 5ab87771eb58..46eac686c8ec 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -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'); + }); });