From 2c9e2a6703474b5f2712e7fc6ef68cbd6b7d7dbe Mon Sep 17 00:00:00 2001 From: Thorsten Basse Date: Sun, 29 Apr 2012 19:23:59 +0200 Subject: [PATCH 1/3] Adding JavaScript version --- tiny.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tiny.js diff --git a/tiny.js b/tiny.js new file mode 100644 index 0000000..3f2af5c --- /dev/null +++ b/tiny.js @@ -0,0 +1,84 @@ +/** + * Tiny.js + * + * A reversible base62 ID obfuscater + * + * Authors: + * Jacob DeHart (original PHP version) and Kyle Bragger (Ruby port) + * and Thorsten Basse (JavaScript port) + * + * Usage: + * obfuscated_id = Tiny.toTiny(123) + * original_id = Tiny.reverseTiny(obfuscated_id) + * + * Configuration: + * You must user Tiny.generateSet() to generate your own set + * Do *not* change this once you start using Tiny, as you won't be able to Tiny.reverseTiny() + * any values toTiny()'ed with another set. + * + */ + +(function() { + var root = this; + var set = 'iPKMFbnBdoSOWTthzrmUDGClAeQVsfRZwyguNEYcpqaIHvJkxXjL'; + var set = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + var warnDefaultSet = function() { + if( set === 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ) console.warn('Tiny.js character set is still default. You need to generate your own.'); + } + var range = function(low, high) { + var arr = []; + for( var i=low; i<=high; i++ ) { + arr.push(i); + } + }; + var Tiny = { + toTiny: function(id) { + warnDefaultSet(); + var hex_n = ''; + id = Math.floor(Math.abs(Number(id))); + var radix = set.length + while (true) { + var r = id % radix; + hex_n = set[r] + hex_n; + id = (id-r) / radix; + if( id === 0 ) break; + } + return hex_n; + }, + reverseTiny: function(string) { + warnDefaultSet(); + var radix = set.length; + var strlen = string.length; + var n = 0; + for( var i=0; i96) arr.push( String.fromCharCode(i) ); + } + arr = arr.concat(range(0, 9)); + arr.sort(function() {return 0.5 - Math.random()}) // shuffle array + return arr.join(''); + } + } + + if (typeof exports !== 'undefined') { + if (typeof module !== 'undefined' && module.exports) { + exports = module.exports = Tiny; + } + exports = Tiny; + } else if (typeof define === 'function' && define.amd) { + // Register as a named module with AMD. + define('Tiny', function() { + return Tiny; + }); + } else { + // Exported as a string, for Closure Compiler "advanced" mode. + root['Tiny'] = Tiny; + } + +})(); \ No newline at end of file From 2cf1e0a22859eccfbf7b01355846c27991d96f75 Mon Sep 17 00:00:00 2001 From: Thorsten Basse Date: Sun, 29 Apr 2012 19:33:45 +0200 Subject: [PATCH 2/3] fixed typo and removed a generated character set --- tiny.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tiny.js b/tiny.js index 3f2af5c..52f0a65 100644 --- a/tiny.js +++ b/tiny.js @@ -12,7 +12,7 @@ * original_id = Tiny.reverseTiny(obfuscated_id) * * Configuration: - * You must user Tiny.generateSet() to generate your own set + * You need to use Tiny.generateSet() to generate your own set * Do *not* change this once you start using Tiny, as you won't be able to Tiny.reverseTiny() * any values toTiny()'ed with another set. * @@ -20,7 +20,6 @@ (function() { var root = this; - var set = 'iPKMFbnBdoSOWTthzrmUDGClAeQVsfRZwyguNEYcpqaIHvJkxXjL'; var set = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; var warnDefaultSet = function() { if( set === 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ) console.warn('Tiny.js character set is still default. You need to generate your own.'); @@ -41,7 +40,9 @@ var r = id % radix; hex_n = set[r] + hex_n; id = (id-r) / radix; - if( id === 0 ) break; + if( id === 0 ) { + break; + } } return hex_n; }, @@ -61,7 +62,7 @@ if (i<91 || i>96) arr.push( String.fromCharCode(i) ); } arr = arr.concat(range(0, 9)); - arr.sort(function() {return 0.5 - Math.random()}) // shuffle array + arr.sort( function() { return 0.5 - Math.random(); } ) // shuffle array return arr.join(''); } } @@ -72,12 +73,10 @@ } exports = Tiny; } else if (typeof define === 'function' && define.amd) { - // Register as a named module with AMD. define('Tiny', function() { return Tiny; }); } else { - // Exported as a string, for Closure Compiler "advanced" mode. root['Tiny'] = Tiny; } From 989ed7f09b989e92278840e91851331554f03a14 Mon Sep 17 00:00:00 2001 From: Thorsten Basse Date: Sat, 19 May 2012 13:33:30 +0300 Subject: [PATCH 3/3] fixed a missing return oO --- tiny.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tiny.js b/tiny.js index 52f0a65..48cb1bf 100644 --- a/tiny.js +++ b/tiny.js @@ -28,7 +28,8 @@ var arr = []; for( var i=low; i<=high; i++ ) { arr.push(i); - } + }; + return arr; }; var Tiny = { toTiny: function(id) {