UNPKG

1.71 kBJavaScriptView Raw
1'use strict'
2
3const deepMap = require('../../utils/collection/deepMap')
4const number = require('../../utils/number')
5
6function factory (type, config, load, typed) {
7 /**
8 * Test whether a value is an integer number.
9 * The function supports `number`, `BigNumber`, and `Fraction`.
10 *
11 * The function is evaluated element-wise in case of Array or Matrix input.
12 *
13 * Syntax:
14 *
15 * math.isInteger(x)
16 *
17 * Examples:
18 *
19 * math.isInteger(2) // returns true
20 * math.isInteger(0) // returns true
21 * math.isInteger(0.5) // returns false
22 * math.isInteger(math.bignumber(500)) // returns true
23 * math.isInteger(math.fraction(4)) // returns true
24 * math.isInteger('3') // returns true
25 * math.isInteger([3, 0.5, -2]) // returns [true, false, true]
26 * math.isInteger(math.complex('2-4i') // throws an error
27 *
28 * See also:
29 *
30 * isNumeric, isPositive, isNegative, isZero
31 *
32 * @param {number | BigNumber | Fraction | Array | Matrix} x Value to be tested
33 * @return {boolean} Returns true when `x` contains a numeric, integer value.
34 * Throws an error in case of an unknown data type.
35 */
36 const isInteger = typed('isInteger', {
37 'number': number.isInteger, // TODO: what to do with isInteger(add(0.1, 0.2)) ?
38
39 'BigNumber': function (x) {
40 return x.isInt()
41 },
42
43 'Fraction': function (x) {
44 return x.d === 1 && isFinite(x.n)
45 },
46
47 'Array | Matrix': function (x) {
48 return deepMap(x, isInteger)
49 }
50 })
51
52 return isInteger
53}
54
55exports.name = 'isInteger'
56exports.factory = factory