PageRenderTime 20ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/functions/var/is_array.js

https://gitlab.com/orvi2014/phpjs
JavaScript | 81 lines | 37 code | 5 blank | 39 comment | 14 complexity | 92d24822b77b4346650c4ce49d5f136f MD5 | raw file
  1. function is_array(mixed_var) {
  2. // discuss at: http://phpjs.org/functions/is_array/
  3. // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  4. // improved by: Legaev Andrey
  5. // improved by: Onno Marsman
  6. // improved by: Brett Zamir (http://brett-zamir.me)
  7. // improved by: Nathan Sepulveda
  8. // improved by: Brett Zamir (http://brett-zamir.me)
  9. // bugfixed by: Cord
  10. // bugfixed by: Manish
  11. // bugfixed by: Brett Zamir (http://brett-zamir.me)
  12. // note: In php.js, javascript objects are like php associative arrays, thus JavaScript objects will also
  13. // note: return true in this function (except for objects which inherit properties, being thus used as objects),
  14. // note: unless you do ini_set('phpjs.objectsAsArrays', 0), in which case only genuine JavaScript arrays
  15. // note: will return true
  16. // example 1: is_array(['Kevin', 'van', 'Zonneveld']);
  17. // returns 1: true
  18. // example 2: is_array('Kevin van Zonneveld');
  19. // returns 2: false
  20. // example 3: is_array({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});
  21. // returns 3: true
  22. // example 4: is_array(function tmp_a(){this.name = 'Kevin'});
  23. // returns 4: false
  24. var ini,
  25. _getFuncName = function(fn) {
  26. var name = (/\W*function\s+([\w\$]+)\s*\(/)
  27. .exec(fn);
  28. if (!name) {
  29. return '(Anonymous)';
  30. }
  31. return name[1];
  32. };
  33. _isArray = function(mixed_var) {
  34. // return Object.prototype.toString.call(mixed_var) === '[object Array]';
  35. // The above works, but let's do the even more stringent approach: (since Object.prototype.toString could be overridden)
  36. // Null, Not an object, no length property so couldn't be an Array (or String)
  37. if (!mixed_var || typeof mixed_var !== 'object' || typeof mixed_var.length !== 'number') {
  38. return false;
  39. }
  40. var len = mixed_var.length;
  41. mixed_var[mixed_var.length] = 'bogus';
  42. // The only way I can think of to get around this (or where there would be trouble) would be to have an object defined
  43. // with a custom "length" getter which changed behavior on each call (or a setter to mess up the following below) or a custom
  44. // setter for numeric properties, but even that would need to listen for specific indexes; but there should be no false negatives
  45. // and such a false positive would need to rely on later JavaScript innovations like __defineSetter__
  46. if (len !== mixed_var.length) {
  47. // We know it's an array since length auto-changed with the addition of a
  48. // numeric property at its length end, so safely get rid of our bogus element
  49. mixed_var.length -= 1;
  50. return true;
  51. }
  52. // Get rid of the property we added onto a non-array object; only possible
  53. // side-effect is if the user adds back the property later, it will iterate
  54. // this property in the older order placement in IE (an order which should not
  55. // be depended on anyways)
  56. delete mixed_var[mixed_var.length];
  57. return false;
  58. };
  59. if (!mixed_var || typeof mixed_var !== 'object') {
  60. return false;
  61. }
  62. // BEGIN REDUNDANT
  63. this.php_js = this.php_js || {};
  64. this.php_js.ini = this.php_js.ini || {};
  65. // END REDUNDANT
  66. ini = this.php_js.ini['phpjs.objectsAsArrays'];
  67. return _isArray(mixed_var) ||
  68. // Allow returning true unless user has called
  69. // ini_set('phpjs.objectsAsArrays', 0) to disallow objects as arrays
  70. ((!ini || (// if it's not set to 0 and it's not 'off', check for objects as arrays
  71. (parseInt(ini.local_value, 10) !== 0 && (!ini.local_value.toLowerCase || ini.local_value.toLowerCase() !==
  72. 'off')))) && (
  73. Object.prototype.toString.call(mixed_var) === '[object Object]' && _getFuncName(mixed_var.constructor) ===
  74. 'Object' // Most likely a literal and intended as assoc. array
  75. ));
  76. }