numeric.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createNumeric = void 0;
  6. var _is = require("../../utils/is.js");
  7. var _factory = require("../../utils/factory.js");
  8. var _noop = require("../../utils/noop.js");
  9. var name = 'numeric';
  10. var dependencies = ['number', '?bignumber', '?fraction'];
  11. var createNumeric = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  12. var _number = _ref.number,
  13. bignumber = _ref.bignumber,
  14. fraction = _ref.fraction;
  15. var validInputTypes = {
  16. string: true,
  17. number: true,
  18. BigNumber: true,
  19. Fraction: true
  20. };
  21. // Load the conversion functions for each output type
  22. var validOutputTypes = {
  23. number: function number(x) {
  24. return _number(x);
  25. },
  26. BigNumber: bignumber ? function (x) {
  27. return bignumber(x);
  28. } : _noop.noBignumber,
  29. Fraction: fraction ? function (x) {
  30. return fraction(x);
  31. } : _noop.noFraction
  32. };
  33. /**
  34. * Convert a numeric input to a specific numeric type: number, BigNumber, or Fraction.
  35. *
  36. * Syntax:
  37. *
  38. * math.numeric(x)
  39. *
  40. * Examples:
  41. *
  42. * math.numeric('4') // returns 4
  43. * math.numeric('4', 'number') // returns 4
  44. * math.numeric('4', 'BigNumber') // returns BigNumber 4
  45. * math.numeric('4', 'Fraction') // returns Fraction 4
  46. * math.numeric(4, 'Fraction') // returns Fraction 4
  47. * math.numeric(math.fraction(2, 5), 'number') // returns 0.4
  48. *
  49. * See also:
  50. *
  51. * number, fraction, bignumber, string, format
  52. *
  53. * @param {string | number | BigNumber | Fraction } value
  54. * A numeric value or a string containing a numeric value
  55. * @param {string} outputType
  56. * Desired numeric output type.
  57. * Available values: 'number', 'BigNumber', or 'Fraction'
  58. * @return {number | BigNumber | Fraction}
  59. * Returns an instance of the numeric in the requested type
  60. */
  61. return function numeric(value) {
  62. var outputType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'number';
  63. var check = arguments.length > 2 ? arguments[2] : undefined;
  64. if (check !== undefined) {
  65. throw new SyntaxError('numeric() takes one or two arguments');
  66. }
  67. var inputType = (0, _is.typeOf)(value);
  68. if (!(inputType in validInputTypes)) {
  69. throw new TypeError('Cannot convert ' + value + ' of type "' + inputType + '"; valid input types are ' + Object.keys(validInputTypes).join(', '));
  70. }
  71. if (!(outputType in validOutputTypes)) {
  72. throw new TypeError('Cannot convert ' + value + ' to type "' + outputType + '"; valid output types are ' + Object.keys(validOutputTypes).join(', '));
  73. }
  74. if (outputType === inputType) {
  75. return value;
  76. } else {
  77. return validOutputTypes[outputType](value);
  78. }
  79. };
  80. });
  81. exports.createNumeric = createNumeric;