Skip to content

Commit f761394

Browse files
committed
some initial gettype work
1 parent 0a28fa0 commit f761394

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

functions/var/gettype.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ function gettype(mixed_var) {
66
// improved by: Brett Zamir (http://brett-zamir.me)
77
// input by: KELAN
88
// depends on: is_float
9+
// note: php.js treats objects as associative arrays, so unless config is set, it will return "array" for objects
910
// note: 1.0 is simplified to 1 before it can be accessed by the function, this makes
1011
// note: it different from the PHP implementation. We can't fix this unfortunately.
1112
// example 1: gettype(1);
1213
// returns 1: 'integer'
1314
// example 2: gettype(undefined);
1415
// returns 2: 'undefined'
15-
// example 3: gettype({0: 'Kevin van Zonneveld'});
16-
// returns 3: 'object'
17-
// example 4: gettype('foo');
18-
// returns 4: 'string'
19-
// example 5: gettype({0: function () {return false;}});
20-
// returns 5: 'object'
21-
// example 6: gettype({0: 'test', length: 1, splice: function () {}});
22-
// example 6: gettype(['test']);
23-
// returns 6: 'object'
16+
// example 3: gettype('foo');
17+
// returns 3: 'string'
18+
// example 4: gettype(['test']);
19+
// returns 4: 'array'
20+
// example 5: gettype({0: 'Kevin van Zonneveld'});
21+
// returns 5: 'array'
22+
// example 6: gettype({0: function () {return false;}});
2423
// returns 6: 'array'
24+
// example 7: gettype({0: 'test', length: 1, splice: function () {}});
25+
// returns 7: 'array'
2526

2627
var s = typeof mixed_var,
2728
name;
@@ -37,7 +38,22 @@ function gettype(mixed_var) {
3738
if (mixed_var !== null) { // From: http://javascript.crockford.com/remedial.html
3839
if (typeof mixed_var.length === 'number' && !(mixed_var.propertyIsEnumerable('length')) && typeof mixed_var
3940
.splice === 'function') {
40-
s = 'array';
41+
42+
// BEGIN REDUNDANT
43+
this.php_js = this.php_js || {};
44+
this.php_js.ini = this.php_js.ini || {};
45+
// END REDUNDANT
46+
47+
// Call ini_set('phpjs.objectsAsArrays', 0) to disallow objects as arrays
48+
ini = this.php_js.ini['phpjs.objectsAsArrays'];
49+
if (!ini || ( // if it's not set to 0 and it's not 'off', check for objects as arrays
50+
(parseInt(ini.local_value, 10) !== 0 && (!ini.local_value.toLowerCase || ini.local_value.toLowerCase() !==
51+
'off')))) {
52+
s = 'object';
53+
}
54+
else {
55+
s = 'array';
56+
}
4157
} else if (mixed_var.constructor && getFuncName(mixed_var.constructor)) {
4258
name = getFuncName(mixed_var.constructor);
4359
if (name === 'Date') {

0 commit comments

Comments
 (0)