From ab99f79b2c42e0535036170767d7ecf4a52f170b Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 24 Oct 2011 19:18:22 -0400 Subject: [PATCH] Added str_trans function --- combine.php | 33 ++---- kis-all.js | 273 ++++++++++++++++++++++++++++++++++++++++++- kis-min.js | 45 ++++---- src/modules/util.js | 276 ++++++++++++++++++++++++++++++++++++++++++-- tests/tests.js | 13 ++- 5 files changed, 582 insertions(+), 58 deletions(-) diff --git a/combine.php b/combine.php index 0391cd8..201e9bd 100755 --- a/combine.php +++ b/combine.php @@ -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 '
'.htmlspecialchars($new_file).'
'; \ No newline at end of file +echo '
'.htmlspecialchars($new_file).'
'; \ No newline at end of file diff --git a/kis-all.js b/kis-all.js index 4129637..623107d 100644 --- a/kis-all.js +++ b/kis-all.js @@ -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; +} + + // -------------------------------------------------------------------------- /** diff --git a/kis-min.js b/kis-min.js index 5dace6d..8bd42aa 100644 --- a/kis-min.js +++ b/kis-min.js @@ -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;e1&&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;b1?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;be?1:ca?1:b1&&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;c1?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;cc?1:da?1:bd?1:ba?1:eto 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; +} + diff --git a/tests/tests.js b/tests/tests.js index afb6fd7..117b351 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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"); + }); }()); \ No newline at end of file