phpjs
Version:
78 lines (73 loc) • 3.88 kB
JavaScript
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
));
}