Skip to content

Instantly share code, notes, and snippets.

@meeDamian
Created June 13, 2013 10:40
Show Gist options
  • Save meeDamian/5772803 to your computer and use it in GitHub Desktop.
Save meeDamian/5772803 to your computer and use it in GitHub Desktop.
var Teleport = function() {};
Teleport._rixits = "0123456789"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "-_";
Teleport.numberToHash = function( n ) {
if( isNaN(Number(n)) || n===null || n===Number.POSITIVE_INFINITY || n<0 ) throw "The input is not valid";
n = Math.floor( n );
var result = '';
do result = this._rixits.charAt(n%64) + result;
while( n=Math.floor(n/64) );
return result; // String
};
Teleport.hashToNumber = function( h ) {
if(typeof h==="undefined") return false;
for(var i=0, result=0; i<h.length; i++) result = (result*64) + this._rixits.indexOf(h.charAt(i));
return result; // Integer
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment