瀏覽代碼

Add UriService

Danilo Bargen 8 年之前
父節點
當前提交
1351d5608c
共有 6 個文件被更改,包括 83 次插入5 次删除
  1. 2 1
      karma.conf.js
  2. 2 0
      src/services.ts
  3. 36 0
      src/services/uri.ts
  4. 5 3
      tests/helpers.js
  5. 36 0
      tests/service/uri.js
  6. 2 1
      tests/testsuite.html

+ 2 - 1
karma.conf.js

@@ -7,8 +7,9 @@ module.exports = function(config) {
             'node_modules/angular-mocks/angular-mocks.js',
             'dist/app.js',
             'tests/filters.js',
-            'tests/service/qrcode.js',
             'tests/service/message.js',
+            'tests/service/qrcode.js',
+            'tests/service/uri.js',
             'tests/service/webclient.js',
             'tests/helpers.js',
         ],

+ 2 - 0
src/services.ts

@@ -32,6 +32,7 @@ import {SettingsService} from './services/settings';
 import {StateService} from './services/state';
 import {StringService} from './services/string';
 import {TitleService} from './services/title';
+import {UriService} from './services/uri';
 import {WebClientService} from './services/webclient';
 
 // Create services for the controller
@@ -56,4 +57,5 @@ angular.module('3ema.services', [])
 .service('StringService', StringService)
 .service('SettingsService', SettingsService)
 .service('MediaboxService', MediaboxService)
+.service('UriService', UriService)
 ;

+ 36 - 0
src/services/uri.ts

@@ -0,0 +1,36 @@
+/**
+ * This file is part of Threema Web.
+ *
+ * Threema Web is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+export class UriService {
+
+    /**
+     * Parse query parameters into an object.
+     *
+     * Based on http://stackoverflow.com/a/8649003/284318
+     */
+    public parseQueryParams(query: string) {
+        if (!(typeof query === 'string' || query instanceof String)) {
+            return null;
+        }
+        if (query.length == 0) {
+            return {};
+        }
+        const objStr = '{"' + decodeURI(query).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}';
+        return JSON.parse(objStr);
+    }
+
+}

+ 5 - 3
tests/helpers.js

@@ -5,16 +5,17 @@ describe('Helpers', function () {
     beforeAll(() => window.onbeforeunload = () => 'Ignoring page reload request');
 
     beforeEach(function () {
-            // Load 3ema.services
+        // Load 3ema.services
         module('3ema.services');
 
         // Inject the $filter function
-        inject(function (StringService) {
+        inject(function(StringService) {
             stringService = StringService;
         });
 
     });
-    describe('byteChunkSplit', function () {
+
+    describe('byteChunkSplit', function() {
         this.testPatterns = (cases, size, offset) => {
             for (let testcase of cases) {
                 const input = testcase[0];
@@ -48,4 +49,5 @@ describe('Helpers', function () {
             ], 20, 10);
         });
     });
+
 });

+ 36 - 0
tests/service/uri.js

@@ -0,0 +1,36 @@
+describe('UriService', function() {
+
+    let $service;
+
+    beforeAll(() => window.onbeforeunload = () => 'Ignoring page reload request');
+
+    beforeEach(function() {
+
+        module('3ema.services');
+
+        // Inject the service
+        inject(function(UriService) {
+            $service = UriService;
+        });
+
+    });
+
+    it('parses query parameters', () => {
+        const parsed = $service.parseQueryParams('foo=bar&baz=a%20b%20c');
+        expect(parsed).toEqual({
+            'foo': 'bar',
+            'baz': 'a b c',
+        });
+    });
+
+    it('parses empty query parameters', () => {
+        const parsed = $service.parseQueryParams('');
+        expect(parsed).toEqual({});
+    });
+
+    it('ignores invalid params', () => {
+        const parsed = $service.parseQueryParams(7);
+        expect(parsed).toEqual(null);
+    });
+
+});

+ 2 - 1
tests/testsuite.html

@@ -17,8 +17,9 @@
         <script src="../dist/app.js"></script>
 
         <script src="filters.js"></script>
-        <script src="service/qrcode.js"></script>
         <script src="service/message.js"></script>
+        <script src="service/qrcode.js"></script>
+        <script src="service/uri.js"></script>
         <script src="service/webclient.js"></script>
         <script src="helpers.js"></script>
     </head>