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 = (0, _factory.factory)(name, dependencies, function (_ref) {
|
23 | var typed = _ref.typed,
|
24 | matrix = _ref.matrix;
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | return typed(name, {
|
61 |
|
62 | 'Array, Index': function ArrayIndex(value, index) {
|
63 | var m = matrix(value);
|
64 | var subset = m.subset(index);
|
65 |
|
66 | return index.isScalar() ? subset : subset.valueOf();
|
67 | },
|
68 | 'Matrix, Index': function MatrixIndex(value, index) {
|
69 | return value.subset(index);
|
70 | },
|
71 | 'Object, Index': _getObjectProperty,
|
72 | 'string, Index': _getSubstring,
|
73 |
|
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 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | exports.createSubset = createSubset;
|
100 |
|
101 | function _getSubstring(str, index) {
|
102 | if (!(0, _is.isIndex)(index)) {
|
103 |
|
104 | throw new TypeError('Index expected');
|
105 | }
|
106 |
|
107 | if (index.size().length !== 1) {
|
108 | throw new _DimensionError.DimensionError(index.size().length, 1);
|
109 | }
|
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 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | function _setSubstring(str, index, replacement, defaultValue) {
|
135 | if (!index || index.isIndex !== true) {
|
136 |
|
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 | }
|
158 |
|
159 |
|
160 | var strLen = str.length;
|
161 | (0, _array.validateIndex)(index.min()[0]);
|
162 | (0, _array.validateIndex)(index.max()[0]);
|
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 | });
|
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 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | function _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 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | function _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 | }
|
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 |