From 29fc978dd69ec2a103dcd775dcfcc031fa536737 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 26 Apr 2024 01:33:11 +0000 Subject: [PATCH] Auto-generated commit --- .gitattributes | 29 +- CONTRIBUTORS | 1 + base/mskreject-map/README.md | 174 ++++++++++ .../benchmark/benchmark.assign.length.js | 106 ++++++ base/mskreject-map/benchmark/benchmark.js | 57 ++++ .../benchmark/benchmark.length.js | 99 ++++++ base/mskreject-map/docs/repl.txt | 87 +++++ base/mskreject-map/docs/types/index.d.ts | 152 +++++++++ base/mskreject-map/docs/types/test.ts | 56 ++++ base/mskreject-map/examples/index.js | 41 +++ base/mskreject-map/lib/assign.js | 237 +++++++++++++ base/mskreject-map/lib/index.js | 67 ++++ base/mskreject-map/lib/main.js | 69 ++++ base/mskreject-map/package.json | 65 ++++ base/mskreject-map/test/test.assign.js | 311 ++++++++++++++++++ base/mskreject-map/test/test.js | 41 +++ base/mskreject-map/test/test.main.js | 114 +++++++ 17 files changed, 1700 insertions(+), 6 deletions(-) create mode 100644 base/mskreject-map/README.md create mode 100644 base/mskreject-map/benchmark/benchmark.assign.length.js create mode 100644 base/mskreject-map/benchmark/benchmark.js create mode 100644 base/mskreject-map/benchmark/benchmark.length.js create mode 100644 base/mskreject-map/docs/repl.txt create mode 100644 base/mskreject-map/docs/types/index.d.ts create mode 100644 base/mskreject-map/docs/types/test.ts create mode 100644 base/mskreject-map/examples/index.js create mode 100644 base/mskreject-map/lib/assign.js create mode 100644 base/mskreject-map/lib/index.js create mode 100644 base/mskreject-map/lib/main.js create mode 100644 base/mskreject-map/package.json create mode 100644 base/mskreject-map/test/test.assign.js create mode 100644 base/mskreject-map/test/test.js create mode 100644 base/mskreject-map/test/test.main.js diff --git a/.gitattributes b/.gitattributes index 10a16e6f..1c88e69c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -40,10 +40,27 @@ *.mov binary # Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false +/lib/node_modules/** -linguist-vendored -linguist-generated -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false +# Configure directories which should *not* be included in GitHub language statistics: +/deps/** linguist-vendored +/dist/** linguist-generated +/workshops/** linguist-vendored + +benchmark/** linguist-vendored +docs/* linguist-documentation +etc/** linguist-vendored +examples/** linguist-documentation +scripts/** linguist-vendored +test/** linguist-vendored +tools/** linguist-vendored + +# Configure files which should *not* be included in GitHub language statistics: +Makefile linguist-vendored +*.mk linguist-vendored +*.jl linguist-vendored +*.py linguist-vendored +*.R linguist-vendored + +# Configure files which should be included in GitHub language statistics: +docs/types/*.d.ts -linguist-documentation diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 05708915..fcfcfa23 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -19,6 +19,7 @@ Chinmay Joshi <86140365+JawHawk@users.noreply.github.com> Christopher Dambamuromo Dan Rose Daniel Killenberger +Daniel Yu <40680511+Daniel777y@users.noreply.github.com> Dominik Moritz Dorrin Sotoudeh EuniceSim142 <77243938+EuniceSim142@users.noreply.github.com> diff --git a/base/mskreject-map/README.md b/base/mskreject-map/README.md new file mode 100644 index 00000000..ececbf93 --- /dev/null +++ b/base/mskreject-map/README.md @@ -0,0 +1,174 @@ + + +# mskrejectMap + +> Apply a mask to a provided input array and map the unmasked values according to a callback function. + +
+ +## Usage + +```javascript +var mskrejectMap = require( '@stdlib/array/base/mskreject-map' ); +``` + +#### mskrejectMap( x, mask, clbk\[, thisArg ] ) + +Returns a new array by applying a mask and mapping the unmasked values according to a callback function. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +function clbk( val ) { + return val * 2; +} + +var y = mskrejectMap( x, [ 0, 1, 0, 1 ], clbk ); +// returns [ 2, 6 ] +``` + +The function supports the following parameters: + +- **x**: input array. +- **mask**: mask array. +- **clbk**: function to apply. +- **thisArg**: applied function execution context (_optional_). + +The `clbk` function is provided three arguments: + +- **value**: current unmasked array element. +- **index**: current unmasked array element index. +- **arr**: input array. + +To set the `clbk` function execution context, provide a `thisArg`. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +var mask = [ 0, 1, 0, 1 ]; + +var increase = 3; + +function clbk( value ) { + return value + this; +} + +var out = mskrejectMap( x, mask, clbk, increase ); +// returns [ 4, 6 ] +``` + +The function **always** returns a new "generic" array. + +#### mskrejectMap.assign( x, mask, out, stride, offset, clbk\[, thisArg ] ) + +Applies a mask to a provided input array, maps the unmasked values according to a callback function, and assigns to elements in a provided output array. + +```javascript +var x = [ 1, 2, 3, 4 ]; + +var mask = [ 1, 0, 1, 0 ]; + +var out = [ 0, 0, 0, 0 ]; + +function clbk( val ) { + return val * 2; +} + +var arr = mskrejectMap.assign( x, mask, out, -2, out.length-1, clbk ); +// returns [ 0, 8, 0, 4 ] + +var bool = ( arr === out ); +// returns true +``` + +The function supports the following parameters: + +- **x**: input array. +- **mask**: mask array. +- **out**: output array. +- **stride**: output array stride. +- **offset**: output array offset. +- **clbk**: function to apply. +- **thisArg**: applied function execution context (_optional_). + +
+ + + +
+ +## Notes + +- If a `mask` array element is falsy, the corresponding element in `x` is **mapped** in the output array; otherwise, the corresponding element in `x` is "masked" and thus **excluded** from the output array. + +
+ + + +
+ +## Examples + + + +```javascript +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var mskrejectMap = require( '@stdlib/array/base/mskreject-map' ); + +// Generate a linearly spaced array: +var x = zeroTo( 20 ); +console.log( x ); + +// Generate a random mask: +var mask = bernoulli( x.length, 0.5, { + 'dtype': 'generic' +}); +console.log( mask ); + +function clbk( val ) { + return val * 2; +} + +// Filter an array using the mask: +var y = mskrejectMap( x, mask, clbk ); +console.log( y ); +``` + +
+ + + + + + + + + + + + + + diff --git a/base/mskreject-map/benchmark/benchmark.assign.length.js b/base/mskreject-map/benchmark/benchmark.assign.length.js new file mode 100644 index 00000000..732ca5ee --- /dev/null +++ b/base/mskreject-map/benchmark/benchmark.assign.length.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var zeroTo = require( './../../../base/zero-to' ); +var zeros = require( './../../../zeros' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var mskrejectMap = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var mask; + var out; + var x; + + x = zeroTo( len, 'generic' ); + mask = zeros( len, 'generic' ); + out = zeros( len, 'generic' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskrejectMap.assign( x, mask, out, 1, 0, function clbk( val ) { + return val * 2; + } ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) || isnan( v[ i%len ] ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':assign:len='+len, f ); + } +} + +main(); diff --git a/base/mskreject-map/benchmark/benchmark.js b/base/mskreject-map/benchmark/benchmark.js new file mode 100644 index 00000000..b52a738c --- /dev/null +++ b/base/mskreject-map/benchmark/benchmark.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isArray = require( '@stdlib/assert/is-array' ); +var zeroTo = require( './../../../base/zero-to' ); +var zeros = require( './../../../base/zeros' ); +var pkg = require( './../package.json' ).name; +var mskrejectMap = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::copy:len=100', function benchmark( b ) { + var x; + var y; + var i; + var v; + + x = zeroTo( 100 ); + y = zeros( x.length ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskrejectMap( x, y, function clbk( val ) { + return val * 2; + } ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/mskreject-map/benchmark/benchmark.length.js b/base/mskreject-map/benchmark/benchmark.length.js new file mode 100644 index 00000000..b51c7c01 --- /dev/null +++ b/base/mskreject-map/benchmark/benchmark.length.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var zeroTo = require( './../../../base/zero-to' ); +var zeros = require( './../../../base/zeros' ); +var isArray = require( '@stdlib/assert/is-array' ); +var pkg = require( './../package.json' ).name; +var mskrejectMap = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = zeroTo( len ); + var y = zeros( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = mskrejectMap( x, y, function clbk( val ) { + return val * 2; + } ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/base/mskreject-map/docs/repl.txt b/base/mskreject-map/docs/repl.txt new file mode 100644 index 00000000..a55f0d5d --- /dev/null +++ b/base/mskreject-map/docs/repl.txt @@ -0,0 +1,87 @@ + +{{alias}}( x, mask, clbk[, thisArg] ) + Returns a new array by applying a mask to a provided input array + and mapping the unmasked values according to a callback function. + + If a mask array element is falsy, the corresponding element in `x` is + mapped in the output array; otherwise, the corresponding element in `x` is + "masked" and thus excluded from the output array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + mask: ArrayLikeObject + Mask array. + + clbk: Function + Mapping function. + + thisArg: any (optional) + Execution context. + + Returns + ------- + out: Array + Output array. + + Examples + -------- + > var x = [ 1, 2, 3, 4 ]; + > function clbk( val ) { return val * 2; } + > var y = {{alias}}( x, [ 0, 1, 0, 1 ], clbk ) + [ 2, 6 ] + + +{{alias}}.assign( x, mask, out, stride, offset, clbk[, thisArg] ) + Applies a mask to a provided input array, maps the unmasked values + according to a callback function, and assigns the unmasked values + to elements in a provided output array. + + If a mask array element is falsy, the corresponding element in `x` is + mapped and included in the output array; otherwise, the corresponding + element in `x` is "masked" and thus excluded from the output array. + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + mask: ArrayLikeObject + Mask array. + + out: ArrayLikeObject + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array offset. + + clbk: Function + Mapping function. + + thisArg: any (optional) + Execution context. + + Returns + ------- + out: ArrayLikeObject + Output array. + + Examples + -------- + > var x = [ 1, 2, 3, 4 ]; + > var m = [ 0, 1, 0, 1 ]; + > var out = [ 0, 0, 0, 0 ]; + > var scale = 5; + > function clbk( val ) { return val * this; } + > var arr = {{alias}}.assign( x, m, out, 2, 0, clbk, scale ) + [ 5, 0, 15, 0 ] + > var bool = ( arr === out ) + true + + See Also + -------- diff --git a/base/mskreject-map/docs/types/index.d.ts b/base/mskreject-map/docs/types/index.d.ts new file mode 100644 index 00000000..b5d77563 --- /dev/null +++ b/base/mskreject-map/docs/types/index.d.ts @@ -0,0 +1,152 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Returns a transformed element. +* +* @param value - current array element +* @returns transformed array element +*/ +type Unary = ( this: U, value: T ) => V; + +/** +* Returns a transformed element. +* +* @param value - current array element +* @param index - current array element index +* @returns transformed array element +*/ +type Binary = ( this: U, value: T, index: number ) => V; + +/** +* Returns a transformed element. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +* @returns transformed array element +*/ +type Ternary = ( this: U, value: T, index: number, arr: Collection | AccessorArrayLike ) => V; + +/** +* Returns a transformed element. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +* @returns transformed array element +*/ +type Callback = Unary | Binary | Ternary; + +/** +* Interface describing `mskrejectMap`. +*/ +interface MskrejectMap { + /** + * Returns a new array by applying a mask and mapping the unmasked values according to a callback function. + * + * @param x - input array + * @param mask - mask array + * @param clbk - mapping function + * @param thisArg - function context + * @returns output array + * + * @example + * var x = [ 1, 2, 3, 4 ]; + * + * var y = mskrejectMap( x, [ 0, 1, 0, 1 ], function( val ) { + * return val * 2; + * } ); + * // returns [ 2, 6 ] + */ + ( x: Collection | AccessorArrayLike, mask: Collection, clbk: Callback, thisArg?: U ): Array; + + /** + * Applies a mask to a provided input array, maps the unmasked values according to a callback function, and assigns to elements in a provided output array. + * + * @param x - input array + * @param mask - mask array + * @param out - output array + * @param stride - output array stride + * @param offset - output array offset + * @param clbk - mapping function + * @param thisArg - function context + * @returns output array + * + * @example + * var x = [ 1, 2, 3, 4 ]; + * var mask = [ 1, 0, 1, 0 ]; + * var out = [ 0, 0, 0, 0 ]; + * + * function clbk( val ) { + * return val * 2; + * } + * + * var arr = mskrejectMap.assign( x, mask, out, -2, out.length-1, clbk ); + * // returns [ 0, 8, 0, 4 ] + * + * var bool = ( arr === out ); + * // returns true + */ + assign( x: Collection | AccessorArrayLike, mask: Collection, out: Collection, stride: number, offset: number, clbk: Callback, thisArg?: U ): Collection; +} + +/** +* Returns a new array by applying a mask and mapping the unmasked values according to a callback function. +* +* @param x - input array +* @param mask - mask array +* @param clbk - mapping function +* @param thisArg - function context +* @returns output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* +* var y = mskrejectMap( x, [ 0, 1, 0, 1 ], function( val ) { +* return val * 2; +* } ); +* // returns [ 2, 6 ] +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 1, 0, 1, 0 ]; +* var out = [ 0, 0, 0, 0 ]; +* +* function clbk( val ) { +* return val * 2; +* } +* +* var arr = mskrejectMap.assign( x, mask, out, -2, out.length-1, clbk ); +* // returns [ 0, 8, 0, 4 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +declare var mskrejectMap: MskrejectMap; + + +// EXPORTS // + +export = mskrejectMap; diff --git a/base/mskreject-map/docs/types/test.ts b/base/mskreject-map/docs/types/test.ts new file mode 100644 index 00000000..0328f60c --- /dev/null +++ b/base/mskreject-map/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import mskrejectMap from './index'; + +// TESTS // + +// The function returns an array... +{ + mskrejectMap( [ 1, 2, 3, 4 ], [ 0, 0, 0, 0 ], function( val ) { return val * 2 } ); // $ExpectType number[] + mskrejectMap( [ 1, 2, 3, 4 ], [ 0, 0, 0, 0 ], function( val ) { return val * 2 } ); // $ExpectType any[] + mskrejectMap( [ 1, 2, 3, 4 ], [ 0, 0, 0, 0 ], function( val ) { return val * 2 } ); // $ExpectType number[] + mskrejectMap( [ '1', '2', '3', '4' ], [ 0, 0, 0, 0 ], function( val ) { return val } ); // $ExpectType string[] +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object... +{ + mskrejectMap( 1, [ 0, 0 ] ); // $ExpectError + mskrejectMap( true, [ 0, 0 ] ); // $ExpectError + mskrejectMap( false, [ 0, 0 ] ); // $ExpectError + mskrejectMap( null, [ 0, 0 ] ); // $ExpectError + mskrejectMap( void 0, [ 0, 0 ] ); // $ExpectError + mskrejectMap( {}, [ 0, 0 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... +{ + mskrejectMap( [], 1 ); // $ExpectError + mskrejectMap( [], true ); // $ExpectError + mskrejectMap( [], false ); // $ExpectError + mskrejectMap( [], null ); // $ExpectError + mskrejectMap( [], void 0 ); // $ExpectError + mskrejectMap( [], {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + mskrejectMap(); // $ExpectError + mskrejectMap( [] ); // $ExpectError + mskrejectMap( [], [], [] ); // $ExpectError +} diff --git a/base/mskreject-map/examples/index.js b/base/mskreject-map/examples/index.js new file mode 100644 index 00000000..6d03159b --- /dev/null +++ b/base/mskreject-map/examples/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var zeroTo = require( './../../../base/zero-to' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var mskrejectMap = require( './../lib' ); + +// Generate a linearly spaced array: +var x = zeroTo( 20 ); +console.log( x ); + +// Generate a random mask: +var mask = bernoulli( x.length, 0.5, { + 'dtype': 'generic' +}); +console.log( mask ); + +function clbk( val ) { + return val * 2; +} + +// Filter an array using the mask: +var y = mskrejectMap( x, mask, clbk ); +console.log( y ); diff --git a/base/mskreject-map/lib/assign.js b/base/mskreject-map/lib/assign.js new file mode 100644 index 00000000..8f101576 --- /dev/null +++ b/base/mskreject-map/lib/assign.js @@ -0,0 +1,237 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isComplexDataType = require( './../../../base/assert/is-complex-floating-point-data-type' ); +var arraylike2object = require( './../../../base/arraylike2object' ); +var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); + + +// FUNCTIONS // + +/** +* Applies a mask to an indexed array, maps the unmasked values according to a callback function, and assigns to elements in an indexed output array. +* +* @private +* @param {Collection} x - input array +* @param {IntegerArray} mask - mask array +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} clbk - function to apply +* @param {*} [thisArg] - function context +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = [ 0, 0, 0, 0 ]; +* +* var arr = indexed( x, mask, out, 1, 0, function( val ) { +* return val * 2; +* } ); +* // returns [ 2, 6, 0, 0 ] +*/ +function indexed( x, mask, out, stride, offset, clbk, thisArg ) { + var io; + var i; + + io = offset; + for ( i = 0; i < x.length; i++ ) { + if ( !mask[ i ] ) { + out[ io ] = clbk.call(thisArg, x[ i ], i, x ); + io += stride; + } + } + return out; +} + +/** +* Applies a mask to an accessor array, maps the unmasked values according to a callback function, and assigns to elements in an accessor output array. +* +* @private +* @param {Object} x - input array object +* @param {Object} mask - mask array object +* @param {Object} out - output array object +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} clbk - function to apply +* @param {*} [thisArg] - function context +* @returns {Collection} output array +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 1, 2, 3, 4 ] ); +* var mask = toAccessorArray( [ 0, 1, 0, 1 ] ); +* +* var out = toAccessorArray( [ 0, 0, 0, 0 ] ); +* var arr = accessors( arraylike2object( x ), arraylike2object( mask ), arraylike2object( out ), 1, 0, function( val ) { +* return val * 2; +* } ); +* +* var v = arr.get( 0 ); +* // returns 2 +* +* v = arr.get( 1 ); +* // returns 6 +*/ +function accessors( x, mask, out, stride, offset, clbk, thisArg ) { + var xdata; + var mdata; + var odata; + var xget; + var mget; + var oset; + var io; + var i; + + xdata = x.data; + mdata = mask.data; + odata = out.data; + + xget = x.accessors[ 0 ]; + mget = mask.accessors[ 0 ]; + oset = out.accessors[ 1 ]; + + io = offset; + for ( i = 0; i < xdata.length; i++ ) { + if ( !mget( mdata, i ) ) { + oset( odata, io, clbk.call( thisArg, xget( xdata, i ), i, xdata ) ); + io += stride; + } + } + return odata; +} + +/** +* Applies a mask to a complex array, maps the unmasked values according to a callback function, and assigns to elements in a complex output array. +* +* @private +* @param {Collection} x - real-valued floating-point input array view +* @param {Object} mask - mask array object +* @param {Collection} out - real-valued floating-point output array view +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} clbk - function to apply +* @param {*} [thisArg] - function context +* @returns {Collection} output array view +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = new Float64Array( 8 ); +* +* var arr = complex( x, arraylike2object( mask ), out, 1, 0, function( val ) { +* return val * 2; +* } ); +* // returns [ 2.0, 4.0, 10.0, 12.0, 0.0, 0.0, 0.0, 0.0 ] +*/ +function complex( x, mask, out, stride, offset, clbk, thisArg ) { + var mdata; + var mget; + var io; + var so; + var i; + var j; + + mdata = mask.data; + mget = mask.accessors[ 0 ]; + + so = stride * 2; // note: multiply by 2, as real-valued array consists of interleaved real and imaginary components + io = offset * 2; + for ( i = 0; i < mdata.length; i++ ) { + if ( !mget( mdata, i ) ) { + j = i * 2; + out[ io ] = clbk.call( thisArg, x[ j ], j, x ); + out[ io+1 ] = clbk.call( thisArg, x[ j+1 ], j+1, x ); + io += so; + } + } + return out; +} + + +// MAIN // + +/** +* Applies a mask to a provided input array, maps the unmasked values according to a callback function, and assigns to elements in a provided output array. +* +* @param {Collection} x - input array +* @param {Collection} mask - mask array +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @param {Function} clbk - function to apply +* @param {*} [thisArg] - function context +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = [ 0, 0 ]; +* var arr = assign( x, mask, out, 1, 0, function( val ) { +* return val * 2; +* } ); +* // returns [ 2, 6 ] +* +* var bool = ( arr === out ); +* // returns true +*/ +function assign( x, mask, out, stride, offset, clbk, thisArg ) { + var xo; + var mo; + var oo; + + xo = arraylike2object( x ); + mo = arraylike2object( mask ); + oo = arraylike2object( out ); + if ( + xo.accessorProtocol || + mo.accessorProtocol || + oo.accessorProtocol + ) { + // Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland... + if ( + isComplexDataType( xo.dtype ) && + isComplexDataType( oo.dtype ) + ) { + complex( reinterpret( x, 0 ), mo, reinterpret( out, 0 ), stride, offset, clbk, thisArg ); // eslint-disable-line max-len + return out; + } + accessors( xo, mo, oo, stride, offset, clbk, thisArg ); + return out; + } + indexed( x, mask, out, stride, offset, clbk, thisArg ); + return out; +} + + +// EXPORTS // + +module.exports = assign; diff --git a/base/mskreject-map/lib/index.js b/base/mskreject-map/lib/index.js new file mode 100644 index 00000000..6d7da218 --- /dev/null +++ b/base/mskreject-map/lib/index.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Apply a mask to a provided input array and map the unmasked values according to a callback function. +* +* @module @stdlib/array/base/mskreject-map +* +* @example +* var mskrejectMap = require( '@stdlib/array/base/mskreject-map' ); +* +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var y = mskrejectMap( x, mask, function( val ) { +* return val * 2; +* } ); +* // returns [ 2, 6 ] +* +* @example +* var mskrejectMap = require( '@stdlib/array/base/mskreject-map' ); +* +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = [ 0, 0 ]; +* var arr = mskrejectMap.assign( x, mask, out, 1, 0, function( val ) { +* return val + this; +* }, 5 ); +* // returns [ 6, 8 ] +* +* var bool = ( arr === out ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/mskreject-map/lib/main.js b/base/mskreject-map/lib/main.js new file mode 100644 index 00000000..e8e56c16 --- /dev/null +++ b/base/mskreject-map/lib/main.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolveGetter = require( './../../../base/resolve-getter' ); + + +// MAIN // + +/** +* Returns a new array by applying a mask to a provided input array and mapping the unmasked values according to a callback function. +* +* @param {Collection} x - input array +* @param {Collection} mask - mask array +* @param {Function} clbk - function to apply +* @param {*} [thisArg] - function context +* @returns {Array} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var y = mskrejectMap( x, mask, function( val ) { +* return val * 2; +* } ); +* // returns [ 2, 6 ] +*/ +function mskrejectMap( x, mask, clbk, thisArg ) { + var xget; + var mget; + var out; + var i; + + // Resolve accessors for retrieving array elements: + xget = resolveGetter( x ); + mget = resolveGetter( mask ); + + // Extract each desired element from the provided array... + out = []; + for ( i = 0; i < x.length; i++ ) { + if ( !mget( mask, i ) ) { + out.push( clbk.call( thisArg, xget( x, i ), i, x ) ); // use `Array#push` to ensure "fast" elements + } + } + return out; +} + + +// EXPORTS // + +module.exports = mskrejectMap; diff --git a/base/mskreject-map/package.json b/base/mskreject-map/package.json new file mode 100644 index 00000000..1a1f7650 --- /dev/null +++ b/base/mskreject-map/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/array/base/mskreject-map", + "version": "0.0.0", + "description": "Apply a mask to a provided input array and map the unmasked values according to a callback function.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "utilities", + "utils", + "generic", + "array", + "take", + "extract", + "copy", + "index", + "mask", + "reject", + "filter" + ] +} diff --git a/base/mskreject-map/test/test.assign.js b/base/mskreject-map/test/test.assign.js new file mode 100644 index 00000000..387e162e --- /dev/null +++ b/base/mskreject-map/test/test.assign.js @@ -0,0 +1,311 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Complex64Array = require( './../../../complex64' ); +var toAccessorArray = require( './../../../base/to-accessor-array' ); +var Int32Array = require( './../../../int32' ); +var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); +var zeros = require( './../../../zeros' ); +var mskrejectMap = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mskrejectMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function rejects array elements (generic)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = [ 1, 2, 3, 4 ]; + + function clbk( val ) { + return val * 2; + } + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 2, 'generic' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + expected = [ 4, 8 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + out = zeros( 0, 'generic' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + expected = []; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 0 ]; + out = zeros( 1, 'generic' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + expected = [ 8 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + out = zeros( 4, 'generic' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + expected = [ 2, 4, 6, 8 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 4, 'generic' ); + actual = mskrejectMap( x, mask, out, -2, out.length-1, clbk ); + expected = [ 0, 8, 0, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function rejects array elements (real typed array)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var inc = 5; + var x; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + + function clbk( val ) { + return val + this; // eslint-disable-line no-invalid-this + } + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 2, 'int32' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk, inc ); + expected = new Int32Array( [ 7, 9 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + out = zeros( 0, 'int32' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk, inc ); + expected = new Int32Array( [] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 0 ]; + out = zeros( 1, 'int32' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk, inc ); + expected = new Int32Array( [ 9 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + out = zeros( 4, 'int32' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk, inc ); + expected = new Int32Array( [ 6, 7, 8, 9 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 4, 'int32' ); + actual = mskrejectMap( x, mask, out, -2, out.length-1, clbk, inc ); + expected = new Int32Array( [ 0, 9, 0, 7 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function rejects array elements (complex typed array)', function test( t ) { + var expected; + var actual; + var scale = 5; + var mask; + var out; + var x; + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + function clbk( val ) { + return val * this; // eslint-disable-line no-invalid-this + } + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 2, 'complex64' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk, scale ); + expected = new Complex64Array( [ scale * 3.0, scale * 4.0, scale * 7.0, scale * 8.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + out = zeros( 0, 'complex64' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk, scale ); + expected = new Complex64Array( [] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 1, 1, 0 ]; + out = zeros( 1, 'complex64' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk, scale ); + expected = new Complex64Array( [ scale * 7.0, scale * 8.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + out = zeros( 4, 'complex64' ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk, scale ); + expected = new Complex64Array( [ scale * 1.0, scale * 2.0, scale * 3.0, scale * 4.0, scale * 5.0, scale * 6.0, scale * 7.0, scale * 8.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 0, 1, 0 ]; + out = zeros( 4, 'complex64' ); + actual = mskrejectMap( x, mask, out, -2, out.length-1, clbk, scale ); + expected = new Complex64Array( [ 0.0, 0.0, scale * 7.0, scale * 8.0, 0.0, 0.0, scale * 3.0, scale * 4.0 ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function rejects array elements (accessors)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + + function clbk( val ) { + return val * 2; + } + + mask = toAccessorArray( [ 1, 0, 1, 0 ] ); + out = toAccessorArray( zeros( 2, 'generic' ) ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + expected = [ 4, 8 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 1, 1, 1, 1 ] ); + out = toAccessorArray( zeros( 0, 'generic' ) ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + expected = []; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 1, 1, 1, 0 ] ); + out = toAccessorArray( zeros( 1, 'generic' ) ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + expected = [ 8 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 0, 0, 0, 0 ] ); + out = toAccessorArray( zeros( 4, 'generic' ) ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + expected = [ 2, 4, 6, 8 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + mask = toAccessorArray( [ 1, 0, 1, 0 ] ); + out = toAccessorArray( zeros( 4, 'generic' ) ); + actual = mskrejectMap( x, mask, out, -2, out.length-1, clbk ); + expected = [ 0, 8, 0, 4 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + isEqual( actual, expected ); + + t.end(); + + function isEqual( actual, expected ) { + var i; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.get( i ), expected[ i ], 'returns expected value' ); + } + } +}); + +tape( 'the function returns leaves an output array unchanged if provided a second argument which masks all input array values', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + mask = [ 1, 1, 1, 1 ]; + + function clbk( val ) { + return val * 2; + } + + x = [ 1, 2, 3, 4 ]; + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Int32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + out = [ 0, 0, 0, 0 ]; + expected = [ 0, 0, 0, 0 ]; + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + out = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + expected = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + actual = mskrejectMap( x, mask, out, 1, 0, clbk ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); diff --git a/base/mskreject-map/test/test.js b/base/mskreject-map/test/test.js new file mode 100644 index 00000000..e746a025 --- /dev/null +++ b/base/mskreject-map/test/test.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var hasMethod = require( '@stdlib/assert/is-method' ); +var mskrejectMap = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mskrejectMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( mskrejectMap, 'assign' ), true, 'returns expected value' ); + t.strictEqual( hasMethod( mskrejectMap, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/base/mskreject-map/test/test.main.js b/base/mskreject-map/test/test.main.js new file mode 100644 index 00000000..6e0289a6 --- /dev/null +++ b/base/mskreject-map/test/test.main.js @@ -0,0 +1,114 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isArray = require( '@stdlib/assert/is-array' ); +var mskrejectMap = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mskrejectMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function rejects array elements', function test( t ) { + var expected; + var actual; + var mask; + var x; + + x = [ 1, 2, 3, 4 ]; + + function clbk( val ) { + return val * 2; + } + + mask = [ 0, 1, 0, 1 ]; + actual = mskrejectMap( x, mask, clbk ); + expected = [ 2, 6 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + actual = mskrejectMap( x, mask, clbk ); + expected = []; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 1 ]; + actual = mskrejectMap( x, mask, clbk ); + expected = [ 2, 4, 6 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an empty array if provided empty arrays', function test( t ) { + function clbk( val ) { + return val * 2; + } + t.deepEqual( mskrejectMap( [], [], clbk ), [], 'returns expected value' ); + t.end(); +}); + +tape( 'the function applies a mask to a provided input array and maps the unmasked values according to different callback functions', function test( t ) { + var expected; + var actual; + var mask; + var x; + + x = [ 1, 2, 3, 4 ]; + + function clbk1( val ) { + return val * 2; + } + + function clbk2( val ) { + return val * val; + } + + mask = [ 0, 1, 0, 1 ]; + actual = mskrejectMap( x, mask, clbk1 ); + expected = [ 2, 6 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = mskrejectMap( x, mask, clbk2 ); + expected = [ 1, 9 ]; + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an empty array if provided empty arrays', function test( t ) { + function clbk( val ) { + return val * 2; + } + t.deepEqual( mskrejectMap( [], [], clbk ), [], 'returns expected value' ); + t.end(); +});