UNPKG

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