@stdlib/ndarray-filter 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-2026 The Stdlib Authors.
package/README.md ADDED
@@ -0,0 +1,321 @@
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
+ # filter
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
+ > Return a shallow copy of an [ndarray][@stdlib/ndarray/ctor] containing only those elements which pass a test implemented by a predicate function.
37
+
38
+ <section class="intro">
39
+
40
+ </section>
41
+
42
+ <!-- /.intro -->
43
+
44
+ <section class="installation">
45
+
46
+ ## Installation
47
+
48
+ ```bash
49
+ npm install @stdlib/ndarray-filter
50
+ ```
51
+
52
+ </section>
53
+
54
+ <section class="usage">
55
+
56
+ ## Usage
57
+
58
+ ```javascript
59
+ var filter = require( '@stdlib/ndarray-filter' );
60
+ ```
61
+
62
+ #### filter( x\[, options], predicate\[, thisArg] )
63
+
64
+ Returns a shallow copy of an [ndarray][@stdlib/ndarray/ctor] containing only those elements which pass a test implemented by a `predicate` function.
65
+
66
+ <!-- eslint-disable max-len -->
67
+
68
+ ```javascript
69
+ var Float64Array = require( '@stdlib/array-float64' );
70
+ var ndarray = require( '@stdlib/ndarray-ctor' );
71
+
72
+ function predicate( z ) {
73
+ return z > 6.0;
74
+ }
75
+
76
+ var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
77
+ var shape = [ 2, 3 ];
78
+ var strides = [ 6, 1 ];
79
+ var offset = 1;
80
+
81
+ var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
82
+ // returns <ndarray>
83
+
84
+ var y = filter( x, predicate );
85
+ // returns <ndarray>[ 8.0, 9.0, 10.0 ]
86
+ ```
87
+
88
+ The function accepts the following arguments:
89
+
90
+ - **x**: input [ndarray][@stdlib/ndarray/ctor].
91
+ - **options**: function options _(optional)_.
92
+ - **predicate**: predicate function.
93
+ - **thisArg**: predicate function execution context _(optional)_.
94
+
95
+ The function accepts the following options:
96
+
97
+ - **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. If not specified, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor].
98
+ - **order**: index iteration order. By default, the function iterates over elements according to the [layout order][@stdlib/ndarray/orders] of the provided [ndarray][@stdlib/ndarray/ctor]. Accordingly, for row-major input [ndarrays][@stdlib/ndarray/ctor], the last dimension indices increment fastest. For column-major input [ndarrays][@stdlib/ndarray/ctor], the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manner, regardless of the input [ndarray][@stdlib/ndarray/ctor]'s layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input [ndarray][@stdlib/ndarray/ctor] may, in some circumstances, result in performance degradation due to cache misses. Must be either `'row-major'` or `'column-major'`.
99
+
100
+ By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option.
101
+
102
+ <!-- eslint-disable max-len -->
103
+
104
+ ```javascript
105
+ var Float64Array = require( '@stdlib/array-float64' );
106
+ var ndarray = require( '@stdlib/ndarray-ctor' );
107
+ var dtype = require( '@stdlib/ndarray-dtype' );
108
+
109
+ function predicate( z ) {
110
+ return z > 6.0;
111
+ }
112
+
113
+ var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
114
+ var shape = [ 2, 3 ];
115
+ var strides = [ 6, 1 ];
116
+ var offset = 1;
117
+
118
+ var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
119
+ // returns <ndarray>
120
+
121
+ var opts = {
122
+ 'dtype': 'float32'
123
+ };
124
+ var y = filter( x, opts, predicate );
125
+ // returns <ndarray>[ 8.0, 9.0, 10.0 ]
126
+
127
+ var dt = dtype( y );
128
+ // returns 'float32'
129
+ ```
130
+
131
+ To set the `predicate` function execution context, provide a `thisArg`.
132
+
133
+ <!-- eslint-disable no-invalid-this, max-len -->
134
+
135
+ ```javascript
136
+ var Float64Array = require( '@stdlib/array-float64' );
137
+ var ndarray = require( '@stdlib/ndarray-ctor' );
138
+
139
+ function predicate( z ) {
140
+ this.count += 1;
141
+ return z > 6.0;
142
+ }
143
+
144
+ var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
145
+ var shape = [ 2, 3 ];
146
+ var strides = [ 6, 1 ];
147
+ var offset = 1;
148
+
149
+ var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
150
+ // returns <ndarray>
151
+
152
+ var ctx = {
153
+ 'count': 0
154
+ };
155
+ var y = filter( x, predicate, ctx );
156
+ // returns <ndarray>[ 8.0, 9.0, 10.0 ]
157
+
158
+ var count = ctx.count;
159
+ // returns 6
160
+ ```
161
+
162
+ The `predicate` function is provided the following arguments:
163
+
164
+ - **value**: current array element.
165
+ - **indices**: current array element indices.
166
+ - **arr**: the input [ndarray][@stdlib/ndarray/ctor].
167
+
168
+ </section>
169
+
170
+ <!-- /.usage -->
171
+
172
+ <section class="notes">
173
+
174
+ ## Notes
175
+
176
+ - The function does **not** perform explicit casting (e.g., from a real-valued floating-point number to a complex floating-point number). Any such casting should be performed **prior to** calling this function.
177
+ - The function **always** returns a one-dimensional [ndarray][@stdlib/ndarray/ctor].
178
+
179
+ </section>
180
+
181
+ <!-- /.notes -->
182
+
183
+ <section class="examples">
184
+
185
+ ## Examples
186
+
187
+ <!-- eslint no-undef: "error" -->
188
+
189
+ ```javascript
190
+ var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
191
+ var ndarray2array = require( '@stdlib/ndarray-to-array' );
192
+ var naryFunction = require( '@stdlib/utils-nary-function' );
193
+ var array = require( '@stdlib/ndarray-array' );
194
+ var isPositive = require( '@stdlib/assert-is-positive-number' ).isPrimitive;
195
+ var filter = require( '@stdlib/ndarray-filter' );
196
+
197
+ var buffer = discreteUniform( 10, -100, 100, {
198
+ 'dtype': 'generic'
199
+ });
200
+ var x = array( buffer, {
201
+ 'shape': [ 5, 2 ],
202
+ 'dtype': 'generic'
203
+ });
204
+ console.log( ndarray2array( x ) );
205
+
206
+ var y = filter( x, naryFunction( isPositive, 1 ) );
207
+ console.log( ndarray2array( y ) );
208
+ ```
209
+
210
+ </section>
211
+
212
+ <!-- /.examples -->
213
+
214
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
215
+
216
+ <section class="related">
217
+
218
+ * * *
219
+
220
+ ## See Also
221
+
222
+ - <span class="package-name">[`@stdlib/ndarray-filter-map`][@stdlib/ndarray/filter-map]</span><span class="delimiter">: </span><span class="description">filter and map elements in an input ndarray to elements in a new output ndarray according to a callback function.</span>
223
+ - <span class="package-name">[`@stdlib/ndarray-map`][@stdlib/ndarray/map]</span><span class="delimiter">: </span><span class="description">apply a callback to elements in an input ndarray and assign results to elements in a new output ndarray.</span>
224
+ - <span class="package-name">[`@stdlib/ndarray-reject`][@stdlib/ndarray/reject]</span><span class="delimiter">: </span><span class="description">return a shallow copy of an ndarray containing only those elements which fail a test implemented by a predicate function.</span>
225
+ - <span class="package-name">[`@stdlib/ndarray-slice`][@stdlib/ndarray/slice]</span><span class="delimiter">: </span><span class="description">return a read-only view of an input ndarray.</span>
226
+
227
+ </section>
228
+
229
+ <!-- /.related -->
230
+
231
+
232
+ <section class="main-repo" >
233
+
234
+ * * *
235
+
236
+ ## Notice
237
+
238
+ 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.
239
+
240
+ 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].
241
+
242
+ #### Community
243
+
244
+ [![Chat][chat-image]][chat-url]
245
+
246
+ ---
247
+
248
+ ## License
249
+
250
+ See [LICENSE][stdlib-license].
251
+
252
+
253
+ ## Copyright
254
+
255
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
256
+
257
+ </section>
258
+
259
+ <!-- /.stdlib -->
260
+
261
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
262
+
263
+ <section class="links">
264
+
265
+ [npm-image]: http://img.shields.io/npm/v/@stdlib/ndarray-filter.svg
266
+ [npm-url]: https://npmjs.org/package/@stdlib/ndarray-filter
267
+
268
+ [test-image]: https://github.com/stdlib-js/ndarray-filter/actions/workflows/test.yml/badge.svg?branch=v0.1.0
269
+ [test-url]: https://github.com/stdlib-js/ndarray-filter/actions/workflows/test.yml?query=branch:v0.1.0
270
+
271
+ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/ndarray-filter/main.svg
272
+ [coverage-url]: https://codecov.io/github/stdlib-js/ndarray-filter?branch=main
273
+
274
+ <!--
275
+
276
+ [dependencies-image]: https://img.shields.io/david/stdlib-js/ndarray-filter.svg
277
+ [dependencies-url]: https://david-dm.org/stdlib-js/ndarray-filter/main
278
+
279
+ -->
280
+
281
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
282
+ [chat-url]: https://stdlib.zulipchat.com
283
+
284
+ [stdlib]: https://github.com/stdlib-js/stdlib
285
+
286
+ [stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
287
+
288
+ [umd]: https://github.com/umdjs/umd
289
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
290
+
291
+ [deno-url]: https://github.com/stdlib-js/ndarray-filter/tree/deno
292
+ [deno-readme]: https://github.com/stdlib-js/ndarray-filter/blob/deno/README.md
293
+ [umd-url]: https://github.com/stdlib-js/ndarray-filter/tree/umd
294
+ [umd-readme]: https://github.com/stdlib-js/ndarray-filter/blob/umd/README.md
295
+ [esm-url]: https://github.com/stdlib-js/ndarray-filter/tree/esm
296
+ [esm-readme]: https://github.com/stdlib-js/ndarray-filter/blob/esm/README.md
297
+ [branches-url]: https://github.com/stdlib-js/ndarray-filter/blob/main/branches.md
298
+
299
+ [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/ndarray-filter/main/LICENSE
300
+
301
+ [@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/ndarray-ctor
302
+
303
+ [@stdlib/ndarray/dtypes]: https://www.npmjs.com/package/@stdlib/ndarray-dtypes
304
+
305
+ [@stdlib/ndarray/orders]: https://www.npmjs.com/package/@stdlib/ndarray-orders
306
+
307
+ <!-- <related-links> -->
308
+
309
+ [@stdlib/ndarray/filter-map]: https://www.npmjs.com/package/@stdlib/ndarray-filter-map
310
+
311
+ [@stdlib/ndarray/map]: https://www.npmjs.com/package/@stdlib/ndarray-map
312
+
313
+ [@stdlib/ndarray/reject]: https://www.npmjs.com/package/@stdlib/ndarray-reject
314
+
315
+ [@stdlib/ndarray/slice]: https://www.npmjs.com/package/@stdlib/ndarray-slice
316
+
317
+ <!-- </related-links> -->
318
+
319
+ </section>
320
+
321
+ <!-- /.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 filter from '../docs/types/index';
3
+ export = filter;
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";var z=function(r,a){return function(){return a||r((a={exports:{}}).exports,a),a.exports}};var j=z(function(J,E){"use strict";var V=require("@stdlib/assert-is-plain-object"),b=require("@stdlib/assert-is-function"),C=require("@stdlib/assert-is-ndarray-like"),F=require("@stdlib/ndarray-base-assert-is-order"),O=require("@stdlib/assert-has-own-property"),P=require("@stdlib/ndarray-base-buffer-ctors"),D=require("@stdlib/array-base-zeros"),I=require("@stdlib/ndarray-shape"),T=require("@stdlib/ndarray-dtype"),L=require("@stdlib/ndarray-order"),N=require("@stdlib/ndarray-base-numel"),S=require("@stdlib/ndarray-base-next-cartesian-index").assign,A=require("@stdlib/blas-base-gcopy"),v=require("@stdlib/string-format");function B(r,a,g,k){var f,p,t,i,e,c,m,n,h,u,y,s,l,o,w,q,d;if(!C(r))throw new TypeError(v("invalid argument. First argument must be an ndarray-like object. Value: `%s`.",r));if(arguments.length<3?i=a:arguments.length===3?b(a)?(i=a,m=g):(f=!0,e=a,i=g):(f=!0,e=a,i=g,m=k),!b(i))throw new TypeError(v("invalid argument. Callback argument must be a function. Value: `%s`.",i));if(f){if(!V(e))throw new TypeError(v("invalid argument. Options argument must be an object. Value: `%s`.",e));if(O(e,"dtype")?s=e.dtype:s=T(r),O(e,"order")){if(!F(e.order))throw new TypeError(v("invalid option. `%s` option must be a recognized order. Option: `%s`.","order",e.order));n=e.order}}else s=T(r);if(c=P(s),c===null)throw new TypeError(v("invalid option. `%s` option must be a recognized data type. Option: `%s`.","dtype",e.dtype));for(n===void 0&&(n=L(r)),l=I(r),o=N(l),p=l.length,n==="row-major"?h=p-1:h=0,u=D(p),t={length:0},d=0;d<o;d++)d>0&&(u=S(l,n,u,h,u)),q=r.get.apply(r,u),i.call(m,q,u.slice(),r)&&(t[t.length]=q,t.length+=1);return o=t.length,y=new c(o),A(o,t,1,y,1),w=new r.constructor(s,y,[o],[1],0,n),w}E.exports=B});var G=j();module.exports=G;
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
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../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// MODULES //\n\nvar isPlainObject = require( '@stdlib/assert-is-plain-object' );\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isndarrayLike = require( '@stdlib/assert-is-ndarray-like' );\nvar isOrder = require( '@stdlib/ndarray-base-assert-is-order' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar ctors = require( '@stdlib/ndarray-base-buffer-ctors' );\nvar zeros = require( '@stdlib/array-base-zeros' );\nvar getShape = require( '@stdlib/ndarray-shape' );\nvar getDType = require( '@stdlib/ndarray-dtype' );\nvar getOrder = require( '@stdlib/ndarray-order' );\nvar numel = require( '@stdlib/ndarray-base-numel' );\nvar nextCartesianIndex = require( '@stdlib/ndarray-base-next-cartesian-index' ).assign;\nvar gcopy = require( '@stdlib/blas-base-gcopy' );\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Returns a shallow copy of an ndarray containing only those elements which pass a test implemented by a predicate function.\n*\n* @param {ndarray} x - input ndarray\n* @param {Options} [options] - function options\n* @param {string} [options.dtype] - output array data type\n* @param {boolean} [options.order] - index iteration order\n* @param {Callback} predicate - predicate function\n* @param {*} [thisArg] - predicate execution context\n* @throws {TypeError} first argument must be an ndarray-like object\n* @throws {TypeError} callback argument must be a function\n* @throws {TypeError} options argument must be an object\n* @returns {ndarray} output ndarray\n*\n* @example\n* var isEven = require( '@stdlib/assert-is-even' ).isPrimitive;\n* var Float64Array = require( '@stdlib/array-float64' );\n* var ndarray = require( '@stdlib/ndarray-ctor' );\n*\n* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );\n* var shape = [ 2, 3 ];\n* var strides = [ 6, 1 ];\n* var offset = 1;\n*\n* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );\n* // returns <ndarray>\n*\n* var y = filter( x, isEven );\n* // returns <ndarray>[ 2.0, 4.0, 8.0, 10.0 ]\n*/\nfunction filter( x, options, predicate, thisArg ) {\n\tvar hasOpts;\n\tvar ndims;\n\tvar cache;\n\tvar clbk;\n\tvar opts;\n\tvar ctor;\n\tvar ctx;\n\tvar ord;\n\tvar dim;\n\tvar idx;\n\tvar buf;\n\tvar dt;\n\tvar sh;\n\tvar N;\n\tvar y;\n\tvar v;\n\tvar i;\n\tif ( !isndarrayLike( x ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );\n\t}\n\tif ( arguments.length < 3 ) {\n\t\tclbk = options;\n\t} else if ( arguments.length === 3 ) {\n\t\tif ( isFunction( options ) ) {\n\t\t\tclbk = options;\n\t\t\tctx = predicate;\n\t\t} else {\n\t\t\thasOpts = true;\n\t\t\topts = options;\n\t\t\tclbk = predicate;\n\t\t}\n\t} else {\n\t\thasOpts = true;\n\t\topts = options;\n\t\tclbk = predicate;\n\t\tctx = thisArg;\n\t}\n\tif ( !isFunction( clbk ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) );\n\t}\n\tif ( hasOpts ) {\n\t\tif ( !isPlainObject( opts ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) );\n\t\t}\n\t\tif ( hasOwnProp( opts, 'dtype' ) ) {\n\t\t\tdt = opts.dtype;\n\t\t} else {\n\t\t\tdt = getDType( x );\n\t\t}\n\t\tif ( hasOwnProp( opts, 'order' ) ) {\n\t\t\tif ( !isOrder( opts.order ) ) {\n\t\t\t\tthrow new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', opts.order ) );\n\t\t\t}\n\t\t\tord = opts.order;\n\t\t}\n\t} else {\n\t\tdt = getDType( x );\n\t}\n\t// Resolve an output array buffer constructor:\n\tctor = ctors( dt );\n\tif ( ctor === null ) {\n\t\t// The only way we should get here is if the user provided an unsupported data type, as `getDType` should error if the input array has an unrecognized/unsupported data type...\n\t\tthrow new TypeError( format( 'invalid option. `%s` option must be a recognized data type. Option: `%s`.', 'dtype', opts.dtype ) );\n\t}\n\t// Resolve the iteration order:\n\tif ( ord === void 0 ) {\n\t\tord = getOrder( x );\n\t}\n\t// Resolve the input array shape:\n\tsh = getShape( x );\n\n\t// Compute the number of array elements:\n\tN = numel( sh );\n\n\t// Retrieve the number of dimensions:\n\tndims = sh.length;\n\n\t// Resolve the dimension in which indices should iterate fastest:\n\tif ( ord === 'row-major' ) {\n\t\tdim = ndims - 1;\n\t} else { // ord === 'column-major'\n\t\tdim = 0;\n\t}\n\t// Initialize an index array workspace:\n\tidx = zeros( ndims );\n\n\t// Initialize a value cache for those elements which pass a predicate function (note: unfortunately, we use an associative array here, as no other good options. If we use a \"generic\" array, we are limited to 2^32-1 elements. If we allocate, say, a Float64Array buffer for storing indices, we risk materializing lazily-materialized input ndarray values again (e.g., lazy accessor ndarray), which could be expensive. If we allocate a workspace buffer of equal size to the input ndarray to store materialized values, we'd then need to perform another copy in order to shrink the buffer, as, otherwise, could be holding on to significantly more memory than needed for the returned ndarray. There are likely other options, but all involve complexity, so the simplest option is used here.):\n\tcache = {\n\t\t'length': 0\n\t};\n\n\t// Filter elements according to a predicate function...\n\tfor ( i = 0; i < N; i++ ) {\n\t\tif ( i > 0 ) {\n\t\t\tidx = nextCartesianIndex( sh, ord, idx, dim, idx );\n\t\t}\n\t\tv = x.get.apply( x, idx );\n\t\tif ( clbk.call( ctx, v, idx.slice(), x ) ) {\n\t\t\tcache[ cache.length ] = v;\n\t\t\tcache.length += 1;\n\t\t}\n\t}\n\t// Retrieve the number of cached elements:\n\tN = cache.length;\n\n\t// Allocate an output array buffer:\n\tbuf = new ctor( N );\n\n\t// Copy cached elements to the output array buffer:\n\tgcopy( N, cache, 1, buf, 1 );\n\n\t// Create an output ndarray:\n\ty = new x.constructor( dt, buf, [ N ], [ 1 ], 0, ord );\n\n\treturn y;\n}\n\n\n// EXPORTS //\n\nmodule.exports = filter;\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* Return a shallow copy of an ndarray containing only those elements which pass a test implemented by a predicate function.\n*\n* @module @stdlib/ndarray-filter\n*\n* @example\n* var isEven = require( '@stdlib/assert-is-even' ).isPrimitive;\n* var Float64Array = require( '@stdlib/array-float64' );\n* var ndarray = require( '@stdlib/ndarray-ctor' );\n* var filter = require( '@stdlib/ndarray-filter' );\n*\n* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );\n* var shape = [ 2, 3 ];\n* var strides = [ 6, 1 ];\n* var offset = 1;\n*\n* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );\n* // returns <ndarray>\n*\n* var y = filter( x, isEven );\n* // returns <ndarray>[ 2.0, 4.0, 8.0, 10.0 ]\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,cAsBA,IAAIC,EAAgB,QAAS,gCAAiC,EAC1DC,EAAa,QAAS,4BAA6B,EACnDC,EAAgB,QAAS,gCAAiC,EAC1DC,EAAU,QAAS,sCAAuC,EAC1DC,EAAa,QAAS,iCAAkC,EACxDC,EAAQ,QAAS,mCAAoC,EACrDC,EAAQ,QAAS,0BAA2B,EAC5CC,EAAW,QAAS,uBAAwB,EAC5CC,EAAW,QAAS,uBAAwB,EAC5CC,EAAW,QAAS,uBAAwB,EAC5CC,EAAQ,QAAS,4BAA6B,EAC9CC,EAAqB,QAAS,2CAA4C,EAAE,OAC5EC,EAAQ,QAAS,yBAA0B,EAC3CC,EAAS,QAAS,uBAAwB,EAmC9C,SAASC,EAAQC,EAAGC,EAASC,EAAWC,EAAU,CACjD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACJ,GAAK,CAACjC,EAAea,CAAE,EACtB,MAAM,IAAI,UAAWF,EAAQ,gFAAiFE,CAAE,CAAE,EAmBnH,GAjBK,UAAU,OAAS,EACvBO,EAAON,EACI,UAAU,SAAW,EAC3Bf,EAAYe,CAAQ,GACxBM,EAAON,EACPS,EAAMR,IAENE,EAAU,GACVI,EAAOP,EACPM,EAAOL,IAGRE,EAAU,GACVI,EAAOP,EACPM,EAAOL,EACPQ,EAAMP,GAEF,CAACjB,EAAYqB,CAAK,EACtB,MAAM,IAAI,UAAWT,EAAQ,uEAAwES,CAAK,CAAE,EAE7G,GAAKH,EAAU,CACd,GAAK,CAACnB,EAAeuB,CAAK,EACzB,MAAM,IAAI,UAAWV,EAAQ,qEAAsEU,CAAK,CAAE,EAO3G,GALKnB,EAAYmB,EAAM,OAAQ,EAC9BO,EAAKP,EAAK,MAEVO,EAAKtB,EAAUO,CAAE,EAEbX,EAAYmB,EAAM,OAAQ,EAAI,CAClC,GAAK,CAACpB,EAASoB,EAAK,KAAM,EACzB,MAAM,IAAI,UAAWV,EAAQ,wEAAyE,QAASU,EAAK,KAAM,CAAE,EAE7HG,EAAMH,EAAK,KACZ,CACD,MACCO,EAAKtB,EAAUO,CAAE,EAIlB,GADAS,EAAOnB,EAAOyB,CAAG,EACZN,IAAS,KAEb,MAAM,IAAI,UAAWX,EAAQ,4EAA6E,QAASU,EAAK,KAAM,CAAE,EA8BjI,IA3BKG,IAAQ,SACZA,EAAMjB,EAAUM,CAAE,GAGnBgB,EAAKxB,EAAUQ,CAAE,EAGjBiB,EAAItB,EAAOqB,CAAG,EAGdX,EAAQW,EAAG,OAGNL,IAAQ,YACZC,EAAMP,EAAQ,EAEdO,EAAM,EAGPC,EAAMtB,EAAOc,CAAM,EAGnBC,EAAQ,CACP,OAAU,CACX,EAGMc,EAAI,EAAGA,EAAIH,EAAGG,IACdA,EAAI,IACRP,EAAMjB,EAAoBoB,EAAIL,EAAKE,EAAKD,EAAKC,CAAI,GAElDM,EAAInB,EAAE,IAAI,MAAOA,EAAGa,CAAI,EACnBN,EAAK,KAAMG,EAAKS,EAAGN,EAAI,MAAM,EAAGb,CAAE,IACtCM,EAAOA,EAAM,MAAO,EAAIa,EACxBb,EAAM,QAAU,GAIlB,OAAAW,EAAIX,EAAM,OAGVQ,EAAM,IAAIL,EAAMQ,CAAE,EAGlBpB,EAAOoB,EAAGX,EAAO,EAAGQ,EAAK,CAAE,EAG3BI,EAAI,IAAIlB,EAAE,YAAae,EAAID,EAAK,CAAEG,CAAE,EAAG,CAAE,CAAE,EAAG,EAAGN,CAAI,EAE9CO,CACR,CAKAlC,EAAO,QAAUe,IClJjB,IAAIsB,EAAO,IAKX,OAAO,QAAUA",
6
+ "names": ["require_main", "__commonJSMin", "exports", "module", "isPlainObject", "isFunction", "isndarrayLike", "isOrder", "hasOwnProp", "ctors", "zeros", "getShape", "getDType", "getOrder", "numel", "nextCartesianIndex", "gcopy", "format", "filter", "x", "options", "predicate", "thisArg", "hasOpts", "ndims", "cache", "clbk", "opts", "ctor", "ctx", "ord", "dim", "idx", "buf", "dt", "sh", "N", "y", "v", "i", "main"]
7
+ }