UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

547 lines (413 loc) 14.5 kB
<!-- Generated by Rakefile:build --> <strong> wHrOxPsVfW </strong> on 2012-09-13 03:17:27 <br /> <hr /> <strong> David Lundgren </strong> on 2012-09-12 09:12:39 <br /> It would be nice to add the following otherwise date objects are always detected as being empty. <pre><code> if (mixed_var instanceof Date) { return isNaN(Number(mixed_var)); } </code></pre> Idea taken from http://frugalcoder.us/post/2010/02/15/js-is-empty.aspx <hr /> <strong> mildevivy </strong> on 2012-08-23 06:36:56 <br /> <hr /> <strong> Rafal </strong> on 2012-06-21 09:27:00 <br /> @pabrams: thanks. I've modified the function to make it more readable and got rid of most lint warnings. Updated code available on github (https://raw.github.com/kvz/phpjs/master/functions/var/empty.js) <hr /> <strong> pabrams </strong> on 2012-06-21 03:52:09 <br /> Hi, for the first two js lint issues, try some carriage returns. Line #26 will also be much easier to read, especially if you format it nicely. Each new line after <pre><code>if (mixed_var === &quot;&quot;</code></pre> could start with a constant number of tabs and then <pre><code>|| mixed_var === </code></pre>. How 'bout that? Personally, I find code like line #26 to be inexcusable because it's a real pain to read, and very easy to avoid, if using a decent editor. <hr /> <strong> CymnMda </strong> on 2012-06-09 01:42:25 <br /> buy azithro http://zithromaxs.com/ zithromax stomach upset <hr /> <strong> CasiariwarSic </strong> on 2012-05-22 09:02:51 <br /> <hr /> <strong> pletcherdgh </strong> on 2012-04-04 13:18:33 <br /> <hr /> <strong> <a href="http://credit.sotatka.ru/" rel="nofollow">excemeexhaumP</a> </strong> on 2012-03-19 18:53:25 <br /> +1 <hr /> <strong> carpinteyronyc </strong> on 2012-03-15 06:11:29 <br /> <hr /> <strong> <a href="schemes-shop.com/shop/grundig_p37_060_service.html" rel="nofollow">Drug-Esota</a> </strong> on 2011-12-16 09:57:00 <br /> <hr /> <strong> Sany-heem </strong> on 2011-11-19 15:50:39 <br /> <hr /> <strong> Stephan </strong> on 2011-11-16 09:54:29 <br /> Hello, When testing a Date-Object, it always returns true: it's an object but has no keys. No idea if this is the best solution. It works for me. I check if getMonth is available. Maybe there is a better way? <pre><code> if (typeof mixed_var == 'object') { for (key in mixed_var) { return false; } if (mixed_var.getMonth) { return false; } return true; } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-10-07 23:59:33 <br /> @max4ever: Sorry, I mean it returns false--it indicates it is non-empty. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-10-07 23:58:19 <br /> @max4ever: I don't know if you have any older version of the function or whether you were guessing that an array would not return &quot;object&quot; as a type (as it does in JavaScript even for arrays which are themselves objects), but in your example the empty() call returns true. <hr /> <strong> max4ever </strong> on 2011-10-07 12:42:50 <br /> I think checks for array should be added, it doesn't work with <pre><code> var my_ditta = new Array(); my_ditta['tasse'] = 1; my_ditta['cartonaggio'] = 1; empty(my_ditta); //=&gt; gives true, but it's obviously false </code></pre> <hr /> <strong> <a href="http://pharmacyworldrx.com/" rel="nofollow">utenothence</a> </strong> on 2011-09-07 13:15:23 <br /> <hr /> <strong> l </strong> on 2011-08-02 04:14:40 <br /> lol <hr /> <strong> <a href="http://topbrandszone.com/" rel="nofollow">bitPlaftwat</a> </strong> on 2011-07-05 07:12:06 <br /> <hr /> <strong> ProownCibbowl </strong> on 2011-07-04 21:25:24 <br /> <hr /> <strong> Krinkle </strong> on 2010-10-23 22:48:58 <br /> The JSLint warning about for &quot;for in&quot; is quite correct. If an array has prototypes it returns false (&quot;not empty&quot;). To fix this, one can simply check the .length to be 0 strictly. <pre><code> function empty (v) { var key; if (v === &quot;&quot; || v === 0 || v === &quot;0&quot; || v === null || v === false || typeof v === 'undefined') { return true; } if (v.length === 0) { return true; } if (typeof v === 'object') { for (key in v) { return false; } return true; } return false; } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-06-21 04:54:23 <br /> Kevin, I believe dmitriy means filtering via hasOwnProperty inside the for loop. I can make the change, but for this function I wonder whether some users might like to verify that the array/object is _really_ empty (including of prototype-inherited properties). Thoughts? Maybe add an ini_set() configuration option to allow both cases? <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2010-06-19 16:28:00 <br /> @ dmitriy kulichkin: Could you maybe provide an example? <hr /> <strong> dmitriy kulichkin </strong> on 2010-06-09 19:02:18 <br /> empty([]) will fail if we extend Array with some methods via prototype. I suppose we should check our keys do not to be function. Does it have a sense? <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-11-16 13:23:27 <br /> @Stoyan Kyosev: I committed a change at http://github.com/kvz/phpjs/commit/dd40b8850a29c0617a40c7ca5ed2ae90fd720a22 to use the &quot;typeof&quot; check, which doesn't suffer from the same problem (nor will it encourage bad practices for those who see our code and don't know why we're doing it). However, I'm not really sure it is justified to do this for all our other functions which test for it since 1) It takes up a little more space, and 2) JavaScript has quite a few ways for you to redefine things (e.g., var isNaN = 5;). Any opinions? But it doesn't hurt, so there ya go... :) Thanks for the input... <hr /> <strong> <a href="http://www.svest.org" rel="nofollow">Stoyan Kyosev</a> </strong> on 2009-11-16 12:18:11 <br /> As noted in http://www.sitepoint.com/blogs/2009/11/12/google-closure-how-not-to-write-javascript/: &quot;This function checks if a particular variable has a value defined. Or it does, unless a 3rd party script sets the global undefined variable to something else. &quot; <pre><code> var undefined = 5; </code></pre> &quot;u might think that anyone who assigns a value to undefined deserves what they get, but the fix in this case is trivial: simply declare a local undefined variable for use within the function!simply declare a local undefined variable for use within the function!&quot; function empty(val) { var undefined; return val !== undefined; }; I suggest to use this &quot;fix&quot; in the current implementation of 'empty'. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-04-14 12:38:54 <br /> @ Knoxius: Sometimes the most simple functions require the most insane JavaScript code. Seen echo ? ;) <hr /> <strong> Brett Zamir </strong> on 2009-04-13 07:23:21 <br /> @Knoxius, PHP.JS is treating objects as associative arrays, including ones without a length property, so that wouldn't work for our project. We are also attempting to mimic PHP behavior fully for those familiar with PHP or wishing to use its approach. For example, your function would not consider any number to be non-empty, whereas PHP only considers 0 as such. Of course, your function might work just fine for your own purposes, but we're trying to build a reliable, consistent API, and PHP is the standard to go by (if we just made our own decisions at every turn, there wouldn't be much justification for sticking to PHP function names, as it'd probably just be more confusing for those who expected it to behave as in PHP). <hr /> <strong> <a href="http://knoxius.com/" rel="nofollow">Knoxius</a> </strong> on 2009-04-09 09:12:47 <br /> This version seems a bit complex for the mock-function. Couldn't it be simpler by doing something like this: <pre><code> function empty(string) { if(string.length == 0) { return true; } else { return false; } } </code></pre> It seems a lot simpler, and it still works perfectly fine. Example: <pre><code> var myString = 'This is a string.'; if(empty(null)) { //Returns true alert(&quot;It is empty.&quot;); } else if(empty(myString)) { //Returns false alert(&quot;It is not empty.&quot;); } </code></pre> There could be something about your code that is better, but it seems a bit too complex for what it could be. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-03-04 15:02:28 <br /> @ Marc Jansen: Fixed in svn, thank you! <hr /> <strong> <a href="http://selectoid.wordpress.com" rel="nofollow">Marc Jansen</a> </strong> on 2009-03-03 11:59:22 <br /> Nice work folks, but I think I found a bug: shouldn't the following code yield 'false'? <pre><code> var anObj = { 'aFunc' : function () { alert('humpty'); } }; alert( empty( anObj ) ); // alerts true, but the object contains an element </code></pre> This is IMHO wrong for arrays as well: <pre><code> var anArr = [ function () { alert( 'dumpty' ); } ]; alert( empty( anArr ) ) ; // alerts true, but the array contains an element </code></pre> Named functions should behave the same. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-02-11 02:39:23 <br /> @ Brett Zamir: wicked <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-02-10 08:59:02 <br /> If it's all right, Kevin, I'll change the text for !== 'function' to be a test for whether o.hasOwnProperty(i). That solves the prototype problem (though even that might be considered non-empty) but avoids the current issue of considering this to be empty: var o = {method:function(){}} <hr /> <strong> Brett Zamir </strong> on 2008-11-03 10:27:16 <br /> If you like, here's a little bit cleaner and slightly safer implementation, I think: <pre><code> function create_function (args, code) { //$newfunc = create_function('$a,$b', 'return $a + $b;'); // alert($newfunc(5, 4)); var argmnts = []; argmnts = args.split(/,\s*/); return Function.apply(null, argmnts.concat(code)); } </code></pre> But if one just wishes an anonymous function, they can just do: <pre><code>call_user_func_array(function(arg) {...}, $a);</code></pre> or if they need a string <pre><code>call_user_func_array(new Function($arg1, $code), $a);</code></pre> For one behaving more like PHP in returning a global name for the new function: <pre><code> function create_function (args, code) { // $newfunc = create_function('$a,$b', 'return $a + $b;'); // alert($newfunc); // 'lambda_1' // alert( window[$newfunc](5, 3) ); // 8 (need window to call it, since the PHP behavior is to return a string) var funcName = ''; if (!this.php_js) { this.php_js = {}; } if (!this.php_js.create_function_ct) { this.php_js.create_function_ct = 0; } this.php_js.create_function_ct++; funcName = 'lambda_'+this.php_js.create_function_ct; eval(funcName+' = function (' + args + ') { ' + code + '}'); return funcName; } </code></pre> <hr /> <strong> <a href="www.integrasoftwae.it" rel="nofollow">Francesco</a> </strong> on 2008-10-26 13:46:37 <br /> If you extend the Array or Object prototype, empty([]) and empty({}) return true... i've added if (typeof mixed_var[key] !== 'function' ) this test before returning false in the for cicle, thanks <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-10-06 16:49:26 <br /> @ Onno Marsman: Excellent thought and well executed Onno! <hr /> <strong> Onno Marsman </strong> on 2008-10-06 16:39:14 <br /> typeof array will be 'object' so the check for 'array' maxes no sense: <pre><code> function empty( mixed_var ) { if (mixed_var === &amp;quot;&amp;quot; || mixed_var === 0 || mixed_var === &amp;quot;0&amp;quot; || mixed_var === null || mixed_var === false || mixed_var === undefined || (mixed_var instanceof Array &amp;amp;&amp;amp; mixed_var.length === 0) ) { return true; } </code></pre> Something else: what about empty({}) ? If you consider {} an object as in PHP then it will never be empty. But I would never use it like that. It would make more sense to call empty({}) when {} could be considered as an associative array, and in that case it is empty and should return true: <pre><code> function empty( mixed_var ) { if (mixed_var === &amp;quot;&amp;quot; || mixed_var === 0 || mixed_var === &amp;quot;0&amp;quot; || mixed_var === null || mixed_var === false || mixed_var === undefined ){ return true; } if (typeof mixed_var == 'object') { for (var i in mixed_var) { return false; } return true; } return false; } </code></pre> Note that in this implementation I removed the line that handles arrays because the section that takes care of objects handles this correctly for arrays anyway. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-09-05 21:36:24 <br /> @ Onno Marsman: Thanks again man! Remember if you have a homepage or something, I can add it to the credit sections. <hr /> <strong> Onno Marsman </strong> on 2008-09-04 16:36:10 <br /> should also return true on undefined <hr /> <strong> IT </strong> on 2008-01-26 18:33:59 <br /> Awesome! Exactly what I needed ;) <hr /> <strong> <a href="http://selectoid.wordpress.com" rel="nofollow">Marc Jansen</a> </strong> on 2009-03-03 11:47:37 <br /> Nice work folks, but I think I found a bug: shouldn't the following code yield 'false'? <pre><code> var anObj = { 'aFunc' : function () { alert('humpty'); } }; alert( empty(anObj) ) // alerts true, but the object contains an element </code></pre> // is IMHO wrong for arrays as well: <pre><code> var anArr = [ function () { alert('humpty'); } ]; alert( empty(anArr) ) // alerts true, but the array contains an element </code></pre> Named functions should behave the same. <hr />