Skip to content

Instantly share code, notes, and snippets.

@gansbrest
Last active August 29, 2015 14:08
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 gansbrest/db318d6145532d47b240 to your computer and use it in GitHub Desktop.
Save gansbrest/db318d6145532d47b240 to your computer and use it in GitHub Desktop.
Uglify uri
var strtr = function(str, from, to) {
// discuss at: http://phpjs.org/functions/strtr/
// original by: Brett Zamir (http://brett-zamir.me)
// input by: uestla
// input by: Alan C
// input by: Taras Bogach
// input by: jpfle
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// bugfixed by: Brett Zamir (http://brett-zamir.me)
// bugfixed by: Brett Zamir (http://brett-zamir.me)
// depends on: krsort
// depends on: ini_set
// example 1: $trans = {'hello' : 'hi', 'hi' : 'hello'};
// example 1: strtr('hi all, I said hello', $trans)
// returns 1: 'hello all, I said hi'
// example 2: strtr('äaabaåccasdeöoo', 'äåö','aao');
// returns 2: 'aaabaaccasdeooo'
// example 3: strtr('ääääääää', 'ä', 'a');
// returns 3: 'aaaaaaaa'
// example 4: strtr('http', 'pthxyz','xyzpth');
// returns 4: 'zyyx'
// example 5: strtr('zyyx', 'pthxyz','xyzpth');
// returns 5: 'http'
// example 6: strtr('aa', {'a':1,'aa':2});
// returns 6: '2'
var fr = '',
i = 0,
j = 0,
lenStr = 0,
lenFrom = 0,
tmpStrictForIn = false,
fromTypeStr = '',
toTypeStr = '',
istr = '';
var tmpFrom = [];
var tmpTo = [];
var ret = '';
var match = false;
// Received replace_pairs?
// Convert to normal from->to chars
if (typeof from === 'object') {
// Not thread-safe; temporarily set to true
tmpStrictForIn = this.ini_set('phpjs.strictForIn', false);
from = this.krsort(from);
this.ini_set('phpjs.strictForIn', tmpStrictForIn);
for (fr in from) {
if (from.hasOwnProperty(fr)) {
tmpFrom.push(fr);
tmpTo.push(from[fr]);
}
}
from = tmpFrom;
to = tmpTo;
}
// Walk through subject and replace chars when needed
lenStr = str.length;
lenFrom = from.length;
fromTypeStr = typeof from === 'string';
toTypeStr = typeof to === 'string';
for (i = 0; i < lenStr; i++) {
match = false;
if (fromTypeStr) {
istr = str.charAt(i);
for (j = 0; j < lenFrom; j++) {
if (istr == from.charAt(j)) {
match = true;
break;
}
}
} else {
for (j = 0; j < lenFrom; j++) {
if (str.substr(i, from[j].length) == from[j]) {
match = true;
// Fast forward
i = (i + from[j].length) - 1;
break;
}
}
}
if (match) {
ret += toTypeStr ? to.charAt(j) : to[j];
} else {
ret += str.charAt(i);
}
}
return ret;
};
var rotUrl = function($url) {
return strtr($url,
'./-:?=&%# ZQXJKVWPY abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHILMNORSTU',
'ZQXJKVWPY ./-:?=&%# 123456789ABCDEFGHILMNORSTUabcdefghijklmnopqrstuvwxyz');
};
var derotUrl = function($url) {
return strtr($url,
'ZQXJKVWPY ./-:?=&%# 123456789ABCDEFGHILMNORSTUabcdefghijklmnopqrstuvwxyz',
'./-:?=&%# ZQXJKVWPY abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHILMNORSTU');
};
var rotted = rotUrl("catId=21&depId=27;17&catId=27");
console.log(rotted);
var derotted = derotUrl("31Mr4VbaW45Gr4Vbg;ag");
console.log(derotted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment