assign.js (2025B)
1 "use strict"; 2 3 var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); 4 5 Object.defineProperty(exports, "__esModule", { 6 value: true 7 }); 8 exports.assignFactory = assignFactory; 9 10 var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); 11 12 var _errorTransform = require("../../transform/utils/errorTransform.js"); 13 14 var _customs = require("../../../utils/customs.js"); 15 16 function assignFactory(_ref) { 17 var subset = _ref.subset, 18 matrix = _ref.matrix; 19 20 /** 21 * Replace part of an object: 22 * 23 * - Assign a property to an object 24 * - Replace a part of a string 25 * - Replace a matrix subset 26 * 27 * @param {Object | Array | Matrix | string} object 28 * @param {Index} index 29 * @param {*} value 30 * @return {Object | Array | Matrix | string} Returns the original object 31 * except in case of a string 32 */ 33 // TODO: change assign to return the value instead of the object 34 return function assign(object, index, value) { 35 try { 36 if (Array.isArray(object)) { 37 // we use matrix.subset here instead of the function subset because we must not clone the contents 38 return matrix(object).subset(index, value).valueOf(); 39 } else if (object && typeof object.subset === 'function') { 40 // Matrix 41 return object.subset(index, value); 42 } else if (typeof object === 'string') { 43 // TODO: move setStringSubset into a separate util file, use that 44 return subset(object, index, value); 45 } else if ((0, _typeof2.default)(object) === 'object') { 46 if (!index.isObjectProperty()) { 47 throw TypeError('Cannot apply a numeric index as object property'); 48 } 49 50 (0, _customs.setSafeProperty)(object, index.getObjectProperty(), value); 51 return object; 52 } else { 53 throw new TypeError('Cannot apply index: unsupported type of object'); 54 } 55 } catch (err) { 56 throw (0, _errorTransform.errorTransform)(err); 57 } 58 }; 59 }