UNPKG

mathjs

Version:

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.

45 lines (39 loc) 1.01 kB
'use strict'; /** * Determine the type of a variable * * type(x) * * The following types are recognized: * * 'undefined' * 'null' * 'boolean' * 'number' * 'string' * 'Array' * 'Function' * 'Date' * 'RegExp' * 'Object' * * @param {*} x * @return {string} Returns the name of the type. Primitive types are lower case, * non-primitive types are upper-camel-case. * For example 'number', 'string', 'Array', 'Date'. */ exports.type = function(x) { var type = typeof x; if (type === 'object') { if (x === null) return 'null'; if (x instanceof Boolean) return 'boolean'; if (x instanceof Number) return 'number'; if (x instanceof String) return 'string'; if (Array.isArray(x)) return 'Array'; if (x instanceof Date) return 'Date'; if (x instanceof RegExp) return 'RegExp'; return 'Object'; } if (type === 'function') return 'Function'; return type; };