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.

52 lines (47 loc) 1.6 kB
'use strict'; var errorTransform = require('../../transform/error.transform').transform; function factory (type, config, load, typed) { var subset = load(require('../../../function/matrix/subset')); var matrix = load(require('../../../type/matrix/function/matrix')); /** * Replace part of an object: * * - Assign a property to an object * - Replace a part of a string * - Replace a matrix subset * * @param {Object | Array | Matrix | string} object * @param {Index} index * @param {*} value * @return {Object | Array | Matrix | string} Returns the original object * except in case of a string */ return function assign(object, index, value) { try { if (Array.isArray(object)) { return matrix(object).subset(index, value).valueOf(); } else if (object && typeof object.subset === 'function') { // Matrix return object.subset(index, value); } else if (typeof object === 'string') { // TODO: move setStringSubset into a separate util file, use that return subset(object, index, value); } else if (typeof object === 'object') { if (!index.isObjectProperty()) { throw TypeError('Cannot apply a numeric index as object property'); } object[index.getObjectProperty()] = value; return object; } else { throw new TypeError('Cannot apply index: unsupported type of object'); } } catch (err) { throw errorTransform(err); } } } exports.factory = factory;