Differentiate between flags and empty keys in $location.hashSearch

* #foo?key=var&flag&emptyKey= should parse into
  {key:'val', flag: true, emptyKey: ''}
* added docs and spec for parseKeyValue function
This commit is contained in:
Igor Minar 2010-09-27 16:00:05 -07:00 committed by Misko Hevery
parent 984acdc627
commit eb8d46d380
3 changed files with 26 additions and 8 deletions

View file

@ -370,13 +370,17 @@ function compile(element, existingScope) {
} }
///////////////////////////////////////////////// /////////////////////////////////////////////////
function parseKeyValue(keyValue) { /**
* Parses an escaped url query string into key-value pairs.
* @return Object.<(string|boolean)>
*/
function parseKeyValue(/**string*/keyValue) {
var obj = {}, key_value, key; var obj = {}, key_value, key;
foreach((keyValue || "").split('&'), function(keyValue){ foreach((keyValue || "").split('&'), function(keyValue){
if (keyValue) { if (keyValue) {
key_value = keyValue.split('='); key_value = keyValue.split('=');
key = unescape(key_value[0]); key = unescape(key_value[0]);
obj[key] = key_value[1] ? unescape(key_value[1]) : true; obj[key] = isDefined(key_value[1]) ? unescape(key_value[1]) : true;
} }
}); });
return obj; return obj;

View file

@ -79,5 +79,17 @@ describe('equals', function(){
expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true); expect(equals({name:'misko'}, {name:'misko', $id:2})).toEqual(true);
expect(equals({name:'misko', $id:1}, {name:'misko'})).toEqual(true); expect(equals({name:'misko', $id:1}, {name:'misko'})).toEqual(true);
}); });
}); });
describe('parseKeyValue', function() {
it('should parse a string into key-value pairs', function() {
expect(parseKeyValue('')).toEqual({});
expect(parseKeyValue('simple=pair')).toEqual({simple: 'pair'});
expect(parseKeyValue('first=1&second=2')).toEqual({first: '1', second: '2'});
expect(parseKeyValue('escaped%20key=escaped%20value')).
toEqual({'escaped key': 'escaped value'});
expect(parseKeyValue('emptyKey=')).toEqual({emptyKey: ''});
expect(parseKeyValue('flag1&key=value&flag2')).
toEqual({flag1: true, key: 'value', flag2: true});
});
})

View file

@ -77,21 +77,23 @@ describe("service", function(){
describe("$location", function(){ describe("$location", function(){
it("should inject $location", function(){ it("should inject $location", function(){
scope.$location.parse('http://host:123/p/a/t/h.html?query=value#path?key=value'); scope.$location.parse('http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=');
expect(scope.$location.href).toEqual("http://host:123/p/a/t/h.html?query=value#path?key=value"); expect(scope.$location.href).
toEqual("http://host:123/p/a/t/h.html?query=value#path?key=value&flag&key2=");
expect(scope.$location.protocol).toEqual("http"); expect(scope.$location.protocol).toEqual("http");
expect(scope.$location.host).toEqual("host"); expect(scope.$location.host).toEqual("host");
expect(scope.$location.port).toEqual("123"); expect(scope.$location.port).toEqual("123");
expect(scope.$location.path).toEqual("/p/a/t/h.html"); expect(scope.$location.path).toEqual("/p/a/t/h.html");
expect(scope.$location.search).toEqual({query:'value'}); expect(scope.$location.search).toEqual({query:'value'});
expect(scope.$location.hash).toEqual('path?key=value'); expect(scope.$location.hash).toEqual('path?key=value&flag&key2=');
expect(scope.$location.hashPath).toEqual('path'); expect(scope.$location.hashPath).toEqual('path');
expect(scope.$location.hashSearch).toEqual({key:'value'}); expect(scope.$location.hashSearch).toEqual({key: 'value', flag: true, key2: ''});
scope.$location.hashPath = 'page=http://path'; scope.$location.hashPath = 'page=http://path';
scope.$location.hashSearch = {k:'a=b'}; scope.$location.hashSearch = {k:'a=b'};
expect(scope.$location.toString()).toEqual('http://host:123/p/a/t/h.html?query=value#page%3Dhttp%3A//path?k=a%3Db'); expect(scope.$location.toString()).
toEqual('http://host:123/p/a/t/h.html?query=value#page%3Dhttp%3A//path?k=a%3Db');
}); });
it('should parse file://', function(){ it('should parse file://', function(){