phpjs
Version:
145 lines (127 loc) • 5.64 kB
Markdown
---
layout: page
title: "JavaScript is_array function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/is_array:437
- /functions/view/is_array
- /functions/view/437
- /functions/is_array:437
- /functions/437
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's is_array
{% codeblock var/is_array.js lang:js https://raw.github.com/kvz/phpjs/master/functions/var/is_array.js raw on github %}
function is_array (mixed_var) {
// From: http://phpjs.org/functions
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Legaev Andrey
// + bugfixed by: Cord
// + bugfixed by: Manish
// + improved by: Onno Marsman
// + improved by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + improved by: Nathan Sepulveda
// + improved by: Brett Zamir (http://brett-zamir.me)
// % note 1: In php.js, javascript objects are like php associative arrays, thus JavaScript objects will also
// % note 1: return true in this function (except for objects which inherit properties, being thus used as objects),
// % note 1: unless you do ini_set('phpjs.objectsAsArrays', 0), in which case only genuine JavaScript arrays
// % note 1: will return true
// * example 1: is_array(['Kevin', 'van', 'Zonneveld']);
// * returns 1: true
// * example 2: is_array('Kevin van Zonneveld');
// * returns 2: false
// * example 3: is_array({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});
// * returns 3: true
// * example 4: is_array(function tmp_a(){this.name = 'Kevin'});
// * returns 4: false
var ini,
_getFuncName = function (fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
},
_isArray = function (mixed_var) {
// return Object.prototype.toString.call(mixed_var) === '[object Array]';
// The above works, but let's do the even more stringent approach: (since Object.prototype.toString could be overridden)
// Null, Not an object, no length property so couldn't be an Array (or String)
if (!mixed_var || typeof mixed_var !== 'object' || typeof mixed_var.length !== 'number') {
return false;
}
var len = mixed_var.length;
mixed_var[mixed_var.length] = 'bogus';
// The only way I can think of to get around this (or where there would be trouble) would be to have an object defined
// with a custom "length" getter which changed behavior on each call (or a setter to mess up the following below) or a custom
// setter for numeric properties, but even that would need to listen for specific indexes; but there should be no false negatives
// and such a false positive would need to rely on later JavaScript innovations like __defineSetter__
if (len !== mixed_var.length) { // We know it's an array since length auto-changed with the addition of a
// numeric property at its length end, so safely get rid of our bogus element
mixed_var.length -= 1;
return true;
}
// Get rid of the property we added onto a non-array object; only possible
// side-effect is if the user adds back the property later, it will iterate
// this property in the older order placement in IE (an order which should not
// be depended on anyways)
delete mixed_var[mixed_var.length];
return false;
};
if (!mixed_var || typeof mixed_var !== 'object') {
return false;
}
// BEGIN REDUNDANT
this.php_js = this.php_js || {};
this.php_js.ini = this.php_js.ini || {};
// END REDUNDANT
ini = this.php_js.ini['phpjs.objectsAsArrays'];
return _isArray(mixed_var) ||
// Allow returning true unless user has called
// ini_set('phpjs.objectsAsArrays', 0) to disallow objects as arrays
((!ini || ( // if it's not set to 0 and it's not 'off', check for objects as arrays
(parseInt(ini.local_value, 10) !== 0 && (!ini.local_value.toLowerCase || ini.local_value.toLowerCase() !== 'off')))
) && (
Object.prototype.toString.call(mixed_var) === '[object Object]' && _getFuncName(mixed_var.constructor) === 'Object' // Most likely a literal and intended as assoc. array
));
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/var/is_array.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/var/is_array.js)
### Example 1
This code
{% codeblock lang:js example %}
is_array(['Kevin', 'van', 'Zonneveld']);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
is_array('Kevin van Zonneveld');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
false
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
is_array({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}
### Other PHP functions in the var extension
{% render_partial _includes/custom/var.html %}