JAVASCRIPT   8

array_search.js

Guest on 23rd August 2021 04:47:37 PM

  1. /**<dependency>[array_search]</dependency>**/
  2. function array_search (needle, haystack, argStrict) { /* eslint-disable-line camelcase*/
  3.  
  4.   var strict = !!argStrict;
  5.   var key = '';
  6.  
  7.   if (typeof needle === 'object' && needle.exec) {
  8.     /*[ Duck-type for RegExp ]*/
  9.     if (!strict) {
  10.       /*[ Let's consider case sensitive searches as strict ]*/
  11.       var flags = 'i' + (needle.global ? 'g' : '') +
  12.         (needle.multiline ? 'm' : '') +
  13.         /*[ sticky is FF only ]*/
  14.         (needle.sticky ? 'y' : '');
  15.       needle = new RegExp(needle.source, flags);
  16.     }
  17.     for (key in haystack) {
  18.       if (haystack.hasOwnProperty(key)) {
  19.         if (needle.test(haystack[key])) {
  20.           return key;
  21.         }
  22.       }
  23.     }
  24.     return false;
  25.   }
  26.  
  27.   for (key in haystack) {
  28.     if (haystack.hasOwnProperty(key)) {
  29.       if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) { /* eslint-disable-line eqeqeq*/
  30.         return key;
  31.       }
  32.     }
  33.   }
  34.  
  35.   return false;
  36. };

Raw Paste


Login or Register to edit or fork this paste. It's free.