1 | 'use strict'
|
2 |
|
3 | const clone = require('../../utils/object').clone
|
4 | const validateIndex = require('../../utils/array').validateIndex
|
5 | const getSafeProperty = require('../../utils/customs').getSafeProperty
|
6 | const setSafeProperty = require('../../utils/customs').setSafeProperty
|
7 | const DimensionError = require('../../error/DimensionError')
|
8 |
|
9 | function factory (type, config, load, typed) {
|
10 | const matrix = load(require('../../type/matrix/function/matrix'))
|
11 |
|
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 | const subset = typed('subset', {
|
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 | subset.toTex = undefined
|
91 |
|
92 | return subset
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function _getSubstring (str, index) {
|
102 | if (!type.isIndex(index)) {
|
103 |
|
104 | throw new TypeError('Index expected')
|
105 | }
|
106 | if (index.size().length !== 1) {
|
107 | throw new DimensionError(index.size().length, 1)
|
108 | }
|
109 |
|
110 |
|
111 | const strLen = str.length
|
112 | validateIndex(index.min()[0], strLen)
|
113 | validateIndex(index.max()[0], strLen)
|
114 |
|
115 | const range = index.dimension(0)
|
116 |
|
117 | let substr = ''
|
118 | range.forEach(function (v) {
|
119 | substr += str.charAt(v)
|
120 | })
|
121 |
|
122 | return substr
|
123 | }
|
124 |
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | function _setSubstring (str, index, replacement, defaultValue) {
|
136 | if (!index || index.isIndex !== true) {
|
137 |
|
138 | throw new TypeError('Index expected')
|
139 | }
|
140 | if (index.size().length !== 1) {
|
141 | throw new DimensionError(index.size().length, 1)
|
142 | }
|
143 | if (defaultValue !== undefined) {
|
144 | if (typeof defaultValue !== 'string' || defaultValue.length !== 1) {
|
145 | throw new TypeError('Single character expected as defaultValue')
|
146 | }
|
147 | } else {
|
148 | defaultValue = ' '
|
149 | }
|
150 |
|
151 | const range = index.dimension(0)
|
152 | const len = range.size()[0]
|
153 |
|
154 | if (len !== replacement.length) {
|
155 | throw new DimensionError(range.size()[0], replacement.length)
|
156 | }
|
157 |
|
158 |
|
159 | const strLen = str.length
|
160 | validateIndex(index.min()[0])
|
161 | validateIndex(index.max()[0])
|
162 |
|
163 |
|
164 | const chars = []
|
165 | for (let i = 0; i < strLen; i++) {
|
166 | chars[i] = str.charAt(i)
|
167 | }
|
168 |
|
169 | range.forEach(function (v, i) {
|
170 | chars[v] = replacement.charAt(i[0])
|
171 | })
|
172 |
|
173 |
|
174 | if (chars.length > strLen) {
|
175 | for (let 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(index.size(), 1)
|
196 | }
|
197 |
|
198 | const key = index.dimension(0)
|
199 | if (typeof key !== 'string') {
|
200 | throw new TypeError('String expected as index to retrieve an object property')
|
201 | }
|
202 |
|
203 | return getSafeProperty(object, key)
|
204 | }
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | function _setObjectProperty (object, index, replacement) {
|
215 | if (index.size().length !== 1) {
|
216 | throw new DimensionError(index.size(), 1)
|
217 | }
|
218 |
|
219 | const key = index.dimension(0)
|
220 | if (typeof key !== 'string') {
|
221 | throw new TypeError('String expected as index to retrieve an object property')
|
222 | }
|
223 |
|
224 |
|
225 | const updated = clone(object)
|
226 | setSafeProperty(updated, key, replacement)
|
227 |
|
228 | return updated
|
229 | }
|
230 |
|
231 | exports.name = 'subset'
|
232 | exports.factory = factory
|