mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
47 lines (44 loc) • 1.59 kB
JavaScript
'use strict';
function factory(type, config, load, typed) {
var isNumeric = load(require('./isNumeric'));
/**
* Test whether a value is an numeric value.
*
* In case of a string, true is returned if the string contains a numeric value.
*
* Syntax:
*
* math.hasNumericValue(x)
*
* Examples:
*
* math.hasNumericValue(2) // returns true
* math.hasNumericValue('2') // returns true
* math.isNumeric('2') // returns false
* math.hasNumericValue(0) // returns true
* math.hasNumericValue(math.bignumber(500)) // returns true
* math.hasNumericValue(math.fraction(4)) // returns true
* math.hasNumericValue(math.complex('2-4i') // returns false
* math.hasNumericValue([2.3, 'foo', false]) // returns [true, false, true]
*
* See also:
*
* isZero, isPositive, isNegative, isInteger, isNumeric
*
* @param {*} x Value to be tested
* @return {boolean} Returns true when `x` is a `number`, `BigNumber`,
* `Fraction`, `Boolean`, or a `String` containing number. Returns false for other types.
* Throws an error in case of unknown types.
*/
var hasNumericValue = typed('hasNumericValue', {
'string': function string(x) {
return x.trim().length > 0 && !isNaN(Number(x));
},
'any': function any(x) {
return isNumeric(x);
}
});
return hasNumericValue;
}
exports.name = 'hasNumericValue';
exports.factory = factory;