@stdlib/array-to-fancy 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -11
- package/dist/index.js +57 -55
- package/dist/index.js.map +4 -4
- package/docs/types/index.d.ts +22 -133
- package/lib/error_message.js +1 -1
- package/lib/get_elements.js +2 -3
- package/lib/index.js +2 -0
- package/lib/set.js +5 -0
- package/lib/set_elements.js +130 -0
- package/lib/set_slice.js +3 -2
- package/lib/validator.js +36 -6
- package/package.json +51 -81
package/docs/types/index.d.ts
CHANGED
|
@@ -20,27 +20,8 @@
|
|
|
20
20
|
|
|
21
21
|
/// <reference types="@stdlib/types"/>
|
|
22
22
|
|
|
23
|
-
import { Collection, ArrayLike, AccessorArrayLike,
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Interface describing an index object.
|
|
27
|
-
*/
|
|
28
|
-
interface IndexObject {
|
|
29
|
-
/**
|
|
30
|
-
* Underlying array index data.
|
|
31
|
-
*/
|
|
32
|
-
data: Collection | AccessorArrayLike<any>;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Index type.
|
|
36
|
-
*/
|
|
37
|
-
type: 'mask' | 'bool' | 'int';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Underlying array data type.
|
|
41
|
-
*/
|
|
42
|
-
dtype: DataType | null;
|
|
43
|
-
}
|
|
23
|
+
import { Collection, ArrayLike, AccessorArrayLike, ComplexTypedArray, TypedArray, BooleanTypedArray, ArrayIndexObject } from '@stdlib/types/array';
|
|
24
|
+
import ArrayIndex = require( '@stdlib/array-index' );
|
|
44
25
|
|
|
45
26
|
/**
|
|
46
27
|
* Interface describing a cache for resolving array index objects.
|
|
@@ -52,7 +33,7 @@ interface Cache {
|
|
|
52
33
|
* @param id - identifier
|
|
53
34
|
* @returns index data
|
|
54
35
|
*/
|
|
55
|
-
get( id: any ):
|
|
36
|
+
get( id: any ): ArrayIndexObject | null;
|
|
56
37
|
}
|
|
57
38
|
|
|
58
39
|
/**
|
|
@@ -93,17 +74,6 @@ interface Array2Fancy {
|
|
|
93
74
|
*
|
|
94
75
|
* var v = y[ ':' ];
|
|
95
76
|
* // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
|
|
96
|
-
*/
|
|
97
|
-
( x: Float64Array, options?: Options ): Float64Array;
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Converts an array to an object supporting fancy indexing.
|
|
101
|
-
*
|
|
102
|
-
* @param x - input array
|
|
103
|
-
* @param options - function options
|
|
104
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
105
|
-
* @param options.cache - cache for resolving array index objects
|
|
106
|
-
* @returns fancy array
|
|
107
77
|
*
|
|
108
78
|
* @example
|
|
109
79
|
* var Float32Array = require( '@stdlib/array-float32' );
|
|
@@ -115,17 +85,6 @@ interface Array2Fancy {
|
|
|
115
85
|
*
|
|
116
86
|
* var v = y[ ':' ];
|
|
117
87
|
* // returns <Float32Array>[ 1.0, 2.0, 3.0, 4.0 ]
|
|
118
|
-
*/
|
|
119
|
-
( x: Float32Array, options?: Options ): Float32Array;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Converts an array to an object supporting fancy indexing.
|
|
123
|
-
*
|
|
124
|
-
* @param x - input array
|
|
125
|
-
* @param options - function options
|
|
126
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
127
|
-
* @param options.cache - cache for resolving array index objects
|
|
128
|
-
* @returns fancy array
|
|
129
88
|
*
|
|
130
89
|
* @example
|
|
131
90
|
* var Complex128Array = require( '@stdlib/array-complex128' );
|
|
@@ -137,17 +96,6 @@ interface Array2Fancy {
|
|
|
137
96
|
*
|
|
138
97
|
* var v = y[ ':' ];
|
|
139
98
|
* // returns <Complex128Array>[ 1.0, 2.0, 3.0, 4.0 ]
|
|
140
|
-
*/
|
|
141
|
-
( x: Complex128Array, options?: Options ): Complex128Array;
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Converts an array to an object supporting fancy indexing.
|
|
145
|
-
*
|
|
146
|
-
* @param x - input array
|
|
147
|
-
* @param options - function options
|
|
148
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
149
|
-
* @param options.cache - cache for resolving array index objects
|
|
150
|
-
* @returns fancy array
|
|
151
99
|
*
|
|
152
100
|
* @example
|
|
153
101
|
* var Complex64Array = require( '@stdlib/array-complex64' );
|
|
@@ -159,17 +107,6 @@ interface Array2Fancy {
|
|
|
159
107
|
*
|
|
160
108
|
* var v = y[ ':' ];
|
|
161
109
|
* // returns <Complex64Array>[ 1.0, 2.0, 3.0, 4.0 ]
|
|
162
|
-
*/
|
|
163
|
-
( x: Complex64Array, options?: Options ): Complex64Array;
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Converts an array to an object supporting fancy indexing.
|
|
167
|
-
*
|
|
168
|
-
* @param x - input array
|
|
169
|
-
* @param options - function options
|
|
170
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
171
|
-
* @param options.cache - cache for resolving array index objects
|
|
172
|
-
* @returns fancy array
|
|
173
110
|
*
|
|
174
111
|
* @example
|
|
175
112
|
* var Int32Array = require( '@stdlib/array-int32' );
|
|
@@ -181,17 +118,6 @@ interface Array2Fancy {
|
|
|
181
118
|
*
|
|
182
119
|
* var v = y[ ':' ];
|
|
183
120
|
* // returns <Int32Array>[ 1, 2, 3, 4 ]
|
|
184
|
-
*/
|
|
185
|
-
( x: Int32Array, options?: Options ): Int32Array;
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Converts an array to an object supporting fancy indexing.
|
|
189
|
-
*
|
|
190
|
-
* @param x - input array
|
|
191
|
-
* @param options - function options
|
|
192
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
193
|
-
* @param options.cache - cache for resolving array index objects
|
|
194
|
-
* @returns fancy array
|
|
195
121
|
*
|
|
196
122
|
* @example
|
|
197
123
|
* var Int16Array = require( '@stdlib/array-int16' );
|
|
@@ -203,17 +129,6 @@ interface Array2Fancy {
|
|
|
203
129
|
*
|
|
204
130
|
* var v = y[ ':' ];
|
|
205
131
|
* // returns <Int16Array>[ 1, 2, 3, 4 ]
|
|
206
|
-
*/
|
|
207
|
-
( x: Int16Array, options?: Options ): Int16Array;
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Converts an array to an object supporting fancy indexing.
|
|
211
|
-
*
|
|
212
|
-
* @param x - input array
|
|
213
|
-
* @param options - function options
|
|
214
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
215
|
-
* @param options.cache - cache for resolving array index objects
|
|
216
|
-
* @returns fancy array
|
|
217
132
|
*
|
|
218
133
|
* @example
|
|
219
134
|
* var Int8Array = require( '@stdlib/array-int8' );
|
|
@@ -225,17 +140,6 @@ interface Array2Fancy {
|
|
|
225
140
|
*
|
|
226
141
|
* var v = y[ ':' ];
|
|
227
142
|
* // returns <Int8Array>[ 1, 2, 3, 4 ]
|
|
228
|
-
*/
|
|
229
|
-
( x: Int8Array, options?: Options ): Int8Array;
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Converts an array to an object supporting fancy indexing.
|
|
233
|
-
*
|
|
234
|
-
* @param x - input array
|
|
235
|
-
* @param options - function options
|
|
236
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
237
|
-
* @param options.cache - cache for resolving array index objects
|
|
238
|
-
* @returns fancy array
|
|
239
143
|
*
|
|
240
144
|
* @example
|
|
241
145
|
* var Uint32Array = require( '@stdlib/array-uint32' );
|
|
@@ -247,17 +151,6 @@ interface Array2Fancy {
|
|
|
247
151
|
*
|
|
248
152
|
* var v = y[ ':' ];
|
|
249
153
|
* // returns <Uint32Array>[ 1, 2, 3, 4 ]
|
|
250
|
-
*/
|
|
251
|
-
( x: Uint32Array, options?: Options ): Uint32Array;
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Converts an array to an object supporting fancy indexing.
|
|
255
|
-
*
|
|
256
|
-
* @param x - input array
|
|
257
|
-
* @param options - function options
|
|
258
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
259
|
-
* @param options.cache - cache for resolving array index objects
|
|
260
|
-
* @returns fancy array
|
|
261
154
|
*
|
|
262
155
|
* @example
|
|
263
156
|
* var Uint16Array = require( '@stdlib/array-uint16' );
|
|
@@ -269,17 +162,6 @@ interface Array2Fancy {
|
|
|
269
162
|
*
|
|
270
163
|
* var v = y[ ':' ];
|
|
271
164
|
* // returns <Uint16Array>[ 1, 2, 3, 4 ]
|
|
272
|
-
*/
|
|
273
|
-
( x: Uint16Array, options?: Options ): Uint16Array;
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Converts an array to an object supporting fancy indexing.
|
|
277
|
-
*
|
|
278
|
-
* @param x - input array
|
|
279
|
-
* @param options - function options
|
|
280
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
281
|
-
* @param options.cache - cache for resolving array index objects
|
|
282
|
-
* @returns fancy array
|
|
283
165
|
*
|
|
284
166
|
* @example
|
|
285
167
|
* var Uint8Array = require( '@stdlib/array-uint8' );
|
|
@@ -291,17 +173,6 @@ interface Array2Fancy {
|
|
|
291
173
|
*
|
|
292
174
|
* var v = y[ ':' ];
|
|
293
175
|
* // returns <Uint8Array>[ 1, 2, 3, 4 ]
|
|
294
|
-
*/
|
|
295
|
-
( x: Uint8Array, options?: Options ): Uint8Array;
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Converts an array to an object supporting fancy indexing.
|
|
299
|
-
*
|
|
300
|
-
* @param x - input array
|
|
301
|
-
* @param options - function options
|
|
302
|
-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
|
|
303
|
-
* @param options.cache - cache for resolving array index objects
|
|
304
|
-
* @returns fancy array
|
|
305
176
|
*
|
|
306
177
|
* @example
|
|
307
178
|
* var Uint8ClampedArray = require( '@stdlib/array-uint8c' );
|
|
@@ -314,7 +185,7 @@ interface Array2Fancy {
|
|
|
314
185
|
* var v = y[ ':' ];
|
|
315
186
|
* // returns <Uint8ClampedArray>[ 1, 2, 3, 4 ]
|
|
316
187
|
*/
|
|
317
|
-
( x:
|
|
188
|
+
<T extends TypedArray | ComplexTypedArray | BooleanTypedArray>( x: T, options?: Options ): T;
|
|
318
189
|
|
|
319
190
|
/**
|
|
320
191
|
* Converts an array to an object supporting fancy indexing.
|
|
@@ -415,6 +286,24 @@ interface Array2Fancy {
|
|
|
415
286
|
* // returns [ 1, 2, 3, 4 ]
|
|
416
287
|
*/
|
|
417
288
|
factory( options?: Options ): Array2Fancy;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Array index constructor.
|
|
292
|
+
*
|
|
293
|
+
* @param x - input array
|
|
294
|
+
* @param options - function options
|
|
295
|
+
* @param options.persist - boolean indicating whether to continue persisting an index object after first usage
|
|
296
|
+
* @returns ArrayIndex instance
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* var Uint8Array = require( '@stdlib/array-uint8' );
|
|
300
|
+
*
|
|
301
|
+
* var x = new Uint8Array( [ 1, 0, 1, 0 ] );
|
|
302
|
+
*
|
|
303
|
+
* var idx = array2fancy.idx( x );
|
|
304
|
+
* // returns <ArrayIndex>
|
|
305
|
+
*/
|
|
306
|
+
idx: typeof ArrayIndex;
|
|
418
307
|
}
|
|
419
308
|
|
|
420
309
|
/**
|
package/lib/error_message.js
CHANGED
|
@@ -33,7 +33,7 @@ var replace = require( '@stdlib/string-base-replace' );
|
|
|
33
33
|
* @returns {string} updated message
|
|
34
34
|
*/
|
|
35
35
|
function errMessage( msg ) {
|
|
36
|
-
return replace( msg, /^invalid
|
|
36
|
+
return replace( msg, /^invalid arguments?/, 'invalid operation' );
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
|
package/lib/get_elements.js
CHANGED
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
// MODULES //
|
|
22
22
|
|
|
23
23
|
var take = require( '@stdlib/array-take' );
|
|
24
|
-
var mskfilter = require( '@stdlib/array-
|
|
25
|
-
var mskreject = require( '@stdlib/array-
|
|
24
|
+
var mskfilter = require( '@stdlib/array-mskfilter' );
|
|
25
|
+
var mskreject = require( '@stdlib/array-mskreject' );
|
|
26
26
|
var format = require( '@stdlib/string-format' );
|
|
27
27
|
var prop2array = require( './prop2array.js' );
|
|
28
28
|
|
|
@@ -36,7 +36,6 @@ var prop2array = require( './prop2array.js' );
|
|
|
36
36
|
* @param {Object} target - target object
|
|
37
37
|
* @param {string} property - index string
|
|
38
38
|
* @param {Object} ctx - context object
|
|
39
|
-
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
|
|
40
39
|
* @param {Object} ctx.cache - cache for resolving array index objects
|
|
41
40
|
* @param {Function} ctx.postGetArray - function to process a retrieved array
|
|
42
41
|
* @throws {Error} invalid array index
|
package/lib/index.js
CHANGED
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
// MODULES //
|
|
51
51
|
|
|
52
52
|
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
53
|
+
var ArrayIndex = require( '@stdlib/array-index' );
|
|
53
54
|
var main = require( './main.js' );
|
|
54
55
|
var factory = require( './factory.js' );
|
|
55
56
|
|
|
@@ -57,6 +58,7 @@ var factory = require( './factory.js' );
|
|
|
57
58
|
// MAIN //
|
|
58
59
|
|
|
59
60
|
setReadOnly( main, 'factory', factory );
|
|
61
|
+
setReadOnly( main, 'idx', ArrayIndex );
|
|
60
62
|
|
|
61
63
|
|
|
62
64
|
// EXPORTS //
|
package/lib/set.js
CHANGED
|
@@ -23,6 +23,8 @@
|
|
|
23
23
|
var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
|
|
24
24
|
var hasProperty = require( '@stdlib/assert-has-property' );
|
|
25
25
|
var isIntegerString = require( './is_integer_string.js' );
|
|
26
|
+
var isArrayIndexString = require( './is_array_index_string.js' );
|
|
27
|
+
var setElements = require( './set_elements.js' );
|
|
26
28
|
var setElement = require( './set_element.js' );
|
|
27
29
|
var setValue = require( './set_value.js' );
|
|
28
30
|
var setSlice = require( './set_slice.js' );
|
|
@@ -71,6 +73,9 @@ function factory( ctx ) {
|
|
|
71
73
|
if ( hasProperty( property ) || !isString( property ) ) {
|
|
72
74
|
return setValue( target, property, value, ctx );
|
|
73
75
|
}
|
|
76
|
+
if ( isArrayIndexString( property ) ) {
|
|
77
|
+
return setElements( target, property, value, ctx );
|
|
78
|
+
}
|
|
74
79
|
out = setSlice( target, property, value, receiver, ctx );
|
|
75
80
|
if ( out ) {
|
|
76
81
|
return out;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isMostlySafeCast = require( '@stdlib/array-base-assert-is-mostly-safe-data-type-cast' );
|
|
24
|
+
var isRealDataType = require( '@stdlib/array-base-assert-is-real-data-type' );
|
|
25
|
+
var isComplexDataType = require( '@stdlib/array-base-assert-is-complex-floating-point-data-type' );
|
|
26
|
+
var isCollection = require( '@stdlib/assert-is-collection' );
|
|
27
|
+
var scalar2array = require( '@stdlib/array-from-scalar' );
|
|
28
|
+
var dtype = require( '@stdlib/array-dtype' );
|
|
29
|
+
var put = require( '@stdlib/array-put' );
|
|
30
|
+
var place = require( '@stdlib/array-place' );
|
|
31
|
+
var convert = require( '@stdlib/array-convert' );
|
|
32
|
+
var where = require( '@stdlib/array-base-where' ).assign;
|
|
33
|
+
var format = require( '@stdlib/string-format' );
|
|
34
|
+
var prop2array = require( './prop2array.js' );
|
|
35
|
+
var errMessage = require( './error_message.js' );
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// MAIN //
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Replaces the elements specified by an array index.
|
|
42
|
+
*
|
|
43
|
+
* @private
|
|
44
|
+
* @param {Object} target - target object
|
|
45
|
+
* @param {string} property - index string
|
|
46
|
+
* @param {*} value - new value(s)
|
|
47
|
+
* @param {Object} ctx - context object
|
|
48
|
+
* @param {string} ctx.dtype - array data type
|
|
49
|
+
* @param {Object} ctx.cache - cache for resolving array index objects
|
|
50
|
+
* @param {Function} ctx.validator - function for validating new values
|
|
51
|
+
* @param {(Function|null)} ctx.preSetElement - function for normalizing new values (if necessary)
|
|
52
|
+
* @throws {Error} invalid array index
|
|
53
|
+
* @throws {RangeError} index exceeds array bounds
|
|
54
|
+
* @throws {Error} assigned value must be broadcast compatible with target array selection
|
|
55
|
+
* @throws {TypeError} assigned value cannot be safely cast to the target array data type
|
|
56
|
+
* @throws {TypeError} target array must have a supported data type
|
|
57
|
+
* @returns {boolean} boolean indicating whether assignment succeeded
|
|
58
|
+
*/
|
|
59
|
+
function setElements( target, property, value, ctx ) {
|
|
60
|
+
var tdt;
|
|
61
|
+
var vdt;
|
|
62
|
+
var idx;
|
|
63
|
+
var err;
|
|
64
|
+
var v;
|
|
65
|
+
|
|
66
|
+
idx = prop2array( property, ctx.cache );
|
|
67
|
+
tdt = ctx.dtype || 'generic';
|
|
68
|
+
if ( isCollection( value ) ) {
|
|
69
|
+
// When handling collections, we delegate to implementation APIs (see below) to perform argument validation (e.g., ensuring a (mostly) safe cast, broadcast compatibility, etc), so we just reassign the value here:
|
|
70
|
+
v = value;
|
|
71
|
+
} else {
|
|
72
|
+
// When provided a "scalar", we need to check whether the value can be safely cast to the target array data type:
|
|
73
|
+
err = ctx.validator( value, tdt );
|
|
74
|
+
if ( err ) {
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
if ( ctx.preSetElement ) {
|
|
78
|
+
v = ctx.preSetElement( value );
|
|
79
|
+
} else {
|
|
80
|
+
v = value;
|
|
81
|
+
}
|
|
82
|
+
// As the scalar can be safely cast, convert the scalar to an array having the same data type as the target array to allow for broadcasting during assignment:
|
|
83
|
+
v = scalar2array( v, tdt );
|
|
84
|
+
vdt = tdt;
|
|
85
|
+
}
|
|
86
|
+
if ( idx.type === 'int' ) {
|
|
87
|
+
try {
|
|
88
|
+
put( target, idx.data, v ); // note: defer to `put` for ensuring a mostly safe cast
|
|
89
|
+
} catch ( err ) {
|
|
90
|
+
throw new err.constructor( errMessage( err.message ) );
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
if ( idx.type === 'bool' ) {
|
|
95
|
+
try {
|
|
96
|
+
place( target, idx.data, v, {
|
|
97
|
+
'mode': 'strict_broadcast'
|
|
98
|
+
});
|
|
99
|
+
} catch ( err ) {
|
|
100
|
+
throw new err.constructor( errMessage( err.message ) );
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
if ( vdt === void 0 ) {
|
|
105
|
+
vdt = dtype( value ) || 'generic';
|
|
106
|
+
}
|
|
107
|
+
// Safe casts are always allowed and allow same kind casts (i.e., downcasts) only when the target array data type is floating-point...
|
|
108
|
+
if ( !isMostlySafeCast( vdt, tdt ) ) {
|
|
109
|
+
throw new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', vdt, tdt ) );
|
|
110
|
+
}
|
|
111
|
+
// When performing a real-to-complex assignment, interpret the real-valued array as containing real components with implied imaginary components equal to zero and explicitly convert to a complex-valued array...
|
|
112
|
+
if ( isComplexDataType( tdt ) && isRealDataType( vdt ) ) {
|
|
113
|
+
v = convert( v, tdt );
|
|
114
|
+
}
|
|
115
|
+
if ( idx.type === 'mask' ) {
|
|
116
|
+
// NOTE: we intentionally deviate from boolean array indexing here and interpret the mask as applying to both the target and values array, thus requiring that the assigned value array be broadcast compatible with the target array and NOT just the selected elements as in boolean array indexing
|
|
117
|
+
try {
|
|
118
|
+
where( idx.data, target, v, target, 1, 0 );
|
|
119
|
+
} catch ( err ) {
|
|
120
|
+
throw new err.constructor( errMessage( err.message ) );
|
|
121
|
+
}
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
throw new Error( format( 'invalid operation. Unrecognized array index type. Value: `%s`.', idx.type ) );
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
// EXPORTS //
|
|
129
|
+
|
|
130
|
+
module.exports = setElements;
|
package/lib/set_slice.js
CHANGED
|
@@ -40,6 +40,7 @@ var errMessage = require( './error_message.js' );
|
|
|
40
40
|
* @param {Object} ctx - context object
|
|
41
41
|
* @param {string} ctx.dtype - array data type
|
|
42
42
|
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
|
|
43
|
+
* @param {Function} ctx.validator - function for validating new values
|
|
43
44
|
* @throws {Error} invalid slice operation
|
|
44
45
|
* @throws {RangeError} slice exceeds array bounds
|
|
45
46
|
* @throws {Error} assigned value must be broadcast compatible with target array view
|
|
@@ -67,14 +68,14 @@ function setSlice( target, property, value, receiver, ctx ) {
|
|
|
67
68
|
throw err;
|
|
68
69
|
}
|
|
69
70
|
// As the scalar can be safely cast, convert the scalar to an array having the same data type as the target array to allow for broadcasting during slice assignment:
|
|
70
|
-
v = scalar2array( value, ctx.dtype );
|
|
71
|
+
v = scalar2array( value, ctx.dtype || 'generic' );
|
|
71
72
|
}
|
|
72
73
|
try {
|
|
73
74
|
sliceAssign( v, receiver, s, ctx.strict );
|
|
74
|
-
return true;
|
|
75
75
|
} catch ( err ) {
|
|
76
76
|
throw new err.constructor( errMessage( err.message ) );
|
|
77
77
|
}
|
|
78
|
+
return true;
|
|
78
79
|
}
|
|
79
80
|
|
|
80
81
|
|
package/lib/validator.js
CHANGED
|
@@ -22,10 +22,12 @@
|
|
|
22
22
|
|
|
23
23
|
var isNumber = require( '@stdlib/assert-is-number' ).isPrimitive;
|
|
24
24
|
var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive;
|
|
25
|
+
var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;
|
|
25
26
|
var isComplexLike = require( '@stdlib/assert-is-complex-like' );
|
|
26
27
|
var isRealFloatingDataType = require( '@stdlib/array-base-assert-is-real-floating-point-data-type' );
|
|
27
28
|
var isUnsignedIntegerDataType = require( '@stdlib/array-base-assert-is-unsigned-integer-data-type' );
|
|
28
29
|
var isSignedIntegerDataType = require( '@stdlib/array-base-assert-is-signed-integer-data-type' );
|
|
30
|
+
var isBooleanDataType = require( '@stdlib/array-base-assert-is-boolean-data-type' );
|
|
29
31
|
var isSafeCast = require( '@stdlib/array-base-assert-is-safe-data-type-cast' );
|
|
30
32
|
var minDataType = require( '@stdlib/array-min-dtype' );
|
|
31
33
|
var minSignedIntegerDataType = require( '@stdlib/array-base-min-signed-integer-dtype' );
|
|
@@ -52,7 +54,32 @@ function validateGeneric() {
|
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
/**
|
|
55
|
-
* Verifies whether a provided value can be safely assigned to an element in an array
|
|
57
|
+
* Verifies whether a provided value can be safely assigned to an element in an array having a boolean data type.
|
|
58
|
+
*
|
|
59
|
+
* @private
|
|
60
|
+
* @param {*} value - input value
|
|
61
|
+
* @param {string} dtype - array data type
|
|
62
|
+
* @returns {(Error|null)} error object or null
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* var err = validateBoolean( true, 'bool' );
|
|
66
|
+
* // returns null
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* var Complex128 = require( '@stdlib/complex-float64-ctor' );
|
|
70
|
+
*
|
|
71
|
+
* var err = validateBoolean( new Complex128( 5.0, 6.0 ), 'bool' );
|
|
72
|
+
* // returns <TypeError>
|
|
73
|
+
*/
|
|
74
|
+
function validateBoolean( value, dtype ) {
|
|
75
|
+
if ( isBoolean( value ) ) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
return new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', typeof value, dtype ) );
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Verifies whether a provided value can be safely assigned to an element in an array having a real-valued floating-point data type.
|
|
56
83
|
*
|
|
57
84
|
* @private
|
|
58
85
|
* @param {*} value - input value
|
|
@@ -64,7 +91,7 @@ function validateGeneric() {
|
|
|
64
91
|
* // returns null
|
|
65
92
|
*
|
|
66
93
|
* @example
|
|
67
|
-
* var Complex128 = require( '@stdlib/complex-float64' );
|
|
94
|
+
* var Complex128 = require( '@stdlib/complex-float64-ctor' );
|
|
68
95
|
*
|
|
69
96
|
* var err = validateRealFloating( new Complex128( 5.0, 6.0 ), 'float64' );
|
|
70
97
|
* // returns <TypeError>
|
|
@@ -80,7 +107,7 @@ function validateRealFloating( value, dtype ) {
|
|
|
80
107
|
}
|
|
81
108
|
|
|
82
109
|
/**
|
|
83
|
-
* Verifies whether a provided value can be safely assigned to an element in an array
|
|
110
|
+
* Verifies whether a provided value can be safely assigned to an element in an array having a complex-valued floating-point data type.
|
|
84
111
|
*
|
|
85
112
|
* @private
|
|
86
113
|
* @param {*} value - input value
|
|
@@ -88,7 +115,7 @@ function validateRealFloating( value, dtype ) {
|
|
|
88
115
|
* @returns {(Error|null)} error object or null
|
|
89
116
|
*
|
|
90
117
|
* @example
|
|
91
|
-
* var Complex128 = require( '@stdlib/complex-float64' );
|
|
118
|
+
* var Complex128 = require( '@stdlib/complex-float64-ctor' );
|
|
92
119
|
*
|
|
93
120
|
* var err = validateComplexFloating( new Complex128( 5.0, 6.0 ), 'complex128' );
|
|
94
121
|
* // returns null
|
|
@@ -105,7 +132,7 @@ function validateComplexFloating( value, dtype ) {
|
|
|
105
132
|
}
|
|
106
133
|
|
|
107
134
|
/**
|
|
108
|
-
* Verifies whether a provided value can be safely assigned to an element in an array
|
|
135
|
+
* Verifies whether a provided value can be safely assigned to an element in an array having a signed integer data type.
|
|
109
136
|
*
|
|
110
137
|
* @private
|
|
111
138
|
* @param {*} value - input value
|
|
@@ -139,7 +166,7 @@ function validateSignedInteger( value, dtype ) {
|
|
|
139
166
|
}
|
|
140
167
|
|
|
141
168
|
/**
|
|
142
|
-
* Verifies whether a provided value can be safely assigned to an element in an array
|
|
169
|
+
* Verifies whether a provided value can be safely assigned to an element in an array having an unsigned integer data type.
|
|
143
170
|
*
|
|
144
171
|
* @private
|
|
145
172
|
* @param {*} value - input value
|
|
@@ -201,6 +228,9 @@ function validator( dtype ) {
|
|
|
201
228
|
if ( isSignedIntegerDataType( dtype ) ) {
|
|
202
229
|
return validateSignedInteger;
|
|
203
230
|
}
|
|
231
|
+
if ( isBooleanDataType( dtype ) ) {
|
|
232
|
+
return validateBoolean;
|
|
233
|
+
}
|
|
204
234
|
// Case: isComplexDataType( dtype ) === true
|
|
205
235
|
return validateComplexFloating;
|
|
206
236
|
}
|