UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

222 lines (129 loc) 4.5 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://www.friv.mn" rel="nofollow">Friv games</a> </strong> on 2012-09-28 15:38:33 <br /> I want to thank you for clearing this issue. I looked everywhere. <hr /> <strong> <a href="http://www.friv.name" rel="nofollow">Friv</a> </strong> on 2012-07-10 09:08:25 <br /> Cool code. Many thanks! <hr /> <strong> <a href="http://an3m1.com/" rel="nofollow">??????? ????? ???</a> </strong> on 2012-03-22 13:51:31 <br /> Anyone looking for what’s new, visit the gate Yes <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-11-25 17:15:40 <br /> @ Brett Zamir: Cool, thx! <hr /> <strong> Brett Zamir </strong> on 2008-11-21 00:03:51 <br /> Yeah, you were right, sorry. Here it is to accept associative as well as regular arrays for 'keys': Examples: <pre><code>$keys = {'a':'foo', 2:5, 3:10, 4:'bar'}; //$keys = ['foo', 5, 10, 'bar'];</code></pre> <pre><code>function array_fill_keys (keys, value) { var retObj={}; for (var key in keys) { retObj[keys[key]] = value; } return retObj; }</code></pre> Sorry for the trouble... <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-11-18 12:22:10 <br /> @ Brett Zamir: Cool, I've committed it. Will be online shortly. Are you sure by the way, that PHP does not allow associative arrays as 'keys'? Otherwise we will have to change the for-loop to support this. <hr /> <strong> Brett Zamir </strong> on 2008-11-16 10:04:07 <br /> Here's one for array_fill_keys() with a sample based on the PHP manual page for array_fill_keys(): $keys = ['foo', 5, 10, 'bar']; $a = array_fill_keys($keys, 'banana'); <pre><code> function array_fill_keys (keys, value) { for (var i=0, retObj={}; i &amp;lt; keys.length; i++) { retObj[keys[i]] = value; } return retObj; }</code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-01-29 20:48:46 <br /> @ _argos: Hey argos, Thanks for your input, I've added everything to the project. If you know any more, feel free to leave another comment any time :) <hr /> <strong> waldo malqui silva aka _argos </strong> on 2008-01-29 17:31:35 <br /> Hi Kevin. I have some ports to your project, and I wanna know if you change my real name by _argos, sorry for duplicate one function ( array_fill ) and make one function (array_pad) on that you are working : <pre><code> var Test1 = 4.2; var Test2 = -4.2; var Test3 = 5; var Test4 = -5; var Test5 = 'prueba'; console.log ( abs ( Test1 ) ); console.log ( abs ( Test2 ) ); console.log ( abs ( Test3 ) ); console.log ( abs ( Test4 ) ); console.log ( abs ( Test5 ) ); function abs ( mixed_number ) { return ( ( !isNaN ( mixed_number ) ) ? ( ( mixed_number &amp;lt; 0 ) ? ( mixed_number * -1 ) : mixed_number ) : 0 ); } var TestA = [ 7,8,9 ]; console.log ( array_pad ( TestA, 2, 'a' ) ); console.log ( array_pad ( TestA, 5, 'a' ) ); console.log ( array_pad ( TestA, 5, 2 ) ); console.log ( array_pad ( TestA, -5, 'a' ) ); console.log ( array_pad ( TestA, -5, 2 ) ); function array_pad ( input, pad_size, pad_value ) { var pad = []; if ( input instanceof Array &amp;amp;&amp;amp; !isNaN ( pad_size ) ) { var newArray = []; var newLength = ( ( pad_size &amp;lt; 0 ) ? ( pad_size * -1 ) : pad_size ); if ( newLength &amp;gt; input.length ) { for ( var i = 0; i &amp;lt; ( newLength - input.length ); i++ ) { newArray [ i ] = pad_value; } pad = ( ( pad_size &amp;lt; 0 ) ? newArray.concat ( input ) : input.concat ( newArray ) ); } else { pad = input; } } return pad; } function array_fill ( start_key, num, val ) { var fill = []; if ( !isNaN ( start_key ) &amp;amp;&amp;amp; !isNaN ( num ) ) { for ( var i = start_key; i &amp;lt; (start_key + num ); i++ ) { fill [ i ] = val; } } return fill; } console.log ( strlen ( 'alexa' ) ); function strlen ( str ) { return str.length; } console.log ( implode ( '-', [1,2,3,4,5,6,7,8,9,0] ) ); function implode ( glue, pieces ) { return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces ); } </code></pre> <hr />