hasNumericValue.js 1.7 KB

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