Added str_trans function

This commit is contained in:
Timothy Warren 2011-10-24 19:18:22 -04:00
parent 21ffcb19c9
commit ab99f79b2c
5 changed files with 582 additions and 58 deletions

View File

@ -8,11 +8,13 @@
//The name of the source folder
$folder = "src";
$src_folder = "{$folder}/modules";
$core_folder = "{$folder}/core";
$files = array();
//Get all the source files
if($dir = opendir($folder))
if($dir = opendir($src_folder))
{
while(($file = readdir($dir)) !== FALSE)
{
@ -26,37 +28,20 @@ if($dir = opendir($folder))
closedir($dir);
}
//Define files that aren't modules
$special_files = array(
'core.js',
);
//Filter out special files
$src_files = array_diff($files, $special_files);
//Start with the core
$new_file = file_get_contents($folder."/core.js") . "\n";
$new_file = file_get_contents($core_folder."/core.js") . "\n";
//Add the opening of the function for the modules
$new_file .= "\n// --------------------------------------------------------------------------\n\n";
//Add the modules
foreach($src_files as $f)
foreach($files as $f)
{
$farray = file($folder."/".$f, FILE_IGNORE_NEW_LINES);
$farray = file($src_folder."/".$f, FILE_IGNORE_NEW_LINES);
$flen = count($farray);
//Indent each module 1 tab, for neatness
for($i=0;$i<$flen;$i++)
{
if($farray[$i] == ""){ continue; }
$farray[$i] = "\t".$farray[$i];
}
$module = implode("\n", $farray);
$new_file .= "\n\t// --------------------------------------------------------------------------\n\n".$module."\n";
$new_file .= "\n// --------------------------------------------------------------------------\n\n".$module."\n";
}
@ -71,8 +56,8 @@ curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=te
$output = curl_exec($ch);
curl_close($ch);
file_put_contents("kis-min.js", $output);
file_put_contents("kis-custom-min.js", $output);
//Display the output on-screen too
echo '<pre>'.htmlspecialchars($new_file).'</pre>';
echo '<pre style="-moz-tab-size:4;">'.htmlspecialchars($new_file).'</pre>';

View File

@ -793,6 +793,7 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
* Util Object
*
* Various object and string manipulation functions
* Note: these are based on similar phpjs functions: http://phpjs.org
*/
(function(){
@ -977,12 +978,106 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
return new_obj;
},
str_trans: function(string, from, to)
{
},
str_replace: function(from, to, string)
str_trans: function(str, from, to)
{
var froms = [],
tos = [],
ret = '',
match = false,
from_len = 0,
str_len = 0,
to_len = 0,
to_is_str = '',
from_is_str = '',
strx = '',
strw = '',
stry = '',
from_strx = '',
new_str = '',
f,
i,
j;
//Replace pairs? add them to the internal arrays
if(typeof from === 'object')
{
// Sort the keys in descending order for better
// replacement functionality
from = this.reverse_key_sort(from);
for(f in from)
{
if(from.hasOwnProperty(f))
{
froms.push(f);
tos.push(from[f]);
}
}
from = froms;
to = tos;
}
//Go through the string, and replace characters as needed
str_len = str.length;
from_len = from.length;
to_len = to.length;
to_is_str = typeof to === 'string';
from_is_str = typeof from === 'string';
for(i=0; i < str_len; i++)
{
match = false;
if(from_is_str)
{
strw = str.charAt(i-1);
strx = str.charAt(i);
stry = str.charAt(i+1);
for(j=0; j < from_len; j++)
{
/*if(from_len !== to_len)
{
//Double check matches when the strings are different lengths
}
else
{*/
if(strx == from.charAt(j))
{
match = true;
break;
}
//}
}
}
else
{
for(j=0; j < from_len; j++)
{
if(str.substr(i, from[j].length) == from[j])
{
match = true;
//Go past the current match
i = (i + from[j].length) -1;
break;
}
}
}
if(match)
{
new_str += (to_is_str) ? to.charAt(j) : to[j];
}
else
{
new_str += str.charAt(i);
}
}
return new_str;
}
};
@ -990,6 +1085,174 @@ if (typeof document !== "undefined" && !("classList" in document.createElement("
$_.ext('util', u);
}());
function strtr (str, from, to) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// + input by: uestla
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Alan C
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Taras Bogach
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: jpfle
// + 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') {
//tmpStrictForIn = this.ini_set('phpjs.strictForIn', false); // Not thread-safe; temporarily set to true
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;
}
function krsort (inputArr) {
// http://kevin.vanzonneveld.net
// + original by: GeekFG (http://geekfg.blogspot.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir (http://brett-zamir.me)
// % note 1: The examples are correct, this is a new way
// % note 2: This function deviates from PHP in returning a copy of the array instead
// % note 2: of acting by reference and returning true; this was necessary because
// % note 2: IE does not allow deleting and re-adding of properties without caching
// % note 2: of property position; you can set the ini of "phpjs.strictForIn" to true to
// % note 2: get the PHP behavior, but use this only if you are in an environment
// % note 2: such as Firefox extensions where for-in iteration order is fixed and true
// % note 2: property deletion is supported. Note that we intend to implement the PHP
// % note 2: behavior by default if IE ever does allow it; only gives shallow copy since
// % note 2: is by reference in PHP anyways
// % note 3: Since JS objects' keys are always strings, and (the
// % note 3: default) SORT_REGULAR flag distinguishes by key type,
// % note 3: if the content is a numeric string, we treat the
// % note 3: "original type" as numeric.
// - depends on: i18n_loc_get_default
// * example 1: data = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'};
// * example 1: data = krsort(data);
// * results 1: {d: 'lemon', c: 'apple', b: 'banana', a: 'orange'}
// * example 2: ini_set('phpjs.strictForIn', true);
// * example 2: data = {2: 'van', 3: 'Zonneveld', 1: 'Kevin'};
// * example 2: krsort(data);
// * results 2: data == {3: 'Kevin', 2: 'van', 1: 'Zonneveld'}
// * returns 2: true
var tmp_arr = {},
keys = [],
sorter, i, k, that = this,
strictForIn = false,
populateArr = {};
//sorter = ;
// Make a list of key names
for (k in inputArr) {
if (inputArr.hasOwnProperty(k)) {
keys.push(k);
}
}
keys.sort(function (b, a) {
var aFloat = parseFloat(a),
bFloat = parseFloat(b),
aNumeric = aFloat + '' === a,
bNumeric = bFloat + '' === b;
if (aNumeric && bNumeric) {
return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
} else if (aNumeric && !bNumeric) {
return 1;
} else if (!aNumeric && bNumeric) {
return -1;
}
return a > b ? 1 : a < b ? -1 : 0;
});
// Rebuild array with sorted key names
for (i = 0; i < keys.length; i++) {
k = keys[i];
tmp_arr[k] = inputArr[k];
}
for (i in tmp_arr) {
if (tmp_arr.hasOwnProperty(i)) {
populateArr[i] = tmp_arr[i];
}
}
return populateArr;
}
// --------------------------------------------------------------------------
/**

45
kis-min.js vendored
View File

@ -1,21 +1,24 @@
(function(){if(document.querySelectorAll){var b,e,f,c;e=function(a){if(typeof a!=="string"||typeof a==="undefined")return a;if(a.match(/^#([\w\-]+$)/))return document.getElementById(a.split("#")[1]);else a=a.match(/^([\w\-]+)$/)?document.getElementsByTagName(a):document.querySelectorAll(a);return a.length===1?a[0]:a};b=function(a){c=typeof a==="undefined"?typeof b.el!=="undefined"?b.el:document.documentElement:typeof a!=="object"?e(a):a;b.prototype.el=c;var a=f(b),d;for(d in a)if(typeof a[d]==="object")a[d].el=
c;a.el=c;return a};f=function(a){var d;if(typeof a!=="undefined"){if(typeof Object.create!=="undefined")return Object.create(a);d=typeof a;if(!(d!=="object"&&d!=="function"))return d=function(){},d.prototype=a,new d}};b.ext=function(a,d){d.el=c;b[a]=d};b.ext("each",function(a){if(typeof c.length!=="undefined"&&c!==window){var d=c.length;if(d!==0)for(var b,e=0;e<d;e++)b=c.item(e)?c.item(e):c[e],a(b)}else a(c)});b.type=function(a){return function(){return a&&a!==this}.call(a)?(typeof a).toLowerCase():
{}.toString.call(a).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()};b=window.$_=window.$_||b;b.$=e;if(typeof window.console==="undefined")window.console={log:function(){}};if(typeof String.prototype.trim==="undefined")String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}}})();
typeof document!=="undefined"&&!("classList"in document.createElement("a"))&&function(b){var b=(b.HTMLElement||b.Element).prototype,e=Object,f=String.prototype.trim||function(){return this.replace(/^\s+|\s+$/g,"")},c=Array.prototype.indexOf||function(a){for(var d=0,c=this.length;d<c;d++)if(d in this&&this[d]===a)return d;return-1},a=function(a,d){this.name=a;this.code=DOMException[a];this.message=d},d=function(d,b){if(b==="")throw new a("SYNTAX_ERR","An invalid or illegal string was specified");if(/\s/.test(b))throw new a("INVALID_CHARACTER_ERR",
"String contains an invalid character");return c.call(d,b)},h=function(a){for(var d=f.call(a.className),d=d?d.split(/\s+/):[],c=0,b=d.length;c<b;c++)this.push(d[c]);this._updateClassName=function(){a.className=this.toString()}},g=h.prototype=[],l=function(){return new h(this)};a.prototype=Error.prototype;g.item=function(d){return this[d]||null};g.contains=function(a){a+="";return d(this,a)!==-1};g.add=function(a){a+="";d(this,a)===-1&&(this.push(a),this._updateClassName())};g.remove=function(a){a+=
"";a=d(this,a);a!==-1&&(this.splice(a,1),this._updateClassName())};g.toggle=function(a){a+="";d(this,a)===-1?this.add(a):this.remove(a)};g.toString=function(){return this.join(" ")};if(e.defineProperty){g={get:l,enumerable:true,configurable:true};try{e.defineProperty(b,"classList",g)}catch(j){if(j.number===-2146823252)g.enumerable=false,e.defineProperty(b,"classList",g)}}else e.prototype.__defineGetter__&&b.__defineGetter__("classList",l)}(self);
(function(){function b(c,a,d){var b,e;if(typeof c.hasAttribute!=="undefined")c.hasAttribute(a)&&(b=c.getAttribute(a)),e=true;else if(typeof c[a]!=="undefined")b=c[a],e=false;else if(a==="class"&&typeof c.className!=="undefined")a="className",b=c.className,e=false;if(typeof b==="undefined"&&(typeof d==="undefined"||d===null))console.log(d),console.log(c),console.log("Element does not have the selected attribute");else{if(typeof d==="undefined")return b;typeof d!=="undefined"&&d!==null?e===true?c.setAttribute(a,
d):c[a]=d:d===null&&(e===true?c.removeAttribute(a):delete c[a]);return typeof d!=="undefined"?d:b}}function e(c){return c.replace(/(\-[a-z])/g,function(a){return a.toUpperCase().replace("-","")})}function f(c,a,d){var b,a=e(a);b={outerHeight:"offsetHeight",outerWidth:"offsetWidth",top:"posTop"};if(typeof d==="undefined"&&c.style[a]!=="undefined")return c.style[a];else if(typeof d==="undefined"&&c.style[b[a]]!=="undefined")return c.style[b[a]];typeof c.style[a]!=="undefined"?c.style[a]=d:c.style[b[a]]?
c.style[b[a]]=d:console.log("Property "+a+" nor an equivalent seems to exist")}$_.ext("dom",{addClass:function(c){$_.each(function(a){a.classList.add(c)})},removeClass:function(c){$_.each(function(a){a.classList.remove(c)})},hide:function(){this.css("display","none")},show:function(c){typeof c==="undefined"&&(c="block");this.css("display",c)},attr:function(c,a){var d=this.el;if(d.length>1&&typeof a==="undefined")console.log(d),console.log("Must be a singular element");else if(d.length>1&&typeof a!==
"undefined")$_.each(function(d){return b(d,c,a)});else return b(d,c,a)},text:function(c){var a,d,b;b=this.el;d=typeof b.innerText!=="undefined"?"innerText":typeof b.textContent!=="undefined"?"textContent":"innerHTML";a=b[d];return typeof c!=="undefined"?b[d]=c:a},css:function(c,a){if(typeof a==="undefined")return f(this.el,c);$_.each(function(d){f(d,c,a)})}})})();
(function(){$_.ext("store",{get:function(b){return JSON.parse(localStorage.getItem(b))},set:function(b,e){typeof e!=="string"&&(e=JSON.stringify(e));localStorage.setItem(b,e)},remove:function(b){localStorage.removeItem(b)},getAll:function(){var b,e,f;e=localStorage.length;f={};for(b=0;b<e;b++){var c=localStorage.key(b),a=localStorage.getItem(c);f[c]=a}return f}})})();
(function(){$_.hb=history.pushState?false:true;$_.ext("qs",{parse:function(b){var b=b||$_.hb,e,f,c,a;c={};if(b===true)b=location.hash.split("#!/"),b=b.length>1?b[1]:"";else if(b===false||b===void 0)b=window.location.search.substring(1);else return false;e=b.split("&");f=e.length;for(b=0;b<f;b++){a=e[b].split("=");if(a.length<2)break;c[a[0]]=a[1]}return c},set:function(b,e,f){var f=f||$_.hb,c=this.parse(f);b!==void 0&&e!==void 0&&(c[b]=e);var b=[],a;for(a in c)c.hasOwnProperty(a)&&b.push(a+"="+c[a]);
c=b.join("&");if(f===true)c="!/"+c,location.hash=c;return c},get:function(b,e){var e=e||$_.hb,f=this.parse(e);return f[b]?f[b]:""}})})();
(function(){var b={_do:function(b,f,c,a){typeof c==="undefined"&&(c=function(){});var d=typeof window.XMLHttpRequest!=="undefined"?new XMLHttpRequest:false,a=a?"POST":"GET";b+=a==="GET"?"?"+this._serialize(f):"";d.open(a,b);d.onreadystatechange=function(){d.readyState===4&&c(d.responseText)};a==="POST"?(d.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),d.send(this._serialize(f))):d.send(null)},_serialize:function(b){var f=[],c;for(c in b)if(b.hasOwnProperty(c)&&typeof b[c]!==
"function"){var a=b[c].toString();c=encodeURIComponent(c);a=encodeURIComponent(a);f.push(c+"="+a)}return f.join("&")}};$_.ext("get",function(e,f,c){b._do(e,f,c,false)});$_.ext("post",function(e,f,c){b._do(e,f,c,true)})})();
(function(){$_.ext("util",{reverse_key_sort:function(b){var e=[],f=0,c={},a,e=this.object_keys(b);e.sort(function(a,b){var c=parseFloat(b),e=parseFloat(a),f=c+""===b,i=e+""===a;if(f&&i)return c>e?1:c<e?-1:0;else if(f&&!i)return 1;else if(!f&&i)return-1;return b>a?1:b<a?-1:0});f=e.length;for(a=0;a<f;a++)c[e[a]]=b[e[a]];return c},object_keys:function(b){var e=[],f;for(f in b)b.hasOwnProperty(f)&&e.push(f);return e},object_values:function(b){var e=[],f;for(f in b)e.push(b[f]);return e},array_combine:function(b,
e){var f={},c,a=0;$_.type(b)!=="array"&&(b=this.object_values(b));$_.type(e)!=="array"&&(e=this.object_values(e));c=b.length;if(c!==e.length)return console.log("Object combine requires two arrays of the same size"),false;for(a=0;a<c;a++)f[b[a]]=e[a];return f},object_merge:function(){var b=Array.prototype.slice.call(arguments),e=b.length,f={},c,a=0,d,h,g;c=true;for(d=0;d<e;d++)if($_.type(b[d])!=="array"){c=false;break}if(c){f=[];for(d=0;d<e;d++)f=f.contact(b[d]);return f}for(d=0,g=0;d<e;d++)if(c=b[d],
$_.type(c)=="array")for(h=0,a=c.length;h<a;h++)f[g++]=c[h];else for(h in c)c.hasOwnProperty(h)&&(parseInt(h,10)+""===h?f[g++]=c[h]:f[h]=c[h]);return f},str_trans:function(){},str_replace:function(){}})})();
(function(){var b,e,f,c,a;typeof document.addEventListener!=="undefined"?(b=function(a,b,c){typeof a.addEventListener!=="undefined"&&a.addEventListener(b,c,false)},e=function(a,b,c){typeof a.removeEventListener!=="undefined"&&a.removeEventListener(b,c,false)}):typeof document.attachEvent!=="undefined"&&(b=function(a,b,c){var d;function f(){c.apply(arguments)}typeof a.attachEvent!=="undefined"?(e(b,c),a.attachEvent("on"+b,f),d=a.KIS_0_3_0=a.KIS_0_3_0||{},a=d,a.listeners=a.listeners||{},a.listeners[b]=
a.listeners[b]||[],a.listeners[b].push({callback:c,listener:f})):console.log("Failed to attach event:"+b+" on "+a)},e=function(a,b,c){if(typeof a.detachEvent!=="undefined"){var e=a.KIS_0_3_0;if(e&&e.listeners&&e.listeners[b])for(var f=e.listeners[b],i=f.length,k=0;k<i;k++)if(f[k].callback===c){a.detachEvent("on"+b,f[k].listener);f.splice(k,1);f.length===0&&delete e.listeners[b];break}}});f=function(a,c,g,l){var j,i;if(typeof a==="undefined")return console.log(arguments),console.log(c),false;if(c.match(/^([\w\-]+)$/))l===
true?b(a,c,g):e(a,c,g);else{c=c.split(" ");i=c.length;for(j=0;j<i;j++)f(a,c[j],g,l)}};c=function(a,b,c){f(a,c,function(){a=$_.$(a)},true)};a=function(a,b,e){c(document.documentElement,a,b,e)};$_.ext("event",{add:function(a,b){$_.each(function(c){f(c,a,b,true)})},remove:function(a,b){$_.each(function(c){f(c,a,b,false)})},live:function(b,c){$_.each(function(e){a(e,b,c)})},delegate:function(a,b,e){$_.each(function(f){c(f,a,b,e)})}})})();
(function(){if(document.querySelectorAll){var c,d,f,b;d=function(a){if(typeof a!=="string"||typeof a==="undefined")return a;if(a.match(/^#([\w\-]+$)/))return document.getElementById(a.split("#")[1]);else a=a.match(/^([\w\-]+)$/)?document.getElementsByTagName(a):document.querySelectorAll(a);return a.length===1?a[0]:a};c=function(a){b=typeof a==="undefined"?typeof c.el!=="undefined"?c.el:document.documentElement:typeof a!=="object"?d(a):a;c.prototype.el=b;var a=f(c),e;for(e in a)if(typeof a[e]==="object")a[e].el=
b;a.el=b;return a};f=function(a){var e;if(typeof a!=="undefined"){if(typeof Object.create!=="undefined")return Object.create(a);e=typeof a;if(!(e!=="object"&&e!=="function"))return e=function(){},e.prototype=a,new e}};c.ext=function(a,e){e.el=b;c[a]=e};c.ext("each",function(a){if(typeof b.length!=="undefined"&&b!==window){var e=b.length;if(e!==0)for(var c,d=0;d<e;d++)c=b.item(d)?b.item(d):b[d],a(c)}else a(b)});c.type=function(a){return function(){return a&&a!==this}.call(a)?(typeof a).toLowerCase():
{}.toString.call(a).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()};c=window.$_=window.$_||c;c.$=d;if(typeof window.console==="undefined")window.console={log:function(){}};if(typeof String.prototype.trim==="undefined")String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}}})();
typeof document!=="undefined"&&!("classList"in document.createElement("a"))&&function(c){var c=(c.HTMLElement||c.Element).prototype,d=Object,f=String.prototype.trim||function(){return this.replace(/^\s+|\s+$/g,"")},b=Array.prototype.indexOf||function(a){for(var e=0,b=this.length;e<b;e++)if(e in this&&this[e]===a)return e;return-1},a=function(a,e){this.name=a;this.code=DOMException[a];this.message=e},e=function(e,d){if(d==="")throw new a("SYNTAX_ERR","An invalid or illegal string was specified");if(/\s/.test(d))throw new a("INVALID_CHARACTER_ERR",
"String contains an invalid character");return b.call(e,d)},h=function(a){for(var e=f.call(a.className),e=e?e.split(/\s+/):[],b=0,d=e.length;b<d;b++)this.push(e[b]);this._updateClassName=function(){a.className=this.toString()}},g=h.prototype=[],l=function(){return new h(this)};a.prototype=Error.prototype;g.item=function(a){return this[a]||null};g.contains=function(a){a+="";return e(this,a)!==-1};g.add=function(a){a+="";e(this,a)===-1&&(this.push(a),this._updateClassName())};g.remove=function(a){a+=
"";a=e(this,a);a!==-1&&(this.splice(a,1),this._updateClassName())};g.toggle=function(a){a+="";e(this,a)===-1?this.add(a):this.remove(a)};g.toString=function(){return this.join(" ")};if(d.defineProperty){g={get:l,enumerable:true,configurable:true};try{d.defineProperty(c,"classList",g)}catch(j){if(j.number===-2146823252)g.enumerable=false,d.defineProperty(c,"classList",g)}}else d.prototype.__defineGetter__&&c.__defineGetter__("classList",l)}(self);
(function(){function c(b,a,e){var d,c;if(typeof b.hasAttribute!=="undefined")b.hasAttribute(a)&&(d=b.getAttribute(a)),c=true;else if(typeof b[a]!=="undefined")d=b[a],c=false;else if(a==="class"&&typeof b.className!=="undefined")a="className",d=b.className,c=false;if(typeof d==="undefined"&&(typeof e==="undefined"||e===null))console.log(e),console.log(b),console.log("Element does not have the selected attribute");else{if(typeof e==="undefined")return d;typeof e!=="undefined"&&e!==null?c===true?b.setAttribute(a,
e):b[a]=e:e===null&&(c===true?b.removeAttribute(a):delete b[a]);return typeof e!=="undefined"?e:d}}function d(b){return b.replace(/(\-[a-z])/g,function(a){return a.toUpperCase().replace("-","")})}function f(b,a,e){var c,a=d(a);c={outerHeight:"offsetHeight",outerWidth:"offsetWidth",top:"posTop"};if(typeof e==="undefined"&&b.style[a]!=="undefined")return b.style[a];else if(typeof e==="undefined"&&b.style[c[a]]!=="undefined")return b.style[c[a]];typeof b.style[a]!=="undefined"?b.style[a]=e:b.style[c[a]]?
b.style[c[a]]=e:console.log("Property "+a+" nor an equivalent seems to exist")}$_.ext("dom",{addClass:function(b){$_.each(function(a){a.classList.add(b)})},removeClass:function(b){$_.each(function(a){a.classList.remove(b)})},hide:function(){this.css("display","none")},show:function(b){typeof b==="undefined"&&(b="block");this.css("display",b)},attr:function(b,a){var e=this.el;if(e.length>1&&typeof a==="undefined")console.log(e),console.log("Must be a singular element");else if(e.length>1&&typeof a!==
"undefined")$_.each(function(e){return c(e,b,a)});else return c(e,b,a)},text:function(b){var a,e,d;d=this.el;e=typeof d.innerText!=="undefined"?"innerText":typeof d.textContent!=="undefined"?"textContent":"innerHTML";a=d[e];return typeof b!=="undefined"?d[e]=b:a},css:function(b,a){if(typeof a==="undefined")return f(this.el,b);$_.each(function(e){f(e,b,a)})}})})();
(function(){$_.ext("store",{get:function(c){return JSON.parse(localStorage.getItem(c))},set:function(c,d){typeof d!=="string"&&(d=JSON.stringify(d));localStorage.setItem(c,d)},remove:function(c){localStorage.removeItem(c)},getAll:function(){var c,d,f;d=localStorage.length;f={};for(c=0;c<d;c++){var b=localStorage.key(c),a=localStorage.getItem(b);f[b]=a}return f}})})();
(function(){$_.hb=history.pushState?false:true;$_.ext("qs",{parse:function(c){var c=c||$_.hb,d,f,b,a;b={};if(c===true)c=location.hash.split("#!/"),c=c.length>1?c[1]:"";else if(c===false||c===void 0)c=window.location.search.substring(1);else return false;d=c.split("&");f=d.length;for(c=0;c<f;c++){a=d[c].split("=");if(a.length<2)break;b[a[0]]=a[1]}return b},set:function(c,d,f){var f=f||$_.hb,b=this.parse(f);c!==void 0&&d!==void 0&&(b[c]=d);var c=[],a;for(a in b)b.hasOwnProperty(a)&&c.push(a+"="+b[a]);
b=c.join("&");if(f===true)b="!/"+b,location.hash=b;return b},get:function(c,d){var d=d||$_.hb,f=this.parse(d);return f[c]?f[c]:""}})})();
(function(){var c={_do:function(d,c,b,a){typeof b==="undefined"&&(b=function(){});var e=typeof window.XMLHttpRequest!=="undefined"?new XMLHttpRequest:false,a=a?"POST":"GET";d+=a==="GET"?"?"+this._serialize(c):"";e.open(a,d);e.onreadystatechange=function(){e.readyState===4&&b(e.responseText)};a==="POST"?(e.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),e.send(this._serialize(c))):e.send(null)},_serialize:function(d){var c=[],b;for(b in d)if(d.hasOwnProperty(b)&&typeof d[b]!==
"function"){var a=d[b].toString();b=encodeURIComponent(b);a=encodeURIComponent(a);c.push(b+"="+a)}return c.join("&")}};$_.ext("get",function(d,f,b){c._do(d,f,b,false)});$_.ext("post",function(d,f,b){c._do(d,f,b,true)})})();
(function(){$_.ext("util",{reverse_key_sort:function(c){var d=[],f=0,b={},a,d=this.object_keys(c);d.sort(function(a,b){var d=parseFloat(b),c=parseFloat(a),f=d+""===b,i=c+""===a;if(f&&i)return d>c?1:d<c?-1:0;else if(f&&!i)return 1;else if(!f&&i)return-1;return b>a?1:b<a?-1:0});f=d.length;for(a=0;a<f;a++)b[d[a]]=c[d[a]];return b},object_keys:function(c){var d=[],f;for(f in c)c.hasOwnProperty(f)&&d.push(f);return d},object_values:function(c){var d=[],f;for(f in c)d.push(c[f]);return d},array_combine:function(c,
d){var f={},b,a=0;$_.type(c)!=="array"&&(c=this.object_values(c));$_.type(d)!=="array"&&(d=this.object_values(d));b=c.length;if(b!==d.length)return console.log("Object combine requires two arrays of the same size"),false;for(a=0;a<b;a++)f[c[a]]=d[a];return f},object_merge:function(){var c=Array.prototype.slice.call(arguments),d=c.length,f={},b,a=0,e,h,g;b=true;for(e=0;e<d;e++)if($_.type(c[e])!=="array"){b=false;break}if(b){f=[];for(e=0;e<d;e++)f=f.contact(c[e]);return f}for(e=0,g=0;e<d;e++)if(b=c[e],
$_.type(b)=="array")for(h=0,a=b.length;h<a;h++)f[g++]=b[h];else for(h in b)b.hasOwnProperty(h)&&(parseInt(h,10)+""===h?f[g++]=b[h]:f[h]=b[h]);return f},str_trans:function(c,d,f){var b=[],a=[],e=false,h=0,g=0,l="",j="",i="",k="",m;if(typeof d==="object"){d=this.reverse_key_sort(d);for(m in d)d.hasOwnProperty(m)&&(b.push(m),a.push(d[m]));d=b;f=a}g=c.length;h=d.length;l=typeof f==="string";j=typeof d==="string";for(b=0;b<g;b++){e=false;if(j){c.charAt(b-1);i=c.charAt(b);c.charAt(b+1);for(a=0;a<h;a++)if(i==
d.charAt(a)){e=true;break}}else for(a=0;a<h;a++)if(c.substr(b,d[a].length)==d[a]){e=true;b=b+d[a].length-1;break}k+=e?l?f.charAt(a):f[a]:c.charAt(b)}return k}})})();
function strtr(c,d,f){var b="",a=0,e=0,h=0,g=0,l="",j="",i="",a=[],e=[],k="",m=false;if(typeof d==="object"){d=this.krsort(d);for(b in d)d.hasOwnProperty(b)&&(a.push(b),e.push(d[b]));d=a;f=e}h=c.length;g=d.length;l=typeof d==="string";j=typeof f==="string";for(a=0;a<h;a++){m=false;if(l){i=c.charAt(a);for(e=0;e<g;e++)if(i==d.charAt(e)){m=true;break}}else for(e=0;e<g;e++)if(c.substr(a,d[e].length)==d[e]){m=true;a=a+d[e].length-1;break}k+=m?j?f.charAt(e):f[e]:c.charAt(a)}return k}
function krsort(c){var d={},f=[],b,a,e={};for(a in c)c.hasOwnProperty(a)&&f.push(a);f.sort(function(a,e){var b=parseFloat(e),d=parseFloat(a),c=b+""===e,f=d+""===a;if(c&&f)return b>d?1:b<d?-1:0;else if(c&&!f)return 1;else if(!c&&f)return-1;return e>a?1:e<a?-1:0});for(b=0;b<f.length;b++)a=f[b],d[a]=c[a];for(b in d)d.hasOwnProperty(b)&&(e[b]=d[b]);return e}
(function(){var c,d,f,b,a;typeof document.addEventListener!=="undefined"?(c=function(a,b,d){typeof a.addEventListener!=="undefined"&&a.addEventListener(b,d,false)},d=function(a,b,d){typeof a.removeEventListener!=="undefined"&&a.removeEventListener(b,d,false)}):typeof document.attachEvent!=="undefined"&&(c=function(a,b,c){var e;function f(){c.apply(arguments)}typeof a.attachEvent!=="undefined"?(d(b,c),a.attachEvent("on"+b,f),e=a.KIS_0_3_0=a.KIS_0_3_0||{},a=e,a.listeners=a.listeners||{},a.listeners[b]=
a.listeners[b]||[],a.listeners[b].push({callback:c,listener:f})):console.log("Failed to attach event:"+b+" on "+a)},d=function(a,b,d){if(typeof a.detachEvent!=="undefined"){var c=a.KIS_0_3_0;if(c&&c.listeners&&c.listeners[b])for(var f=c.listeners[b],i=f.length,k=0;k<i;k++)if(f[k].callback===d){a.detachEvent("on"+b,f[k].listener);f.splice(k,1);f.length===0&&delete c.listeners[b];break}}});f=function(a,b,g,l){var j,i;if(typeof a==="undefined")return console.log(arguments),console.log(b),false;if(b.match(/^([\w\-]+)$/))l===
true?c(a,b,g):d(a,b,g);else{b=b.split(" ");i=b.length;for(j=0;j<i;j++)f(a,b[j],g,l)}};b=function(a,b,c){f(a,c,function(){a=$_.$(a)},true)};a=function(a,c,d){b(document.documentElement,a,c,d)};$_.ext("event",{add:function(a,b){$_.each(function(c){f(c,a,b,true)})},remove:function(a,b){$_.each(function(c){f(c,a,b,false)})},live:function(b,c){$_.each(function(d){a(d,b,c)})},delegate:function(a,c,d){$_.each(function(f){b(f,a,c,d)})}})})();

View File

@ -2,6 +2,7 @@
* Util Object
*
* Various object and string manipulation functions
* Note: these are based on similar phpjs functions: http://phpjs.org
*/
(function(){
@ -186,16 +187,277 @@
return new_obj;
},
str_trans: function(string, from, to)
str_trans: function(str, from, to)
{
},
str_replace: function(from, to, string)
{
var froms = [],
tos = [],
ret = '',
match = false,
from_len = 0,
str_len = 0,
to_len = 0,
to_is_str = '',
from_is_str = '',
strx = '',
strw = '',
stry = '',
from_strx = '',
new_str = '',
f,
i,
j;
//Replace pairs? add them to the internal arrays
if(typeof from === 'object')
{
// Sort the keys in descending order for better
// replacement functionality
from = this.reverse_key_sort(from);
for(f in from)
{
if(from.hasOwnProperty(f))
{
froms.push(f);
tos.push(from[f]);
}
}
from = froms;
to = tos;
}
//Go through the string, and replace characters as needed
str_len = str.length;
from_len = from.length;
to_len = to.length;
to_is_str = typeof to === 'string';
from_is_str = typeof from === 'string';
for(i=0; i < str_len; i++)
{
match = false;
if(from_is_str)
{
strw = str.charAt(i-1);
strx = str.charAt(i);
stry = str.charAt(i+1);
for(j=0; j < from_len; j++)
{
/*if(from_len !== to_len)
{
//Double check matches when the strings are different lengths
}
else
{*/
if(strx == from.charAt(j))
{
match = true;
break;
}
//}
}
}
else
{
for(j=0; j < from_len; j++)
{
if(str.substr(i, from[j].length) == from[j])
{
match = true;
//Go past the current match
i = (i + from[j].length) -1;
break;
}
}
}
if(match)
{
new_str += (to_is_str) ? to.charAt(j) : to[j];
}
else
{
new_str += str.charAt(i);
}
}
return new_str;
}
};
//Add it to the $_ object
$_.ext('util', u);
}());
}());
function strtr (str, from, to) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// + input by: uestla
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Alan C
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Taras Bogach
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: jpfle
// + 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') {
//tmpStrictForIn = this.ini_set('phpjs.strictForIn', false); // Not thread-safe; temporarily set to true
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;
}
function krsort (inputArr) {
// http://kevin.vanzonneveld.net
// + original by: GeekFG (http://geekfg.blogspot.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir (http://brett-zamir.me)
// % note 1: The examples are correct, this is a new way
// % note 2: This function deviates from PHP in returning a copy of the array instead
// % note 2: of acting by reference and returning true; this was necessary because
// % note 2: IE does not allow deleting and re-adding of properties without caching
// % note 2: of property position; you can set the ini of "phpjs.strictForIn" to true to
// % note 2: get the PHP behavior, but use this only if you are in an environment
// % note 2: such as Firefox extensions where for-in iteration order is fixed and true
// % note 2: property deletion is supported. Note that we intend to implement the PHP
// % note 2: behavior by default if IE ever does allow it; only gives shallow copy since
// % note 2: is by reference in PHP anyways
// % note 3: Since JS objects' keys are always strings, and (the
// % note 3: default) SORT_REGULAR flag distinguishes by key type,
// % note 3: if the content is a numeric string, we treat the
// % note 3: "original type" as numeric.
// - depends on: i18n_loc_get_default
// * example 1: data = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'};
// * example 1: data = krsort(data);
// * results 1: {d: 'lemon', c: 'apple', b: 'banana', a: 'orange'}
// * example 2: ini_set('phpjs.strictForIn', true);
// * example 2: data = {2: 'van', 3: 'Zonneveld', 1: 'Kevin'};
// * example 2: krsort(data);
// * results 2: data == {3: 'Kevin', 2: 'van', 1: 'Zonneveld'}
// * returns 2: true
var tmp_arr = {},
keys = [],
sorter, i, k, that = this,
strictForIn = false,
populateArr = {};
//sorter = ;
// Make a list of key names
for (k in inputArr) {
if (inputArr.hasOwnProperty(k)) {
keys.push(k);
}
}
keys.sort(function (b, a) {
var aFloat = parseFloat(a),
bFloat = parseFloat(b),
aNumeric = aFloat + '' === a,
bNumeric = bFloat + '' === b;
if (aNumeric && bNumeric) {
return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
} else if (aNumeric && !bNumeric) {
return 1;
} else if (!aNumeric && bNumeric) {
return -1;
}
return a > b ? 1 : a < b ? -1 : 0;
});
// Rebuild array with sorted key names
for (i = 0; i < keys.length; i++) {
k = keys[i];
tmp_arr[k] = inputArr[k];
}
for (i in tmp_arr) {
if (tmp_arr.hasOwnProperty(i)) {
populateArr[i] = tmp_arr[i];
}
}
return populateArr;
}

View File

@ -312,6 +312,17 @@
});
test("String translate", function(){
var test_str = "chotto",
test_replace = {
cho: 'ちょ',
to: 'と'
},
test_res = "ちょtと",
$trans = {'hello' : 'hi', 'hi' : 'hello'};
equal($_.util.str_trans(test_str, test_replace), test_res, "Correctly replaces substrings from replace pairs");
equal($_.util.str_trans("hi all, I said hello", $trans), 'hello all, I said hi', "Correctly replaces substrings from scalar pair");
});
}());