@stdlib/array-base-accessor-setter 0.0.1
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 +236 -0
- package/docs/repl.txt +60 -0
- package/docs/types/index.d.ts +140 -0
- package/docs/types/test.ts +177 -0
- package/lib/index.js +56 -0
- package/lib/main.js +164 -0
- package/package.json +92 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
package/NOTICE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright (c) 2016-2023 The Stdlib Authors.
|
package/README.md
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2022 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
|
+
# accessorSetter
|
|
22
|
+
|
|
23
|
+
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
|
|
24
|
+
|
|
25
|
+
> Return an accessor function for setting an element in an array-like object supporting the get/set protocol.
|
|
26
|
+
|
|
27
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
28
|
+
|
|
29
|
+
<section class="intro">
|
|
30
|
+
|
|
31
|
+
</section>
|
|
32
|
+
|
|
33
|
+
<!-- /.intro -->
|
|
34
|
+
|
|
35
|
+
<!-- Package usage documentation. -->
|
|
36
|
+
|
|
37
|
+
<section class="installation">
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm install @stdlib/array-base-accessor-setter
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
</section>
|
|
46
|
+
|
|
47
|
+
<section class="usage">
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
var accessorSetter = require( '@stdlib/array-base-accessor-setter' );
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### accessorSetter( dtype )
|
|
56
|
+
|
|
57
|
+
Returns an accessor function for setting an element in an array-like object supporting the get/set protocol.
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
var Complex64Array = require( '@stdlib/array-complex64' );
|
|
61
|
+
var Complex64 = require( '@stdlib/complex-float32' );
|
|
62
|
+
var realf = require( '@stdlib/complex-realf' );
|
|
63
|
+
var imagf = require( '@stdlib/complex-imagf' );
|
|
64
|
+
|
|
65
|
+
var arr = new Complex64Array( [ 1, 2, 3, 4 ] );
|
|
66
|
+
|
|
67
|
+
var set = accessorSetter( 'complex64' );
|
|
68
|
+
set( arr, 1, new Complex64( 10.0, 11.0 ) );
|
|
69
|
+
|
|
70
|
+
var v = arr.get( 1 );
|
|
71
|
+
// returns <Complex64>
|
|
72
|
+
|
|
73
|
+
var re = realf( v );
|
|
74
|
+
// returns 10.0
|
|
75
|
+
|
|
76
|
+
var im = imagf( v );
|
|
77
|
+
// returns 11.0
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The returned accessor function accepts the following arguments:
|
|
81
|
+
|
|
82
|
+
- **arr**: input array.
|
|
83
|
+
- **idx**: element index.
|
|
84
|
+
- **value**: value to set.
|
|
85
|
+
|
|
86
|
+
</section>
|
|
87
|
+
|
|
88
|
+
<!-- /.usage -->
|
|
89
|
+
|
|
90
|
+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
91
|
+
|
|
92
|
+
<section class="notes">
|
|
93
|
+
|
|
94
|
+
## Notes
|
|
95
|
+
|
|
96
|
+
- If provided an unsupported [`dtype`][@stdlib/array/dtypes], the function returns a default accessor function for accessing elements from any indexed array-like object supporting the get/set protocol; otherwise, the function returns an accessor function which should **only** be provided an array instance corresponding to `dtype` (e.g., if `dtype` is `'complex64'`, the returned accessor function should only be provided instances of `Complex64Array`).
|
|
97
|
+
- Accessor functions do **not** verify that provided input arrays are array instances corresponding to `dtype`, as doing so would introduce performance overhead. If array instances corresponding to other data types are provided to an accessor function, JavaScript runtimes will consider the function polymorphic, potentially triggering de-optimization. In order to ensure maximum performance, **always** ensure that an accessor function is monomorphic.
|
|
98
|
+
- Accessor functions do **not** perform bounds checking.
|
|
99
|
+
- Accessor functions do **not** validate input values.
|
|
100
|
+
- Accessor functions do **not** verify that provided input arrays actually implement the get/set protocol.
|
|
101
|
+
- An array-like object supporting the get/set protocol is a data structure in which one accesses elements using explicit `get` and `set` methods (e.g., `Complex64Array` and `Complex128Array`).
|
|
102
|
+
|
|
103
|
+
</section>
|
|
104
|
+
|
|
105
|
+
<!-- /.notes -->
|
|
106
|
+
|
|
107
|
+
<!-- Package usage examples. -->
|
|
108
|
+
|
|
109
|
+
<section class="examples">
|
|
110
|
+
|
|
111
|
+
## Examples
|
|
112
|
+
|
|
113
|
+
<!-- eslint no-undef: "error" -->
|
|
114
|
+
|
|
115
|
+
```javascript
|
|
116
|
+
var Complex128Array = require( '@stdlib/array-complex128' );
|
|
117
|
+
var Complex64Array = require( '@stdlib/array-complex64' );
|
|
118
|
+
var Complex128 = require( '@stdlib/complex-float64' );
|
|
119
|
+
var Complex64 = require( '@stdlib/complex-float32' );
|
|
120
|
+
var zeroTo = require( '@stdlib/array-base-zero-to' );
|
|
121
|
+
var dtype = require( '@stdlib/array-dtype' );
|
|
122
|
+
var accessorSetter = require( '@stdlib/array-base-accessor-setter' );
|
|
123
|
+
|
|
124
|
+
var arr = new Complex128Array( zeroTo( 10 ) );
|
|
125
|
+
accessorSetter( dtype( arr ) )( arr, 2, new Complex128( 100.0, 101.0 ) );
|
|
126
|
+
|
|
127
|
+
var v = arr.get( 2 );
|
|
128
|
+
// returns <Complex128>
|
|
129
|
+
|
|
130
|
+
console.log( '%s', v.toString() );
|
|
131
|
+
// => '100 + 101i'
|
|
132
|
+
|
|
133
|
+
arr = new Complex64Array( zeroTo( 10 ) );
|
|
134
|
+
accessorSetter( dtype( arr ) )( arr, 4, new Complex64( 102.0, 103.0 ) );
|
|
135
|
+
|
|
136
|
+
v = arr.get( 4 );
|
|
137
|
+
// returns <Complex64>
|
|
138
|
+
|
|
139
|
+
console.log( '%s', v.toString() );
|
|
140
|
+
// => '102 + 103i'
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
</section>
|
|
144
|
+
|
|
145
|
+
<!-- /.examples -->
|
|
146
|
+
|
|
147
|
+
<!-- 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. -->
|
|
148
|
+
|
|
149
|
+
<section class="references">
|
|
150
|
+
|
|
151
|
+
</section>
|
|
152
|
+
|
|
153
|
+
<!-- /.references -->
|
|
154
|
+
|
|
155
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
156
|
+
|
|
157
|
+
<section class="related">
|
|
158
|
+
|
|
159
|
+
</section>
|
|
160
|
+
|
|
161
|
+
<!-- /.related -->
|
|
162
|
+
|
|
163
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
<section class="main-repo" >
|
|
167
|
+
|
|
168
|
+
* * *
|
|
169
|
+
|
|
170
|
+
## Notice
|
|
171
|
+
|
|
172
|
+
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.
|
|
173
|
+
|
|
174
|
+
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].
|
|
175
|
+
|
|
176
|
+
#### Community
|
|
177
|
+
|
|
178
|
+
[![Chat][chat-image]][chat-url]
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
|
|
184
|
+
See [LICENSE][stdlib-license].
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
## Copyright
|
|
188
|
+
|
|
189
|
+
Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors].
|
|
190
|
+
|
|
191
|
+
</section>
|
|
192
|
+
|
|
193
|
+
<!-- /.stdlib -->
|
|
194
|
+
|
|
195
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
196
|
+
|
|
197
|
+
<section class="links">
|
|
198
|
+
|
|
199
|
+
[npm-image]: http://img.shields.io/npm/v/@stdlib/array-base-accessor-setter.svg
|
|
200
|
+
[npm-url]: https://npmjs.org/package/@stdlib/array-base-accessor-setter
|
|
201
|
+
|
|
202
|
+
[test-image]: https://github.com/stdlib-js/array-base-accessor-setter/actions/workflows/test.yml/badge.svg?branch=v0.0.1
|
|
203
|
+
[test-url]: https://github.com/stdlib-js/array-base-accessor-setter/actions/workflows/test.yml?query=branch:v0.0.1
|
|
204
|
+
|
|
205
|
+
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-base-accessor-setter/main.svg
|
|
206
|
+
[coverage-url]: https://codecov.io/github/stdlib-js/array-base-accessor-setter?branch=main
|
|
207
|
+
|
|
208
|
+
<!--
|
|
209
|
+
|
|
210
|
+
[dependencies-image]: https://img.shields.io/david/stdlib-js/array-base-accessor-setter.svg
|
|
211
|
+
[dependencies-url]: https://david-dm.org/stdlib-js/array-base-accessor-setter/main
|
|
212
|
+
|
|
213
|
+
-->
|
|
214
|
+
|
|
215
|
+
[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
|
|
216
|
+
[chat-url]: https://gitter.im/stdlib-js/stdlib/
|
|
217
|
+
|
|
218
|
+
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
219
|
+
|
|
220
|
+
[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
|
|
221
|
+
|
|
222
|
+
[umd]: https://github.com/umdjs/umd
|
|
223
|
+
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
|
|
224
|
+
|
|
225
|
+
[deno-url]: https://github.com/stdlib-js/array-base-accessor-setter/tree/deno
|
|
226
|
+
[umd-url]: https://github.com/stdlib-js/array-base-accessor-setter/tree/umd
|
|
227
|
+
[esm-url]: https://github.com/stdlib-js/array-base-accessor-setter/tree/esm
|
|
228
|
+
[branches-url]: https://github.com/stdlib-js/array-base-accessor-setter/blob/main/branches.md
|
|
229
|
+
|
|
230
|
+
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/array-base-accessor-setter/main/LICENSE
|
|
231
|
+
|
|
232
|
+
[@stdlib/array/dtypes]: https://www.npmjs.com/package/@stdlib/stdlib
|
|
233
|
+
|
|
234
|
+
</section>
|
|
235
|
+
|
|
236
|
+
<!-- /.links -->
|
package/docs/repl.txt
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}( dtype )
|
|
3
|
+
Returns an accessor function for setting an element in an array-like object
|
|
4
|
+
supporting the get/set protocol.
|
|
5
|
+
|
|
6
|
+
An accessor function accepts the following arguments:
|
|
7
|
+
|
|
8
|
+
- arr: input array
|
|
9
|
+
- idx: element index
|
|
10
|
+
- value: value to set
|
|
11
|
+
|
|
12
|
+
If provided an unsupported `dtype`, the function returns a default accessor
|
|
13
|
+
function for accessing elements in any indexed array-like object supporting
|
|
14
|
+
the get/set protocol.
|
|
15
|
+
|
|
16
|
+
Otherwise, the function returns an accessor function which should *only* be
|
|
17
|
+
provided an array instance corresponding to `dtype` (e.g., if `dtype` is
|
|
18
|
+
'complex64', the returned accessor function should only be provided
|
|
19
|
+
instances of Complex64Array).
|
|
20
|
+
|
|
21
|
+
Accessor functions do *not* verify that provided input arrays are array
|
|
22
|
+
instances corresponding to `dtype`, as doing so would introduce performance
|
|
23
|
+
overhead. If array instances corresponding to other data types are provided
|
|
24
|
+
to an accessor function, JavaScript runtimes will consider the function
|
|
25
|
+
polymorphic, potentially triggering de-optimization. In order to ensure
|
|
26
|
+
maximum performance, *always* ensure that an accessor function is
|
|
27
|
+
monomorphic.
|
|
28
|
+
|
|
29
|
+
Accessor functions do *not* perform bounds checking.
|
|
30
|
+
|
|
31
|
+
Accessor functions do *not* validate input values.
|
|
32
|
+
|
|
33
|
+
Accessor functions do *not* verify that provided input arrays actually
|
|
34
|
+
implement the get/set protocol.
|
|
35
|
+
|
|
36
|
+
Parameters
|
|
37
|
+
----------
|
|
38
|
+
dtype: string
|
|
39
|
+
Array data type.
|
|
40
|
+
|
|
41
|
+
Returns
|
|
42
|
+
-------
|
|
43
|
+
f: Function
|
|
44
|
+
Accessor function.
|
|
45
|
+
|
|
46
|
+
Examples
|
|
47
|
+
--------
|
|
48
|
+
> var f = {{alias}}( 'complex64' );
|
|
49
|
+
> var x = {{alias:@stdlib/array/complex64}}( [ 1, 2, 3, 4 ] );
|
|
50
|
+
> f( x, 1, new {{alias:@stdlib/complex/float32}}( 10.0, 11.0 ) );
|
|
51
|
+
> var v = x.get( 1 )
|
|
52
|
+
<Complex64>
|
|
53
|
+
> var r = {{alias:@stdlib/complex/realf}}( v )
|
|
54
|
+
10.0
|
|
55
|
+
> var i = {{alias:@stdlib/complex/imagf}}( v )
|
|
56
|
+
11.0
|
|
57
|
+
|
|
58
|
+
See Also
|
|
59
|
+
--------
|
|
60
|
+
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022 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
|
+
// TypeScript Version: 2.0
|
|
20
|
+
|
|
21
|
+
/// <reference types="@stdlib/types"/>
|
|
22
|
+
|
|
23
|
+
import { ComplexLike } from '@stdlib/types/object';
|
|
24
|
+
import { Complex64Array, Complex128Array, AccessorArrayLike } from '@stdlib/types/array';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Sets an element in a `Complex128Array`.
|
|
28
|
+
*
|
|
29
|
+
* @param arr - input array
|
|
30
|
+
* @param idx - element index
|
|
31
|
+
* @param value - value to set
|
|
32
|
+
*/
|
|
33
|
+
type SetComplex128 = ( arr: Complex128Array, idx: number, value: ComplexLike ) => void; // tslint:disable-line:max-line-length
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Sets an element in a `Complex64Array`.
|
|
37
|
+
*
|
|
38
|
+
* @param arr - input array
|
|
39
|
+
* @param idx - element index
|
|
40
|
+
* @param value - value to set
|
|
41
|
+
*/
|
|
42
|
+
type SetComplex64 = ( arr: Complex64Array, idx: number, value: ComplexLike ) => void; // tslint:disable-line:max-line-length
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Sets an element in an array-like object supporting the get/set protocol.
|
|
46
|
+
*
|
|
47
|
+
* @param arr - input array
|
|
48
|
+
* @param idx - element index
|
|
49
|
+
* @param value - value to set
|
|
50
|
+
*/
|
|
51
|
+
type SetArrayLike = ( arr: AccessorArrayLike<any>, idx: number, value: any ) => void; // tslint:disable-line:max-line-length
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns an accessor function for setting an element in a `Complex128Array`.
|
|
55
|
+
*
|
|
56
|
+
* @param dtype - data type
|
|
57
|
+
* @returns accessor function
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* var Complex128Array = require( `@stdlib/array/complex128` );
|
|
61
|
+
* var Complex128 = require( `@stdlib/complex/float64` );
|
|
62
|
+
* var real = require( `@stdlib/array/real` );
|
|
63
|
+
* var imag = require( `@stdlib/array/imag` );
|
|
64
|
+
*
|
|
65
|
+
* var arr = new Complex128Array( [ 1, 2, 3, 4 ] );
|
|
66
|
+
*
|
|
67
|
+
* var set = setter( 'complex128' );
|
|
68
|
+
* set( arr, 1, new Complex128( 10.0, 11.0 ) );
|
|
69
|
+
*
|
|
70
|
+
* var v = arr.get( 1 );
|
|
71
|
+
* // returns <Complex128>
|
|
72
|
+
*
|
|
73
|
+
* var re = real( v );
|
|
74
|
+
* // returns 10.0
|
|
75
|
+
*
|
|
76
|
+
* var im = imag( v );
|
|
77
|
+
* // returns 11.0
|
|
78
|
+
*/
|
|
79
|
+
declare function setter( dtype: 'complex128' ): SetComplex128;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns an accessor function for setting an element in a `Complex64Array`.
|
|
83
|
+
*
|
|
84
|
+
* @param dtype - data type
|
|
85
|
+
* @returns accessor function
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* var Complex64Array = require( `@stdlib/array/complex64` );
|
|
89
|
+
* var Complex64 = require( `@stdlib/complex/float32` );
|
|
90
|
+
* var realf = require( `@stdlib/array/realf` );
|
|
91
|
+
* var imagf = require( `@stdlib/array/imagf` );
|
|
92
|
+
*
|
|
93
|
+
* var arr = new Complex64Array( [ 1, 2, 3, 4 ] );
|
|
94
|
+
*
|
|
95
|
+
* var set = setter( 'complex64' );
|
|
96
|
+
* set( arr, 1, new Complex64( 10.0, 11.0 ) );
|
|
97
|
+
*
|
|
98
|
+
* var v = arr.get( 1 );
|
|
99
|
+
* // returns <Complex64>
|
|
100
|
+
*
|
|
101
|
+
* var re = realf( v );
|
|
102
|
+
* // returns 3.0
|
|
103
|
+
*
|
|
104
|
+
* var im = imagf( v );
|
|
105
|
+
* // returns 4.0
|
|
106
|
+
*/
|
|
107
|
+
declare function setter( dtype: 'complex64' ): SetComplex64;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Returns an accessor function for setting an element in an array-like object supporting the get/set protocol.
|
|
111
|
+
*
|
|
112
|
+
* @param dtype - data type
|
|
113
|
+
* @returns accessor function
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* var arr = [ 1, 2, 3, 4 ];
|
|
117
|
+
*
|
|
118
|
+
* function aget( idx ) {
|
|
119
|
+
* return arr[ idx ];
|
|
120
|
+
* }
|
|
121
|
+
*
|
|
122
|
+
* function aset( value, idx ) {
|
|
123
|
+
* arr[ idx ] = value;
|
|
124
|
+
* }
|
|
125
|
+
*
|
|
126
|
+
* arr.get = aget;
|
|
127
|
+
* arr.set = aset;
|
|
128
|
+
*
|
|
129
|
+
* var set = setter( 'foo' );
|
|
130
|
+
* set( arr, 2, 10 );
|
|
131
|
+
*
|
|
132
|
+
* var v = arr.get( 2 );
|
|
133
|
+
* // returns 3
|
|
134
|
+
*/
|
|
135
|
+
declare function setter( dtype: string ): SetArrayLike;
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
// EXPORTS //
|
|
139
|
+
|
|
140
|
+
export = setter;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022 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
|
+
/// <reference types="@stdlib/types"/>
|
|
20
|
+
|
|
21
|
+
import { AccessorArrayLike } from '@stdlib/types/array';
|
|
22
|
+
import Complex128Array = require( '@stdlib/array-complex128' );
|
|
23
|
+
import Complex64Array = require( '@stdlib/array-complex64' );
|
|
24
|
+
import Complex128 = require( '@stdlib/complex-float64' );
|
|
25
|
+
import Complex64 = require( '@stdlib/complex-float32' );
|
|
26
|
+
import setter = require( './index' );
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns an array-like object supporting the get/set protocol.
|
|
30
|
+
*
|
|
31
|
+
* @return array-like object
|
|
32
|
+
*/
|
|
33
|
+
function accessorArray(): AccessorArrayLike<number> {
|
|
34
|
+
let arr: AccessorArrayLike<number>;
|
|
35
|
+
arr = {
|
|
36
|
+
'0': 1,
|
|
37
|
+
'1': 2,
|
|
38
|
+
'2': 3,
|
|
39
|
+
'3': 4,
|
|
40
|
+
'length': 4,
|
|
41
|
+
'get': ( idx: number ): number => {
|
|
42
|
+
return arr[ idx ]; // tslint:disable-line:no-unsafe-any
|
|
43
|
+
},
|
|
44
|
+
'set': ( value: number, idx: number ): void => {
|
|
45
|
+
arr[ idx ] = value;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
return arr;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// TESTS //
|
|
53
|
+
|
|
54
|
+
// The function returns a function...
|
|
55
|
+
{
|
|
56
|
+
setter( 'complex128' ); // $ExpectType SetComplex128
|
|
57
|
+
setter( 'complex64' ); // $ExpectType SetComplex64
|
|
58
|
+
setter( 'foo' ); // $ExpectType SetArrayLike
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// The compiler throws an error if the function is provided a first argument which is not a string...
|
|
62
|
+
{
|
|
63
|
+
setter( 5 ); // $ExpectError
|
|
64
|
+
setter( true ); // $ExpectError
|
|
65
|
+
setter( false ); // $ExpectError
|
|
66
|
+
setter( null ); // $ExpectError
|
|
67
|
+
setter( {} ); // $ExpectError
|
|
68
|
+
setter( [] ); // $ExpectError
|
|
69
|
+
setter( ( x: number ): number => x ); // $ExpectError
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The compiler throws an error if the function is provided an unsupported number of arguments...
|
|
73
|
+
{
|
|
74
|
+
setter(); // $ExpectError
|
|
75
|
+
setter( 'complex128', {} ); // $ExpectError
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The function returns a function which sets an array element...
|
|
79
|
+
{
|
|
80
|
+
const set1 = setter( 'foo' );
|
|
81
|
+
const x1 = accessorArray();
|
|
82
|
+
set1( x1, 2, 10 ); // $ExpectType void
|
|
83
|
+
|
|
84
|
+
const set2 = setter( 'complex128' );
|
|
85
|
+
const x2 = new Complex128Array( [ 1, 2, 3, 4 ] );
|
|
86
|
+
set2( x2, 1, new Complex128( 5, 6 ) ); // $ExpectType void
|
|
87
|
+
|
|
88
|
+
const set3 = setter( 'complex64' );
|
|
89
|
+
const x3 = new Complex64Array( [ 1, 2, 3, 4 ] );
|
|
90
|
+
set3( x3, 1, new Complex64( 7, 8 ) ); // $ExpectType void
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// The compiler throws an error if the returned function is provided a first argument which is not a accessor array...
|
|
94
|
+
{
|
|
95
|
+
const set1 = setter( 'foo' );
|
|
96
|
+
set1( 5, 1, 3 ); // $ExpectError
|
|
97
|
+
set1( true, 1, 3 ); // $ExpectError
|
|
98
|
+
set1( false, 1, 3 ); // $ExpectError
|
|
99
|
+
set1( null, 1, 3 ); // $ExpectError
|
|
100
|
+
set1( {}, 1, 3 ); // $ExpectError
|
|
101
|
+
|
|
102
|
+
const set2 = setter( 'complex128' );
|
|
103
|
+
set2( 5, 1, new Complex128( 5, 6 ) ); // $ExpectError
|
|
104
|
+
set2( true, 1, new Complex128( 5, 6 ) ); // $ExpectError
|
|
105
|
+
set2( false, 1, new Complex128( 5, 6 ) ); // $ExpectError
|
|
106
|
+
set2( null, 1, new Complex128( 5, 6 ) ); // $ExpectError
|
|
107
|
+
set2( {}, 1, new Complex128( 5, 6 ) ); // $ExpectError
|
|
108
|
+
|
|
109
|
+
const set3 = setter( 'complex64' );
|
|
110
|
+
set3( 5, 1, new Complex64( 7, 8 ) ); // $ExpectError
|
|
111
|
+
set3( true, 1, new Complex64( 7, 8 ) ); // $ExpectError
|
|
112
|
+
set3( false, 1, new Complex64( 7, 8 ) ); // $ExpectError
|
|
113
|
+
set3( null, 1, new Complex64( 7, 8 ) ); // $ExpectError
|
|
114
|
+
set3( {}, 1, new Complex64( 7, 8 ) ); // $ExpectError
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// The compiler throws an error if the returned function is provided a second argument which is not a number...
|
|
118
|
+
{
|
|
119
|
+
const set1 = setter( 'foo' );
|
|
120
|
+
set1( accessorArray(), '5', 3 ); // $ExpectError
|
|
121
|
+
set1( accessorArray(), true, 3 ); // $ExpectError
|
|
122
|
+
set1( accessorArray(), false, 3 ); // $ExpectError
|
|
123
|
+
set1( accessorArray(), null, 3 ); // $ExpectError
|
|
124
|
+
set1( accessorArray(), {}, 3 ); // $ExpectError
|
|
125
|
+
|
|
126
|
+
const set2 = setter( 'complex128' );
|
|
127
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), '5', new Complex128( 5, 6 ) ); // $ExpectError
|
|
128
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), true, new Complex128( 5, 6 ) ); // $ExpectError
|
|
129
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), false, new Complex128( 5, 6 ) ); // $ExpectError
|
|
130
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), null, new Complex128( 5, 6 ) ); // $ExpectError
|
|
131
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), {}, new Complex128( 5, 6 ) ); // $ExpectError
|
|
132
|
+
|
|
133
|
+
const set3 = setter( 'complex64' );
|
|
134
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), '5', new Complex64( 7, 8 ) ); // $ExpectError
|
|
135
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), true, new Complex64( 7, 8 ) ); // $ExpectError
|
|
136
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), false, new Complex64( 7, 8 ) ); // $ExpectError
|
|
137
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), null, new Complex64( 7, 8 ) ); // $ExpectError
|
|
138
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), {}, new Complex64( 7, 8 ) ); // $ExpectError
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// The compiler throws an error if the returned function is provided a third argument which is not a valid value...
|
|
142
|
+
{
|
|
143
|
+
const set2 = setter( 'complex128' );
|
|
144
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), 1, '5' ); // $ExpectError
|
|
145
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), 1, true ); // $ExpectError
|
|
146
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), 1, false ); // $ExpectError
|
|
147
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), 1, null ); // $ExpectError
|
|
148
|
+
set2( new Complex128Array( [ 1, 2, 3, 4 ] ), 1, {} ); // $ExpectError
|
|
149
|
+
|
|
150
|
+
const set3 = setter( 'complex64' );
|
|
151
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), 1, '5' ); // $ExpectError
|
|
152
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), 1, true ); // $ExpectError
|
|
153
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), 1, false ); // $ExpectError
|
|
154
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), 1, null ); // $ExpectError
|
|
155
|
+
set3( new Complex64Array( [ 1, 2, 3, 4 ] ), 1, {} ); // $ExpectError
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// The compiler throws an error if the returned function is provided an unsupported number of arguments...
|
|
159
|
+
{
|
|
160
|
+
const set1 = setter( 'foo' );
|
|
161
|
+
set1(); // $ExpectError
|
|
162
|
+
set1( [] ); // $ExpectError
|
|
163
|
+
set1( [], 1 ); // $ExpectError
|
|
164
|
+
set1( [], 1, 2, 2 ); // $ExpectError
|
|
165
|
+
|
|
166
|
+
const set2 = setter( 'complex128' );
|
|
167
|
+
set2(); // $ExpectError
|
|
168
|
+
set2( new Complex128Array( [] ) ); // $ExpectError
|
|
169
|
+
set2( new Complex128Array( [] ), 1 ); // $ExpectError
|
|
170
|
+
set2( new Complex128Array( [] ), 1, new Complex128( 5, 6 ), 2 ); // $ExpectError
|
|
171
|
+
|
|
172
|
+
const set3 = setter( 'complex64' );
|
|
173
|
+
set3(); // $ExpectError
|
|
174
|
+
set3( new Complex64Array( [] ) ); // $ExpectError
|
|
175
|
+
set3( new Complex64Array( [] ), 1 ); // $ExpectError
|
|
176
|
+
set3( new Complex64Array( [] ), 1, new Complex64( 7, 8 ), 2 ); // $ExpectError
|
|
177
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022 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
|
+
/**
|
|
22
|
+
* Return an accessor function for setting an element in an array-like object supporting the get/set protocol.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/array-base-accessor-setter
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var Complex64Array = require( '@stdlib/array-complex64' );
|
|
28
|
+
* var Complex64 = require( '@stdlib/complex-float32' );
|
|
29
|
+
* var realf = require( '@stdlib/complex-realf' );
|
|
30
|
+
* var imagf = require( '@stdlib/complex-imagf' );
|
|
31
|
+
* var dtype = require( '@stdlib/array-dtype' );
|
|
32
|
+
* var setter = require( '@stdlib/array-base-accessor-setter' );
|
|
33
|
+
*
|
|
34
|
+
* var arr = new Complex64Array( [ 1, 2, 3, 4 ] );
|
|
35
|
+
*
|
|
36
|
+
* var set = setter( dtype( arr ) );
|
|
37
|
+
* set( arr, 1, new Complex64( 10.0, 11.0 ) );
|
|
38
|
+
*
|
|
39
|
+
* var v = arr.get( 1 );
|
|
40
|
+
* // returns <Complex64>
|
|
41
|
+
*
|
|
42
|
+
* var re = realf( v );
|
|
43
|
+
* // returns 10.0
|
|
44
|
+
*
|
|
45
|
+
* var im = imagf( v );
|
|
46
|
+
* // returns 11.0
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
// MODULES //
|
|
50
|
+
|
|
51
|
+
var main = require( './main.js' );
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
// EXPORTS //
|
|
55
|
+
|
|
56
|
+
module.exports = main;
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022 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
|
+
// VARIABLES //
|
|
22
|
+
|
|
23
|
+
var SETTERS = {
|
|
24
|
+
'complex128': setComplex128,
|
|
25
|
+
'complex64': setComplex64,
|
|
26
|
+
'default': setArrayLike
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
// FUNCTIONS //
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Sets an element in a `Complex128Array`.
|
|
34
|
+
*
|
|
35
|
+
* @private
|
|
36
|
+
* @param {Complex128Array} arr - input array
|
|
37
|
+
* @param {NonNegativeInteger} idx - element index
|
|
38
|
+
* @param {(Collection|Complex|ComplexArray)} value - value(s)
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* var Complex128Array = require( '@stdlib/array-complex128' );
|
|
42
|
+
* var Complex128 = require( '@stdlib/complex-float64' );
|
|
43
|
+
* var real = require( '@stdlib/complex-real' );
|
|
44
|
+
* var imag = require( '@stdlib/complex-imag' );
|
|
45
|
+
*
|
|
46
|
+
* var arr = new Complex128Array( [ 1, 2, 3, 4 ] );
|
|
47
|
+
*
|
|
48
|
+
* setComplex128( arr, 1, new Complex128( 10.0, 11.0 ) );
|
|
49
|
+
* var v = arr.get( 1 );
|
|
50
|
+
* // returns <Complex128>
|
|
51
|
+
*
|
|
52
|
+
* var re = real( v );
|
|
53
|
+
* // returns 10.0
|
|
54
|
+
*
|
|
55
|
+
* var im = imag( v );
|
|
56
|
+
* // returns 11.0
|
|
57
|
+
*/
|
|
58
|
+
function setComplex128( arr, idx, value ) {
|
|
59
|
+
arr.set( value, idx );
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Sets an element in a `Complex64Array`.
|
|
64
|
+
*
|
|
65
|
+
* @private
|
|
66
|
+
* @param {Complex64Array} arr - input array
|
|
67
|
+
* @param {NonNegativeInteger} idx - element index
|
|
68
|
+
* @param {(Collection|Complex|ComplexArray)} value - value(s)
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* var Complex64Array = require( '@stdlib/array-complex64' );
|
|
72
|
+
* var Complex64 = require( '@stdlib/complex-float32' );
|
|
73
|
+
* var realf = require( '@stdlib/complex-realf' );
|
|
74
|
+
* var imagf = require( '@stdlib/complex-imagf' );
|
|
75
|
+
*
|
|
76
|
+
* var arr = new Complex64Array( [ 1, 2, 3, 4 ] );
|
|
77
|
+
*
|
|
78
|
+
* setComplex64( arr, 1, new Complex64( 10.0, 11.0 ) );
|
|
79
|
+
* var v = arr.get( 1 );
|
|
80
|
+
* // returns <Complex64>
|
|
81
|
+
*
|
|
82
|
+
* var re = realf( v );
|
|
83
|
+
* // returns 10.0
|
|
84
|
+
*
|
|
85
|
+
* var im = imagf( v );
|
|
86
|
+
* // returns 11.0
|
|
87
|
+
*/
|
|
88
|
+
function setComplex64( arr, idx, value ) {
|
|
89
|
+
arr.set( value, idx );
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Sets an element in an array-like object supporting the get/set protocol.
|
|
94
|
+
*
|
|
95
|
+
* @private
|
|
96
|
+
* @param {Collection} arr - input array
|
|
97
|
+
* @param {NonNegativeInteger} idx - element index
|
|
98
|
+
* @param {(Collection|Complex|ComplexArray)} value - value(s)
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* var arr = [ 1, 2, 3, 4 ];
|
|
102
|
+
*
|
|
103
|
+
* function get( idx ) {
|
|
104
|
+
* return arr[ idx ];
|
|
105
|
+
* }
|
|
106
|
+
*
|
|
107
|
+
* function set( value, idx ) {
|
|
108
|
+
* arr[ idx ] = value;
|
|
109
|
+
* }
|
|
110
|
+
*
|
|
111
|
+
* arr.get = get;
|
|
112
|
+
* arr.set = set;
|
|
113
|
+
*
|
|
114
|
+
* setArrayLike( arr, 2, 10 );
|
|
115
|
+
*
|
|
116
|
+
* var v = arr[ 2 ];
|
|
117
|
+
* // returns 10
|
|
118
|
+
*/
|
|
119
|
+
function setArrayLike( arr, idx, value ) {
|
|
120
|
+
arr.set( value, idx );
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
// MAIN //
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Returns an accessor function for setting an element in an array-like object supporting the get/set protocol.
|
|
128
|
+
*
|
|
129
|
+
* @param {string} dtype - array dtype
|
|
130
|
+
* @returns {Function} accessor
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* var Complex64Array = require( '@stdlib/array-complex64' );
|
|
134
|
+
* var Complex64 = require( '@stdlib/complex-float32' );
|
|
135
|
+
* var realf = require( '@stdlib/complex-realf' );
|
|
136
|
+
* var imagf = require( '@stdlib/complex-imagf' );
|
|
137
|
+
* var dtype = require( '@stdlib/array-dtype' );
|
|
138
|
+
*
|
|
139
|
+
* var arr = new Complex64Array( [ 1, 2, 3, 4 ] );
|
|
140
|
+
*
|
|
141
|
+
* var set = setter( dtype( arr ) );
|
|
142
|
+
* set( arr, 1, new Complex64( 10.0, 11.0 ) );
|
|
143
|
+
*
|
|
144
|
+
* var v = arr.get( 1 );
|
|
145
|
+
* // returns <Complex64>
|
|
146
|
+
*
|
|
147
|
+
* var re = realf( v );
|
|
148
|
+
* // returns 10.0
|
|
149
|
+
*
|
|
150
|
+
* var im = imagf( v );
|
|
151
|
+
* // returns 11.0
|
|
152
|
+
*/
|
|
153
|
+
function setter( dtype ) {
|
|
154
|
+
var f = SETTERS[ dtype ];
|
|
155
|
+
if ( typeof f === 'function' ) {
|
|
156
|
+
return f;
|
|
157
|
+
}
|
|
158
|
+
return SETTERS.default;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
// EXPORTS //
|
|
163
|
+
|
|
164
|
+
module.exports = setter;
|
package/package.json
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/array-base-accessor-setter",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Return an accessor function for setting an element in an array-like object supporting the get/set protocol.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"benchmark": "./benchmark",
|
|
19
|
+
"doc": "./docs",
|
|
20
|
+
"example": "./examples",
|
|
21
|
+
"lib": "./lib",
|
|
22
|
+
"test": "./test"
|
|
23
|
+
},
|
|
24
|
+
"types": "./docs/types",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "make test",
|
|
27
|
+
"test-cov": "make test-cov",
|
|
28
|
+
"examples": "make examples",
|
|
29
|
+
"benchmark": "make benchmark"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://stdlib.io",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git://github.com/stdlib-js/array-base-accessor-setter.git"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@stdlib/types": "^0.0.x"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@stdlib/array-base-zero-to": "^0.0.x",
|
|
44
|
+
"@stdlib/array-complex128": "^0.0.x",
|
|
45
|
+
"@stdlib/array-complex64": "^0.0.x",
|
|
46
|
+
"@stdlib/array-dtype": "^0.0.x",
|
|
47
|
+
"@stdlib/array-filled-by": "^0.0.x",
|
|
48
|
+
"@stdlib/assert-is-function": "^0.0.x",
|
|
49
|
+
"@stdlib/bench": "^0.0.x",
|
|
50
|
+
"@stdlib/complex-float32": "^0.0.x",
|
|
51
|
+
"@stdlib/complex-float64": "^0.0.x",
|
|
52
|
+
"@stdlib/complex-imag": "^0.0.x",
|
|
53
|
+
"@stdlib/complex-imagf": "^0.0.x",
|
|
54
|
+
"@stdlib/complex-real": "^0.0.x",
|
|
55
|
+
"@stdlib/complex-realf": "^0.0.x",
|
|
56
|
+
"@stdlib/math-base-assert-is-nan": "^0.0.x",
|
|
57
|
+
"@stdlib/math-base-assert-is-nanf": "^0.0.x",
|
|
58
|
+
"@stdlib/random-base-discrete-uniform": "^0.0.x",
|
|
59
|
+
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
|
|
60
|
+
"istanbul": "^0.4.1",
|
|
61
|
+
"tap-min": "git+https://github.com/Planeshifter/tap-min.git"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=0.10.0",
|
|
65
|
+
"npm": ">2.7.0"
|
|
66
|
+
},
|
|
67
|
+
"os": [
|
|
68
|
+
"aix",
|
|
69
|
+
"darwin",
|
|
70
|
+
"freebsd",
|
|
71
|
+
"linux",
|
|
72
|
+
"macos",
|
|
73
|
+
"openbsd",
|
|
74
|
+
"sunos",
|
|
75
|
+
"win32",
|
|
76
|
+
"windows"
|
|
77
|
+
],
|
|
78
|
+
"keywords": [
|
|
79
|
+
"stdlib",
|
|
80
|
+
"array",
|
|
81
|
+
"generic",
|
|
82
|
+
"set",
|
|
83
|
+
"accessor",
|
|
84
|
+
"access",
|
|
85
|
+
"retrieve",
|
|
86
|
+
"setter"
|
|
87
|
+
],
|
|
88
|
+
"funding": {
|
|
89
|
+
"type": "patreon",
|
|
90
|
+
"url": "https://www.patreon.com/athan"
|
|
91
|
+
}
|
|
92
|
+
}
|