UNPKG

mathjs

Version:

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.

43 lines (35 loc) 1.09 kB
module.exports = function (math) { var util = require('../../util/index'), Unit = require('../../type/Unit'), collection = require('../../type/collection'), isString = util.string.isString, isUnit = Unit.isUnit, isCollection = collection.isCollection; /** * Change the unit of a value. * * x to unit * to(x, unit) * * For matrices, the function is evaluated element wise. * * @param {Unit | Array | Matrix} x * @param {Unit | Array | Matrix} unit * @return {Unit | Array | Matrix} res */ math.to = function to(x, unit) { if (arguments.length != 2) { throw new math.error.ArgumentsError('to', arguments.length, 2); } if (isUnit(x)) { if (isUnit(unit) || isString(unit)) { return x.to(unit); } } // TODO: add support for string, in that case, convert to unit if (isCollection(x) || isCollection(unit)) { return collection.deepMap2(x, unit, to); } throw new math.error.UnsupportedTypeError('to', math['typeof'](x), math['typeof'](unit)); }; };