UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

381 lines (297 loc) 10.5 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://lucido-media.de/" rel="nofollow">Abro - Lucido Media GbR</a> </strong> on 2012-07-15 21:11:27 <br /> Please consider using indexOf() [ see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf ] if available. <hr /> <strong> <a href="www.mahbubblog.com" rel="nofollow">Mahbubur Rahman</a> </strong> on 2011-08-15 23:34:43 <br /> People who are using jQuery in their project can use $.inArray which gives returns 1 when match is found and -1 when not found. <hr /> <strong> Theriault </strong> on 2011-05-17 21:02:25 <br /> @callumacrae: The indexOf method on the Array object is not supported by Internet Explorer. Also, the PHP JS function can handle Object. <hr /> <strong> <a href="http://lynxphp.com/" rel="nofollow">callumacrae</a> </strong> on 2011-05-17 14:26:01 <br /> This might just be me being stupid, but: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf Why not just force that to be boolean? <hr /> <strong> luigifab </strong> on 2011-01-29 17:31:08 <br /> @Brett Zamir : There is a problem with array in IE if you use my function in_array without the !isNaN(key). That's it. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2011-01-05 13:33:26 <br /> @luigifab: What problem are you referring to? Why would the key need to be a number? (php.js treats objects as associative arrays) What is the relation to IE? <hr /> <strong> luigifab </strong> on 2011-01-05 12:03:27 <br /> Here a recursive function. But, there is a problem with IE without the !isNaN(key). <pre><code> function in_array(needle, haystack) { if ((needle === null) || (haystack === null)) return false; var key = null; if (needle instanceof Array) { for (key in needle) if (needle.hasOwnProperty(key)) { if (in_array(needle[key], haystack)) return true; } } else { for (key in haystack) if (haystack.hasOwnProperty(key)) { if (!isNaN(key) &amp;&amp; (haystack[key] === needle)) return true; } } return false; } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-11-27 04:11:34 <br /> @Jordi: Thanks for the submission. Kevin commented on this earlier that although he supports DRY (Don't Repeat Yourself), it is faster in this case to be longer, since with the way we have it now, there is only need to check the &quot;strict&quot; argument once. So, we will stick with the way it is. Btw, if you are using your version, you should drop the first &quot;if&quot; since it prevents dealing with non-strict cases. Thanks again! <hr /> <strong> Jordi </strong> on 2009-11-26 14:22:36 <br /> Of course that had to be: <pre><code> function in_array (needle, haystack, argStrict) { var key = '', strict = !!argStrict; if (strict) for (key in haystack) if ( (strict &amp;&amp; haystack[key] === needle) || ( !strict &amp;&amp; strict &amp;&amp; haystack[key] == needle ) ) return true; return false; } </code></pre> <hr /> <strong> Jordi </strong> on 2009-11-26 11:06:18 <br /> Shorter: <pre><code> function in_array (needle, haystack, argStrict) { var key = '', strict = !!argStrict; if (strict) for (key in haystack) if ( (strict &amp;&amp; haystack[key] === needle) ) || ( !strict &amp;&amp; strict &amp;&amp; haystack[key] == needle ) return true; return false; } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-10-18 07:03:04 <br /> @billy: Thanks, fixed now in git: http://github.com/kvz/phpjs/commit/e37089b71ed1d32d1b4b4567c70a684c9d0e16f5 Sorry Drydenmaker and Kevin, it was a noble attempt, but search should be checked against -1, and even if that were corrected, there would still be problems as your version would make in_array('needle', ['needles']) return true. <hr /> <strong> Theriault </strong> on 2009-10-18 06:49:30 <br /> I believe the solution to the bug Billy found is that the native JavaScript String.prototype.search returns -1 if the needle is not found, not false like in PHP, what this function expects. Also, turning an array into a string and searching for the needle, while faster, will be incorrect in many situations, such as searching for 'he' against ['the'] will return true, even though 'he' is not in the array. <hr /> <strong> Billy </strong> on 2009-10-17 23:38:26 <br /> Is it just me or does in_array(&quot;whatever&quot;, [&quot;notit&quot;]) return true... This function is majorly broken. Strings arguments always return true, even if the array is empty! <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-08 20:59:44 <br /> @ Drydenmaker: Good point. Didn't bench it but obviously an internal function would be faster than anything we can hack. However your implementation relies on this.is_string which is not a php.js feature. I replaced it with (typeof(needle)=='string') but it seems that also the second test case (where we don't want to find 'vlado') fails. So it needs a little bit of work still. <hr /> <strong> Drydenmaker </strong> on 2009-09-22 18:17:21 <br /> what if you flattened the non-strict side to a string and used a string search to avoid the loop. Something like: <pre><code> in_array: function (needle, haystack, argStrict) { var key = '', strict = !!argStrict; if (strict) { for (key in haystack) { if (haystack[key] === needle) { return true; } } } else { if (this.is_string(needle)) { str = haystack.toString(); return (str.search(needle) !== false) } for (key in haystack) { if (haystack[key] == needle) { return true; } } } return false; } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-05-31 20:27:49 <br /> @ vlado houba: Yes using hash tables is really fast. Obviously we don't control our user's data. But we can make an effort to make in_array as fast as reasonably possible, and I see your optimization is not to check for strict every iteration, but check for it once, and use duplicated loops to do further processing. Though I'm a big fan of DRY, this really makes sense to me and justifies exception so I will implement your proposal. Thanks for sharing! <hr /> <strong> vlado houba </strong> on 2009-05-31 12:46:33 <br /> sorry for posting that same content by mistake... maybee it seems to be an unimporatnt change but i was working on a text analyzing script where things like this one matters a lot for fast searching in small arrays i recommend using the value (string or number) as an index (for sure just for short values..), it much cuts the access times and can be used for advanced structures <pre><code> function in_array(needle, haystack, argStrict) { var found = new Boolean(false); var key; var strict = new Boolean(argStrict); if(strict) { for (key in haystack) { if (haystack[key] === needle) { found = true; break; } } } else { for (key in haystack) { if (haystack[key] == needle) { found = true; break; } } } return found; } </code></pre> <hr /> <strong> Brett Zamir </strong> on 2009-04-05 15:04:05 <br /> @Vladimir, it looks like you posted the same code as we currently have... One very small optimization I do see is &quot;return true&quot; and &quot;return false&quot; instead of found=true, etc. If your optimization was using indexOf, be aware that PHP.JS considers objects to be arrays, so we need to handle them as well... <hr /> <strong> Vladimir Houba </strong> on 2009-04-05 13:39:05 <br /> function's preformance can be improoved by following <pre><code> 01.function in_array(needle, haystack, argStrict) { 02. // Checks if the given value exists in the array 03. // 04. // version: 903.1614 05. // discuss at: http://phpjs.org/functions/in_array 06. // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 07. // * example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']); 08. // * returns 1: true 09. var found = false, key, strict = !!argStrict; 10. 11. for (key in haystack) { 12. if ((strict &amp;&amp; haystack[key] === needle) || (!strict &amp;&amp; haystack[key] == needle)) { 13. found = true; 14. break; 15. } 16. } return found; } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-02-15 15:42:54 <br /> @ Java Dude: No but it has been tested (successfully) in Rhino and I believe Google based their js engine on that. Do you have more information (maybe a line number or something) for us to go on? Thanks in advance! <hr /> <strong> Java Dude </strong> on 2009-02-12 22:14:22 <br /> Has this function been tested on Google Chrome? I am receiving a: Uncaught TypError when using it but on in Google Chrome. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-06-18 17:26:29 <br /> @ peter: Thank you but according to the PHP manual in_array should return a boolean value. Also, your version doesn't support the optional strict argument, so in it's current form I'm not convinced I should replace our version with yours. <hr /> <strong> peter </strong> on 2008-06-17 15:55:02 <br /> <pre><code> function in_array(value, arr) { var key; for (key in arr) { if (arr[key] === value) {return value; } } return null; //false } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-05-12 22:51:47 <br /> @ Adam: If I'm not mistaken this forces strict to be boolean. A single exclamation mark would indeed reverse it's meaning. <hr /> <strong> Adam </strong> on 2008-05-12 20:28:33 <br /> <pre><code> var found = false, key, strict = !!strict; </code></pre> why are you defining strict to be NOT NOT strict ? <hr />