From 93cd8f380ff8ccf1c1cc094e481b9ad1bbd2b996 Mon Sep 17 00:00:00 2001 From: Rory Fitzpatrick Date: Thu, 19 Sep 2013 15:00:25 +0100 Subject: [PATCH] Add optional parameter matching to .is and .includes --- src/state.js | 31 +++++++++++++++++++++++++++---- test/stateSpec.js | 23 ++++++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/state.js b/src/state.js index beb9f8f05..464771c6a 100644 --- a/src/state.js +++ b/src/state.js @@ -377,14 +377,37 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $ return transition; }; - $state.is = function is(stateOrName) { + $state.is = function is(stateOrName, params) { var state = findState(stateOrName); - return (isDefined(state)) ? $state.$current === state : undefined; + + if (!isDefined(state)) { + return undefined; + } + + if ($state.$current !== state) { + return false; + } + + return isDefined(params) ? angular.equals($stateParams, params) : true; }; - $state.includes = function includes(stateOrName) { + $state.includes = function includes(stateOrName, params) { var state = findState(stateOrName); - return (isDefined(state)) ? isDefined($state.$current.includes[state.name]) : undefined; + if (!isDefined(state)) { + return undefined; + } + + if (!isDefined($state.$current.includes[state.name])) { + return false; + } + + var validParams = true; + angular.forEach(params, function(value, key) { + if (!isDefined($stateParams[key]) || $stateParams[key] !== value) { + validParams = false; + } + }); + return validParams; }; $state.href = function href(stateOrName, params, options) { diff --git a/test/stateSpec.js b/test/stateSpec.js index 605a260f9..33f0a3a63 100644 --- a/test/stateSpec.js +++ b/test/stateSpec.js @@ -393,6 +393,13 @@ describe('state', function () { it('should return undefined when queried state does not exist', inject(function ($state) { expect($state.is('Z')).toBeUndefined(); })); + + it('should return true when the current state is passed with matching parameters', inject(function ($state, $q) { + $state.transitionTo(D, {x: 'foo', y: 'bar'}); $q.flush(); + expect($state.is(D, {x: 'foo', y: 'bar'})).toBe(true); + expect($state.is('D', {x: 'foo', y: 'bar'})).toBe(true); + expect($state.is(D, {x: 'bar', y: 'foo'})).toBe(false); + })); }); describe('.includes()', function () { @@ -411,7 +418,21 @@ describe('state', function () { })); it('should return undefined when queried state does not exist', inject(function ($state) { - expect($state.is('Z')).toBeUndefined(); + expect($state.includes('Z')).toBeUndefined(); + })); + + it('should return true when the current state is passed with partial matching parameters', inject(function ($state, $q) { + $state.transitionTo(D, {x: 'foo', y: 'bar'}); $q.flush(); + expect($state.includes(D, {x: 'foo'})).toBe(true); + expect($state.includes(D, {y: 'bar'})).toBe(true); + expect($state.includes('D', {x: 'foo'})).toBe(true); + expect($state.includes(D, {y: 'foo'})).toBe(false); + })); + + it('should return true when the current state is passed with partial matching parameters from state\'s parent', inject(function ($state, $q) { + $state.transitionTo('about.person.item', {person: 'bob', id: 5}); $q.flush(); + expect($state.includes('about.person', {person: 'bob'})).toBe(true); + expect($state.includes('about.person', {person: 'steve'})).toBe(false); })); });