From 49e5265985b8561859d1af498b8144830ae372fd Mon Sep 17 00:00:00 2001 From: Borys_Levytskyi Date: Thu, 14 Jan 2021 15:14:35 +0200 Subject: [PATCH] Improve hash functionality --- src/core/hash.test.ts | 19 +++++++++++++++++++ src/core/hash.ts | 17 +++-------------- src/index.tsx | 2 ++ 3 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 src/core/hash.test.ts diff --git a/src/core/hash.test.ts b/src/core/hash.test.ts new file mode 100644 index 0000000..7feea7b --- /dev/null +++ b/src/core/hash.test.ts @@ -0,0 +1,19 @@ +import hash from './hash'; + +describe('hash tests', () => { + + it('can decode URL', () => { + const actual = hash.decodeHash('#4.3.2.1%2F8'); + expect(actual).toBe('4.3.2.1/8'); + }); + + it('can get hash from encoded url', () => { + const actual = hash.getArgs('#-notrack%7C%7C17%2015%7C%7C16&15'); + expect(actual).toMatchObject(["-notrack", "17 15", "16&15"]); + }); + + it('can get hash from unencoded url', () => { + const actual = hash.getArgs('#1 2|127.0.0.|1&2|192.168.1.1'); + expect(actual).toMatchObject(["1 2|127.0.0.|1&2|192.168.1.1"]); + }); +}); \ No newline at end of file diff --git a/src/core/hash.ts b/src/core/hash.ts index 6d4415d..5624ebe 100644 --- a/src/core/hash.ts +++ b/src/core/hash.ts @@ -1,9 +1,9 @@ export default { encodeHash: function(input:string):string { - return encodeURI(input.trim().replace(/\s/g,',')); + return encodeURIComponent(input.trim().replace(/\s/g,',')); }, decodeHash: function(hashValue:string):string { - return decodeURI(hashValue).replace(/^\#/, '').replace(/,/g,' '); + return decodeURIComponent(hashValue.replace(/^\#/, '')).replace(/,/g,' '); }, getArgs: function (hashValue:string) : string[] { @@ -19,17 +19,6 @@ export default { }; function splitHashList(str: string) : string[] { - var values = []; - if(str.indexOf('||')) { - str.split('||').forEach(function (v) { - if (v.length > 0) { - values.push(v); - } - }); - } else { - values.push(str); - } - - return values; + return str.split('||').filter(s => s.length > 0); } \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index 620ec3d..0c11900 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -53,6 +53,8 @@ function executeStartupCommands() { startupCommands = hashArgs; } + log.debug('Executing startup commands', startupCommands); + startupCommands.forEach(cmd.execute.bind(cmd)); }