UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

284 lines (246 loc) 8.22 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://an3m1.com/" rel="nofollow">????? ????????</a> </strong> on 2012-04-04 14:32:54 <br /> Great job here. I really enjoyed what you had to say. Keep going because you definitely bring a new voice to this subject. Not many people would say what you’ve said and still make it interesting <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-01-16 23:48:30 <br /> @ Brett Zamir: What a beautiful moment it is, when I paste one of your functions, write a testcase, click on 'Play', and see: <pre><code> Test result =========================================================================== array/uasort.js results#1 OKAY </code></pre> That word 'OKAY' is like a little present :) You are awesome Brett. <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-01-16 13:10:05 <br /> Grrrr. Sorry, the 'i' for loop (in sort(), rsort(), and usort() need to have 'var' added in front)--accidental globals... Thx, Brett <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-01-16 13:05:41 <br /> Actually, please just use this one for usort() instead, as I needed to allow the callback as string. There's also another array implementation: uasort(). <pre><code>function usort (inputArr, sorter) { if (typeof sorter === 'string') { sorter = this[sorter]; } else if (sorter instanceof Array) { sorter = this[sorter[0]][sorter[1]]; } var valArr = [], keyArr=[]; for (var k in inputArr) { // Get key and value arrays valArr.push(inputArr[k]); delete inputArr[k] ; } try { valArr.sort(sorter); } catch (e) { return false; } for (i=0; i &amp;lt; valArr.length; i++) { // Repopulate the old array inputArr[i] = valArr[i]; } return true; } $stuff = {&amp;quot;d&amp;quot; : &amp;quot;3&amp;quot;, &amp;quot;a&amp;quot; : &amp;quot;1&amp;quot;, &amp;quot;b&amp;quot; : &amp;quot;11&amp;quot;, &amp;quot;c&amp;quot; : &amp;quot;4&amp;quot;}; usort($stuff, function (a, b) { return(a-b); }); </code></pre> <pre><code>function uasort (inputArr, sorter) { if (typeof sorter === 'string') { sorter = this[sorter]; } else if (sorter instanceof Array) { sorter = this[sorter[0]][sorter[1]]; } var valArr = [], keyArr=[], tempKeyVal, tempValue, ret; var sorterNew = function (keyArr, valArr) { for (var i=valArr.length-2; i &amp;gt;=0; i--) { for (var j=0; j &amp;lt;= i; j++) { ret = sorter(valArr[j+1], valArr[j]); if (ret &amp;lt; 0) { tempValue = valArr[j]; valArr[j] = valArr[j+1]; valArr[j+1] = tempValue; tempKeyVal = keyArr[j]; keyArr[j] = keyArr[j+1]; keyArr[j+1] = tempKeyVal; } } } } for (var k in inputArr) { // Get key and value arrays valArr.push(inputArr[k]); keyArr.push(k); delete inputArr[k] ; } try { sorterNew(keyArr, valArr); // Sort our new temporary arrays } catch(e) { return false; } for (i=0; i &amp;lt; valArr.length; i++) { // Repopulate the old array inputArr[keyArr[i]] = valArr[i]; } return true; } $fruits = {&amp;quot;d&amp;quot; : &amp;quot;lemon&amp;quot;, &amp;quot;a&amp;quot; : &amp;quot;orange&amp;quot;, &amp;quot;b&amp;quot; : &amp;quot;banana&amp;quot;, &amp;quot;c&amp;quot; : &amp;quot;apple&amp;quot;}; uasort($fruits, function (a, b) { if (a &amp;gt; b) return 1; if (a &amp;lt; b) return -1; return 0; }); var $output = ''; for (var $key in $fruits) { $val = $fruits[$key]; $output += $key+' = '+$val+&amp;quot;\n&amp;quot;; } alert($output); /* c = apple b = banana d = lemon a = orange */ </code></pre> <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-01-16 11:54:44 <br /> Sorry, forgot to rename the last one to usort()! <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-01-16 11:52:56 <br /> Here's sort(), rsort(), and usort() which now support objects (though no additions of sorting types for sort() or rsort()): <pre><code>function sort (inputArr, sort_flags) { var valArr = [], keyArr=[]; for (var k in inputArr) { // Get key and value arrays valArr.push(inputArr[k]); delete inputArr[k] ; } var sorter = false; // For now only SORT_NUMERIC has a custom sorter // and SORT_REGULAR, SORT_STRING, and SORT_LOCALE_STRING // are all handled with the default sorter if (sort_flags === 'SORT_NUMERIC') { sorter = function (a, b) { return(a - b); }; } if (sorter !== false) { valArr.sort(sorter); } else { valArr.sort(); } for (i=0; i &amp;lt; valArr.length; i++) { // Repopulate the old array inputArr[i] = valArr[i]; } return true; } $fruits = {&amp;quot;d&amp;quot; : &amp;quot;lemon&amp;quot;, &amp;quot;a&amp;quot; : &amp;quot;orange&amp;quot;, &amp;quot;b&amp;quot; : &amp;quot;banana&amp;quot;, &amp;quot;c&amp;quot; : &amp;quot;apple&amp;quot;}; sort($fruits); var $output = ''; for (var $key in $fruits) { $val = $fruits[$key]; $output += $key+' = '+$val+&amp;quot;\n&amp;quot;; } alert($output); /* 0 = apple 1 = banana 2 = lemon 3 = orange */</code></pre> <pre><code> function rsort (inputArr, sort_flags) { var valArr = [], keyArr=[]; for (var k in inputArr) { // Get key and value arrays valArr.push(inputArr[k]); delete inputArr[k] ; } var sorter = false; // For now only SORT_NUMERIC has a custom sorter // and SORT_REGULAR, SORT_STRING, and SORT_LOCALE_STRING // are all handled with the default sorter if (sort_flags === 'SORT_NUMERIC') { sorter = function (a, b) { return(b - a); }; } if (sorter !== false) { valArr.sort(sorter); } else { valArr.sort(); valArr.reverse(); } for (i=0; i &amp;lt; valArr.length; i++) { // Repopulate the old array inputArr[i] = valArr[i]; } return true; } $fruits = {&amp;quot;d&amp;quot; : &amp;quot;lemon&amp;quot;, &amp;quot;a&amp;quot; : &amp;quot;orange&amp;quot;, &amp;quot;b&amp;quot; : &amp;quot;banana&amp;quot;, &amp;quot;c&amp;quot; : &amp;quot;apple&amp;quot;}; //$fruits = [&amp;quot;lemon&amp;quot;, &amp;quot;orange&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;apple&amp;quot;]; // $fruits = {&amp;quot;d&amp;quot; : &amp;quot;3&amp;quot;, &amp;quot;a&amp;quot; : &amp;quot;1&amp;quot;, &amp;quot;b&amp;quot; : &amp;quot;11&amp;quot;, &amp;quot;c&amp;quot; : &amp;quot;4&amp;quot;}; rsort($fruits); // rsort($fruits, 'SORT_NUMERIC'); var $output = ''; for (var $key in $fruits) { $val = $fruits[$key]; $output += $key+' = '+$val+&amp;quot;\n&amp;quot;; } alert($output); /* 0 = orange 1 = lemon 2 = banana 3 = apple */</code></pre> <pre><code>function sort (inputArr, sorter) { var valArr = [], keyArr=[]; for (var k in inputArr) { // Get key and value arrays valArr.push(inputArr[k]); delete inputArr[k] ; } try { valArr.sort(sorter); } catch (e) { return false; } for (i=0; i &amp;lt; valArr.length; i++) { // Repopulate the old array inputArr[i] = valArr[i]; } return true; } $stuff = {&amp;quot;d&amp;quot; : &amp;quot;3&amp;quot;, &amp;quot;a&amp;quot; : &amp;quot;1&amp;quot;, &amp;quot;b&amp;quot; : &amp;quot;11&amp;quot;, &amp;quot;c&amp;quot; : &amp;quot;4&amp;quot;}; sort($stuff, function (a, b) { return(a-b); }); var $output = ''; for (var $key in $stuff) { $val = $stuff[$key]; $output += $key+' = '+$val+&amp;quot;\n&amp;quot;; } alert($output); /* 0 = 1 1 = 3 2 = 4 3 = 11 */ </code></pre> <hr />