isPositive.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { deepMap } from '../../utils/collection.js';
  2. import { factory } from '../../utils/factory.js';
  3. import { isPositiveNumber } from '../../plain/number/index.js';
  4. var name = 'isPositive';
  5. var dependencies = ['typed'];
  6. export var createIsPositive = /* #__PURE__ */factory(name, dependencies, _ref => {
  7. var {
  8. typed
  9. } = _ref;
  10. /**
  11. * Test whether a value is positive: larger than zero.
  12. * The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`.
  13. *
  14. * The function is evaluated element-wise in case of Array or Matrix input.
  15. *
  16. * Syntax:
  17. *
  18. * math.isPositive(x)
  19. *
  20. * Examples:
  21. *
  22. * math.isPositive(3) // returns true
  23. * math.isPositive(-2) // returns false
  24. * math.isPositive(0) // returns false
  25. * math.isPositive(-0) // returns false
  26. * math.isPositive(0.5) // returns true
  27. * math.isPositive(math.bignumber(2)) // returns true
  28. * math.isPositive(math.fraction(-2, 5)) // returns false
  29. * math.isPositive(math.fraction(1, 3)) // returns true
  30. * math.isPositive('2') // returns true
  31. * math.isPositive([2, 0, -3]) // returns [true, false, false]
  32. *
  33. * See also:
  34. *
  35. * isNumeric, isZero, isNegative, isInteger
  36. *
  37. * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested
  38. * @return {boolean} Returns true when `x` is larger than zero.
  39. * Throws an error in case of an unknown data type.
  40. */
  41. return typed(name, {
  42. number: isPositiveNumber,
  43. BigNumber: function BigNumber(x) {
  44. return !x.isNeg() && !x.isZero() && !x.isNaN();
  45. },
  46. Fraction: function Fraction(x) {
  47. return x.s > 0 && x.n > 0;
  48. },
  49. Unit: typed.referToSelf(self => x => typed.find(self, x.valueType())(x.value)),
  50. 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
  51. });
  52. });