Skip to content

Commit 96d881a

Browse files
committed
Revise is_array a bit
1 parent 8262e9e commit 96d881a

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

functions/var/is_array.js

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ function is_array (mixed_var) {
77
// + improved by: Onno Marsman
88
// + improved by: Brett Zamir (http://brett-zamir.me)
99
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
10+
// + improved by: Nathan
11+
// + improved by: Brett Zamir (http://brett-zamir.me)
1012
// % note 1: In php.js, javascript objects are like php associative arrays, thus JavaScript objects will also
11-
// % note 1: return true in this function (except for objects which inherit properties, being thus used as objects),
12-
// % note 1: unless you do ini_set('phpjs.objectsAsArrays', true), in which case only genuine JavaScript arrays
13+
// % note 1: return true in this function (except for objects which inherit properties, being thus used as objects),
14+
// % note 1: unless you do ini_set('phpjs.objectsAsArrays', 0), in which case only genuine JavaScript arrays
1315
// % note 1: will return true
1416
// * example 1: is_array(['Kevin', 'van', 'Zonneveld']);
1517
// * returns 1: true
@@ -20,50 +22,42 @@ function is_array (mixed_var) {
2022
// * example 4: is_array(function tmp_a(){this.name = 'Kevin'});
2123
// * returns 4: false
2224

23-
var key = '';
24-
var getFuncName = function (fn) {
25-
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
26-
if (!name) {
27-
return '(Anonymous)';
28-
}
29-
return name[1];
30-
};
31-
32-
if (!mixed_var) {
25+
var _getFuncName = function (fn) {
26+
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
27+
if (!name) {
28+
return '(Anonymous)';
29+
}
30+
return name[1];
31+
},
32+
_isArray = function (mixed_var) {
33+
return Object.prototype.toString.call(mixed_var) === '[object Array]';
34+
// Other approaches:
35+
// && mixed_var.hasOwnProperty('length') && // Not non-enumerable because of being on parent class
36+
// !mixed_var.propertyIsEnumerable('length') && // Since is own property, if not enumerable, it must be a built-in function
37+
// _getFuncName(mixed_var.constructor) !== 'String'; // exclude String(), but not another function returning String()
38+
};
39+
40+
if (!mixed_var || typeof mixed_var !== 'object') {
3341
return false;
3442
}
3543

3644
// BEGIN REDUNDANT
3745
this.php_js = this.php_js || {};
3846
this.php_js.ini = this.php_js.ini || {};
3947
// END REDUNDANT
40-
41-
if (typeof mixed_var === 'object') {
42-
43-
if (this.php_js.ini['phpjs.objectsAsArrays'] && // Strict checking for being a JavaScript array (only check this way if call ini_set('phpjs.objectsAsArrays', 0) to disallow objects as arrays)
44-
(
45-
(this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase &&
46-
this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase() === 'off') ||
47-
parseInt(this.php_js.ini['phpjs.objectsAsArrays'].local_value, 10) === 0)
48-
) {
49-
return mixed_var.hasOwnProperty('length') && // Not non-enumerable because of being on parent class
50-
!mixed_var.propertyIsEnumerable('length') && // Since is own property, if not enumerable, it must be a built-in function
51-
getFuncName(mixed_var.constructor) !== 'String'; // exclude String()
52-
}
53-
54-
if (mixed_var.hasOwnProperty) {
55-
for (key in mixed_var) {
56-
// Checks whether the object has the specified property
57-
// if not, we figure it's not an object in the sense of a php-associative-array.
58-
if (false === mixed_var.hasOwnProperty(key)) {
59-
return false;
60-
}
61-
}
62-
}
63-
64-
// Read discussion at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_is_array/
65-
return true;
66-
}
67-
68-
return false;
69-
}
48+
var ini = this.php_js.ini['phpjs.objectsAsArrays'];
49+
50+
return _isArray(mixed_var) ||
51+
// Allow returning true unless user has called
52+
// ini_set('phpjs.objectsAsArrays', 0) to disallow objects as arrays
53+
(!ini ||
54+
( // if it's not set to 0 and it's not 'off', check for objects as arrays
55+
(parseInt(ini.local_value, 10) !== 0 &&
56+
(!ini.local_value.toLowerCase || ini.local_value.toLowerCase() !== 'off'))
57+
)
58+
) &&
59+
(
60+
Object.prototype.toString.call(mixed_var) === '[object Object]' &&
61+
_getFuncName(mixed_var.constructor) === 'Object' // Most likely a literal and intended as assoc. array
62+
);
63+
}

0 commit comments

Comments
 (0)