diff --git a/lib/node_modules/@stdlib/array/base/map/README.md b/lib/node_modules/@stdlib/array/base/map/README.md new file mode 100644 index 000000000000..c96dbc01a710 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/README.md @@ -0,0 +1,191 @@ + + +# map + +> Apply a callback function to elements in an input array and assign results to elements in a new output array. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var map = require( '@stdlib/array/base/map' ); +``` + +#### map( x, fcn\[, thisArg] ) + +Applies a callback function to elements in an input array and assigns results to elements in a new output array. + +```javascript +var naryFunction = require( '@stdlib/utils/nary-function' ); +var abs = require( '@stdlib/math/base/special/abs' ); + +var x = [ -1.0, -2.0, -3.0, -4.0 ]; + +var y = map( x, naryFunction( abs, 1 ) ); +// returns [ 1.0, 2.0, 3.0, 4.0 ] +``` + +The function accepts the following arguments: + +- **x**: input array. +- **fcn**: callback function. +- **thisArg**: callback execution context (_optional_). + +To set the callback function's execution context, provide a `thisArg`. + + + +```javascript +function count( x ) { + this.count += 1; + return x; +} + +var x = [ 1.0, 2.0, 3.0, 4.0 ]; + +var ctx = { + 'count': 0 +}; + +var y = map( x, count, ctx ); +// returns [ 1.0, 2.0, 3.0, 4.0 ] + +var v = ctx.count; +// returns 4 +``` + +The callback function is provided the following arguments: + +- **value**: current array element. +- **index**: current element index. +- **arr**: input array. + +#### map.assign( x, y, stride, offset, fcn\[, thisArg] ) + +Applies a callback function to elements in an input array and assigns results to elements in an output array. + +```javascript +var naryFunction = require( '@stdlib/utils/nary-function' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var abs = require( '@stdlib/math/base/special/abs' ); + +var x = [ -1.0, -2.0, -3.0, -4.0 ]; + +var y = zeros( x.length ); + +var out = map.assign( x, y, 1, 0, naryFunction( abs, 1 ) ); +// returns [ 1.0, 2.0, 3.0, 4.0 ] + +var bool = ( out === y ); +// returns true +``` + +The function accepts the following arguments: + +- **x**: input array. +- **y**: output array. +- **stride**: stride length for output array. +- **offset**: starting index for output array. +- **fcn**: callback function. +- **thisArg**: callback execution context (_optional_). + +
+ + + + + +
+ +## Notes + +- If provided an array-like object having a `map` method, the function defers execution to that method and assumes that the method API has the following signature: + + ```text + x.map( fcn, thisArg ) + ``` + +- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var map = require( '@stdlib/array/base/map' ); + +var x = discreteUniform( 10, -10, 10, { + 'dtype': 'float64' +}); + +var y = map( x, naryFunction( abs, 1 ) ); +console.log( y ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/map/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/array/base/map/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..ad4b356656fa --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/benchmark/benchmark.assign.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var identity = require( '@stdlib/number/float64/base/identity' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var pkg = require( './../package.json' ).name; +var map = require( './../lib' ).assign; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var y; + + x = uniform( len, -100.0, 100.0, { + 'dtype': 'generic' + }); + y = zeros( len ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = map( x, y, 1, 0, identity ); + if ( isnan( out[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + + if ( isnan( out[ i % len ] ) ) { + b.fail( 'should not return NaN' ); + } + 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:dtype=generic,len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/map/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/map/benchmark/benchmark.js new file mode 100644 index 000000000000..c57bee8baba3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/benchmark/benchmark.js @@ -0,0 +1,100 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var identity = require( '@stdlib/number/float64/base/identity' ); +var pkg = require( './../package.json' ).name; +var map = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100.0, 100.0, { + 'dtype': 'generic' + }); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = map( x, identity ); + if ( isnan( out[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + + if ( isnan( out[ i % len ] ) ) { + b.fail( 'should not return NaN' ); + } + 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+':dtype=generic,len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/array/base/map/docs/repl.txt b/lib/node_modules/@stdlib/array/base/map/docs/repl.txt new file mode 100644 index 000000000000..980ebde95e87 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/docs/repl.txt @@ -0,0 +1,80 @@ + +{{alias}}( x, fcn[, thisArg] ) + Applies a callback function to elements in an input array and assigns + results to elements in a new output array. + + The callback function is provided the following arguments: + + - value: current array element. + - index: current array element index. + - arr: the input array. + + If provided an array-like object having a `map` method, the function defers + execution to that method and assumes that the method has the following + signature: + + x.map( fcn, thisArg ) + + Parameters + ---------- + x: Array|TypedArray|Object + Input array. + + fcn: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: Array|TypedArray|Object + Output array. + + Examples + -------- + > var x = [ -1, -2, -3, -4 ]; + > var y = {{alias}}( x, {{alias:@stdlib/math/base/special/abs}} ) + [ 1, 2, 3, 4 ] + + +{{alias}}.assign( x, y, stride, offset, fcn[, thisArg] ) + Applies a callback function to elements in an input array and assigns + results to elements in an output array. + + Parameters + ---------- + x: Array|TypedArray|Object + Input array. + + y: Array|TypedArray|Object + Output array. + + stride: integer + Stride length for output array. + + offset: integer + Starting index for output array. + + fcn: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: Array|TypedArray|Object + Output array. + + Examples + -------- + > var x = [ -1, -2, -3, -4 ]; + > var y = [ 0, 0, 0, 0 ]; + > var out = {{alias}}.assign( x, y, 1, 0, {{alias:@stdlib/math/base/special/abs}} ) + [ 1, 2, 3, 4 ] + > var bool = ( out === y ) + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/array/base/map/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/map/docs/types/index.d.ts new file mode 100644 index 000000000000..e1c21f28aad5 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/docs/types/index.d.ts @@ -0,0 +1,288 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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, TypedArray } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = Collection | AccessorArrayLike; + +/** +* Output array. +*/ +type OutputArray = Collection | AccessorArrayLike; + +/** +* Interface for an object having a `map` method. +*/ +interface ObjectWithMap { + /** + * Invokes a callback for each element in an array and assigns results to a new output array. + * + * @param clbk - callback to apply + * @param thisArg - callback execution context + * @returns output array + */ + map( clbk: Callback, V, unknown>, thisArg?: unknown ): ObjectWithMap; +} + +/** +* An array having a `map` method. +*/ +type ArrayWithMap = InputArray & ObjectWithMap; + +/** +* Callback invoked for each array element. +* +* @returns result +*/ +type Nullary = ( this: ThisArg ) => V; + +/** +* Callback invoked for each array element. +* +* @param value - current array element +* @returns result +*/ +type Unary = ( this: ThisArg, value: T ) => V; + +/** +* Callback invoked for each array element. +* +* @param value - current array element +* @param index - current array element index +* @returns result +*/ +type Binary = ( this: ThisArg, value: T, index: number ) => V; + +/** +* Callback invoked for each array element. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +* @returns result +*/ +type Ternary = ( this: ThisArg, value: T, index: number, arr: U ) => V; + +/** +* Callback invoked for each array element. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +* @returns result +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Interface describing the main export. +*/ +interface Routine { + /** + * Applies a callback function to elements in an input array and assigns results to elements in a new output array. + * + * @param x - input array object + * @param fcn - callback function + * @param thisArg - callback execution context + * @returns output array + * + * @example + * var ones = require( '@stdlib/array/base/ones' ); + * + * function scale( v ) { + * return v * 10; + * } + * + * var x = ones( 4 ); + * // returns [ 0, 0, 0, 0 ] + * + * var y = map( x, scale ); + * // returns [ 10, 10, 10, 10 ] + */ + = Array, V = unknown, W extends Array = Array, ThisArg = unknown>( x: U, fcn: Callback, thisArg?: ThisParameterType> ): W; + + /** + * Applies a callback function to elements in an input array and assigns results to elements in a new output array. + * + * @param x - input array object + * @param fcn - callback function + * @param thisArg - callback execution context + * @returns output array + * + * @example + * var ones = require( '@stdlib/array/ones' ); + * + * function scale( v ) { + * return v * 10; + * } + * + * var x = ones( 4, 'int32' ); + * // returns [ 0, 0, 0, 0 ] + * + * var y = map( x, scale ); + * // returns [ 10, 10, 10, 10 ] + */ + ( x: U, fcn: Callback, thisArg?: ThisParameterType> ): U; + + /** + * Applies a callback function to elements in an input array and assigns results to elements in a new output array. + * + * ## Notes + * + * - We assume that an input array having a `map` method returns an array of the same class, and, thus, the output array should also have a `map` method. + * + * @param x - input array object + * @param fcn - callback function + * @param thisArg - callback execution context + * @returns output array + * + * @example + * var ones = require( '@stdlib/array/base/ones' ); + * + * function scale( v ) { + * return v * 10; + * } + * + * var x = ones( 4 ); + * // returns [ 0, 0, 0, 0 ] + * + * var y = map( x, scale ); + * // returns [ 10, 10, 10, 10 ] + */ + = ArrayWithMap, V = unknown, W extends ArrayWithMap = ArrayWithMap, ThisArg = unknown>( x: U, fcn: Callback, thisArg?: ThisParameterType> ): W; + + /** + * Applies a callback function to elements in an input array and assigns results to elements in a new output array. + * + * ## Notes + * + * - When an input array does not have a `map` method, we always return a new "generic" array. + * + * @param x - input array object + * @param fcn - callback function + * @param thisArg - callback execution context + * @returns output array + * + * @example + * var toAccessorArray = require( '@stdlib/array/to-accessor-array' ); + * var ones = require( '@stdlib/array/base/ones' ); + * + * function scale( v ) { + * return v * 10; + * } + * + * var x = ones( 4 ); + * // returns [ 0, 0, 0, 0 ] + * + * var y = map( toAccessorArray( x ), scale ); + * // returns [ 10, 10, 10, 10 ] + */ + = InputArray, V = unknown, ThisArg = unknown>( x: U, fcn: Callback, thisArg?: ThisParameterType> ): Array; + + /** + * Applies a callback function to elements in an input array and assigns results to elements in an output array. + * + * @param x - input array object + * @param y - output array object + * @param stride - stride length for output array + * @param offset - starting index for output array + * @param fcn - callback function + * @param thisArg - callback execution context + * @returns output array object + * + * @example + * var ones = require( '@stdlib/array/base/ones' ); + * var zeros = require( '@stdlib/array/base/zeros' ); + * + * function scale( v ) { + * return v * 10; + * } + * var x = ones( 4 ); + * var y = zeros( x.length ); + * + * var out = map.assign( x, y, 1, 0, scale ); + * // returns [ 10, 10, 10, 10 ] + * + * var bool = ( out === y ); + * // returns true + * + * @example + * var toAccessorArray = require( '@stdlib/array/to-accessor-array' ); + * var ones = require( '@stdlib/array/base/ones' ); + * var zeros = require( '@stdlib/array/base/zeros' ); + * + * function scale( v ) { + * return v * 10; + * } + * var x = ones( 4 ); + * var y = zeros( x.length ); + * + * var out = map.assign( toAccessorArray( x ), toAccessorArray( y ), 1, 0 scale ); + * // y => [ 10, 10, 10, 10 ] + */ + assign = InputArray, V = unknown, W extends OutputArray = OutputArray, ThisArg = unknown>( x: U, y: W, stride: number, offset: number, fcn: Callback, thisArg?: ThisParameterType> ): W; +} + +/** +* Applies a callback function to elements in an input array and assigns results to elements in a new output array. +* +* @param x - input array +* @param fcn - callback function +* @param thisArg - callback execution context +* @returns output array +* +* @example +* var ones = require( '@stdlib/array/base/ones' ); +* +* function scale( v ) { +* return v * 10; +* } +* +* var x = ones( 4 ); +* var y = map( x, scale ); +* // returns [ 10, 10, 10, 10 ] +* +* @example +* var ones = require( '@stdlib/array/base/ones' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* +* function scale( v ) { +* return v * 10; +* } +* +* var x = ones( 4 ); +* var y = zeros( x.length ); +* +* var out = map.assign( x, y, 1, 0, scale ); +* // y => [ 10, 10, 10, 10 ] +* +* var bool = ( out === y ); +* // returns true +*/ +declare var map: Routine; + + +// EXPORTS // + +export = map; diff --git a/lib/node_modules/@stdlib/array/base/map/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/map/docs/types/test.ts new file mode 100644 index 000000000000..f0a7323b0c61 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/docs/types/test.ts @@ -0,0 +1,263 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +import map = require( './index' ); + +/** +* Callback function. +* +* @param v - input value +* @returns output value +*/ +function fcn( v: number ): number { + return v * 2; +} + + +// TESTS // + +// The function returns an array... +{ + const x = [ 1, 2, 3 ]; + + map( x, fcn ); // $ExpectType number[] + map( new Float64Array( x ), fcn ); // $ExpectType Float64Array + map( new Float32Array( x ), fcn ); // $ExpectType Float32Array + map( new Int32Array( x ), fcn ); // $ExpectType Int32Array + map( new Int16Array( x ), fcn ); // $ExpectType Int16Array + map( new Int8Array( x ), fcn ); // $ExpectType Int8Array + map( new Uint32Array( x ), fcn ); // $ExpectType Uint32Array + map( new Uint16Array( x ), fcn ); // $ExpectType Uint16Array + map( new Uint8Array( x ), fcn ); // $ExpectType Uint8Array + map( new Uint8ClampedArray( x ), fcn ); // $ExpectType Uint8ClampedArray + map( toAccessorArray( x ), fcn ); // $ExpectType number[] + + map( x, fcn, {} ); // $ExpectType number[] + map( new Float64Array( x ), fcn, {} ); // $ExpectType Float64Array + map( new Float32Array( x ), fcn, {} ); // $ExpectType Float32Array + map( new Int32Array( x ), fcn, {} ); // $ExpectType Int32Array + map( new Int16Array( x ), fcn, {} ); // $ExpectType Int16Array + map( new Int8Array( x ), fcn, {} ); // $ExpectType Int8Array + map( new Uint32Array( x ), fcn, {} ); // $ExpectType Uint32Array + map( new Uint16Array( x ), fcn, {} ); // $ExpectType Uint16Array + map( new Uint8Array( x ), fcn, {} ); // $ExpectType Uint8Array + map( new Uint8ClampedArray( x ), fcn, {} ); // $ExpectType Uint8ClampedArray + map( toAccessorArray( x ), fcn, {} ); // $ExpectType number[] +} + +// The compiler throws an error if the function is provided a first argument which is not a collection... +{ + map( 'abc', fcn ); // $ExpectError + map( 3.14, fcn ); // $ExpectError + map( true, fcn ); // $ExpectError + map( false, fcn ); // $ExpectError + map( null, fcn ); // $ExpectError + map( {}, fcn ); // $ExpectError + map( ( x: number ): number => x, fcn ); // $ExpectError + + map( 'abc', fcn, {} ); // $ExpectError + map( 3.14, fcn, {} ); // $ExpectError + map( true, fcn, {} ); // $ExpectError + map( false, fcn, {} ); // $ExpectError + map( null, fcn, {} ); // $ExpectError + map( {}, fcn, {} ); // $ExpectError + map( ( x: number ): number => x, fcn, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a function... +{ + map( [ 1, 2, 3 ], 'abc' ); // $ExpectError + map( [ 1, 2, 3 ], 2 ); // $ExpectError + map( [ 1, 2, 3 ], false ); // $ExpectError + map( [ 1, 2, 3 ], true ); // $ExpectError + map( [ 1, 2, 3 ], null ); // $ExpectError + map( [ 1, 2, 3 ], void 0 ); // $ExpectError + map( [ 1, 2, 3 ], {} ); // $ExpectError + map( [ 1, 2, 3 ], [] ); // $ExpectError + + map( [ 1, 2, 3 ], 'abc', {} ); // $ExpectError + map( [ 1, 2, 3 ], 2, {} ); // $ExpectError + map( [ 1, 2, 3 ], false, {} ); // $ExpectError + map( [ 1, 2, 3 ], true, {} ); // $ExpectError + map( [ 1, 2, 3 ], null, {} ); // $ExpectError + map( [ 1, 2, 3 ], void 0, {} ); // $ExpectError + map( [ 1, 2, 3 ], {}, {} ); // $ExpectError + map( [ 1, 2, 3 ], [], {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + map(); // $ExpectError + map( [ 1, 2, 3 ] ); // $ExpectError + map( [ 1, 2, 3 ], fcn, {}, 3 ); // $ExpectError +} + +// Attached to the main export is an `assign` method which returns a collection... +{ + const x = [ 1, 2, 3 ]; + const y = [ 0, 0, 0 ]; + + map.assign( x, y, 1, 0, fcn ); // $ExpectType number[] + map.assign( new Float64Array( x ), new Float64Array( y ), 1, 0, fcn ); // $ExpectType Float64Array + map.assign( new Float32Array( x ), new Float32Array( y ), 1, 0, fcn ); // $ExpectType Float32Array + map.assign( new Int32Array( x ), new Int32Array( y ), 1, 0, fcn ); // $ExpectType Int32Array + map.assign( new Int16Array( x ), new Int16Array( y ), 1, 0, fcn ); // $ExpectType Int16Array + map.assign( new Int8Array( x ), new Int8Array( y ), 1, 0, fcn ); // $ExpectType Int8Array + map.assign( new Uint32Array( x ), new Uint32Array( y ), 1, 0, fcn ); // $ExpectType Uint32Array + map.assign( new Uint16Array( x ), new Uint16Array( y ), 1, 0, fcn ); // $ExpectType Uint16Array + map.assign( new Uint8Array( x ), new Uint8Array( y ), 1, 0, fcn ); // $ExpectType Uint8Array + map.assign( new Uint8ClampedArray( x ), new Uint8ClampedArray( y ), 1, 0, fcn ); // $ExpectType Uint8ClampedArray + map.assign( toAccessorArray( x ), y, 1, 0, fcn ); // $ExpectType number[] + map.assign( toAccessorArray( x ), toAccessorArray( y ), 1, 0, fcn ); // $ExpectType AccessorArrayLike + + map.assign( x, y, 1, 0, fcn, {} ); // $ExpectType number[] + map.assign( new Float64Array( x ), new Float64Array( y ), 1, 0, fcn, {} ); // $ExpectType Float64Array + map.assign( new Float32Array( x ), new Float32Array( y ), 1, 0, fcn, {} ); // $ExpectType Float32Array + map.assign( new Int32Array( x ), new Int32Array( y ), 1, 0, fcn, {} ); // $ExpectType Int32Array + map.assign( new Int16Array( x ), new Int16Array( y ), 1, 0, fcn, {} ); // $ExpectType Int16Array + map.assign( new Int8Array( x ), new Int8Array( y ), 1, 0, fcn, {} ); // $ExpectType Int8Array + map.assign( new Uint32Array( x ), new Uint32Array( y ), 1, 0, fcn, {} ); // $ExpectType Uint32Array + map.assign( new Uint16Array( x ), new Uint16Array( y ), 1, 0, fcn, {} ); // $ExpectType Uint16Array + map.assign( new Uint8Array( x ), new Uint8Array( y ), 1, 0, fcn, {} ); // $ExpectType Uint8Array + map.assign( new Uint8ClampedArray( x ), new Uint8ClampedArray( y ), 1, 0, fcn, {} ); // $ExpectType Uint8ClampedArray + map.assign( toAccessorArray( x ), y, 1, 0, fcn, {} ); // $ExpectType number[] + map.assign( toAccessorArray( x ), toAccessorArray( y ), 1, 0, fcn, {} ); // $ExpectType AccessorArrayLike +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not a collection... +{ + const y = [ 0, 0, 0 ]; + + map.assign( 2, y, 1, 0, fcn ); // $ExpectError + map.assign( false, y, 1, 0, fcn ); // $ExpectError + map.assign( true, y, 1, 0, fcn ); // $ExpectError + map.assign( null, y, 1, 0, fcn ); // $ExpectError + map.assign( void 0, y, 1, 0, fcn ); // $ExpectError + map.assign( {}, y, 1, 0, fcn ); // $ExpectError + + map.assign( 2, y, 1, 0, fcn, {} ); // $ExpectError + map.assign( false, y, 1, 0, fcn, {} ); // $ExpectError + map.assign( true, y, 1, 0, fcn, {} ); // $ExpectError + map.assign( null, y, 1, 0, fcn, {} ); // $ExpectError + map.assign( void 0, y, 1, 0, fcn, {} ); // $ExpectError + map.assign( {}, y, 1, 0, fcn, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not a collection... +{ + const x = [ 1, 2, 3 ]; + + map.assign( x, 2, 1, 0, fcn ); // $ExpectError + map.assign( x, false, 1, 0, fcn ); // $ExpectError + map.assign( x, true, 1, 0, fcn ); // $ExpectError + map.assign( x, null, 1, 0, fcn ); // $ExpectError + map.assign( x, void 0, 1, 0, fcn ); // $ExpectError + map.assign( x, {}, 1, 0, fcn ); // $ExpectError + + map.assign( x, 2, 1, 0, fcn, {} ); // $ExpectError + map.assign( x, false, 1, 0, fcn, {} ); // $ExpectError + map.assign( x, true, 1, 0, fcn, {} ); // $ExpectError + map.assign( x, null, 1, 0, fcn, {} ); // $ExpectError + map.assign( x, void 0, 1, 0, fcn, {} ); // $ExpectError + map.assign( x, {}, 1, 0, fcn, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not a number... +{ + const x = [ 1, 2, 3 ]; + const y = [ 0, 0, 0 ]; + + map.assign( x, y, '5', 0, fcn ); // $ExpectError + map.assign( x, y, false, 0, fcn ); // $ExpectError + map.assign( x, y, true, 0, fcn ); // $ExpectError + map.assign( x, y, null, 0, fcn ); // $ExpectError + map.assign( x, y, void 0, 0, fcn ); // $ExpectError + map.assign( x, y, {}, 0, fcn ); // $ExpectError + map.assign( x, y, [], 0, fcn ); // $ExpectError + map.assign( x, y, ( x: number ): number => x, 0, fcn ); // $ExpectError + + map.assign( x, y, '5', 0, fcn, {} ); // $ExpectError + map.assign( x, y, false, 0, fcn, {} ); // $ExpectError + map.assign( x, y, true, 0, fcn, {} ); // $ExpectError + map.assign( x, y, null, 0, fcn, {} ); // $ExpectError + map.assign( x, y, void 0, 0, fcn, {} ); // $ExpectError + map.assign( x, y, {}, 0, fcn, {} ); // $ExpectError + map.assign( x, y, [], 0, fcn, {} ); // $ExpectError + map.assign( x, y, ( x: number ): number => x, 0, fcn, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not a number... +{ + const x = [ 1, 2, 3 ]; + const y = [ 0, 0, 0 ]; + + map.assign( x, y, 1, '5', fcn ); // $ExpectError + map.assign( x, y, 1, false, fcn ); // $ExpectError + map.assign( x, y, 1, true, fcn ); // $ExpectError + map.assign( x, y, 1, null, fcn ); // $ExpectError + map.assign( x, y, 1, void 0, fcn ); // $ExpectError + map.assign( x, y, 1, {}, fcn ); // $ExpectError + map.assign( x, y, 1, [], fcn ); // $ExpectError + map.assign( x, y, 1, ( x: number ): number => x, fcn ); // $ExpectError + + map.assign( x, y, 1, '5', fcn, {} ); // $ExpectError + map.assign( x, y, 1, false, fcn, {} ); // $ExpectError + map.assign( x, y, 1, true, fcn, {} ); // $ExpectError + map.assign( x, y, 1, null, fcn, {} ); // $ExpectError + map.assign( x, y, 1, void 0, fcn, {} ); // $ExpectError + map.assign( x, y, 1, {}, fcn, {} ); // $ExpectError + map.assign( x, y, 1, [], fcn, {} ); // $ExpectError + map.assign( x, y, 1, ( x: number ): number => x, fcn, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fifth argument which is not a function... +{ + const x = [ 1, 2, 3 ]; + const y = [ 0, 0, 0 ]; + + map.assign( x, y, 1, 0, '5' ); // $ExpectError + map.assign( x, y, 1, 0, 5 ); // $ExpectError + map.assign( x, y, 1, 0, false ); // $ExpectError + map.assign( x, y, 1, 0, true ); // $ExpectError + map.assign( x, y, 1, 0, null ); // $ExpectError + map.assign( x, y, 1, 0, void 0 ); // $ExpectError + map.assign( x, y, 1, 0, {} ); // $ExpectError + map.assign( x, y, 1, 0, [] ); // $ExpectError + + map.assign( x, y, 1, 0, '5', {} ); // $ExpectError + map.assign( x, y, 1, 0, 5, {} ); // $ExpectError + map.assign( x, y, 1, 0, false, {} ); // $ExpectError + map.assign( x, y, 1, 0, true, {} ); // $ExpectError + map.assign( x, y, 1, 0, null, {} ); // $ExpectError + map.assign( x, y, 1, 0, void 0, {} ); // $ExpectError + map.assign( x, y, 1, 0, {}, {} ); // $ExpectError + map.assign( x, y, 1, 0, [], {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = [ 1, 2, 3 ]; + const y = [ 0, 0, 0 ]; + + map.assign(); // $ExpectError + map.assign( x ); // $ExpectError + map.assign( x, y ); // $ExpectError + map.assign( x, y, 1, 0 ); // $ExpectError + map.assign( x, y, 1, 0, fcn, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/map/examples/index.js b/lib/node_modules/@stdlib/array/base/map/examples/index.js new file mode 100644 index 000000000000..43971acdcad4 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/examples/index.js @@ -0,0 +1,32 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var map = require( './../lib' ); + +var x = discreteUniform( 10, -10, 10, { + 'dtype': 'float64' +}); +console.log( x ); + +var y = map( x, naryFunction( abs, 1 ) ); +console.log( y ); diff --git a/lib/node_modules/@stdlib/array/base/map/lib/assign.js b/lib/node_modules/@stdlib/array/base/map/lib/assign.js new file mode 100644 index 000000000000..d1e7f08d9e2e --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/lib/assign.js @@ -0,0 +1,164 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 arraylike2object = require( '@stdlib/array/base/arraylike2object' ); + + +// FUNCTIONS // + +/** +* Applies a callback function to elements in an input array and assigns results to elements in an output array. +* +* @private +* @param {Collection} x - input array +* @param {Collection} y - output array +* @param {integer} stride - stride length for output array +* @param {NonNegativeInteger} offset - starting index for output array +* @param {Function} fcn - callback function +* @param {*} thisArg - callback execution context +* @returns {Collection} output array +* +* @example +* var ones = require( '@stdlib/array/base/ones' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* +* function scale( x ) { +* return x * 10.0; +* } +* +* var x = ones( 4 ); +* var y = zeros( x.length ); +* var out = internal( x, y, 1, 0, scale ); +* // returns [ 10.0, 10.0, 10.0, 10.0 ] +* +* var bool = ( out === y ); +* // returns true +*/ +function internal( x, y, stride, offset, fcn, thisArg ) { + var io; + var i; + + io = offset; + for ( i = 0; i < x.length; i++ ) { + y[ io ] = fcn.call( thisArg, x[ i ], i, x ); + io += stride; + } + return y; +} + +/** +* Applies a callback function to elements in an input array and assigns results to elements in an output array. +* +* @private +* @param {Object} x - input array object +* @param {Object} y - output array object +* @param {integer} stride - stride length for output array +* @param {NonNegativeInteger} offset - starting index for output array +* @param {Function} fcn - callback function +* @param {*} thisArg - callback execution context +* @returns {Object} output array object +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* var ones = require( '@stdlib/array/base/ones' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* +* function scale( x ) { +* return x * 10.0; +* } +* +* var x = ones( 4 ); +* var y = zeros( x.length ); +* +* var out = accessors( arraylike2object( toAccessorArray( x ) ), arraylike2object( toAccessorArray( y ) ), 1, 0, scale ); +* // y => [ 10.0, 10.0, 10.0, 10.0 ] +*/ +function accessors( x, y, stride, offset, fcn, thisArg ) { + var xdata; + var ydata; + var xget; + var yset; + var io; + var i; + + xdata = x.data; + ydata = y.data; + xget = x.accessors[ 0 ]; + yset = y.accessors[ 1 ]; + + io = offset; + for ( i = 0; i < xdata.length; i++ ) { + yset( ydata, io, fcn.call( thisArg, xget( xdata, i ), i, xdata ) ); + io += stride; + } + return y; +} + + +// MAIN // + +/** +* Applies a callback function to elements in an input array and assigns results to elements in an output array. +* +* @param {Collection} x - input array +* @param {Collection} y - output array +* @param {integer} stride - stride length for output array +* @param {NonNegativeInteger} offset - starting index for output array +* @param {Function} fcn - callback function +* @param {*} [thisArg] - callback execution context +* @returns {Collection} output array +* +* @example +* var ones = require( '@stdlib/array/base/ones' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* +* function scale( x ) { +* return x * 10.0; +* } +* +* var x = ones( 4 ); +* var y = zeros( x.length ); +* +* var out = assign( x, y, 1, 0, scale ); +* // returns [ 10.0, 10.0, 10.0, 10.0 ] +* +* var bool = ( out === y ); +* // returns true +*/ +function assign( x, y, stride, offset, fcn, thisArg ) { + var xobj; + var yobj; + + xobj = arraylike2object( x ); + yobj = arraylike2object( y ); + if ( xobj.accessorProtocol || yobj.accessorProtocol ) { + accessors( xobj, yobj, stride, offset, fcn, thisArg ); + return y; + } + return internal( x, y, stride, offset, fcn, thisArg ); +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/array/base/map/lib/index.js b/lib/node_modules/@stdlib/array/base/map/lib/index.js new file mode 100644 index 000000000000..2ed13b9474d6 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/lib/index.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 callback function to elements in an input array and assign results to elements in a new output array. +* +* @module @stdlib/array/base/map +* +* @example +* var ones = require( '@stdlib/array/base/ones' ); +* var map = require( '@stdlib/array/base/map' ); +* +* function scale( x ) { +* return x * 10.0; +* } +* +* var x = ones( 4 ); +* var y = map( x, scale ); +* // returns [ 10.0, 10.0, 10.0, 10.0 ] +* +* @example +* var ones = require( '@stdlib/array/base/ones' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var map = require( '@stdlib/array/base/map' ); +* +* function scale( x ) { +* return x * 10.0; +* } +* +* var x = ones( 4 ); +* var y = zeros( x.length ); +* +* var out = map.assign( x, y, scale ); +* // returns [ 10.0, 10.0, 10.0, 10.0 ] +* +* var bool = ( out === y ); +* // 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; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/array/base/map/lib/main.js b/lib/node_modules/@stdlib/array/base/map/lib/main.js new file mode 100644 index 000000000000..46633336c959 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/lib/main.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 zeros = require( '@stdlib/array/base/zeros' ); +var assign = require( './assign.js' ); + + +// FUNCTIONS // + +/** +* Tests whether an object has a specified method. +* +* @private +* @param {Object} obj - input object +* @param {string} method - method name +* @returns {boolean} boolean indicating whether an object has a specified method +* +* @example +* var bool = hasMethod( [], 'map' ); +* // returns true +* +* @example +* var bool = hasMethod( [], 'beep' ); +* // returns false +*/ +function hasMethod( obj, method ) { + return ( typeof obj[ method ] === 'function' ); +} + + +// MAIN // + +/** +* Applies a callback function to elements in an input array and assigns results to elements in a new output array. +* +* @param {Collection} x - input array +* @param {Function} fcn - callback function +* @param {*} [thisArg] - callback execution context +* @returns {Collection} output array +* +* @example +* var ones = require( '@stdlib/array/base/ones' ); +* +* function scale( x ) { +* return x * 10.0; +* } +* +* var x = ones( 4 ); +* var y = map( x, scale ); +* // returns [ 10.0, 10.0, 10.0, 10.0 ] +*/ +function map( x, fcn, thisArg ) { + if ( hasMethod( x, 'map' ) ) { + return x.map( fcn, thisArg ); + } + return assign( x, zeros( x.length ), 1, 0, fcn, thisArg ); +} + + +// EXPORTS // + +module.exports = map; diff --git a/lib/node_modules/@stdlib/array/base/map/package.json b/lib/node_modules/@stdlib/array/base/map/package.json new file mode 100644 index 000000000000..07a44c5738a3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/array/base/map", + "version": "0.0.0", + "description": "Apply a callback function to elements in an input array and assign results to elements in a new output array.", + "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", + "stdtypes", + "types", + "data", + "structure", + "array", + "foreach", + "for-each", + "generic", + "callback", + "map", + "array.map" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/map/test/test.assign.js b/lib/node_modules/@stdlib/array/base/map/test/test.assign.js new file mode 100644 index 000000000000..2fb212715a14 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/test/test.assign.js @@ -0,0 +1,433 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var map = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof map, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function applies a provided callback to elements in an input array and assigns results to elements in an output array (generic)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = zeros( x.length ); + expected = [ 10, 20, 30, 40 ]; + + out = map( x, y, 1, 0, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function applies a provided callback to elements in an input array and assigns results to elements in an output array (typed array)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + y = new Float64Array( x.length ); + expected = new Float64Array( [ 10.0, 20.0, 30.0, 40.0 ] ); + + out = map( x, y, 1, 0, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10.0; + } +}); + +tape( 'the function applies a provided callback to elements in an input array and assigns results to elements in an output array (accessors)', function test( t ) { + var expected; + var ybuf; + var out; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + ybuf = zeros( x.length ); + y = toAccessorArray( ybuf ); + + expected = [ 10, 20, 30, 40 ]; + + out = map( toAccessorArray( x ), y, 1, 0, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function applies a provided callback to elements in an input array and assigns results to elements in an output array (array-like object)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = { + 'length': 4, + '0': 1, + '1': 2, + '2': 3, + '3': 4 + }; + y = zeros( x.length ); + expected = [ 10, 20, 30, 40 ]; + + out = map( x, y, 1, 0, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var out; + var ctx; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = zeros( x.length ); + ctx = { + 'factor': 10 + }; + expected = [ 10, 20, 30, 40 ]; + + out = map( x, y, 1, 0, scale, ctx ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * this.factor; // eslint-disable-line no-invalid-this + } +}); + +tape( 'the function supports providing a `stride` parameter (generic)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = zeros( x.length * 2 ); + expected = [ 10, 0, 20, 0, 30, 0, 40, 0 ]; + + out = map( x, y, 2, 0, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing a `stride` parameter (typed array)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + y = new Float64Array( x.length * 2 ); + expected = new Float64Array( [ 10.0, 0.0, 20.0, 0.0, 30.0, 0.0, 40.0, 0.0 ] ); // eslint-disable-line max-len + + out = map( x, y, 2, 0, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10.0; + } +}); + +tape( 'the function supports providing a `stride` parameter (accessors)', function test( t ) { + var expected; + var ybuf; + var out; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + ybuf = zeros( x.length * 2 ); + y = toAccessorArray( ybuf ); + + expected = [ 10, 0, 20, 0, 30, 0, 40, 0 ]; + + out = map( toAccessorArray( x ), y, 2, 0, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing a `stride` parameter (array-like object)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = { + 'length': 4, + '0': 1, + '1': 2, + '2': 3, + '3': 4 + }; + y = zeros( x.length * 2 ); + expected = [ 10, 0, 20, 0, 30, 0, 40, 0 ]; + + out = map( x, y, 2, 0, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing a negative `stride` parameter (generic)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = zeros( x.length ); + expected = [ 40, 30, 20, 10 ]; + + out = map( x, y, -1, 3, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing a negative `stride` parameter (typed array)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + y = new Float64Array( x.length ); + expected = new Float64Array( [ 40, 30, 20, 10 ] ); + + out = map( x, y, -1, 3, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10.0; + } +}); + +tape( 'the function supports providing a negative `stride` parameter (accessors)', function test( t ) { + var expected; + var ybuf; + var out; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + ybuf = zeros( x.length ); + y = toAccessorArray( ybuf ); + + expected = [ 40, 30, 20, 10 ]; + + out = map( toAccessorArray( x ), y, -1, 3, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing a negative `stride` parameter (array-like object)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = { + 'length': 4, + '0': 1, + '1': 2, + '2': 3, + '3': 4 + }; + y = zeros( x.length ); + expected = [ 40, 30, 20, 10 ]; + + out = map( x, y, -1, 3, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing an `offset` parameter (generic)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + y = zeros( x.length * 2 ); + expected = [ 0, 10, 0, 20, 0, 30, 0, 40 ]; + + out = map( x, y, 2, 1, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing an `offset` parameter (typed array)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + y = new Float64Array( x.length * 2 ); + expected = new Float64Array( [ 0.0, 10.0, 0.0, 20.0, 0.0, 30.0, 0.0, 40.0 ] ); // eslint-disable-line max-len + + out = map( x, y, 2, 1, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10.0; + } +}); + +tape( 'the function supports providing an `offset` parameter (accessors)', function test( t ) { + var expected; + var ybuf; + var out; + var x; + var y; + + x = [ 1, 2, 3, 4 ]; + ybuf = zeros( x.length * 2 ); + y = toAccessorArray( ybuf ); + + expected = [ 0, 10, 0, 20, 0, 30, 0, 40 ]; + + out = map( toAccessorArray( x ), y, 2, 1, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( ybuf, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing an `offset` parameter (array-like object)', function test( t ) { + var expected; + var out; + var x; + var y; + + x = { + 'length': 4, + '0': 1, + '1': 2, + '2': 3, + '3': 4 + }; + y = zeros( x.length * 2 ); + expected = [ 0, 10, 0, 20, 0, 30, 0, 40 ]; + + out = map( x, y, 2, 1, scale ); + + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); diff --git a/lib/node_modules/@stdlib/array/base/map/test/test.js b/lib/node_modules/@stdlib/array/base/map/test/test.js new file mode 100644 index 000000000000..654cd9c5c0dc --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/test/test.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 map = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof map, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( typeof map.assign, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/map/test/test.main.js b/lib/node_modules/@stdlib/array/base/map/test/test.main.js new file mode 100644 index 000000000000..ee1e49d3d285 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/map/test/test.main.js @@ -0,0 +1,142 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); +var map = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof map, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function applies a provided callback to elements in an input array and assigns results to elements in a new output array (generic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 1, 2, 3, 4 ]; + expected = [ 10, 20, 30, 40 ]; + + actual = map( x, scale ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function applies a provided callback to elements in an input array and assigns results to elements in a new output array (typed array)', function test( t ) { + var expected; + var actual; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + expected = new Float64Array( [ 10.0, 20.0, 30.0, 40.0 ] ); + + actual = map( x, scale ); + + t.strictEqual( isFloat64Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10.0; + } +}); + +tape( 'the function applies a provided callback to elements in an input array and assigns results to elements in a new output array (accessors)', function test( t ) { + var expected; + var actual; + var x; + + x = [ 1, 2, 3, 4 ]; + expected = [ 10, 20, 30, 40 ]; + + actual = map( toAccessorArray( x ), scale ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function applies a provided callback to elements in an input array and assigns results to elements in a new output array (array-like object)', function test( t ) { + var expected; + var actual; + var x; + + x = { + 'length': 4, + '0': 1, + '1': 2, + '2': 3, + '3': 4 + }; + expected = [ 10, 20, 30, 40 ]; + + actual = map( x, scale ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * 10; + } +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var actual; + var ctx; + var x; + + x = [ 1, 2, 3, 4 ]; + ctx = { + 'factor': 10 + }; + expected = [ 10, 20, 30, 40 ]; + + actual = map( x, scale, ctx ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + + function scale( v ) { + return v * this.factor; // eslint-disable-line no-invalid-this + } +});