mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
57 lines (53 loc) • 1.66 kB
JavaScript
;
var deepMap = require('../../../utils/collection/deepMap');
function factory(type, config, load, typed) {
/**
* Create a unit. Depending on the passed arguments, the function
* will create and return a new math.type.Unit object.
* When a matrix is provided, all elements will be converted to units.
*
* Syntax:
*
* math.unit(unit : string)
* math.unit(value : number, unit : string)
*
* Examples:
*
* const a = math.unit(5, 'cm') // returns Unit 50 mm
* const b = math.unit('23 kg') // returns Unit 23 kg
* a.to('m') // returns Unit 0.05 m
*
* See also:
*
* bignumber, boolean, complex, index, matrix, number, string, createUnit
*
* @param {* | Array | Matrix} args A number and unit.
* @return {Unit | Array | Matrix} The created unit
*/
var unit = typed('unit', {
'Unit': function Unit(x) {
return x.clone();
},
'string': function string(x) {
if (type.Unit.isValuelessUnit(x)) {
return new type.Unit(null, x); // a pure unit
}
return type.Unit.parse(x, {
allowNoUnits: true
}); // a unit with value, like '5cm'
},
'number | BigNumber | Fraction | Complex, string': function numberBigNumberFractionComplexString(value, unit) {
return new type.Unit(value, unit);
},
'Array | Matrix': function ArrayMatrix(x) {
return deepMap(x, unit);
}
});
unit.toTex = {
1: "\\left(${args[0]}\\right)",
2: "\\left(\\left(${args[0]}\\right)${args[1]}\\right)"
};
return unit;
}
exports.name = 'unit';
exports.factory = factory;