UNPKG

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