UNPKG

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