UNPKG

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