UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

401 lines (317 loc) 9.98 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://an3m1.com/" rel="nofollow">???? ????</a> </strong> on 2012-04-04 14:33:53 <br /> Write more, that’s all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what you’re talking about, why waste your intelligence on just posting videos to your blog when you could be giving us something enlightening to read <hr /> <strong> <a href="http://ironmagma.com/" rel="nofollow">Philip Peterson</a> </strong> on 2010-12-06 14:22:13 <br /> Just want to point out that if the base is 10, we should be using ~~&quot;32&quot; (e.g.) instead of parseInt, as it is MUCH faster. http://jsperf.com/bitwise-not-not-vs-parseint <hr /> <strong> zeroneta </strong> on 2010-01-05 22:04:05 <br /> 后面那个是有问题的 没有把 a 正确 传递 请使用 意思为这个的 <pre><code> _.intval = function( a, s ) { return is_bool( a ) ? a * 1 : _._.isFinite( a = _._.parseInt( a, s || 10 ) ) ? a : 0; }, </code></pre> <hr /> <strong> zeroneta </strong> on 2010-01-05 22:01:10 <br /> <pre><code> _.intval = function( a, s ) { return is_bool( a ) ? a * 1 : _._.isFinite( a = _._.parseInt( a, s || 10 ) ) ? a : 0; }, </code></pre> 或者 <pre><code> _.intval = function( a, s ) { return is_bool( a ) ? a * 1 : _._.parseInt( a, s || 10 ) ? a : 0; }, </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-08 14:17:24 <br /> @ Brett Zamir: Thanks for fixing friend. I found there was an issue with the deployment. I fixed that just now. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-10-06 20:38:55 <br /> For now please see http://github.com/kvz/phpjs/blob/147b53a515907136c7804fe93f4bfb75a9c39d01/functions/var/intval.js -- the change hasn't yet come through the site. <hr /> <strong> Stefan Richter </strong> on 2009-10-06 17:00:06 <br /> I thought this function returns a number like PHP, not a string. E.g. expected intval('1') + 1 = 2 but the result of this is '11'. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-09-17 05:25:12 <br /> @JD Kasinsky: Yes, parseInt will often do the trick, but we're shooting for exact PHP behavior here... <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-09-17 05:22:30 <br /> @Matteo: Glad you caught that. Fixed in git (also did some other clean-up). For now, see latest version at http://github.com/kvz/phpjs/commits/master/functions/var/intval.js <hr /> <strong> <a href="http://www.caboom.tv" rel="nofollow">JD Kasinsky</a> </strong> on 2009-09-17 01:04:28 <br /> This function should work in the most cases: <pre><code> var v='10px'; v=parseInt(v); alert(v); </code></pre> Bye JD <hr /> <strong> Matteo </strong> on 2009-09-16 16:37:08 <br /> using this function as above, if I do something like <pre><code> alert(is_integer(intval('12'))) </code></pre> I always get FALSE while I'd expect to get TRUE. This happens 'cause if the argument is a string, you return a string and not a number. Why is so? Shouldn't intval always return a number? <hr /> <strong> mkl.keck </strong> on 2009-03-06 16:12:52 <br /> Imprived function intval: <pre><code> function intval() { var a = arguments; var v = a[0]; var t = typeof(a[0]) var b = ( (typeof(a[1]) !== 'undefined' &amp;amp;&amp;amp; !isNaN(a[1])) ? parseInt(a[1]) : 10 ); switch (t.substring(0, 1).toLowerCase()) { case 'b': return ( (v === true) ? 1 : 0 ); case 's': var r = parseInt(v * 1); return ( (!isNaN(v) &amp;amp;&amp;amp; isFinite(v)) ? r.toString(b) : 0 ); case 'n': return ( isFinite(v) ? Math.floor(v) : 0 ); default: return 0; } } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-05-31 14:19:59 <br /> @ Alex: Thank you Alex. Added! <hr /> <strong> Alex </strong> on 2008-05-30 15:10:13 <br /> function isInt(x) { var y=parseInt(x); if (isNaN(y)) return false; return x==y &amp;amp;&amp;amp; x.toString()==y.toString(); } taken from http://community.livejournal.com/nullzone/1223.html <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-05-14 21:55:11 <br /> @ stenci: Thanks a lot for your improvements stensi! <hr /> <strong> stensi </strong> on 2008-05-14 04:33:39 <br /> Sorry for double post. I didn't give the correct code syntax highlighting first time round. I noticed I the example I gave had an incorrect call: <pre><code> alert( intval_modified(num) ); </code></pre> Should of course be: <pre><code> alert( intval(num) ); </code></pre> <hr /> <strong> stensi </strong> on 2008-05-14 04:29:49 <br /> Just stumbled across your PHP.js library, awesome work Kevin :) I think your intval could be improved a little to handle JavaScripts &amp;quot;Infinity&amp;quot; value. for example, using current PHP.js intval: <pre><code> function intval( mixed_var, base ) { // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: intval('Kevin van Zonneveld'); // * returns 1: 0 // * example 2: intval(4.2); // * returns 2: 4 // * example 3: intval(42, 8); // * returns 3: 42 var tmp; if( typeof( mixed_var ) == 'string' ){ tmp = parseInt(mixed_var); if(isNaN(tmp)){ return 0; } else{ return tmp.toString(base || 10); } } else if( typeof( mixed_var ) == 'number' ){ return Math.floor(mixed_var); } else{ return 0; } } // set num to: Infinity var num = 1 / 0; // using current PHP.js intval returns: Infinity alert( intval(num) ); // Output: Infinity </code></pre> Using modified intval to handle Infinity: <pre><code> function intval( mixed_var, base ) { // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: intval('Kevin van Zonneveld'); // * returns 1: 0 // * example 2: intval(4.2); // * returns 2: 4 // * example 3: intval(42, 8); // * returns 3: 42 var tmp; if( typeof( mixed_var ) == 'string' ){ tmp = parseInt(mixed_var); if(isNaN(tmp) || !isFinite(tmp)){ return 0; } else{ return tmp.toString(base || 10); } } else if( typeof( mixed_var ) == 'number' &amp;amp;&amp;amp; isFinite(mixed_var) ){ return Math.floor(mixed_var); } else{ return 0; } } // set num to: Infinity var num = 1 / 0; // using intval_modified to handle Infinity returns: 0 alert( intval_modified(num) ); // Output: 0 </code></pre> Basically, the changes are: <pre><code>if(isNaN(tmp)){</code></pre> to: <pre><code>if(isNaN(tmp) || !isFinite(tmp)){</code></pre> and... <pre><code>} else if( typeof( mixed_var ) == 'number' ){</code></pre> to: <pre><code>} else if( typeof( mixed_var ) == 'number' &amp;amp;&amp;amp; isFinite(mixed_var) ){</code></pre> <hr /> <strong> stensi </strong> on 2008-05-14 04:27:38 <br /> Just stumbled across your PHP.js library, awesome work Kevin :) I think your intval could be improved a little to handle JavaScripts &amp;quot;Infinity&amp;quot; value better. For example, using current PHP.js intval: <pre><code> function intval( mixed_var, base ) { // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: intval('Kevin van Zonneveld'); // * returns 1: 0 // * example 2: intval(4.2); // * returns 2: 4 // * example 3: intval(42, 8); // * returns 3: 42 var tmp; if( typeof( mixed_var ) == 'string' ){ tmp = parseInt(mixed_var); if(isNaN(tmp)){ return 0; } else{ return tmp.toString(base || 10); } } else if( typeof( mixed_var ) == 'number' ){ return Math.floor(mixed_var); } else{ return 0; } } // set num to: Infinity var num = 1 / 0; // using current PHP.js intval returns: Infinity alert( intval(num) ); // Output: Infinity </code></pre> Using modified intval to handle Infinity: <pre><code> function intval( mixed_var, base ) { // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: intval('Kevin van Zonneveld'); // * returns 1: 0 // * example 2: intval(4.2); // * returns 2: 4 // * example 3: intval(42, 8); // * returns 3: 42 var tmp; if( typeof( mixed_var ) == 'string' ){ tmp = parseInt(mixed_var); if(isNaN(tmp) || !isFinite(tmp)){ return 0; } else{ return tmp.toString(base || 10); } } else if( typeof( mixed_var ) == 'number' &amp;amp;&amp;amp; isFinite(mixed_var) ){ return Math.floor(mixed_var); } else{ return 0; } } // set num to: Infinity var num = 1 / 0; // using intval_modified to handle Infinity returns: 0 alert( intval_modified(num) ); // Output: 0 </code></pre> Basically, the changes are: <pre><code>if(isNaN(tmp)){</code></pre> to: <pre><code>if(isNaN(tmp) || !isFinite(tmp)){</code></pre> and... <pre><code>} else if( typeof( mixed_var ) == 'number' ){</code></pre> to: <pre><code>} else if( typeof( mixed_var ) == 'number' &amp;amp;&amp;amp; isFinite(mixed_var) ){</code></pre> <hr />