Created
October 25, 2017 15:44
-
-
Save Swimburger/e7e05c2132e1e6e593e44fa803fbe7ac to your computer and use it in GitHub Desktop.
Small hash parser and generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path1 = "/slfkj/sadfkl#cat=5,id=dfsklsdkajf"; | |
var path2 = "/slfkj/sadfkl#cat=a,id=dAS"; | |
var path3 = "/slfkj/sadfkl#cat=asf,id=BL"; | |
function getHashValues(path){ | |
var result = {}; | |
var hash = path.slice(path.indexOf('#') + 1); | |
var keyValues = hash.split(','); | |
for(var i = 0; i < keyValues.length; i++){ | |
var keyValue = keyValues[i]; | |
var sep = keyValue.indexOf('='); | |
var key = keyValue.slice(0, sep); | |
var value = keyValue.slice(sep + 1); | |
result[key] = value; | |
} | |
return result; | |
} | |
function createHashString(obj){ | |
var hash = ''; | |
for(var key in obj){ | |
hash += key + '=' + obj[key] + ','; | |
} | |
hash = hash.slice(0, -1); | |
return hash; | |
} | |
console.log(getHashValues(path1)); | |
console.log(getHashValues(path2)); | |
console.log(getHashValues(path3)); | |
var obj1 = {cat: 1, id: 'akllkd'}; | |
var obj2 = {cat: 'asdf', id: 'a'}; | |
var obj3 = {cat: 555, id: 'asd'}; | |
console.log(createHashString(obj1)); | |
console.log(createHashString(obj2)); | |
console.log(createHashString(obj3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment