subset.js (7697B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createSubset = void 0; 7 8 var _is = require("../../utils/is.js"); 9 10 var _object = require("../../utils/object.js"); 11 12 var _array = require("../../utils/array.js"); 13 14 var _customs = require("../../utils/customs.js"); 15 16 var _DimensionError = require("../../error/DimensionError.js"); 17 18 var _factory = require("../../utils/factory.js"); 19 20 var name = 'subset'; 21 var dependencies = ['typed', 'matrix']; 22 var createSubset = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 23 var typed = _ref.typed, 24 matrix = _ref.matrix; 25 26 /** 27 * Get or set a subset of a matrix or string. 28 * 29 * Syntax: 30 * math.subset(value, index) // retrieve a subset 31 * math.subset(value, index, replacement [, defaultValue]) // replace a subset 32 * 33 * Examples: 34 * 35 * // get a subset 36 * const d = [[1, 2], [3, 4]] 37 * math.subset(d, math.index(1, 0)) // returns 3 38 * math.subset(d, math.index([0, 1], 1)) // returns [[2], [4]] 39 * 40 * // replace a subset 41 * const e = [] 42 * const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]] and e = [[5, 0, 6]] 43 * const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 6], [0, 7]] 44 * 45 * // get submatrix using ranges 46 * const M = [ 47 * [1,2,3], 48 * [4,5,6], 49 * [7,8,9] 50 * ] 51 * math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1,2,3],[4,5,6]] 52 * 53 * See also: 54 * 55 * size, resize, squeeze, index 56 * 57 * @param {Array | Matrix | string} matrix An array, matrix, or string 58 * @param {Index} index 59 * For each dimension of the target, specifies an index or a list of 60 * indices to fetch or set. `subset` uses the cartesian product of 61 * the indices specified in each dimension. 62 * @param {*} [replacement] An array, matrix, or scalar. 63 * If provided, the subset is replaced with replacement. 64 * If not provided, the subset is returned 65 * @param {*} [defaultValue=undefined] Default value, filled in on new entries when 66 * the matrix is resized. If not provided, 67 * math.matrix elements will be left undefined. 68 * @return {Array | Matrix | string} Either the retrieved subset or the updated matrix. 69 */ 70 return typed(name, { 71 // get subset 72 'Array, Index': function ArrayIndex(value, index) { 73 var m = matrix(value); 74 var subset = m.subset(index); // returns a Matrix 75 76 return index.isScalar() ? subset : subset.valueOf(); // return an Array (like the input) 77 }, 78 'Matrix, Index': function MatrixIndex(value, index) { 79 return value.subset(index); 80 }, 81 'Object, Index': _getObjectProperty, 82 'string, Index': _getSubstring, 83 // set subset 84 'Array, Index, any': function ArrayIndexAny(value, index, replacement) { 85 return matrix((0, _object.clone)(value)).subset(index, replacement, undefined).valueOf(); 86 }, 87 'Array, Index, any, any': function ArrayIndexAnyAny(value, index, replacement, defaultValue) { 88 return matrix((0, _object.clone)(value)).subset(index, replacement, defaultValue).valueOf(); 89 }, 90 'Matrix, Index, any': function MatrixIndexAny(value, index, replacement) { 91 return value.clone().subset(index, replacement); 92 }, 93 'Matrix, Index, any, any': function MatrixIndexAnyAny(value, index, replacement, defaultValue) { 94 return value.clone().subset(index, replacement, defaultValue); 95 }, 96 'string, Index, string': _setSubstring, 97 'string, Index, string, string': _setSubstring, 98 'Object, Index, any': _setObjectProperty 99 }); 100 }); 101 /** 102 * Retrieve a subset of a string 103 * @param {string} str string from which to get a substring 104 * @param {Index} index An index or list of indices (character positions) 105 * @returns {string} substring 106 * @private 107 */ 108 109 exports.createSubset = createSubset; 110 111 function _getSubstring(str, index) { 112 if (!(0, _is.isIndex)(index)) { 113 // TODO: better error message 114 throw new TypeError('Index expected'); 115 } 116 117 if (index.size().length !== 1) { 118 throw new _DimensionError.DimensionError(index.size().length, 1); 119 } // validate whether the range is out of range 120 121 122 var strLen = str.length; 123 (0, _array.validateIndex)(index.min()[0], strLen); 124 (0, _array.validateIndex)(index.max()[0], strLen); 125 var range = index.dimension(0); 126 var substr = ''; 127 range.forEach(function (v) { 128 substr += str.charAt(v); 129 }); 130 return substr; 131 } 132 /** 133 * Replace a substring in a string 134 * @param {string} str string to be replaced 135 * @param {Index} index An index or list of indices (character positions) 136 * @param {string} replacement Replacement string 137 * @param {string} [defaultValue] Default value to be uses when resizing 138 * the string. is ' ' by default 139 * @returns {string} result 140 * @private 141 */ 142 143 144 function _setSubstring(str, index, replacement, defaultValue) { 145 if (!index || index.isIndex !== true) { 146 // TODO: better error message 147 throw new TypeError('Index expected'); 148 } 149 150 if (index.size().length !== 1) { 151 throw new _DimensionError.DimensionError(index.size().length, 1); 152 } 153 154 if (defaultValue !== undefined) { 155 if (typeof defaultValue !== 'string' || defaultValue.length !== 1) { 156 throw new TypeError('Single character expected as defaultValue'); 157 } 158 } else { 159 defaultValue = ' '; 160 } 161 162 var range = index.dimension(0); 163 var len = range.size()[0]; 164 165 if (len !== replacement.length) { 166 throw new _DimensionError.DimensionError(range.size()[0], replacement.length); 167 } // validate whether the range is out of range 168 169 170 var strLen = str.length; 171 (0, _array.validateIndex)(index.min()[0]); 172 (0, _array.validateIndex)(index.max()[0]); // copy the string into an array with characters 173 174 var chars = []; 175 176 for (var i = 0; i < strLen; i++) { 177 chars[i] = str.charAt(i); 178 } 179 180 range.forEach(function (v, i) { 181 chars[v] = replacement.charAt(i[0]); 182 }); // initialize undefined characters with a space 183 184 if (chars.length > strLen) { 185 for (var _i = strLen - 1, _len = chars.length; _i < _len; _i++) { 186 if (!chars[_i]) { 187 chars[_i] = defaultValue; 188 } 189 } 190 } 191 192 return chars.join(''); 193 } 194 /** 195 * Retrieve a property from an object 196 * @param {Object} object 197 * @param {Index} index 198 * @return {*} Returns the value of the property 199 * @private 200 */ 201 202 203 function _getObjectProperty(object, index) { 204 if (index.size().length !== 1) { 205 throw new _DimensionError.DimensionError(index.size(), 1); 206 } 207 208 var key = index.dimension(0); 209 210 if (typeof key !== 'string') { 211 throw new TypeError('String expected as index to retrieve an object property'); 212 } 213 214 return (0, _customs.getSafeProperty)(object, key); 215 } 216 /** 217 * Set a property on an object 218 * @param {Object} object 219 * @param {Index} index 220 * @param {*} replacement 221 * @return {*} Returns the updated object 222 * @private 223 */ 224 225 226 function _setObjectProperty(object, index, replacement) { 227 if (index.size().length !== 1) { 228 throw new _DimensionError.DimensionError(index.size(), 1); 229 } 230 231 var key = index.dimension(0); 232 233 if (typeof key !== 'string') { 234 throw new TypeError('String expected as index to retrieve an object property'); 235 } // clone the object, and apply the property to the clone 236 237 238 var updated = (0, _object.clone)(object); 239 (0, _customs.setSafeProperty)(updated, key, replacement); 240 return updated; 241 }