PHP's gettype in JavaScript

How to use

You you can install via yarn add locutus and require this function via const gettype = require('locutus/php/var/gettype').

It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.

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'

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.

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
}

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we assimilate first iterations with imperfections, hoping for others to take issue with-and improve them. This unorthodox approach has worked very well to foster fun and fruitful collaboration, but please be reminded to use our creations at your own risk. THE SOFTWARE IS PROVIDED "AS IS" has never been more true than for Locutus.

Now go and: [ View on GitHub | Edit on GitHub | View Raw ]


« More PHP var functions


Star