1 | import { isIndex } from '../../utils/is'
|
2 | import { clone } from '../../utils/object'
|
3 | import { validateIndex } from '../../utils/array'
|
4 | import { getSafeProperty, setSafeProperty } from '../../utils/customs'
|
5 | import { DimensionError } from '../../error/DimensionError'
|
6 | import { factory } from '../../utils/factory'
|
7 |
|
8 | const name = 'subset'
|
9 | const dependencies = ['typed', 'matrix']
|
10 |
|
11 | export const createSubset = factory(name, dependencies, ({ typed, matrix }) => {
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | return typed(name, {
|
47 |
|
48 | 'Array, Index': function (value, index) {
|
49 | const m = matrix(value)
|
50 | const subset = m.subset(index)
|
51 | return index.isScalar()
|
52 | ? subset
|
53 | : subset.valueOf()
|
54 | },
|
55 |
|
56 | 'Matrix, Index': function (value, index) {
|
57 | return value.subset(index)
|
58 | },
|
59 |
|
60 | 'Object, Index': _getObjectProperty,
|
61 |
|
62 | 'string, Index': _getSubstring,
|
63 |
|
64 |
|
65 | 'Array, Index, any': function (value, index, replacement) {
|
66 | return matrix(clone(value))
|
67 | .subset(index, replacement, undefined)
|
68 | .valueOf()
|
69 | },
|
70 |
|
71 | 'Array, Index, any, any': function (value, index, replacement, defaultValue) {
|
72 | return matrix(clone(value))
|
73 | .subset(index, replacement, defaultValue)
|
74 | .valueOf()
|
75 | },
|
76 |
|
77 | 'Matrix, Index, any': function (value, index, replacement) {
|
78 | return value.clone().subset(index, replacement)
|
79 | },
|
80 |
|
81 | 'Matrix, Index, any, any': function (value, index, replacement, defaultValue) {
|
82 | return value.clone().subset(index, replacement, defaultValue)
|
83 | },
|
84 |
|
85 | 'string, Index, string': _setSubstring,
|
86 | 'string, Index, string, string': _setSubstring,
|
87 | 'Object, Index, any': _setObjectProperty
|
88 | })
|
89 | })
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function _getSubstring (str, index) {
|
99 | if (!isIndex(index)) {
|
100 |
|
101 | throw new TypeError('Index expected')
|
102 | }
|
103 | if (index.size().length !== 1) {
|
104 | throw new DimensionError(index.size().length, 1)
|
105 | }
|
106 |
|
107 |
|
108 | const strLen = str.length
|
109 | validateIndex(index.min()[0], strLen)
|
110 | validateIndex(index.max()[0], strLen)
|
111 |
|
112 | const range = index.dimension(0)
|
113 |
|
114 | let substr = ''
|
115 | range.forEach(function (v) {
|
116 | substr += str.charAt(v)
|
117 | })
|
118 |
|
119 | return substr
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | function _setSubstring (str, index, replacement, defaultValue) {
|
133 | if (!index || index.isIndex !== true) {
|
134 |
|
135 | throw new TypeError('Index expected')
|
136 | }
|
137 | if (index.size().length !== 1) {
|
138 | throw new DimensionError(index.size().length, 1)
|
139 | }
|
140 | if (defaultValue !== undefined) {
|
141 | if (typeof defaultValue !== 'string' || defaultValue.length !== 1) {
|
142 | throw new TypeError('Single character expected as defaultValue')
|
143 | }
|
144 | } else {
|
145 | defaultValue = ' '
|
146 | }
|
147 |
|
148 | const range = index.dimension(0)
|
149 | const len = range.size()[0]
|
150 |
|
151 | if (len !== replacement.length) {
|
152 | throw new DimensionError(range.size()[0], replacement.length)
|
153 | }
|
154 |
|
155 |
|
156 | const strLen = str.length
|
157 | validateIndex(index.min()[0])
|
158 | validateIndex(index.max()[0])
|
159 |
|
160 |
|
161 | const chars = []
|
162 | for (let i = 0; i < strLen; i++) {
|
163 | chars[i] = str.charAt(i)
|
164 | }
|
165 |
|
166 | range.forEach(function (v, i) {
|
167 | chars[v] = replacement.charAt(i[0])
|
168 | })
|
169 |
|
170 |
|
171 | if (chars.length > strLen) {
|
172 | for (let i = strLen - 1, len = chars.length; i < len; i++) {
|
173 | if (!chars[i]) {
|
174 | chars[i] = defaultValue
|
175 | }
|
176 | }
|
177 | }
|
178 |
|
179 | return chars.join('')
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | function _getObjectProperty (object, index) {
|
190 | if (index.size().length !== 1) {
|
191 | throw new DimensionError(index.size(), 1)
|
192 | }
|
193 |
|
194 | const key = index.dimension(0)
|
195 | if (typeof key !== 'string') {
|
196 | throw new TypeError('String expected as index to retrieve an object property')
|
197 | }
|
198 |
|
199 | return getSafeProperty(object, key)
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | function _setObjectProperty (object, index, replacement) {
|
211 | if (index.size().length !== 1) {
|
212 | throw new DimensionError(index.size(), 1)
|
213 | }
|
214 |
|
215 | const key = index.dimension(0)
|
216 | if (typeof key !== 'string') {
|
217 | throw new TypeError('String expected as index to retrieve an object property')
|
218 | }
|
219 |
|
220 |
|
221 | const updated = clone(object)
|
222 | setSafeProperty(updated, key, replacement)
|
223 |
|
224 | return updated
|
225 | }
|