mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
'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;
|
|
};
|
|
|
|
/**
|
|
* Test whether a value is a scalar
|
|
* @param x
|
|
* @return {boolean} Returns true when x is a scalar, returns false when
|
|
* x is a Matrix or Array.
|
|
*/
|
|
exports.isScalar = function (x) {
|
|
return !((x && x.isMatrix) || Array.isArray(x));
|
|
};
|