PHP's gettype in JavaScript

Here’s what our current JavaScript equivalent to PHP's gettype looks like.

module.exports = function gettype (mixedVar) {
// discuss at: https://locutus.io/php/gettype/
// original by: Paulo Freitas
// improved by: Kevin van Zonneveld (https://kvz.io)
// improved by: Douglas Crockford (https://javascript.crockford.com)
// improved by: Brett Zamir (https://brett-zamir.me)
// input by: KELAN
// note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
// note 1: it different from the PHP implementation. We can't fix this unfortunately.
// example 1: gettype(1)
// returns 1: 'integer'
// example 2: gettype(undefined)
// returns 2: 'undefined'
// example 3: gettype({0: 'Kevin van Zonneveld'})
// returns 3: 'object'
// example 4: gettype('foo')
// returns 4: 'string'
// example 5: gettype({0: function () {return false;}})
// returns 5: 'object'
// example 6: gettype({0: 'test', length: 1, splice: function () {}})
// returns 6: 'object'
// example 7: gettype(['test'])
// returns 7: 'array'
const isFloat = require('../var/is_float')
let s = typeof mixedVar
let name
const _getFuncName = function (fn) {
const name = (/\W*function\s+([\w$]+)\s*\(/).exec(fn)
if (!name) {
return '(Anonymous)'
}
return name[1]
}
if (s === 'object') {
if (mixedVar !== null) {
// From: https://javascript.crockford.com/remedial.html
// @todo: Break up this lengthy if statement
if (typeof mixedVar.length === 'number' &&
!(mixedVar.propertyIsEnumerable('length')) &&
typeof mixedVar.splice === 'function') {
s = 'array'
} else if (mixedVar.constructor && _getFuncName(mixedVar.constructor)) {
name = _getFuncName(mixedVar.constructor)
if (name === 'Date') {
// not in PHP
s = 'date'
} else if (name === 'RegExp') {
// not in PHP
s = 'regexp'
} else if (name === 'LOCUTUS_Resource') {
// Check against our own resource constructor
s = 'resource'
}
}
} else {
s = 'null'
}
} else if (s === 'number') {
s = isFloat(mixedVar) ? 'double' : 'integer'
}
return s
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/var/gettype'). You could also require the var module in full so that you could access var.gettype instead.

If you intend to target the browser, you can then use a module bundler such as Parcel, webpack, Browserify, or rollup.js. This can be important because Locutus allows modern JavaScript in the source files, meaning it may not work in all browsers without a build/transpile step. Locutus does transpile all functions to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • 1.0 is simplified to 1 before it can be accessed by the function, this makes it different from the PHP implementation. We can’t fix this unfortunately.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1gettype(1)'integer'
2gettype(undefined)'undefined'
3gettype({0: 'Kevin van Zonneveld'})'object'
4gettype('foo')'string'
5gettype({0: function () {return false;}})'object'
6gettype({0: 'test', length: 1, splice: function () {}})'object'
7gettype(['test'])'array'

« More PHP var functions


Star