unaryPlus.js (1830B)
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 /** 14 * Unary plus operation. 15 * Boolean values and strings will be converted to a number, numeric values will be returned as is. 16 * 17 * For matrices, the function is evaluated element wise. 18 * 19 * Syntax: 20 * 21 * math.unaryPlus(x) 22 * 23 * Examples: 24 * 25 * math.unaryPlus(3.5) // returns 3.5 26 * math.unaryPlus(1) // returns 1 27 * 28 * See also: 29 * 30 * unaryMinus, add, subtract 31 * 32 * @param {number | BigNumber | Fraction | string | Complex | Unit | Array | Matrix} x 33 * Input value 34 * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} 35 * Returns the input value when numeric, converts to a number when input is non-numeric. 36 */ 37 return typed(name, { 38 number: unaryPlusNumber, 39 Complex: function Complex(x) { 40 return x; // complex numbers are immutable 41 }, 42 BigNumber: function BigNumber(x) { 43 return x; // bignumbers are immutable 44 }, 45 Fraction: function Fraction(x) { 46 return x; // fractions are immutable 47 }, 48 Unit: function Unit(x) { 49 return x.clone(); 50 }, 51 'Array | Matrix': function ArrayMatrix(x) { 52 // deep map collection, skip zeros since unaryPlus(0) = 0 53 return deepMap(x, this, true); 54 }, 55 'boolean | string': function booleanString(x) { 56 // convert to a number or bignumber 57 return config.number === 'BigNumber' ? new BigNumber(+x) : +x; 58 } 59 }); 60 });