Skip to content

Instantly share code, notes, and snippets.

@atk
Forked from 140bytes/LICENSE.txt
Created September 22, 2011 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atk/1234999 to your computer and use it in GitHub Desktop.
Save atk/1234999 to your computer and use it in GitHub Desktop.
reverse hashing

Reverse Hashing

Hashing is a technique to save space in bigger scripts by e.g. replacing "querySelectorAll" in document and all those other lengthy camelCase methods with shorter names like "qSA" . Reverse hashing just tries to find the method or property by their shorter name:

r(document,'qSA','div') // should return the result of document.querySelectorAll('div') r(this,'iW') // returns this(=window).innerWidth

Warning: the first found match for the short name will be used, which may lead to errors on multiple matches.

function(
o, // object
h, // hash name (e.g. "qSA" for "querySelectorAll")
i, // property name placeholder
a // arguments placeholder
){
// prepare arguments
a=[].slice.call(arguments, 2);
// search all properties/methods
for (i in o)
// if the shortened property/method name matches the hash name
if (h == i.replace(/(^.|[A-Z])[a-z]*/g, "$1"))
// return the result of the call if arguments present or otherwise the property
return a[0] ? o[i].apply(o,a) : o[i]
}
function(o,h,i,a){a=[].slice.call(arguments,2);for(i in o)if(h==i.replace(/(^.|[A-Z])[a-z]*/g,"$1"))return a[0]?o[i].apply(o,a):o[i]}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Alex Kloss <alexthkloss@web.de>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "reverse hashing",
"description": "finding properties and methods by short hash names",
"keywords": [
"hash",
"method",
"properties",
"shorten",
"call"
]
}
<!DOCTYPE html>
<title>Reverse Hashing works</title>
<div>Expected value: <b>Reverse Hashing works</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var myFunction = function(o,h,i,a){a=[].slice.call(arguments,2);for(i in o)if(h==i.replace(/(^.|[A-Z])[a-z]*/g,"$1"))return a[0]?o[i].apply(o,a):o[i]}
myFunction(document,'gEBI','ret').innerHTML = myFunction(document,'t')
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment