addScalar.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { factory } from '../../utils/factory.js';
  2. import { addNumber } from '../../plain/number/index.js';
  3. var name = 'addScalar';
  4. var dependencies = ['typed'];
  5. export var createAddScalar = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed
  8. } = _ref;
  9. /**
  10. * Add two scalar values, `x + y`.
  11. * This function is meant for internal use: it is used by the public function
  12. * `add`
  13. *
  14. * This function does not support collections (Array or Matrix).
  15. *
  16. * @param {number | BigNumber | Fraction | Complex | Unit} x First value to add
  17. * @param {number | BigNumber | Fraction | Complex} y Second value to add
  18. * @return {number | BigNumber | Fraction | Complex | Unit} Sum of `x` and `y`
  19. * @private
  20. */
  21. return typed(name, {
  22. 'number, number': addNumber,
  23. 'Complex, Complex': function ComplexComplex(x, y) {
  24. return x.add(y);
  25. },
  26. 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
  27. return x.plus(y);
  28. },
  29. 'Fraction, Fraction': function FractionFraction(x, y) {
  30. return x.add(y);
  31. },
  32. 'Unit, Unit': typed.referToSelf(self => (x, y) => {
  33. if (x.value === null || x.value === undefined) {
  34. throw new Error('Parameter x contains a unit with undefined value');
  35. }
  36. if (y.value === null || y.value === undefined) {
  37. throw new Error('Parameter y contains a unit with undefined value');
  38. }
  39. if (!x.equalBase(y)) throw new Error('Units do not match');
  40. var res = x.clone();
  41. res.value = typed.find(self, [res.valueType(), y.valueType()])(res.value, y.value);
  42. res.fixPrefix = false;
  43. return res;
  44. })
  45. });
  46. });