parse parameters in HashService

This commit is contained in:
Georg Ehrke 2016-12-21 17:07:25 +01:00
parent de7b48dd3d
commit c89b88df53
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43
2 changed files with 79 additions and 7 deletions

View File

@ -24,7 +24,7 @@ app.service('HashService', function ($location) {
const context = {
hashId: null,
parameters: null,
parameters: new Map(),
};
(function() {
@ -37,6 +37,10 @@ app.service('HashService', function ($location) {
if (hash.startsWith('#')) {
hash = hash.substr(1);
if (hash.startsWith('/')) {
hash = hash.substr(1);
}
}
// the hashes must comply with the following convention
@ -48,14 +52,18 @@ app.service('HashService', function ($location) {
// #subscribe_to_webcal?url=https%3A%2F%2Fwww.foo.bar%2F
//
// hashes without a question mark after the id will be ignored
if (!hash.includes('?')) {
return;
}
const questionMarkPosition = hash.indexOf('?');
context.hashId = hash.substr(0, questionMarkPosition);
context.parameters = hash.substr(questionMarkPosition + 1);
const parameters = hash.substr(questionMarkPosition + 1);
parameters.split('&').forEach((part) => {
const [key, value] = part.split('=');
context.parameters.set(key, decodeURIComponent(value));
});
}());
/**

View File

@ -112,13 +112,17 @@ describe('HashService', () => {
}));
it ('should return call registered callbacks', () => {
const callback1 = jasmine.createSpy();
let map;
const callback1 = jasmine.createSpy().and.callFake((p) => map = p);
const callback2 = jasmine.createSpy();
HashService.runIfApplicable('fancy_id', callback1);
HashService.runIfApplicable('another_fancy_id', callback2);
expect(callback1).toHaveBeenCalledWith('param1=value1&param2=value2');
expect(callback1).toHaveBeenCalledWith(jasmine.any(Map));
expect(map.size).toEqual(2);
expect(map.get('param1')).toEqual('value1');
expect(map.get('param2')).toEqual('value2');
expect(callback2).not.toHaveBeenCalled();
});
});
@ -136,13 +140,73 @@ describe('HashService', () => {
}));
it ('should handle hashes beginning with #', () => {
const callback1 = jasmine.createSpy();
let map;
const callback1 = jasmine.createSpy().and.callFake((p) => map = p);
const callback2 = jasmine.createSpy();
HashService.runIfApplicable('fancy_id', callback1);
HashService.runIfApplicable('another_fancy_id', callback2);
expect(callback1).toHaveBeenCalledWith('param1=value1&param2=value2');
expect(callback1).toHaveBeenCalledWith(jasmine.any(Map));
expect(map.size).toEqual(2);
expect(map.get('param1')).toEqual('value1');
expect(map.get('param2')).toEqual('value2');
expect(callback2).not.toHaveBeenCalled();
});
});
describe('', () => {
beforeEach(module('Calendar', function ($provide) {
$location = {};
$location.hash = jasmine.createSpy().and.returnValue('#fancy_id?param1=value1&param2=http%3A%2F%2Fwww.foobar.crazytld%2Fde-sk.ics');
$provide.value('$location', $location);
}));
beforeEach(inject(function (_HashService_) {
HashService = _HashService_;
}));
it ('should handle hashes beginning with #', () => {
let map;
const callback1 = jasmine.createSpy().and.callFake((p) => map = p);
const callback2 = jasmine.createSpy();
HashService.runIfApplicable('fancy_id', callback1);
HashService.runIfApplicable('another_fancy_id', callback2);
expect(callback1).toHaveBeenCalledWith(jasmine.any(Map));
expect(map.size).toEqual(2);
expect(map.get('param1')).toEqual('value1');
expect(map.get('param2')).toEqual('http://www.foobar.crazytld/de-sk.ics');
expect(callback2).not.toHaveBeenCalled();
});
});
describe('', () => {
beforeEach(module('Calendar', function ($provide) {
$location = {};
$location.hash = jasmine.createSpy().and.returnValue('#/fancy_id?param1=value1&param2=http%3A%2F%2Fwww.foobar.crazytld%2Fde-sk.ics');
$provide.value('$location', $location);
}));
beforeEach(inject(function (_HashService_) {
HashService = _HashService_;
}));
it ('should handle hashes beginning with #/', () => {
let map;
const callback1 = jasmine.createSpy().and.callFake((p) => map = p);
const callback2 = jasmine.createSpy();
HashService.runIfApplicable('fancy_id', callback1);
HashService.runIfApplicable('another_fancy_id', callback2);
expect(callback1).toHaveBeenCalledWith(jasmine.any(Map));
expect(map.size).toEqual(2);
expect(map.get('param1')).toEqual('value1');
expect(map.get('param2')).toEqual('http://www.foobar.crazytld/de-sk.ics');
expect(callback2).not.toHaveBeenCalled();
});
});