@stdlib/array-to-fancy 0.1.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/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +646 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +443 -0
- package/lib/ctor.js +94 -0
- package/lib/defaults.js +48 -0
- package/lib/error_constructor.js +53 -0
- package/lib/error_message.js +42 -0
- package/lib/factory.js +169 -0
- package/lib/get.js +79 -0
- package/lib/get_array_wrapper.js +51 -0
- package/lib/get_element.js +46 -0
- package/lib/get_elements.js +63 -0
- package/lib/get_slice.js +60 -0
- package/lib/get_value.js +70 -0
- package/lib/has_proxy_support.js +40 -0
- package/lib/index.js +64 -0
- package/lib/is_array_index_string.js +51 -0
- package/lib/is_integer_string.js +51 -0
- package/lib/main.js +68 -0
- package/lib/prop2array.js +69 -0
- package/lib/prop2slice.js +163 -0
- package/lib/re_array_index.js +47 -0
- package/lib/re_integer.js +47 -0
- package/lib/re_subseq.js +47 -0
- package/lib/resolve_index.js +66 -0
- package/lib/set.js +86 -0
- package/lib/set_element.js +65 -0
- package/lib/set_element_wrapper.js +65 -0
- package/lib/set_slice.js +83 -0
- package/lib/set_value.js +40 -0
- package/lib/validate.js +74 -0
- package/lib/validator.js +211 -0
- package/package.json +146 -0
package/README.md
ADDED
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2024 The Stdlib Authors.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
-->
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
<details>
|
|
23
|
+
<summary>
|
|
24
|
+
About stdlib...
|
|
25
|
+
</summary>
|
|
26
|
+
<p>We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.</p>
|
|
27
|
+
<p>The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.</p>
|
|
28
|
+
<p>When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.</p>
|
|
29
|
+
<p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
|
|
30
|
+
</details>
|
|
31
|
+
|
|
32
|
+
# array2fancy
|
|
33
|
+
|
|
34
|
+
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
|
|
35
|
+
|
|
36
|
+
> Convert an array to an object supporting fancy indexing.
|
|
37
|
+
|
|
38
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
39
|
+
|
|
40
|
+
<section class="intro">
|
|
41
|
+
|
|
42
|
+
An array supporting **fancy indexing** is an array which supports slicing via indexing expressions for both retrieval and assignment.
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
var array2fancy = require( '@stdlib/array-to-fancy' );
|
|
46
|
+
|
|
47
|
+
// Create a plain array:
|
|
48
|
+
var x = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
|
|
49
|
+
|
|
50
|
+
// Turn the plain array into a "fancy" array:
|
|
51
|
+
var y = array2fancy( x );
|
|
52
|
+
|
|
53
|
+
// Select the first 3 elements:
|
|
54
|
+
var v = y[ ':3' ];
|
|
55
|
+
// returns [ 1, 2, 3 ]
|
|
56
|
+
|
|
57
|
+
// Select every other element, starting from the second element:
|
|
58
|
+
v = y[ '1::2' ];
|
|
59
|
+
// returns [ 2, 4, 6, 8 ]
|
|
60
|
+
|
|
61
|
+
// Select every other element, in reverse order, starting with the least element:
|
|
62
|
+
v = y[ '::-2' ];
|
|
63
|
+
// returns [ 8, 6, 4, 2 ]
|
|
64
|
+
|
|
65
|
+
// Set all elements to the same value:
|
|
66
|
+
y[ ':' ] = 9;
|
|
67
|
+
|
|
68
|
+
// Create a shallow copy by selecting all elements:
|
|
69
|
+
v = y[ ':' ];
|
|
70
|
+
// returns [ 9, 9, 9, 9, 9, 9, 9, 9 ]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
</section>
|
|
74
|
+
|
|
75
|
+
<!-- /.intro -->
|
|
76
|
+
|
|
77
|
+
<!-- Package usage documentation. -->
|
|
78
|
+
|
|
79
|
+
<section class="installation">
|
|
80
|
+
|
|
81
|
+
## Installation
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
npm install @stdlib/array-to-fancy
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
</section>
|
|
88
|
+
|
|
89
|
+
<section class="usage">
|
|
90
|
+
|
|
91
|
+
## Usage
|
|
92
|
+
|
|
93
|
+
```javascript
|
|
94
|
+
var array2fancy = require( '@stdlib/array-to-fancy' );
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### array2fancy( x\[, options] )
|
|
98
|
+
|
|
99
|
+
Converts an array to an object supporting fancy indexing.
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
var Slice = require( '@stdlib/slice-ctor' );
|
|
103
|
+
|
|
104
|
+
var x = [ 1, 2, 3, 4 ];
|
|
105
|
+
|
|
106
|
+
var y = array2fancy( x );
|
|
107
|
+
// returns <Array>
|
|
108
|
+
|
|
109
|
+
// Normal element access:
|
|
110
|
+
var v = y[ 0 ];
|
|
111
|
+
// returns 1
|
|
112
|
+
|
|
113
|
+
v = y[ 1 ];
|
|
114
|
+
// returns 2
|
|
115
|
+
|
|
116
|
+
// Using negative integers:
|
|
117
|
+
v = y[ -1 ];
|
|
118
|
+
// returns 4
|
|
119
|
+
|
|
120
|
+
v = y[ -2 ];
|
|
121
|
+
// returns 3
|
|
122
|
+
|
|
123
|
+
// Using subsequence expressions:
|
|
124
|
+
v = y[ '1::2' ];
|
|
125
|
+
// returns [ 2, 4 ]
|
|
126
|
+
|
|
127
|
+
// Using Slice objects:
|
|
128
|
+
v = y[ new Slice( 1, null, 2 ) ];
|
|
129
|
+
// returns [ 2, 4 ]
|
|
130
|
+
|
|
131
|
+
// Assignment:
|
|
132
|
+
y[ '1:3' ] = 5;
|
|
133
|
+
v = y[ ':' ];
|
|
134
|
+
// returns [ 1, 5, 5, 4 ]
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The function supports the following options:
|
|
138
|
+
|
|
139
|
+
- **cache**: cache for resolving array index objects. Must have a `get` method which accepts a single argument: a string identifier associated with an array index.
|
|
140
|
+
|
|
141
|
+
If an array index associated with a provided identifier exists, the `get` method should return an object having the following properties:
|
|
142
|
+
|
|
143
|
+
- **data**: the underlying index array.
|
|
144
|
+
- **type**: the index type. Must be either `'mask'`, `'bool'`, or `'int'`.
|
|
145
|
+
- **dtype**: the [data type][@stdlib/array/dtypes] of the underlying array.
|
|
146
|
+
|
|
147
|
+
If an array index is not associated with a provided identifier, the `get` method should return `null`.
|
|
148
|
+
|
|
149
|
+
Default: [`ArrayIndex`][@stdlib/array/index].
|
|
150
|
+
|
|
151
|
+
- **strict**: boolean indicating whether to enforce strict bounds checking. Default: `false`.
|
|
152
|
+
|
|
153
|
+
By default, the function returns a fancy array which does **not** enforce strict bounds checking. For example,
|
|
154
|
+
|
|
155
|
+
```javascript
|
|
156
|
+
var y = array2fancy( [ 1, 2, 3, 4 ] );
|
|
157
|
+
|
|
158
|
+
var v = y[ 10 ];
|
|
159
|
+
// returns undefined
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
To enforce strict bounds checking, set the `strict` option to `true`.
|
|
163
|
+
|
|
164
|
+
<!-- run throws: true -->
|
|
165
|
+
|
|
166
|
+
```javascript
|
|
167
|
+
var y = array2fancy( [ 1, 2, 3, 4 ], {
|
|
168
|
+
'strict': true
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
var v = y[ 10 ];
|
|
172
|
+
// throws <RangeError>
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### array2fancy.factory( \[options] )
|
|
176
|
+
|
|
177
|
+
Returns a function for converting an array to an object supporting fancy indexing.
|
|
178
|
+
|
|
179
|
+
```javascript
|
|
180
|
+
var fcn = array2fancy.factory();
|
|
181
|
+
|
|
182
|
+
var x = [ 1, 2, 3, 4 ];
|
|
183
|
+
|
|
184
|
+
var y = fcn( x );
|
|
185
|
+
// returns <Array>
|
|
186
|
+
|
|
187
|
+
var v = y[ ':' ];
|
|
188
|
+
// returns [ 1, 2, 3, 4 ]
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
The function supports the following options:
|
|
192
|
+
|
|
193
|
+
- **cache**: default cache for resolving array index objects. Must have a `get` method which accepts a single argument: a string identifier associated with an array index.
|
|
194
|
+
|
|
195
|
+
If an array index associated with a provided identifier exists, the `get` method should return an object having the following properties:
|
|
196
|
+
|
|
197
|
+
- **data**: the underlying index array.
|
|
198
|
+
- **type**: the index type. Must be either `'mask'`, `'bool'`, or `'int'`.
|
|
199
|
+
- **dtype**: the [data type][@stdlib/array/dtypes] of the underlying array.
|
|
200
|
+
|
|
201
|
+
If an array index is not associated with a provided identifier, the `get` method should return `null`.
|
|
202
|
+
|
|
203
|
+
Default: [`ArrayIndex`][@stdlib/array/index].
|
|
204
|
+
|
|
205
|
+
- **strict**: boolean indicating whether to enforce strict bounds checking by default. Default: `false`.
|
|
206
|
+
|
|
207
|
+
By default, the function returns a function which, by default, does **not** enforce strict bounds checking. For example,
|
|
208
|
+
|
|
209
|
+
```javascript
|
|
210
|
+
var fcn = array2fancy.factory();
|
|
211
|
+
|
|
212
|
+
var y = fcn( [ 1, 2, 3, 4 ] );
|
|
213
|
+
|
|
214
|
+
var v = y[ 10 ];
|
|
215
|
+
// returns undefined
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
To enforce strict bounds checking by default, set the `strict` option to `true`.
|
|
219
|
+
|
|
220
|
+
<!-- run throws: true -->
|
|
221
|
+
|
|
222
|
+
```javascript
|
|
223
|
+
var fcn = array2fancy.factory({
|
|
224
|
+
'strict': true
|
|
225
|
+
});
|
|
226
|
+
var y = fcn( [ 1, 2, 3, 4 ] );
|
|
227
|
+
|
|
228
|
+
var v = y[ 10 ];
|
|
229
|
+
// throws <RangeError>
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
The returned function supports the same options as above. When the returned function is provided option values, those values override the factory method defaults.
|
|
233
|
+
|
|
234
|
+
</section>
|
|
235
|
+
|
|
236
|
+
<!-- /.usage -->
|
|
237
|
+
|
|
238
|
+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
239
|
+
|
|
240
|
+
* * *
|
|
241
|
+
|
|
242
|
+
<section class="notes">
|
|
243
|
+
|
|
244
|
+
## Notes
|
|
245
|
+
|
|
246
|
+
- A fancy array shares the **same** data as the provided input array. Hence, any mutations to the returned array will affect the underlying input array and vice versa.
|
|
247
|
+
- For operations returning a new array (e.g., when slicing or invoking an instance method), a fancy array returns a new fancy array having the same configuration as specified by `options`.
|
|
248
|
+
- A fancy array supports indexing using positive and negative integers (both numeric literals and strings), [`Slice`][@stdlib/slice/ctor] instances, [subsequence expressions][@stdlib/slice/seq2slice], and [index arrays][@stdlib/array/index] (boolean, mask, and integer).
|
|
249
|
+
- A fancy array supports all properties and methods of the input array, and, thus, a fancy array can be consumed by any API which supports array-like objects.
|
|
250
|
+
- Indexing expressions provide a convenient and powerful means for creating and operating on array views; however, their use does entail a performance cost. Indexing expressions are best suited for interactive use (e.g., in the [REPL][@stdlib/repl]) and scripting. For performance critical applications, prefer equivalent functional APIs supporting array-like objects.
|
|
251
|
+
- In older JavaScript environments which do **not** support [`Proxy`][@stdlib/proxy/ctor] objects, the use of indexing expressions is **not** supported.
|
|
252
|
+
|
|
253
|
+
### Bounds Checking
|
|
254
|
+
|
|
255
|
+
By default, fancy arrays do **not** enforce strict bounds checking across index expressions. The motivation for the default fancy array behavior stems from a desire to maintain parity with plain arrays; namely, the returning of `undefined` when accessing a single non-existent property.
|
|
256
|
+
|
|
257
|
+
Accordingly, when `strict` is `false`, one may observe the following behaviors:
|
|
258
|
+
|
|
259
|
+
<!-- run throws: true -->
|
|
260
|
+
|
|
261
|
+
```javascript
|
|
262
|
+
var idx = require( '@stdlib/array-index' );
|
|
263
|
+
|
|
264
|
+
var x = array2fancy( [ 1, 2, 3, 4 ], {
|
|
265
|
+
'strict': false
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// Access a non-existent property:
|
|
269
|
+
var v = x[ 'foo' ];
|
|
270
|
+
// returns undefined
|
|
271
|
+
|
|
272
|
+
// Access an out-of-bounds index:
|
|
273
|
+
v = x[ 10 ];
|
|
274
|
+
// returns undefined
|
|
275
|
+
|
|
276
|
+
v = x[ -10 ];
|
|
277
|
+
// returns undefined
|
|
278
|
+
|
|
279
|
+
// Access an out-of-bounds slice:
|
|
280
|
+
v = x[ '10:' ];
|
|
281
|
+
// returns []
|
|
282
|
+
|
|
283
|
+
// Access one or more out-of-bounds indices:
|
|
284
|
+
v = x[ idx( [ 10, 20 ] ) ];
|
|
285
|
+
// throws <RangeError>
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
When `strict` is `true`, fancy arrays normalize index behavior and consistently enforce strict bounds checking.
|
|
289
|
+
|
|
290
|
+
<!-- run throws: true -->
|
|
291
|
+
|
|
292
|
+
```javascript
|
|
293
|
+
var idx = require( '@stdlib/array-index' );
|
|
294
|
+
|
|
295
|
+
var x = array2fancy( [ 1, 2, 3, 4 ], {
|
|
296
|
+
'strict': true
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Access a non-existent property:
|
|
300
|
+
var v = x[ 'foo' ];
|
|
301
|
+
// returns undefined
|
|
302
|
+
|
|
303
|
+
// Access an out-of-bounds index:
|
|
304
|
+
v = x[ 10 ];
|
|
305
|
+
// throws <RangeError>
|
|
306
|
+
|
|
307
|
+
v = x[ -10 ];
|
|
308
|
+
// throws <RangeError>
|
|
309
|
+
|
|
310
|
+
// Access an out-of-bounds slice:
|
|
311
|
+
v = x[ '10:' ];
|
|
312
|
+
// throws <RangeError>
|
|
313
|
+
|
|
314
|
+
// Access one or more out-of-bounds indices:
|
|
315
|
+
v = x[ idx( [ 10, 20 ] ) ];
|
|
316
|
+
// throws <RangeError>
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### Broadcasting
|
|
320
|
+
|
|
321
|
+
Fancy arrays support **broadcasting** in which assigned scalars and single-element arrays are repeated (without additional memory allocation) to match the length of a target array instance.
|
|
322
|
+
|
|
323
|
+
```javascript
|
|
324
|
+
var y = array2fancy( [ 1, 2, 3, 4 ] );
|
|
325
|
+
|
|
326
|
+
// Broadcast a scalar:
|
|
327
|
+
y[ ':' ] = 5;
|
|
328
|
+
var v = y[ ':' ];
|
|
329
|
+
// returns [ 5, 5, 5, 5 ]
|
|
330
|
+
|
|
331
|
+
// Broadcast a single-element array:
|
|
332
|
+
y[ ':' ] = [ 6 ];
|
|
333
|
+
v = y[ ':' ];
|
|
334
|
+
// returns [ 6, 6, 6, 6 ]
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
Fancy array broadcasting follows the [same rules][@stdlib/ndarray/base/broadcast-shapes] as for [ndarrays][@stdlib/ndarray/ctor]. Consequently, when assigning arrays to slices, the array on the right-hand-side must be broadcast-compatible with number of elements in the slice. For example, each assignment expression in the following example follows broadcast rules and is thus valid.
|
|
338
|
+
|
|
339
|
+
```javascript
|
|
340
|
+
var y = array2fancy( [ 1, 2, 3, 4 ] );
|
|
341
|
+
|
|
342
|
+
y[ ':' ] = [ 5, 6, 7, 8 ];
|
|
343
|
+
var v = y[ ':' ];
|
|
344
|
+
// returns [ 5, 6, 7, 8 ]
|
|
345
|
+
|
|
346
|
+
y[ '1::2' ] = [ 9, 10 ];
|
|
347
|
+
v = y[ ':' ];
|
|
348
|
+
// returns [ 5, 9, 7, 10 ]
|
|
349
|
+
|
|
350
|
+
y[ '1::2' ] = [ 11 ];
|
|
351
|
+
v = y[ ':' ];
|
|
352
|
+
// returns [ 5, 11, 7, 11 ]
|
|
353
|
+
|
|
354
|
+
y[ '1::2' ] = 12;
|
|
355
|
+
v = y[ ':' ];
|
|
356
|
+
// returns [ 5, 12, 7, 12 ]
|
|
357
|
+
|
|
358
|
+
// Out-of-bounds slices (i.e., slices with zero elements):
|
|
359
|
+
y[ '10:20' ] = [ 13 ];
|
|
360
|
+
v = y[ ':' ];
|
|
361
|
+
// returns [ 5, 12, 7, 12 ]
|
|
362
|
+
|
|
363
|
+
y[ '10:20' ] = 13;
|
|
364
|
+
v = y[ ':' ];
|
|
365
|
+
// returns [ 5, 12, 7, 12 ]
|
|
366
|
+
|
|
367
|
+
y[ '10:20' ] = [];
|
|
368
|
+
v = y[ ':' ];
|
|
369
|
+
// returns [ 5, 12, 7, 12 ]
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
However, the following assignment expressions are not valid.
|
|
373
|
+
|
|
374
|
+
<!-- run throws: true -->
|
|
375
|
+
|
|
376
|
+
```javascript
|
|
377
|
+
var y = array2fancy( [ 1, 2, 3, 4 ] );
|
|
378
|
+
|
|
379
|
+
y[ ':' ] = [ 5, 6 ];
|
|
380
|
+
// throws <Error>
|
|
381
|
+
|
|
382
|
+
// Out-of-bounds slice (i.e., a slice with zero elements):
|
|
383
|
+
y[ '10:20' ] = [ 8, 9, 10, 11 ];
|
|
384
|
+
// throws <Error>
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### Casting
|
|
388
|
+
|
|
389
|
+
Fancy arrays support [(mostly) safe casts][@stdlib/array/mostly-safe-casts] (i.e., any cast which can be performed without overflow or loss of precision, with the exception of floating-point arrays which are also allowed to downcast from higher precision to lower precision).
|
|
390
|
+
|
|
391
|
+
```javascript
|
|
392
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
393
|
+
var Int32Array = require( '@stdlib/array-int32' );
|
|
394
|
+
|
|
395
|
+
var x = new Int32Array( [ 1, 2, 3, 4 ] );
|
|
396
|
+
var y = array2fancy( x );
|
|
397
|
+
|
|
398
|
+
// 8-bit unsigned integer values can be safely cast to 32-bit signed integer values:
|
|
399
|
+
y[ ':' ] = new Uint8Array( [ 5, 6, 7, 8 ] );
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
When attempting to perform an unsafe cast, fancy arrays will raise an exception.
|
|
403
|
+
|
|
404
|
+
<!-- run throws: true -->
|
|
405
|
+
|
|
406
|
+
```javascript
|
|
407
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
408
|
+
|
|
409
|
+
var x = new Uint8Array( [ 1, 2, 3, 4 ] );
|
|
410
|
+
var y = array2fancy( x );
|
|
411
|
+
|
|
412
|
+
// Attempt to assign a non-integer value:
|
|
413
|
+
y[ ':' ] = 3.14;
|
|
414
|
+
// throws <TypeError>
|
|
415
|
+
|
|
416
|
+
// Attempt to assign a negative value:
|
|
417
|
+
y[ ':' ] = -3;
|
|
418
|
+
// throws <TypeError>
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
When assigning a real-valued scalar to a complex number array (e.g., [`Complex128Array`][@stdlib/array/complex128] or [`Complex64Array`][@stdlib/array/complex64]), a fancy array will cast the real-valued scalar to a complex number argument having an imaginary component equal to zero.
|
|
422
|
+
|
|
423
|
+
```javascript
|
|
424
|
+
var Complex128Array = require( '@stdlib/array-complex128' );
|
|
425
|
+
var real = require( '@stdlib/complex-real' );
|
|
426
|
+
var imag = require( '@stdlib/complex-imag' );
|
|
427
|
+
|
|
428
|
+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
|
|
429
|
+
var y = array2fancy( x );
|
|
430
|
+
|
|
431
|
+
// Retrieve the first element:
|
|
432
|
+
var v = y[ 0 ];
|
|
433
|
+
// returns <Complex128>
|
|
434
|
+
|
|
435
|
+
var re = real( v );
|
|
436
|
+
// returns 1.0
|
|
437
|
+
|
|
438
|
+
var im = imag( v );
|
|
439
|
+
// returns 2.0
|
|
440
|
+
|
|
441
|
+
// Assign a real-valued scalar to the first element:
|
|
442
|
+
y[ 0 ] = 9.0;
|
|
443
|
+
|
|
444
|
+
v = y[ 0 ];
|
|
445
|
+
// returns <Complex128>
|
|
446
|
+
|
|
447
|
+
re = real( v );
|
|
448
|
+
// returns 9.0
|
|
449
|
+
|
|
450
|
+
im = imag( v );
|
|
451
|
+
// returns 0.0
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
</section>
|
|
455
|
+
|
|
456
|
+
<!-- /.notes -->
|
|
457
|
+
|
|
458
|
+
<!-- Package usage examples. -->
|
|
459
|
+
|
|
460
|
+
* * *
|
|
461
|
+
|
|
462
|
+
<section class="examples">
|
|
463
|
+
|
|
464
|
+
## Examples
|
|
465
|
+
|
|
466
|
+
<!-- eslint no-undef: "error" -->
|
|
467
|
+
|
|
468
|
+
```javascript
|
|
469
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
470
|
+
var Int32Array = require( '@stdlib/array-int32' );
|
|
471
|
+
var idx = require( '@stdlib/array-index' );
|
|
472
|
+
var array2fancy = require( '@stdlib/array-to-fancy' );
|
|
473
|
+
|
|
474
|
+
var x = [ 1, 2, 3, 4, 5, 6 ];
|
|
475
|
+
var y = array2fancy( x );
|
|
476
|
+
// returns <Array>
|
|
477
|
+
|
|
478
|
+
// Slice retrieval:
|
|
479
|
+
var z = y[ '1::2' ];
|
|
480
|
+
// returns [ 2, 4, 6 ]
|
|
481
|
+
|
|
482
|
+
z = y[ '-2::-2' ];
|
|
483
|
+
// returns [ 5, 3, 1 ]
|
|
484
|
+
|
|
485
|
+
z = y[ '1:4' ];
|
|
486
|
+
// returns [ 2, 3, 4 ]
|
|
487
|
+
|
|
488
|
+
// Slice assignment:
|
|
489
|
+
y[ '4:1:-1' ] = 10;
|
|
490
|
+
z = y[ ':' ];
|
|
491
|
+
// returns [ 1, 2, 10, 10, 10, 6 ]
|
|
492
|
+
|
|
493
|
+
y[ '2:5' ] = [ -10, -9, -8 ];
|
|
494
|
+
z = y[ ':' ];
|
|
495
|
+
// returns [ 1, 2, -10, -9, -8, 6 ]
|
|
496
|
+
|
|
497
|
+
// Array index retrieval:
|
|
498
|
+
var i = idx( [ 1, 3, 4 ] ); // integer index array
|
|
499
|
+
z = y[ i ];
|
|
500
|
+
// returns [ 2, -9, -8 ]
|
|
501
|
+
|
|
502
|
+
i = idx( [ true, false, false, true, true, true ] ); // boolean array
|
|
503
|
+
z = y[ i ];
|
|
504
|
+
// returns [ 1, -9, -8, 6 ]
|
|
505
|
+
|
|
506
|
+
i = idx( new Uint8Array( [ 0, 0, 1, 0, 0, 1 ] ) ); // mask array
|
|
507
|
+
z = y[ i ];
|
|
508
|
+
// returns [ 1, 2, -9, -8 ]
|
|
509
|
+
|
|
510
|
+
i = idx( new Int32Array( [ 0, 0, 1, 1, 2, 2 ] ) ); // integer index array
|
|
511
|
+
z = y[ i ];
|
|
512
|
+
// returns [ 1, 1, 2, 2, -10, -10 ]
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
</section>
|
|
516
|
+
|
|
517
|
+
<!-- /.examples -->
|
|
518
|
+
|
|
519
|
+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
520
|
+
|
|
521
|
+
<section class="references">
|
|
522
|
+
|
|
523
|
+
</section>
|
|
524
|
+
|
|
525
|
+
<!-- /.references -->
|
|
526
|
+
|
|
527
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
528
|
+
|
|
529
|
+
<section class="related">
|
|
530
|
+
|
|
531
|
+
* * *
|
|
532
|
+
|
|
533
|
+
## See Also
|
|
534
|
+
|
|
535
|
+
- <span class="package-name">[`@stdlib/array-slice`][@stdlib/array/slice]</span><span class="delimiter">: </span><span class="description">return a shallow copy of a portion of an array.</span>
|
|
536
|
+
- <span class="package-name">[`@stdlib/ndarray-fancy`][@stdlib/ndarray/fancy]</span><span class="delimiter">: </span><span class="description">fancy multidimensional array constructor.</span>
|
|
537
|
+
|
|
538
|
+
</section>
|
|
539
|
+
|
|
540
|
+
<!-- /.related -->
|
|
541
|
+
|
|
542
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
<section class="main-repo" >
|
|
546
|
+
|
|
547
|
+
* * *
|
|
548
|
+
|
|
549
|
+
## Notice
|
|
550
|
+
|
|
551
|
+
This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
|
|
552
|
+
|
|
553
|
+
For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
|
|
554
|
+
|
|
555
|
+
#### Community
|
|
556
|
+
|
|
557
|
+
[![Chat][chat-image]][chat-url]
|
|
558
|
+
|
|
559
|
+
---
|
|
560
|
+
|
|
561
|
+
## License
|
|
562
|
+
|
|
563
|
+
See [LICENSE][stdlib-license].
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
## Copyright
|
|
567
|
+
|
|
568
|
+
Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
569
|
+
|
|
570
|
+
</section>
|
|
571
|
+
|
|
572
|
+
<!-- /.stdlib -->
|
|
573
|
+
|
|
574
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
575
|
+
|
|
576
|
+
<section class="links">
|
|
577
|
+
|
|
578
|
+
[npm-image]: http://img.shields.io/npm/v/@stdlib/array-to-fancy.svg
|
|
579
|
+
[npm-url]: https://npmjs.org/package/@stdlib/array-to-fancy
|
|
580
|
+
|
|
581
|
+
[test-image]: https://github.com/stdlib-js/array-to-fancy/actions/workflows/test.yml/badge.svg?branch=v0.1.0
|
|
582
|
+
[test-url]: https://github.com/stdlib-js/array-to-fancy/actions/workflows/test.yml?query=branch:v0.1.0
|
|
583
|
+
|
|
584
|
+
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-to-fancy/main.svg
|
|
585
|
+
[coverage-url]: https://codecov.io/github/stdlib-js/array-to-fancy?branch=main
|
|
586
|
+
|
|
587
|
+
<!--
|
|
588
|
+
|
|
589
|
+
[dependencies-image]: https://img.shields.io/david/stdlib-js/array-to-fancy.svg
|
|
590
|
+
[dependencies-url]: https://david-dm.org/stdlib-js/array-to-fancy/main
|
|
591
|
+
|
|
592
|
+
-->
|
|
593
|
+
|
|
594
|
+
[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
|
|
595
|
+
[chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
|
|
596
|
+
|
|
597
|
+
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
598
|
+
|
|
599
|
+
[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
|
|
600
|
+
|
|
601
|
+
[umd]: https://github.com/umdjs/umd
|
|
602
|
+
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
|
|
603
|
+
|
|
604
|
+
[deno-url]: https://github.com/stdlib-js/array-to-fancy/tree/deno
|
|
605
|
+
[deno-readme]: https://github.com/stdlib-js/array-to-fancy/blob/deno/README.md
|
|
606
|
+
[umd-url]: https://github.com/stdlib-js/array-to-fancy/tree/umd
|
|
607
|
+
[umd-readme]: https://github.com/stdlib-js/array-to-fancy/blob/umd/README.md
|
|
608
|
+
[esm-url]: https://github.com/stdlib-js/array-to-fancy/tree/esm
|
|
609
|
+
[esm-readme]: https://github.com/stdlib-js/array-to-fancy/blob/esm/README.md
|
|
610
|
+
[branches-url]: https://github.com/stdlib-js/array-to-fancy/blob/main/branches.md
|
|
611
|
+
|
|
612
|
+
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/array-to-fancy/main/LICENSE
|
|
613
|
+
|
|
614
|
+
[@stdlib/repl]: https://www.npmjs.com/package/@stdlib/repl
|
|
615
|
+
|
|
616
|
+
[@stdlib/proxy/ctor]: https://www.npmjs.com/package/@stdlib/proxy-ctor
|
|
617
|
+
|
|
618
|
+
[@stdlib/slice/ctor]: https://www.npmjs.com/package/@stdlib/slice-ctor
|
|
619
|
+
|
|
620
|
+
[@stdlib/slice/seq2slice]: https://www.npmjs.com/package/@stdlib/slice-seq2slice
|
|
621
|
+
|
|
622
|
+
[@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/ndarray-ctor
|
|
623
|
+
|
|
624
|
+
[@stdlib/ndarray/base/broadcast-shapes]: https://www.npmjs.com/package/@stdlib/ndarray-base-broadcast-shapes
|
|
625
|
+
|
|
626
|
+
[@stdlib/array/mostly-safe-casts]: https://www.npmjs.com/package/@stdlib/array-mostly-safe-casts
|
|
627
|
+
|
|
628
|
+
[@stdlib/array/complex128]: https://www.npmjs.com/package/@stdlib/array-complex128
|
|
629
|
+
|
|
630
|
+
[@stdlib/array/complex64]: https://www.npmjs.com/package/@stdlib/array-complex64
|
|
631
|
+
|
|
632
|
+
[@stdlib/array/index]: https://www.npmjs.com/package/@stdlib/array-index
|
|
633
|
+
|
|
634
|
+
[@stdlib/array/dtypes]: https://www.npmjs.com/package/@stdlib/array-dtypes
|
|
635
|
+
|
|
636
|
+
<!-- <related-links> -->
|
|
637
|
+
|
|
638
|
+
[@stdlib/array/slice]: https://www.npmjs.com/package/@stdlib/array-slice
|
|
639
|
+
|
|
640
|
+
[@stdlib/ndarray/fancy]: https://www.npmjs.com/package/@stdlib/ndarray-fancy
|
|
641
|
+
|
|
642
|
+
<!-- </related-links> -->
|
|
643
|
+
|
|
644
|
+
</section>
|
|
645
|
+
|
|
646
|
+
<!-- /.links -->
|
package/SECURITY.md
ADDED
package/dist/index.d.ts
ADDED