unaryPlus.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createUnaryPlus = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var _collection = require("../../utils/collection.js");
  8. var _index = require("../../plain/number/index.js");
  9. var name = 'unaryPlus';
  10. var dependencies = ['typed', 'config', 'BigNumber'];
  11. var createUnaryPlus = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  12. var typed = _ref.typed,
  13. config = _ref.config,
  14. BigNumber = _ref.BigNumber;
  15. /**
  16. * Unary plus operation.
  17. * Boolean values and strings will be converted to a number, numeric values will be returned as is.
  18. *
  19. * For matrices, the function is evaluated element wise.
  20. *
  21. * Syntax:
  22. *
  23. * math.unaryPlus(x)
  24. *
  25. * Examples:
  26. *
  27. * math.unaryPlus(3.5) // returns 3.5
  28. * math.unaryPlus(1) // returns 1
  29. *
  30. * See also:
  31. *
  32. * unaryMinus, add, subtract
  33. *
  34. * @param {number | BigNumber | Fraction | string | Complex | Unit | Array | Matrix} x
  35. * Input value
  36. * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
  37. * Returns the input value when numeric, converts to a number when input is non-numeric.
  38. */
  39. return typed(name, {
  40. number: _index.unaryPlusNumber,
  41. Complex: function Complex(x) {
  42. return x; // complex numbers are immutable
  43. },
  44. BigNumber: function BigNumber(x) {
  45. return x; // bignumbers are immutable
  46. },
  47. Fraction: function Fraction(x) {
  48. return x; // fractions are immutable
  49. },
  50. Unit: function Unit(x) {
  51. return x.clone();
  52. },
  53. // deep map collection, skip zeros since unaryPlus(0) = 0
  54. 'Array | Matrix': typed.referToSelf(function (self) {
  55. return function (x) {
  56. return (0, _collection.deepMap)(x, self, true);
  57. };
  58. }),
  59. 'boolean | string': function booleanString(x) {
  60. // convert to a number or bignumber
  61. return config.number === 'BigNumber' ? new BigNumber(+x) : +x;
  62. }
  63. });
  64. });
  65. exports.createUnaryPlus = createUnaryPlus;