unaryPlus.js 1.8 KB

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