UNPKG

1.58 kBJavaScriptView Raw
1'use strict'
2
3function factory (type, config, load, typed) {
4 const isNumeric = load(require('./isNumeric'))
5 /**
6 * Test whether a value is an numeric value.
7 *
8 * In case of a string, true is returned if the string contains a numeric value.
9 *
10 * Syntax:
11 *
12 * math.hasNumericValue(x)
13 *
14 * Examples:
15 *
16 * math.hasNumericValue(2) // returns true
17 * math.hasNumericValue('2') // returns true
18 * math.isNumeric('2') // returns false
19 * math.hasNumericValue(0) // returns true
20 * math.hasNumericValue(math.bignumber(500)) // returns true
21 * math.hasNumericValue(math.fraction(4)) // returns true
22 * math.hasNumericValue(math.complex('2-4i') // returns false
23 * math.hasNumericValue([2.3, 'foo', false]) // returns [true, false, true]
24 *
25 * See also:
26 *
27 * isZero, isPositive, isNegative, isInteger, isNumeric
28 *
29 * @param {*} x Value to be tested
30 * @return {boolean} Returns true when `x` is a `number`, `BigNumber`,
31 * `Fraction`, `Boolean`, or a `String` containing number. Returns false for other types.
32 * Throws an error in case of unknown types.
33 */
34 const hasNumericValue = typed('hasNumericValue', {
35 'string': function (x) {
36 return x.trim().length > 0 && !isNaN(Number(x))
37 },
38 'any': function (x) {
39 return isNumeric(x)
40 }
41 })
42
43 return hasNumericValue
44}
45
46exports.name = 'hasNumericValue'
47exports.factory = factory