UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

310 lines (246 loc) 10.9 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://an3m1.com/" rel="nofollow"> ????? ????? ????</a> </strong> on 2012-04-04 14:31:36 <br /> This is a very informative article. I was looking for these things and here I found it. I am doing a project and this information is very useful me. Some things in here I have not thought about before <hr /> <strong> Chris Wright </strong> on 2011-03-08 15:01:46 <br /> - Added support for integer values of STR_PAD_x constants as defined in PHP. - Removed unused variable 'i' from private member 'str_pad_repeater' <pre><code> function str_pad (input, pad_length, pad_string, pad_type) { // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + namespaced by: Michael White (http://getsprink.com) // + input by: Marco van Oort // + bugfixed by: Brett Zamir (http://brett-zamir.me) // * example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT'); // * returns 1: '-=-=-=-=-=-Kevin van Zonneveld' // * example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH'); // * returns 2: '------Kevin van Zonneveld-----' var half = '', pad_to_go; var str_pad_repeater = function (s, len) { var collect = ''; while (collect.length &lt; len) { collect += s; } collect = collect.substr(0, len); return collect; }; input += ''; pad_string = pad_string !== undefined ? pad_string : ' '; switch (pad_type) { case 'STR_PAD_LEFT': case 0: pad_type = 0; break; case 'STR_PAD_BOTH': case 2: pad_type = 2; break; case 'STR_PAD_RIGHT': case 1: default: pad_type = 1; break; } if ((pad_to_go = pad_length - input.length) &gt; 0) { if (pad_type == 0) { // Pad left input = str_pad_repeater(pad_string, pad_to_go) + input; } else if (pad_type == 1) { // Pad right input = input + str_pad_repeater(pad_string, pad_to_go); } else if (pad_type == 2) { // Pad both half = str_pad_repeater(pad_string, Math.ceil(pad_to_go / 2)); input = half + input + half; input = input.substr(0, pad_length); } } return input; } </code></pre> <hr /> <strong> <a href="http://blog.kukawski.pl" rel="nofollow">Rafa? Kukawski</a> </strong> on 2010-04-29 23:06:32 <br /> Regarding the second test case, str_pad on my server (php version 5.2.12) when using STR_PAD_BOTH, first padds from right and then from left, so when there are for example 15 characters to be added, 8 characters are added to the right and 7 to the left. If it's not the case on your server or on a newer PHP version, please inform and we'll align our code. <hr /> <strong> <a href="http://blog.kukawski.pl" rel="nofollow">Rafa? Kukawski</a> </strong> on 2010-04-29 22:41:48 <br /> @Kevin van Zonneveld: Thanks for your feedback. I will check the second test case tomorrow and test the function with some &quot;edge&quot;-scenarios. Regarding the lint warning about using len before it being declared, I used this JS feature to get undefined value without using global undefined variable, but now I think I should have used it, cause I'm already using global Array function and some other phpjs function also use these variables. I will rewrite this part too (there are many more possibilities to get undefined value, or I could just declare the variable at the very beginning). We/You should also define some rules about using global variables/functions/constructors like undefined, Array, as their usage isn't always safe, cause they can be overwritten. Some developers avoid using them and some just don't bother at all. About using conditional operator, I don't see anything wrong with it. I think, in this case the code is well self-explaining. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2010-04-29 15:37:31 <br /> Hey there Rafal, My second testcase returned: -----Kevin van Zonneveld------ with your function. So that needs a bit of tinkering still. Also jslint said: Lint at line 17 character 13: 'len' was used before it was defined. var len = Then on a personal note, I can't say I'm the world's biggest fan of concatenating ? : constructions as they tend to lead to short, but more difficult to maintain code. However in this case I feel the maintainability is still improved compared to the current implementation so I'd probably still include it in php.js Thanks so much! <hr /> <strong> <a href="http://blog.kukawski.pl" rel="nofollow">Rafa? Kukawski</a> </strong> on 2010-04-28 09:19:31 <br /> Oh, sorry, I shouldn't also accept empty string. Please change <pre><code>pad_string = String(pad_string === len ? ' ' : pad_string);</code></pre> to <pre><code>pad_string = (pad_string !== len &amp;&amp; ''+pad_string) || ' ';</code></pre> <hr /> <strong> <a href="http://blog.kukawski.pl" rel="nofollow">Rafa? Kukawski</a> </strong> on 2010-04-28 08:44:41 <br /> My approach to str_pad <pre><code>function str_pad(input, pad_length, pad_string, pad_type){ input += ''; // casting any type to string pad_length |= 0; // casting any type to integer pad_string = String(pad_string === len ? ' ' : pad_string); // checking for undefined value for pad_string var charsRequired = Math.max(0, pad_length - input.length); pad_string = Array(Math.ceil(charsRequired / pad_string.length) + 1).join(pad_string); var len = pad_type === 'STR_PAD_BOTH' ? [charsRequired / 2 | 0, Math.ceil(charsRequired / 2)] : pad_type === 'STR_PAD_LEFT' ? [charsRequired, 0] : [0, charsRequired]; return pad_string.substr(0, len[0]) + input + pad_string.substr(0, len[1]); }</code></pre> It's a bit shorter. I think the performance will be slightly worser cause of using Array(len).join(). Regarding the pad_string parameter. I am converting any value except undefined to string. When undefined is passed, a string containing 1 space character is used instead. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-08-22 10:07:08 <br /> Good catch--fixed. <hr /> <strong> Marco van Oort </strong> on 2009-08-21 16:22:55 <br /> I thought it would be worth mentioning the following: according to php.net, the third parameter should have a default value of an interspace, e.g. $pad_string = &quot; &quot;;. I do not know what exactly is the best way to set such a default value, but anyway, I would suggest to add this default value so this function is more equilavent to php's function. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-04-17 12:52:54 <br /> @ Jonas Raoni &amp;amp; Philip: I think we should stick with speed &amp;amp; readability. For compactness, people can optionally use a minified version, but it makes development with multiple coders a bit harder to make such a compact version the leading one. Do you guys agree? <hr /> <strong> Philip </strong> on 2008-04-17 05:51:46 <br /> Hmm, the one we have is actually somewhat faster, at least in Firefox. It does seem more compact, though. <hr /> <strong> Jonas Raoni </strong> on 2008-04-17 04:59:31 <br /> I didn't saw this pad before, I've made one a long time ago if you find it useful here it is (sorry about the format, I'm lazy to change the codes hehe): String.prototype.pad = function(l, s, t){ return s || (s = &amp;quot; &amp;quot;), (l -= this.length) &amp;gt; 0 ? (s = new Array(Math.ceil(l / s.length) + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2)) + this + s.substr(0, l - t) : this; }; l = length s = string t = if 0 pads on the left, if 1 right, if 2 both sides <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-09 19:44:24 <br /> @ waldo malqui silva: No Problem! I've included your name in my function. If you have any remarks on it, let me know! <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-09 19:30:41 <br /> @ waldo malqui silva : I was just working on count, and trimmed it down a bit: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_count/ Does this also seem right to you, or am I overlooking something? <hr /> <strong> waldo malqui silva </strong> on 2008-01-09 19:29:19 <br /> Sorry Kevin, I don't read your function list, anyway if my implementation of PHP's count is good you can use :)))) <hr /> <strong> waldo malqui silva </strong> on 2008-01-09 19:21:08 <br /> Hi this is my implementation of PHP's count <pre><code> var Prueba = [1,2,3,4,5,6,7,8,9,0]; var Prueba2 = null; var Prueba3 = 'waldo'; var Prueba4 = false; var Prueba5 = [[[1,['a','b','c'],3],2,3,4,5],['a','b','c','d','e'],[1,2,3,4,5],['a','b','c','d','e'],[1,2,3,4,5]]; var Prueba6 = { 'one' : [1,2,3,4,5], 'two' : ['a','b','c'], 'fun' : function () { alert ( 'Testing...' ); }, 'four' : { 'values' : [1,2,3,4,5,6] } } alert ( count ( Prueba6, 'COUNT_RECURSIVE' ) ); function count ( mixed_var, mode) { var elements = 0; if ( mixed_var instanceof Array ) { if ( mode == 'COUNT_RECURSIVE' || mode == 1 ) { for ( var item in mixed_var ) { if ( mixed_var[item] instanceof Array || mixed_var[item] instanceof Object ) { elements += count ( mixed_var[item], 'COUNT_RECURSIVE' ); } elements++; } } else { elements = mixed_var.length; } } else if ( mixed_var instanceof Object ) { for ( var item in mixed_var ) { if ( mixed_var[item] instanceof Array || mixed_var[item] instanceof Object ) { if ( mode == 'COUNT_RECURSIVE' || mode == 1 ) { elements += count ( mixed_var[item], 'COUNT_RECURSIVE' ); } elements++; } } } else if ( mixed_var != null ) { elements = 1; } return elements; } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-05 22:39:59 <br /> @ Aaron Saray: Thanks for the suggestion, I've added a section: 'In The Works', which will be updated on every page. <hr /> <strong> Aaron Saray </strong> on 2008-01-05 22:14:20 <br /> Do you have a list of functions that are &amp;quot;in the works&amp;quot; so that people don't duplicate effort? <hr />