Improve hash functionality

This commit is contained in:
Borys_Levytskyi
2021-01-14 15:14:35 +02:00
parent 027d052099
commit 49e5265985
3 changed files with 24 additions and 14 deletions

19
src/core/hash.test.ts Normal file
View File

@@ -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"]);
});
});

View File

@@ -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);
}

View File

@@ -53,6 +53,8 @@ function executeStartupCommands() {
startupCommands = hashArgs;
}
log.debug('Executing startup commands', startupCommands);
startupCommands.forEach(cmd.execute.bind(cmd));
}