UNPKG

1.88 kBJavaScriptView Raw
1'use strict'
2
3const deepMap = require('../../utils/collection/deepMap')
4
5function factory (type, config, load, typed) {
6 /**
7 * Test whether a value is positive: larger than zero.
8 * The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`.
9 *
10 * The function is evaluated element-wise in case of Array or Matrix input.
11 *
12 * Syntax:
13 *
14 * math.isPositive(x)
15 *
16 * Examples:
17 *
18 * math.isPositive(3) // returns true
19 * math.isPositive(-2) // returns false
20 * math.isPositive(0) // returns false
21 * math.isPositive(-0) // returns false
22 * math.isPositive(0.5) // returns true
23 * math.isPositive(math.bignumber(2)) // returns true
24 * math.isPositive(math.fraction(-2, 5)) // returns false
25 * math.isPositive(math.fraction(1,3)) // returns false
26 * math.isPositive('2') // returns true
27 * math.isPositive([2, 0, -3]) // returns [true, false, false]
28 *
29 * See also:
30 *
31 * isNumeric, isZero, isNegative, isInteger
32 *
33 * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested
34 * @return {boolean} Returns true when `x` is larger than zero.
35 * Throws an error in case of an unknown data type.
36 */
37 const isPositive = typed('isPositive', {
38 'number': function (x) {
39 return x > 0
40 },
41
42 'BigNumber': function (x) {
43 return !x.isNeg() && !x.isZero() && !x.isNaN()
44 },
45
46 'Fraction': function (x) {
47 return x.s > 0 && x.n > 0
48 },
49
50 'Unit': function (x) {
51 return isPositive(x.value)
52 },
53
54 'Array | Matrix': function (x) {
55 return deepMap(x, isPositive)
56 }
57 })
58
59 return isPositive
60}
61
62exports.name = 'isPositive'
63exports.factory = factory