Skip to content

Commit 8939d05

Browse files
committed
feat($state): allow parameters to pass unharmed
[BREAK] This is a breaking change: state parameters are no longer automatically coerced to strings, and unspecified parameter values are now set to undefined rather than null.
1 parent b7f074f commit 8939d05

File tree

4 files changed

+22
-39
lines changed

4 files changed

+22
-39
lines changed

src/common.js

-17
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,6 @@ function inheritParams(currentParams, newParams, $current, $to) {
108108
return extend({}, inherited, newParams);
109109
}
110110

111-
/**
112-
* Normalizes a set of values to string or `null`, filtering them by a list of keys.
113-
*
114-
* @param {Array} keys The list of keys to normalize/return.
115-
* @param {Object} values An object hash of values to normalize.
116-
* @return {Object} Returns an object hash of normalized string values.
117-
*/
118-
function normalize(keys, values) {
119-
var normalized = {};
120-
121-
forEach(keys, function (name) {
122-
var value = values[name];
123-
normalized[name] = (value != null) ? String(value) : null;
124-
});
125-
return normalized;
126-
}
127-
128111
/**
129112
* Performs a non-strict comparison of the subset of two objects, defined by a list of keys.
130113
*

src/state.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
814814
return $q.when($state.current);
815815
}
816816

817-
// Normalize/filter parameters before we pass them to event handlers etc.
818-
toParams = normalize(to.params, toParams || {});
817+
// Filter parameters before we pass them to event handlers etc.
818+
toParams = filterByKeys(to.params, toParams || {});
819819

820820
// Broadcast start event and cancel the transition if requested
821821
if (options.notify) {
@@ -1102,7 +1102,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
11021102
if (!nav || !nav.url) {
11031103
return null;
11041104
}
1105-
return $urlRouter.href(nav.url, normalize(state.params, params || {}), { absolute: options.absolute });
1105+
return $urlRouter.href(nav.url, filterByKeys(state.params, params || {}), { absolute: options.absolute });
11061106
};
11071107

11081108
/**

test/stateDirectivesSpec.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ describe('uiStateRef', function() {
125125
$q.flush();
126126

127127
expect($state.current.name).toEqual('contacts.item.detail');
128-
expect($stateParams).toEqual({ id: "5" });
128+
expect($stateParams).toEqual({ id: 5 });
129129
}));
130130

131131
it('should transition when given a click that contains no data (fake-click)', inject(function($state, $stateParams, $document, $q) {
@@ -142,7 +142,7 @@ describe('uiStateRef', function() {
142142
$q.flush();
143143

144144
expect($state.current.name).toEqual('contacts.item.detail');
145-
expect($stateParams).toEqual({ id: "5" });
145+
expect($stateParams).toEqual({ id: 5 });
146146
}));
147147

148148
it('should not transition states when ctrl-clicked', inject(function($state, $stateParams, $document, $q) {
@@ -153,7 +153,7 @@ describe('uiStateRef', function() {
153153
$q.flush();
154154

155155
expect($state.current.name).toEqual('');
156-
expect($stateParams).toEqual({ id: "5" });
156+
expect($stateParams).toEqual({ id: 5 });
157157
}));
158158

159159
it('should not transition states when meta-clicked', inject(function($state, $stateParams, $document, $q) {
@@ -164,7 +164,7 @@ describe('uiStateRef', function() {
164164
$q.flush();
165165

166166
expect($state.current.name).toEqual('');
167-
expect($stateParams).toEqual({ id: "5" });
167+
expect($stateParams).toEqual({ id: 5 });
168168
}));
169169

170170
it('should not transition states when shift-clicked', inject(function($state, $stateParams, $document, $q) {
@@ -175,7 +175,7 @@ describe('uiStateRef', function() {
175175
$q.flush();
176176

177177
expect($state.current.name).toEqual('');
178-
expect($stateParams).toEqual({ id: "5" });
178+
expect($stateParams).toEqual({ id: 5 });
179179
}));
180180

181181
it('should not transition states when middle-clicked', inject(function($state, $stateParams, $document, $q) {
@@ -186,7 +186,7 @@ describe('uiStateRef', function() {
186186
$q.flush();
187187

188188
expect($state.current.name).toEqual('');
189-
expect($stateParams).toEqual({ id: "5" });
189+
expect($stateParams).toEqual({ id: 5 });
190190
}));
191191

192192
it('should not transition states when element has target specified', inject(function($state, $stateParams, $document, $q) {
@@ -198,7 +198,7 @@ describe('uiStateRef', function() {
198198
$q.flush();
199199

200200
expect($state.current.name).toEqual('');
201-
expect($stateParams).toEqual({ id: "5" });
201+
expect($stateParams).toEqual({ id: 5 });
202202
}));
203203

204204
it('should not transition states if preventDefault() is called in click handler', inject(function($state, $stateParams, $document, $q) {
@@ -212,7 +212,7 @@ describe('uiStateRef', function() {
212212
$q.flush();
213213

214214
expect($state.current.name).toEqual('');
215-
expect($stateParams).toEqual({ id: "5" });
215+
expect($stateParams).toEqual({ id: 5 });
216216
}));
217217

218218
it('should allow passing params to current state', inject(function($compile, $rootScope, $state) {
@@ -277,7 +277,7 @@ describe('uiStateRef', function() {
277277
$q.flush();
278278

279279
expect($state.$current.name).toBe("contacts.item.detail");
280-
expect($state.params).toEqual({ id: '5' });
280+
expect($state.params).toEqual({ id: 5 });
281281
}));
282282

283283
it('should resolve states from parent uiView', inject(function ($state, $stateParams, $q, $timeout) {

test/stateSpec.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ describe('state', function () {
265265
$q.flush();
266266
expect(called).toBeTruthy();
267267
expect($state.current.name).toEqual('DDD');
268-
expect($state.params).toEqual({ x: '1', y: '2', z: '3', w: '4' });
268+
expect($state.params).toEqual({ x: 1, y: 2, z: 3, w: 4 });
269269
}));
270270

271271
it('can defer a state transition in $stateNotFound', inject(function ($state, $q, $rootScope) {
@@ -282,7 +282,7 @@ describe('state', function () {
282282
$q.flush();
283283
expect(called).toBeTruthy();
284284
expect($state.current.name).toEqual('AA');
285-
expect($state.params).toEqual({ a: '1' });
285+
expect($state.params).toEqual({ a: 1 });
286286
}));
287287

288288
it('can defer and supersede a state transition in $stateNotFound', inject(function ($state, $q, $rootScope) {
@@ -475,7 +475,7 @@ describe('state', function () {
475475
$q.flush();
476476

477477
expect($state.$current.name).toBe('about.person.item');
478-
expect($stateParams).toEqual({ person: 'bob', id: '5' });
478+
expect($stateParams).toEqual({ person: 'bob', id: 5 });
479479

480480
$state.go('^.^.sidebar');
481481
$q.flush();
@@ -603,7 +603,7 @@ describe('state', function () {
603603

604604
it('contains the parameter values for the current state', inject(function ($state, $q) {
605605
initStateTo(D, { x: 'x value', z: 'invalid value' });
606-
expect($state.params).toEqual({ x: 'x value', y: null });
606+
expect($state.params).toEqual({ x: 'x value', y: undefined });
607607
}));
608608
});
609609

@@ -878,24 +878,24 @@ describe('state', function () {
878878

879879
describe('substate and stateParams inheritance', function() {
880880
it('should inherit the parent param', inject(function ($state, $stateParams, $q) {
881-
initStateTo($state.get('root'), {param1: 1});
882-
$state.go('root.sub1', {param2: 2});
881+
initStateTo($state.get('root'), { param1: 1 });
882+
$state.go('root.sub1', { param2: 2 });
883883
$q.flush();
884884
expect($state.current.name).toEqual('root.sub1');
885-
expect($stateParams).toEqual({param1: '1', param2: '2'});
885+
expect($stateParams).toEqual({ param1: 1, param2: 2 });
886886
}));
887887

888888
it('should not inherit siblings\' states', inject(function ($state, $stateParams, $q) {
889-
initStateTo($state.get('root'), {param1: 1});
890-
$state.go('root.sub1', {param2: 2});
889+
initStateTo($state.get('root'), { param1: 1 });
890+
$state.go('root.sub1', { param2: 2 });
891891
$q.flush();
892892
expect($state.current.name).toEqual('root.sub1');
893893

894894
$state.go('root.sub2');
895895
$q.flush();
896896
expect($state.current.name).toEqual('root.sub2');
897897

898-
expect($stateParams).toEqual({param1: '1', param2: null});
898+
expect($stateParams).toEqual({ param1: 1, param2: undefined });
899899
}));
900900
});
901901

0 commit comments

Comments
 (0)