Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 94bcbbe

Browse files
committedJan 30, 2016
cherry-pick 20d6e24 from 0.2.17
1 parent b14470f commit 94bcbbe

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed
 

‎src/params/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function ArrayType(type, mode) {
2222
// Wraps type (.is/.encode/.decode) functions to operate on each value of an array
2323
function arrayHandler(callback, allTruthyMode?: boolean) {
2424
return function handleArray(val) {
25+
if (isArray(val) && val.length === 0) return val;
2526
let arr = arrayWrap(val);
2627
let result = map(arr, callback);
2728
return (allTruthyMode === true) ? filter(result, x => !x).length === 0 : arrayUnwrap(result);

‎src/url/urlMatcher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export class UrlMatcher {
279279
let value: (any|any[]) = match[i + 1];
280280

281281
// if the param value matches a pre-replace pair, replace the value before decoding.
282-
for (let j = 0; j < param.replace; j++) {
282+
for (let j = 0; j < param.replace.length; j++) {
283283
if (param.replace[j].from === value) value = param.replace[j].to;
284284
}
285285
if (value && param.array === true) value = decodePathArray(value);
@@ -381,6 +381,7 @@ export class UrlMatcher {
381381
if (!isPathParam) {
382382
if (encoded == null || (isDefaultValue && squash !== false)) return;
383383
if (!isArray(encoded)) encoded = [<string> encoded];
384+
if (encoded.length === 0) return;
384385

385386
encoded = map(<string[]> encoded, encodeURIComponent).join(`&${param.id}=`);
386387
result += (search ? '&' : '?') + (`${param.id}=${encoded}`);

‎test/urlMatcherFactorySpec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ describe("UrlMatcher", function () {
297297
expect(m.exec($location.path(), $location.search())).toEqual( { param1: undefined } );
298298
$location.url("/foo?param1=bar");
299299
expect(m.exec($location.path(), $location.search())).toEqual( { param1: 'bar' } ); // auto unwrap
300+
$location.url("/foo?param1=");
301+
expect(m.exec($location.path(), $location.search())).toEqual( { param1: undefined } );
300302
$location.url("/foo?param1=bar&param1=baz");
301303
if (angular.isArray($location.search())) // conditional for angular 1.0.8
302304
expect(m.exec($location.path(), $location.search())).toEqual( { param1: ['bar', 'baz'] } );
@@ -359,6 +361,36 @@ describe("UrlMatcher", function () {
359361
expect(m.format({ "param1[]": ['bar', 'baz'] })).toBe("/foo?param1[]=bar&param1[]=baz");
360362
}));
361363

364+
// Test for issue #2222
365+
it("should return default value, if query param is missing.", inject(function($location) {
366+
var m = new UrlMatcher('/state?param1&param2&param3&param5', {
367+
params: {
368+
param1 : 'value1',
369+
param2 : {array: true, value: ['value2']},
370+
param3 : {array: true, value: []},
371+
param5 : {array: true, value: function() {return [];}}
372+
}
373+
});
374+
375+
var expected = {
376+
"param1": 'value1',
377+
"param2": ['value2'],
378+
"param3": [],
379+
"param5": []
380+
};
381+
382+
// Parse url to get Param.value()
383+
var parsed = m.exec("/state");
384+
expect(parsed).toEqualData(expected);
385+
386+
// Pass again through Param.value() for normalization (like transitionTo)
387+
var paramDefs = m.parameters();
388+
var values = common.map(parsed, function(val, key) {
389+
return common.find(paramDefs, function(def) { return def.id === key }).value(val);
390+
});
391+
expect(values).toEqualData(expected);
392+
}));
393+
362394
it("should not be wrapped by ui-router into an array if array: false", inject(function($location) {
363395
var m = new UrlMatcher('/foo?param1', { params: { param1: { array: false } } });
364396

0 commit comments

Comments
 (0)
Please sign in to comment.