@stdlib/array-mskput 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 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-2024 The Stdlib Authors.
package/README.md ADDED
@@ -0,0 +1,256 @@
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
+ # mskput
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
+ > Replace elements of an array with provided values according to a provided mask array.
37
+
38
+ <section class="installation">
39
+
40
+ ## Installation
41
+
42
+ ```bash
43
+ npm install @stdlib/array-mskput
44
+ ```
45
+
46
+ </section>
47
+
48
+ <section class="usage">
49
+
50
+ ## Usage
51
+
52
+ ```javascript
53
+ var mskput = require( '@stdlib/array-mskput' );
54
+ ```
55
+
56
+ #### mskput( x, mask, values\[, options] )
57
+
58
+ Replaces elements of an array with provided values according to a provided mask array.
59
+
60
+ ```javascript
61
+ var x = [ 1, 2, 3, 4 ];
62
+
63
+ var out = mskput( x, [ 1, 0, 1, 0 ], [ 20, 40 ] );
64
+ // returns [ 1, 20, 3, 40 ]
65
+
66
+ var bool = ( out === x );
67
+ // returns true
68
+ ```
69
+
70
+ The function supports the following parameters:
71
+
72
+ - **x**: input array.
73
+ - **mask**: mask array.
74
+ - **values**: values to set.
75
+ - **options**: function options.
76
+
77
+ The function supports the following options:
78
+
79
+ - **mode**: string specifying behavior when the number of `values` does not equal the number of falsy `mask` values. Default: `'repeat'`.
80
+
81
+ The function supports the following modes:
82
+
83
+ - `'strict'`: specifies that the function must raise an exception when the number of `values` does not **exactly** equal the number of falsy `mask` values.
84
+ - `'non_strict'`: specifies that the function must raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
85
+ - `'strict_broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the number of `values` does not **exactly** equal the number of falsy `mask` values.
86
+ - `'broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
87
+ - `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array.
88
+
89
+ In broadcasting modes, the function supports broadcasting a `values` array containing a single element against the number of falsy values in the `mask` array.
90
+
91
+ ```javascript
92
+ var x = [ 1, 2, 3, 4 ];
93
+
94
+ var out = mskput( x, [ 1, 0, 1, 0 ], [ 20 ], {
95
+ 'mode': 'strict_broadcast'
96
+ });
97
+ // returns [ 1, 20, 3, 20 ]
98
+
99
+ var bool = ( out === x );
100
+ // returns true
101
+ ```
102
+
103
+ In repeat mode, the function supports recycling elements in a `values` array to satisfy the number of falsy values in the `mask` array.
104
+
105
+ ```javascript
106
+ var x = [ 1, 2, 3, 4 ];
107
+
108
+ var out = mskput( x, [ 0, 0, 1, 0 ], [ 20, 40 ], {
109
+ 'mode': 'repeat'
110
+ });
111
+ // returns [ 20, 40, 3, 20 ]
112
+
113
+ var bool = ( out === x );
114
+ // returns true
115
+ ```
116
+
117
+ </section>
118
+
119
+ <!-- /.usage -->
120
+
121
+ <section class="notes">
122
+
123
+ ## Notes
124
+
125
+ - The function mutates the input array `x`.
126
+ - If a `mask` array element is falsy, the corresponding element in `x` is **replaced**; otherwise, the corresponding element in `x` is "masked" and thus left unchanged.
127
+ - The `values` array must have a [data type][@stdlib/array/dtypes] which can be [safely cast][@stdlib/array/safe-casts] to the input array data type. Floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the [same kind][@stdlib/array/same-kind-casts] (e.g., element values from a `'float64'` values array can be assigned to corresponding elements in a `'float32'` input array).
128
+
129
+ </section>
130
+
131
+ <!-- /.notes -->
132
+
133
+ <section class="examples">
134
+
135
+ ## Examples
136
+
137
+ <!-- eslint no-undef: "error" -->
138
+
139
+ ```javascript
140
+ var filledBy = require( '@stdlib/array-base-filled-by' );
141
+ var discreteUniform = require( '@stdlib/random-base-discrete-uniform' );
142
+ var bernoulli = require( '@stdlib/random-base-bernoulli' );
143
+ var linspace = require( '@stdlib/array-base-linspace' );
144
+ var mskput = require( '@stdlib/array-mskput' );
145
+
146
+ // Generate a linearly spaced array:
147
+ var x = linspace( 0, 100, 11 );
148
+ console.log( x );
149
+
150
+ // Generate a random mask array:
151
+ var N = discreteUniform( 5, 15 );
152
+ var mask = filledBy( N, bernoulli.factory( 0.3 ) );
153
+ console.log( mask );
154
+
155
+ // Generate an array of random values:
156
+ var values = filledBy( N, discreteUniform.factory( 1000, 2000 ) );
157
+ console.log( values );
158
+
159
+ // Update a random sample of elements in `x`:
160
+ var out = mskput( x, mask, values );
161
+ console.log( out );
162
+ ```
163
+
164
+ </section>
165
+
166
+ <!-- /.examples -->
167
+
168
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
169
+
170
+ <section class="related">
171
+
172
+ </section>
173
+
174
+ <!-- /.related -->
175
+
176
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
177
+
178
+
179
+ <section class="main-repo" >
180
+
181
+ * * *
182
+
183
+ ## Notice
184
+
185
+ 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.
186
+
187
+ 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].
188
+
189
+ #### Community
190
+
191
+ [![Chat][chat-image]][chat-url]
192
+
193
+ ---
194
+
195
+ ## License
196
+
197
+ See [LICENSE][stdlib-license].
198
+
199
+
200
+ ## Copyright
201
+
202
+ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
203
+
204
+ </section>
205
+
206
+ <!-- /.stdlib -->
207
+
208
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
209
+
210
+ <section class="links">
211
+
212
+ [npm-image]: http://img.shields.io/npm/v/@stdlib/array-mskput.svg
213
+ [npm-url]: https://npmjs.org/package/@stdlib/array-mskput
214
+
215
+ [test-image]: https://github.com/stdlib-js/array-mskput/actions/workflows/test.yml/badge.svg?branch=v0.1.0
216
+ [test-url]: https://github.com/stdlib-js/array-mskput/actions/workflows/test.yml?query=branch:v0.1.0
217
+
218
+ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-mskput/main.svg
219
+ [coverage-url]: https://codecov.io/github/stdlib-js/array-mskput?branch=main
220
+
221
+ <!--
222
+
223
+ [dependencies-image]: https://img.shields.io/david/stdlib-js/array-mskput.svg
224
+ [dependencies-url]: https://david-dm.org/stdlib-js/array-mskput/main
225
+
226
+ -->
227
+
228
+ [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
229
+ [chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
230
+
231
+ [stdlib]: https://github.com/stdlib-js/stdlib
232
+
233
+ [stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
234
+
235
+ [umd]: https://github.com/umdjs/umd
236
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
237
+
238
+ [deno-url]: https://github.com/stdlib-js/array-mskput/tree/deno
239
+ [deno-readme]: https://github.com/stdlib-js/array-mskput/blob/deno/README.md
240
+ [umd-url]: https://github.com/stdlib-js/array-mskput/tree/umd
241
+ [umd-readme]: https://github.com/stdlib-js/array-mskput/blob/umd/README.md
242
+ [esm-url]: https://github.com/stdlib-js/array-mskput/tree/esm
243
+ [esm-readme]: https://github.com/stdlib-js/array-mskput/blob/esm/README.md
244
+ [branches-url]: https://github.com/stdlib-js/array-mskput/blob/main/branches.md
245
+
246
+ [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/array-mskput/main/LICENSE
247
+
248
+ [@stdlib/array/dtypes]: https://www.npmjs.com/package/@stdlib/array-dtypes
249
+
250
+ [@stdlib/array/safe-casts]: https://www.npmjs.com/package/@stdlib/array-safe-casts
251
+
252
+ [@stdlib/array/same-kind-casts]: https://www.npmjs.com/package/@stdlib/array-same-kind-casts
253
+
254
+ </section>
255
+
256
+ <!-- /.links -->
package/SECURITY.md ADDED
@@ -0,0 +1,5 @@
1
+ # Security
2
+
3
+ > Policy for reporting security vulnerabilities.
4
+
5
+ See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security).
@@ -0,0 +1,3 @@
1
+ /// <reference path="../docs/types/index.d.ts" />
2
+ import mskput from '../docs/types/index';
3
+ export = mskput;
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";var s=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var c=s(function(P,m){
2
+ function b(){return{mode:"repeat"}}m.exports=b
3
+ });var p=s(function(R,f){
4
+ var w=require('@stdlib/assert-is-plain-object/dist'),h=require('@stdlib/assert-has-own-property/dist'),T=require('@stdlib/array-base-assert-contains/dist').factory,v=require('@stdlib/error-tools-fmtprodmsg/dist'),l=["strict","non_strict","strict_broadcast","broadcast","repeat"],E=T(l);function j(e,r){return w(r)?h(r,"mode")&&(e.mode=r.mode,!E(e.mode))?new TypeError(v('1wN4S',"mode",l.join('", "'),e.mode)):null:new TypeError(v('1wN2V',r));}f.exports=j
5
+ });var q=s(function(z,g){
6
+ var O=require('@stdlib/array-base-assert-is-mostly-safe-data-type-cast/dist'),D=require('@stdlib/array-base-assert-is-real-data-type/dist'),V=require('@stdlib/array-base-assert-is-complex-floating-point-data-type/dist'),d=require('@stdlib/assert-is-collection/dist'),x=require('@stdlib/array-base-mskput/dist'),y=require('@stdlib/array-dtype/dist'),C=require('@stdlib/array-convert/dist'),n=require('@stdlib/error-tools-fmtprodmsg/dist'),M=c(),S=p();function k(e,r,t){var o,u,a,i;if(!d(e))throw new TypeError(n('1wN2O',e));if(!d(r))throw new TypeError(n('1wN2y',r));if(!d(t))throw new TypeError(n('1wN2l',t));if(o=M(),arguments.length>3&&(u=S(o,arguments[3]),u))throw u;if(a=y(e)||"generic",i=y(t)||"generic",!O(i,a))throw new TypeError(n('1wNFu',i,a));return V(a)&&D(i)&&(t=C(t,a)),x(e,r,t,o.mode)}g.exports=k
7
+ });var _=q();module.exports=_;
8
+ /** @license Apache-2.0 */
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../lib/defaults.js", "../lib/validate.js", "../lib/main.js", "../lib/index.js"],
4
+ "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MAIN //\n\n/**\n* Returns default options.\n*\n* @private\n* @returns {Object} default options\n*\n* @example\n* var o = defaults();\n* // returns {...}\n*/\nfunction defaults() {\n\treturn {\n\t\t'mode': 'repeat'\n\t};\n}\n\n\n// EXPORTS //\n\nmodule.exports = defaults;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar contains = require( '@stdlib/array-base-assert-contains' ).factory;\nvar format = require( '@stdlib/string-format' );\n\n\n// VARIABLES //\n\nvar MODES = [\n\t'strict',\n\t'non_strict',\n\t'strict_broadcast',\n\t'broadcast',\n\t'repeat'\n];\nvar isMode = contains( MODES );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {string} [options.mode] - mode option\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'mode': 'strict'\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'mode' ) ) {\n\t\topts.mode = options.mode;\n\t\tif ( !isMode( opts.mode ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be one of the following: \"%s\". Option: `%s`.', 'mode', MODES.join( '\", \"' ), opts.mode ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isMostlySafeCast = require( '@stdlib/array-base-assert-is-mostly-safe-data-type-cast' );\nvar isRealDataType = require( '@stdlib/array-base-assert-is-real-data-type' );\nvar isComplexDataType = require( '@stdlib/array-base-assert-is-complex-floating-point-data-type' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar base = require( '@stdlib/array-base-mskput' );\nvar dtype = require( '@stdlib/array-dtype' );\nvar convert = require( '@stdlib/array-convert' );\nvar format = require( '@stdlib/string-format' );\nvar defaults = require( './defaults.js' );\nvar validate = require( './validate.js' );\n\n\n// MAIN //\n\n/**\n* Replaces elements of an array with provided values according to a provided mask array.\n*\n* @param {Collection} x - input array\n* @param {Collection} mask - mask array\n* @param {Collection} values - values to set\n* @param {Options} [options] - function options\n* @param {string} [options.mode='repeat'] - string specifying behavior when the number of values does not equal the number of falsy mask values\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} second argument must be a collection\n* @throws {TypeError} third argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {Error} must provide valid options\n* @throws {Error} insufficient values to satisfy mask array\n* @throws {Error} number of values does not equal the number of falsy mask values\n* @throws {TypeError} third argument cannot be safely cast to the data type of the first argument\n* @returns {Collection} input array\n*\n* @example\n* var x = [ 1, 2, 3, 4 ];\n*\n* var mask = [ 1, 0, 0, 1 ];\n* var values = [ 20, 30 ];\n*\n* var out = mskput( x, mask, values );\n* // returns [ 1, 20, 30, 4 ]\n*\n* var bool = ( out === x );\n* // returns true\n*\n* @example\n* var x = [ 1, 2, 3, 4 ];\n*\n* var mask = [ 1, 0, 0, 1 ];\n* var values = [ 30 ];\n*\n* var out = mskput( x, mask, values );\n* // returns [ 1, 30, 30, 4 ]\n*\n* var bool = ( out === x );\n* // returns true\n*\n* @example\n* var x = [ 1, 2, 3, 4 ];\n*\n* var mask = [ 0, 0, 1, 0 ];\n* var values = [ 20, 30 ];\n*\n* var out = mskput( x, mask, values );\n* // returns [ 20, 30, 3, 20 ]\n*\n* var bool = ( out === x );\n* // returns true\n*/\nfunction mskput( x, mask, values ) {\n\tvar opts;\n\tvar err;\n\tvar xdt;\n\tvar vdt;\n\tif ( !isCollection( x ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );\n\t}\n\tif ( !isCollection( mask ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', mask ) );\n\t}\n\tif ( !isCollection( values ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Third argument must be an array-like object. Value: `%s`.', values ) );\n\t}\n\topts = defaults();\n\tif ( arguments.length > 3 ) {\n\t\terr = validate( opts, arguments[ 3 ] );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t}\n\txdt = dtype( x ) || 'generic';\n\tvdt = dtype( values ) || 'generic';\n\n\t// Safe casts are always allowed and allow same kind casts (i.e., downcasts) only when the input array data type is floating-point...\n\tif ( !isMostlySafeCast( vdt, xdt ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Third argument cannot be safely cast to the input array data type. Data types: [%s, %s].', vdt, xdt ) );\n\t}\n\t// 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...\n\tif ( isComplexDataType( xdt ) && isRealDataType( vdt ) ) {\n\t\tvalues = convert( values, xdt );\n\t}\n\t// Replace values in the input array:\n\treturn base( x, mask, values, opts.mode );\n}\n\n\n// EXPORTS //\n\nmodule.exports = mskput;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Replace elements of an array with provided values according to a provided mask array.\n*\n* @module @stdlib/array-mskput\n*\n* @example\n* var mskput = require( '@stdlib/array-mskput' );\n*\n* var x = [ 1, 2, 3, 4 ];\n*\n* var mask = [ 1, 0, 0, 1 ];\n* var values = [ 20, 30 ];\n*\n* var out = mskput( x, mask, values );\n* // returns [ 1, 20, 30, 4 ]\n*\n* var bool = ( out === x );\n* // returns true\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
5
+ "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAgCA,SAASC,GAAW,CACnB,MAAO,CACN,KAAQ,QACT,CACD,CAKAD,EAAO,QAAUC,ICzCjB,IAAAC,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAW,QAAS,oCAAqC,EAAE,QAC3DC,EAAS,QAAS,uBAAwB,EAK1CC,EAAQ,CACX,SACA,aACA,mBACA,YACA,QACD,EACIC,EAASH,EAAUE,CAAM,EAwB7B,SAASE,EAAUC,EAAMC,EAAU,CAClC,OAAMR,EAAUQ,CAAQ,EAGnBP,EAAYO,EAAS,MAAO,IAChCD,EAAK,KAAOC,EAAQ,KACf,CAACH,EAAQE,EAAK,IAAK,GAChB,IAAI,UAAWJ,EAAQ,gFAAiF,OAAQC,EAAM,KAAM,MAAO,EAAGG,EAAK,IAAK,CAAE,EAGpJ,KARC,IAAI,UAAWJ,EAAQ,qEAAsEK,CAAQ,CAAE,CAShH,CAKAT,EAAO,QAAUO,IC7EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAmB,QAAS,yDAA0D,EACtFC,EAAiB,QAAS,6CAA8C,EACxEC,EAAoB,QAAS,+DAAgE,EAC7FC,EAAe,QAAS,8BAA+B,EACvDC,EAAO,QAAS,2BAA4B,EAC5CC,EAAQ,QAAS,qBAAsB,EACvCC,EAAU,QAAS,uBAAwB,EAC3CC,EAAS,QAAS,uBAAwB,EAC1CC,EAAW,IACXC,EAAW,IA2Df,SAASC,EAAQC,EAAGC,EAAMC,EAAS,CAClC,IAAIC,EACAC,EACAC,EACAC,EACJ,GAAK,CAACd,EAAcQ,CAAE,EACrB,MAAM,IAAI,UAAWJ,EAAQ,8EAA+EI,CAAE,CAAE,EAEjH,GAAK,CAACR,EAAcS,CAAK,EACxB,MAAM,IAAI,UAAWL,EAAQ,+EAAgFK,CAAK,CAAE,EAErH,GAAK,CAACT,EAAcU,CAAO,EAC1B,MAAM,IAAI,UAAWN,EAAQ,8EAA+EM,CAAO,CAAE,EAGtH,GADAC,EAAON,EAAS,EACX,UAAU,OAAS,IACvBO,EAAMN,EAAUK,EAAM,UAAW,CAAE,CAAE,EAChCC,GACJ,MAAMA,EAOR,GAJAC,EAAMX,EAAOM,CAAE,GAAK,UACpBM,EAAMZ,EAAOQ,CAAO,GAAK,UAGpB,CAACb,EAAkBiB,EAAKD,CAAI,EAChC,MAAM,IAAI,UAAWT,EAAQ,6GAA8GU,EAAKD,CAAI,CAAE,EAGvJ,OAAKd,EAAmBc,CAAI,GAAKf,EAAgBgB,CAAI,IACpDJ,EAASP,EAASO,EAAQG,CAAI,GAGxBZ,EAAMO,EAAGC,EAAMC,EAAQC,EAAK,IAAK,CACzC,CAKAf,EAAO,QAAUW,ICvFjB,IAAIQ,EAAO,IAKX,OAAO,QAAUA",
6
+ "names": ["require_defaults", "__commonJSMin", "exports", "module", "defaults", "require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "contains", "format", "MODES", "isMode", "validate", "opts", "options", "require_main", "__commonJSMin", "exports", "module", "isMostlySafeCast", "isRealDataType", "isComplexDataType", "isCollection", "base", "dtype", "convert", "format", "defaults", "validate", "mskput", "x", "mask", "values", "opts", "err", "xdt", "vdt", "main"]
7
+ }
@@ -0,0 +1,232 @@
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
+ // TypeScript Version: 4.1
20
+
21
+ /// <reference types="@stdlib/types"/>
22
+
23
+ import { Collection, AccessorArrayLike, TypedArray, ComplexTypedArray, BooleanTypedArray } from '@stdlib/types/array';
24
+ import { ComplexLike } from '@stdlib/types/complex';
25
+
26
+ /**
27
+ * Mask array.
28
+ */
29
+ type MaskArray = Collection | AccessorArrayLike<any>;
30
+
31
+ /**
32
+ * Values array.
33
+ */
34
+ type ValuesArray<T> = Collection<T> | AccessorArrayLike<T>;
35
+
36
+ /**
37
+ * Interface describing function options.
38
+ */
39
+ interface Options {
40
+ /**
41
+ * Mode specifying behavior when the number of values to set does not equal the number of falsy values in the mask array.
42
+ *
43
+ * ## Notes
44
+ *
45
+ * - The function supports the following modes:
46
+ *
47
+ * - `'strict'`: specifies that the function must raise an exception when the number of `values` does not **exactly** equal the number of falsy `mask` values.
48
+ * - `'non_strict'`: specifies that the function must raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
49
+ * - `'strict_broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the number of `values` does not **exactly** equal the number of falsy `mask` values.
50
+ * - `'broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
51
+ * - `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array.
52
+ */
53
+ mode?: 'strict' | 'non_strict' | 'strict_broadcast' | 'broadcast' | 'repeat';
54
+ }
55
+
56
+ /**
57
+ * Replaces elements of an array with provided values according to a provided mask array.
58
+ *
59
+ * @param x - input array
60
+ * @param mask - mask array
61
+ * @param values - values to set
62
+ * @param options - function options
63
+ * @returns input array
64
+ *
65
+ * @example
66
+ * var Int32Array = require( '@stdlib/array-int32' );
67
+ *
68
+ * var x = new Int32Array( [ 1, 2, 3, 4 ] );
69
+ *
70
+ * var mask = [ 1, 0, 0, 1 ];
71
+ * var values = [ 20, 30 ];
72
+ *
73
+ * var out = mskput( x, mask, values );
74
+ * // returns <Int32Array>[ 1, 20, 30, 4 ]
75
+ *
76
+ * var bool = ( out === x );
77
+ * // returns true
78
+ *
79
+ * @example
80
+ * var Int32Array = require( '@stdlib/array-int32' );
81
+ *
82
+ * var x = new Int32Array( [ 1, 2, 3, 4 ] );
83
+ *
84
+ * var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ] );
85
+ * // returns <Int32Array>[ 1, 30, 30, 4 ]
86
+ *
87
+ * var bool = ( out === x );
88
+ * // returns true
89
+ */
90
+ declare function mskput<T extends TypedArray | BooleanTypedArray, U = unknown>( x: T, mask: MaskArray, values: ValuesArray<U>, options?: Options ): T;
91
+
92
+ /**
93
+ * Replaces elements of an array with provided values according to a provided mask array.
94
+ *
95
+ * @param x - input array
96
+ * @param mask - mask array
97
+ * @param values - values to set
98
+ * @param options - function options
99
+ * @returns input array
100
+ *
101
+ * @example
102
+ * var Complex128Array = require( '@stdlib/array-complex128' );
103
+ *
104
+ * var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
105
+ *
106
+ * var mask = [ 1, 0, 0, 1 ];
107
+ * var values = new Complex128Array( [ 20.0, 30.0, 40, 5.0 ] );
108
+ *
109
+ * var out = mskput( x, mask, values );
110
+ * // returns <Complex128Array>
111
+ *
112
+ * var bool = ( out === x );
113
+ * // returns true
114
+ *
115
+ * @example
116
+ * var Complex128Array = require( '@stdlib/array-complex128' );
117
+ *
118
+ * var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
119
+ *
120
+ * var mask = [ 1, 0, 0, 1 ];
121
+ * var values = new Complex128Array( [ 20.0, 30.0 ] );
122
+ *
123
+ * var out = mskput( x, mask, values );
124
+ * // returns <Complex128Array>
125
+ *
126
+ * var bool = ( out === x );
127
+ * // returns true
128
+ */
129
+ declare function mskput<T extends ComplexTypedArray>( x: T, mask: MaskArray, values: ValuesArray<ComplexLike | [ number, number ]>, options?: Options ): T;
130
+
131
+ /**
132
+ * Replaces elements of an array with provided values according to a provided mask array.
133
+ *
134
+ * @param x - input array
135
+ * @param mask - mask array
136
+ * @param values - values to set
137
+ * @param options - function options
138
+ * @returns input array
139
+ *
140
+ * @example
141
+ * var x = [ 1, 2, 3, 4 ];
142
+ *
143
+ * var mask = [ 1, 0, 0, 1 ];
144
+ * var values = [ 20, 30 ];
145
+ *
146
+ * var out = mskput( x, mask, values );
147
+ * // returns [ 1, 20, 30, 4 ]
148
+ *
149
+ * var bool = ( out === x );
150
+ * // returns true
151
+ *
152
+ * @example
153
+ * var x = [ 1, 2, 3, 4 ];
154
+ *
155
+ * var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ] );
156
+ * // returns [ 1, 30, 30, 4 ]
157
+ *
158
+ * var bool = ( out === x );
159
+ * // returns true
160
+ */
161
+ declare function mskput<T = unknown, U = unknown>( x: Array<T>, mask: MaskArray, values: ValuesArray<U>, options?: Options ): Array<T | U>;
162
+
163
+ /**
164
+ * Replaces elements of an array with provided values according to a provided mask array.
165
+ *
166
+ * @param x - input array
167
+ * @param mask - mask array
168
+ * @param values - values to set
169
+ * @param options - function options
170
+ * @returns input array
171
+ *
172
+ * @example
173
+ * var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );
174
+ *
175
+ * var x = toAccessorArray( [ 1, 2, 3, 4 ] );
176
+ *
177
+ * var mask = [ 1, 0, 0, 1 ];
178
+ * var values = [ 20, 30 ];
179
+ *
180
+ * var out = mskput( x, mask, values );
181
+ *
182
+ * var bool = ( out === x );
183
+ * // returns true
184
+ *
185
+ * @example
186
+ * var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );
187
+ *
188
+ * var x = toAccessorArray( [ 1, 2, 3, 4 ] );
189
+ *
190
+ * var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ] );
191
+ *
192
+ * var bool = ( out === x );
193
+ * // returns true
194
+ */
195
+ declare function mskput<T = unknown, U = unknown>( x: AccessorArrayLike<T>, mask: MaskArray, values: ValuesArray<U>, options?: Options ): AccessorArrayLike<T | U>;
196
+
197
+ /**
198
+ * Replaces elements of an array with provided values according to a provided mask array.
199
+ *
200
+ * @param x - input array
201
+ * @param mask - mask array
202
+ * @param values - values to set
203
+ * @param options - function options
204
+ * @returns input array
205
+ *
206
+ * @example
207
+ * var x = [ 1, 2, 3, 4 ];
208
+ *
209
+ * var mask = [ 1, 0, 0, 1 ];
210
+ * var values = [ 20, 30 ];
211
+ *
212
+ * var out = mskput( x, mask, values );
213
+ * // returns [ 1, 20, 30, 4 ]
214
+ *
215
+ * var bool = ( out === x );
216
+ * // returns true
217
+ *
218
+ * @example
219
+ * var x = [ 1, 2, 3, 4 ];
220
+ *
221
+ * var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ] );
222
+ * // returns [ 1, 30, 30, 4 ]
223
+ *
224
+ * var bool = ( out === x );
225
+ * // returns true
226
+ */
227
+ declare function mskput<T = unknown, U = unknown>( x: Collection<T>, mask: MaskArray, values: ValuesArray<U>, options?: Options ): Collection<T | U>;
228
+
229
+
230
+ // EXPORTS //
231
+
232
+ export = mskput;
@@ -0,0 +1,42 @@
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
+ // MAIN //
22
+
23
+ /**
24
+ * Returns default options.
25
+ *
26
+ * @private
27
+ * @returns {Object} default options
28
+ *
29
+ * @example
30
+ * var o = defaults();
31
+ * // returns {...}
32
+ */
33
+ function defaults() {
34
+ return {
35
+ 'mode': 'repeat'
36
+ };
37
+ }
38
+
39
+
40
+ // EXPORTS //
41
+
42
+ module.exports = defaults;
package/lib/index.js ADDED
@@ -0,0 +1,48 @@
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
+ /**
22
+ * Replace elements of an array with provided values according to a provided mask array.
23
+ *
24
+ * @module @stdlib/array-mskput
25
+ *
26
+ * @example
27
+ * var mskput = require( '@stdlib/array-mskput' );
28
+ *
29
+ * var x = [ 1, 2, 3, 4 ];
30
+ *
31
+ * var mask = [ 1, 0, 0, 1 ];
32
+ * var values = [ 20, 30 ];
33
+ *
34
+ * var out = mskput( x, mask, values );
35
+ * // returns [ 1, 20, 30, 4 ]
36
+ *
37
+ * var bool = ( out === x );
38
+ * // returns true
39
+ */
40
+
41
+ // MODULES //
42
+
43
+ var main = require( './main.js' );
44
+
45
+
46
+ // EXPORTS //
47
+
48
+ module.exports = main;
package/lib/main.js ADDED
@@ -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 base = require( '@stdlib/array-base-mskput' );
28
+ var dtype = require( '@stdlib/array-dtype' );
29
+ var convert = require( '@stdlib/array-convert' );
30
+ var format = require( '@stdlib/string-format' );
31
+ var defaults = require( './defaults.js' );
32
+ var validate = require( './validate.js' );
33
+
34
+
35
+ // MAIN //
36
+
37
+ /**
38
+ * Replaces elements of an array with provided values according to a provided mask array.
39
+ *
40
+ * @param {Collection} x - input array
41
+ * @param {Collection} mask - mask array
42
+ * @param {Collection} values - values to set
43
+ * @param {Options} [options] - function options
44
+ * @param {string} [options.mode='repeat'] - string specifying behavior when the number of values does not equal the number of falsy mask values
45
+ * @throws {TypeError} first argument must be a collection
46
+ * @throws {TypeError} second argument must be a collection
47
+ * @throws {TypeError} third argument must be a collection
48
+ * @throws {TypeError} options argument must be an object
49
+ * @throws {Error} must provide valid options
50
+ * @throws {Error} insufficient values to satisfy mask array
51
+ * @throws {Error} number of values does not equal the number of falsy mask values
52
+ * @throws {TypeError} third argument cannot be safely cast to the data type of the first argument
53
+ * @returns {Collection} input array
54
+ *
55
+ * @example
56
+ * var x = [ 1, 2, 3, 4 ];
57
+ *
58
+ * var mask = [ 1, 0, 0, 1 ];
59
+ * var values = [ 20, 30 ];
60
+ *
61
+ * var out = mskput( x, mask, values );
62
+ * // returns [ 1, 20, 30, 4 ]
63
+ *
64
+ * var bool = ( out === x );
65
+ * // returns true
66
+ *
67
+ * @example
68
+ * var x = [ 1, 2, 3, 4 ];
69
+ *
70
+ * var mask = [ 1, 0, 0, 1 ];
71
+ * var values = [ 30 ];
72
+ *
73
+ * var out = mskput( x, mask, values );
74
+ * // returns [ 1, 30, 30, 4 ]
75
+ *
76
+ * var bool = ( out === x );
77
+ * // returns true
78
+ *
79
+ * @example
80
+ * var x = [ 1, 2, 3, 4 ];
81
+ *
82
+ * var mask = [ 0, 0, 1, 0 ];
83
+ * var values = [ 20, 30 ];
84
+ *
85
+ * var out = mskput( x, mask, values );
86
+ * // returns [ 20, 30, 3, 20 ]
87
+ *
88
+ * var bool = ( out === x );
89
+ * // returns true
90
+ */
91
+ function mskput( x, mask, values ) {
92
+ var opts;
93
+ var err;
94
+ var xdt;
95
+ var vdt;
96
+ if ( !isCollection( x ) ) {
97
+ throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );
98
+ }
99
+ if ( !isCollection( mask ) ) {
100
+ throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', mask ) );
101
+ }
102
+ if ( !isCollection( values ) ) {
103
+ throw new TypeError( format( 'invalid argument. Third argument must be an array-like object. Value: `%s`.', values ) );
104
+ }
105
+ opts = defaults();
106
+ if ( arguments.length > 3 ) {
107
+ err = validate( opts, arguments[ 3 ] );
108
+ if ( err ) {
109
+ throw err;
110
+ }
111
+ }
112
+ xdt = dtype( x ) || 'generic';
113
+ vdt = dtype( values ) || 'generic';
114
+
115
+ // Safe casts are always allowed and allow same kind casts (i.e., downcasts) only when the input array data type is floating-point...
116
+ if ( !isMostlySafeCast( vdt, xdt ) ) {
117
+ throw new TypeError( format( 'invalid argument. Third argument cannot be safely cast to the input array data type. Data types: [%s, %s].', vdt, xdt ) );
118
+ }
119
+ // 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...
120
+ if ( isComplexDataType( xdt ) && isRealDataType( vdt ) ) {
121
+ values = convert( values, xdt );
122
+ }
123
+ // Replace values in the input array:
124
+ return base( x, mask, values, opts.mode );
125
+ }
126
+
127
+
128
+ // EXPORTS //
129
+
130
+ module.exports = mskput;
@@ -0,0 +1,78 @@
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 isObject = require( '@stdlib/assert-is-plain-object' );
24
+ var hasOwnProp = require( '@stdlib/assert-has-own-property' );
25
+ var contains = require( '@stdlib/array-base-assert-contains' ).factory;
26
+ var format = require( '@stdlib/string-format' );
27
+
28
+
29
+ // VARIABLES //
30
+
31
+ var MODES = [
32
+ 'strict',
33
+ 'non_strict',
34
+ 'strict_broadcast',
35
+ 'broadcast',
36
+ 'repeat'
37
+ ];
38
+ var isMode = contains( MODES );
39
+
40
+
41
+ // MAIN //
42
+
43
+ /**
44
+ * Validates function options.
45
+ *
46
+ * @private
47
+ * @param {Object} opts - destination object
48
+ * @param {Options} options - function options
49
+ * @param {string} [options.mode] - mode option
50
+ * @returns {(Error|null)} null or an error object
51
+ *
52
+ * @example
53
+ * var opts = {};
54
+ * var options = {
55
+ * 'mode': 'strict'
56
+ * };
57
+ * var err = validate( opts, options );
58
+ * if ( err ) {
59
+ * throw err;
60
+ * }
61
+ */
62
+ function validate( opts, options ) {
63
+ if ( !isObject( options ) ) {
64
+ return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
65
+ }
66
+ if ( hasOwnProp( options, 'mode' ) ) {
67
+ opts.mode = options.mode;
68
+ if ( !isMode( opts.mode ) ) {
69
+ return new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'mode', MODES.join( '", "' ), opts.mode ) );
70
+ }
71
+ }
72
+ return null;
73
+ }
74
+
75
+
76
+ // EXPORTS //
77
+
78
+ module.exports = validate;
package/package.json ADDED
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "@stdlib/array-mskput",
3
+ "version": "0.1.0",
4
+ "description": "Replace elements of an array with provided values according to a provided mask array.",
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
+ "doc": "./docs",
19
+ "lib": "./lib",
20
+ "dist": "./dist"
21
+ },
22
+ "types": "./docs/types",
23
+ "scripts": {},
24
+ "homepage": "https://stdlib.io",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git://github.com/stdlib-js/array-mskput.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/stdlib-js/stdlib/issues"
31
+ },
32
+ "dependencies": {
33
+ "@stdlib/array-base-assert-contains": "^0.2.2",
34
+ "@stdlib/array-base-assert-is-complex-floating-point-data-type": "^0.2.1",
35
+ "@stdlib/array-base-assert-is-mostly-safe-data-type-cast": "^0.2.2",
36
+ "@stdlib/array-base-assert-is-real-data-type": "^0.2.1",
37
+ "@stdlib/array-base-mskput": "^0.1.0",
38
+ "@stdlib/array-convert": "^0.2.1",
39
+ "@stdlib/array-dtype": "^0.3.0",
40
+ "@stdlib/assert-has-own-property": "^0.2.2",
41
+ "@stdlib/assert-is-collection": "^0.2.2",
42
+ "@stdlib/assert-is-plain-object": "^0.2.2",
43
+ "@stdlib/string-format": "^0.2.2",
44
+ "@stdlib/error-tools-fmtprodmsg": "^0.2.2"
45
+ },
46
+ "devDependencies": {},
47
+ "engines": {
48
+ "node": ">=0.10.0",
49
+ "npm": ">2.7.0"
50
+ },
51
+ "os": [
52
+ "aix",
53
+ "darwin",
54
+ "freebsd",
55
+ "linux",
56
+ "macos",
57
+ "openbsd",
58
+ "sunos",
59
+ "win32",
60
+ "windows"
61
+ ],
62
+ "keywords": [
63
+ "stdlib",
64
+ "utilities",
65
+ "utils",
66
+ "generic",
67
+ "array",
68
+ "set",
69
+ "replace",
70
+ "put",
71
+ "update",
72
+ "mask",
73
+ "missing",
74
+ "na"
75
+ ],
76
+ "funding": {
77
+ "type": "opencollective",
78
+ "url": "https://opencollective.com/stdlib"
79
+ }
80
+ }