UNPKG

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

45 lines (39 loc) 1.29 kB
'use strict' /** * Attach a transform function to math.index * Adds a property transform containing the transform function. * * This transform creates a one-based index instead of a zero-based index */ function factory (type, config, load) { return function indexTransform () { const args = [] for (let i = 0, ii = arguments.length; i < ii; i++) { let arg = arguments[i] // change from one-based to zero based, and convert BigNumber to number if (type.isRange(arg)) { arg.start-- arg.end -= (arg.step > 0 ? 0 : 2) } else if (arg && arg.isSet === true) { arg = arg.map(function (v) { return v - 1 }) } else if (type.isArray(arg) || type.isMatrix(arg)) { arg = arg.map(function (v) { return v - 1 }) } else if (type.isNumber(arg)) { arg-- } else if (type.isBigNumber(arg)) { arg = arg.toNumber() - 1 } else if (typeof arg === 'string') { // leave as is } else { throw new TypeError('Dimension must be an Array, Matrix, number, string, or Range') } args[i] = arg } const res = new type.Index() type.Index.apply(res, args) return res } } exports.name = 'index' exports.path = 'expression.transform' exports.factory = factory