From 1e166ae7b0a48c9ca00830476196ffe61f0dcffe Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 19 Feb 2025 18:32:07 +0500 Subject: [PATCH 1/8] feat: add array foreach --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/array/base/for-each/README.md | 159 ++++++++++++++++++ .../for-each/benchmark/benchmark.length.js | 108 ++++++++++++ .../@stdlib/array/base/for-each/docs/repl.txt | 36 ++++ .../array/base/for-each/docs/types/index.d.ts | 84 +++++++++ .../array/base/for-each/docs/types/test.ts | 102 +++++++++++ .../array/base/for-each/examples/index.js | 30 ++++ .../@stdlib/array/base/for-each/lib/index.js | 43 +++++ .../@stdlib/array/base/for-each/lib/main.js | 139 +++++++++++++++ .../@stdlib/array/base/for-each/package.json | 64 +++++++ .../@stdlib/array/base/for-each/test/test.js | 128 ++++++++++++++ 10 files changed, 893 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/base/for-each/README.md create mode 100644 lib/node_modules/@stdlib/array/base/for-each/benchmark/benchmark.length.js create mode 100644 lib/node_modules/@stdlib/array/base/for-each/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/base/for-each/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/base/for-each/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/base/for-each/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/base/for-each/package.json create mode 100644 lib/node_modules/@stdlib/array/base/for-each/test/test.js diff --git a/lib/node_modules/@stdlib/array/base/for-each/README.md b/lib/node_modules/@stdlib/array/base/for-each/README.md new file mode 100644 index 000000000000..3f147aa814d6 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/README.md @@ -0,0 +1,159 @@ + + +# forEach + +> Invoke a callback funcion once for each array element. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var forEach = require( '@stdlib/array/base/for-each' ); +``` + +#### forEach( x, fcn\[, thisArg] ) + +Invokes a callback function once for each array element. + +```javascript +var naryFunction = require( '@stdlib/utils/nary-function' ); +var log = require( '@stdlib/console/log' ); + +var x = [ 1, 2, 3, 4 ]; + +forEach( x, naryFunction( log, 1 ) ); +``` + +The function accepts the following arguments: + +- **x**: input array. +- **fcn**: callback to apply. +- **thisArg**: callback execution context _(optional)_. + +To set the callback function execution context, provide a `thisArg`. + + + +```javascript +function accumulate( z ) { + this.sum += z; +} + +var x = [ 1, 2, 3, 4 ]; + +var ctx = { + 'sum': 0 +}; + +forEach( x, accumulate, ctx ); +var sum = ctx.sum; +// returns 10 +``` + +The callback function is provided the following arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the input array. + +
+ + + + + +
+ +## Notes + +- If provided an array-like object having a `forEach` method, the function defers execution to that method and assumes that the method API has the following signature: + + ```text + x.forEach( fcn, thisArg ) + ``` + +- The function support 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 log = require( '@stdlib/console/log' ); +var forEach = require( '@stdlib/array/base/for-each' ); + +var x = discreteUniform( 10, 0, 10, { + 'dtype': 'float64' +}); + +forEach( x, naryFunction( log, 1 ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/for-each/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/for-each/benchmark/benchmark.length.js new file mode 100644 index 000000000000..999c24b5e348 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/benchmark/benchmark.length.js @@ -0,0 +1,108 @@ +/** +* @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 zeroTo = require( '@stdlib/array/base/zero-to' ); +var pkg = require( './../package.json' ).name; +var forEach = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Callback invoked for each ndarray element. +* +* @private +* @param {number} value - array element +* @throws {Error} unexpected error +*/ +function clbk( value ) { + if ( isnan( value ) ) { + throw new Error( 'unexpected error' ); + } +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = zeroTo( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + forEach( x, clbk ); + if ( isnan( x[ i%len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( x[ 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/for-each/docs/repl.txt b/lib/node_modules/@stdlib/array/base/for-each/docs/repl.txt new file mode 100644 index 000000000000..23f7d781dc22 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( x, fcn[, thisArg] ) + Invoke a callback function once for each array element. + + 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 `forEach` method , the function + defers execution to that method and assumes that the method has the + following signature: + + x.forEach( clbk, thisArg ) + + Parameters + ---------- + x: Array|TypedArray|Object + Input array. + + fcn: Function + Callback function. + + thisArg: any (optional) + Callback function execution context. + + Examples + -------- + > var x = [ 1, 2, 3, 4 ]; + > function f( v ) { if ( v !== v ) { throw new Error( '...' ); } }; + > {{alias}}( x, f ); + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts new file mode 100644 index 000000000000..9a23eb7d6318 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts @@ -0,0 +1,84 @@ +/* +* @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 } from '@stdlib/types/array'; + +/** +* Callback invoked for each ndarray element. +*/ +type Nullary = ( this: V ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +*/ +type Unary = ( this: V, value: T ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param index - current array element index +*/ +type Binary = ( this: V, value: T, indices: Array ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +*/ +type Ternary = ( this: V, value: T, indices: Array, arr: U ) => void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param index - current array element index +* @param arr - input array +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Invokes a callback function once for each array element. +* +* @param x - input array +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var x = [ 1, 2, 3, 4]; +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: Collection | AccessorArrayLike, fcn: Callback, V>, thisArg?: ThisParameterType, V>> ): void; + + +// EXPORTS // + +export = forEach; diff --git a/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts new file mode 100644 index 000000000000..61f799964af5 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts @@ -0,0 +1,102 @@ +/* +* @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 forEach = require( './index' ); + +/** +* Callback function. +* +* @param v - ndarray element +* @throws unexpected error +*/ +function clbk( v: any ): void { + if ( v !== v ) { + throw new Error( 'unexpected error' ); + } +} + +// TESTS // + +// The function returns a boolean... +{ + forEach( [ 1, 2, 3 ], clbk ); // $ExpectType void + forEach( new Float64Array( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + forEach( new Float32Array( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + forEach( new Int32Array( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + forEach( new Int16Array( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + forEach( new Int8Array( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + forEach( new Uint32Array( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + forEach( new Uint16Array( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + forEach( new Uint8Array( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + forEach( new Uint8ClampedArray( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + forEach( toAccessorArray( [ 1, 2, 3 ] ), clbk ); // $ExpectType void + + forEach( [ 1, 2, 3 ], clbk, {} ); // $ExpectType void + forEach( new Float64Array( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void + forEach( new Float32Array( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void + forEach( new Int32Array( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void + forEach( new Int16Array( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void + forEach( new Int8Array( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void + forEach( new Uint32Array( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void + forEach( new Uint16Array( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void + forEach( new Uint8Array( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void + forEach( new Uint8ClampedArray( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void + forEach( toAccessorArray( [ 1, 2, 3 ] ), clbk, {} ); // $ExpectType void +} + +// The compiler throws an error if the function is provided a first argument which is not a collection... +{ + forEach( 2, clbk ); // $ExpectError + forEach( false, clbk ); // $ExpectError + forEach( true, clbk ); // $ExpectError + forEach( {}, clbk ); // $ExpectError + + forEach( 2, clbk, {} ); // $ExpectError + forEach( false, clbk, {} ); // $ExpectError + forEach( true, clbk, {} ); // $ExpectError + forEach( {}, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a function... +{ + forEach( [ 1, 2, 3 ], 'abc' ); // $ExpectError + forEach( [ 1, 2, 3 ], 2 ); // $ExpectError + forEach( [ 1, 2, 3 ], false ); // $ExpectError + forEach( [ 1, 2, 3 ], true ); // $ExpectError + forEach( [ 1, 2, 3 ], null ); // $ExpectError + forEach( [ 1, 2, 3 ], void 0 ); // $ExpectError + forEach( [ 1, 2, 3 ], {} ); // $ExpectError + forEach( [ 1, 2, 3 ], [] ); // $ExpectError + + forEach( [ 1, 2, 3 ], 'abc', {} ); // $ExpectError + forEach( [ 1, 2, 3 ], 2, {} ); // $ExpectError + forEach( [ 1, 2, 3 ], false, {} ); // $ExpectError + forEach( [ 1, 2, 3 ], true, {} ); // $ExpectError + forEach( [ 1, 2, 3 ], null, {} ); // $ExpectError + forEach( [ 1, 2, 3 ], void 0, {} ); // $ExpectError + forEach( [ 1, 2, 3 ], {}, {} ); // $ExpectError + forEach( [ 1, 2, 3 ], [], {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + forEach(); // $ExpectError + forEach( [ 1, 2, 3 ] ); // $ExpectError + forEach( [ 1, 2, 3 ], clbk, {}, 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/for-each/examples/index.js b/lib/node_modules/@stdlib/array/base/for-each/examples/index.js new file mode 100644 index 000000000000..1550861332e3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/examples/index.js @@ -0,0 +1,30 @@ +/** +* @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 log = require( '@stdlib/console/log' ); +var forEach = require( './../lib' ); + +var x = discreteUniform( 10, 0, 10, { + 'dtype': 'float64' +}); + +forEach( x, naryFunction( log, 1 ) ); diff --git a/lib/node_modules/@stdlib/array/base/for-each/lib/index.js b/lib/node_modules/@stdlib/array/base/for-each/lib/index.js new file mode 100644 index 000000000000..7dd6ce1a77a2 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/lib/index.js @@ -0,0 +1,43 @@ +/** +* @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'; + +/** +* Invoke a callback function once for each array element. +* +* @module @stdlib/array/base/for-each +* +* @example +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* var forEach = require( '@stdlib/array/base/for-each' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* forEach( x, naryFunction( log, 1 ) ); +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/for-each/lib/main.js b/lib/node_modules/@stdlib/array/base/for-each/lib/main.js new file mode 100644 index 000000000000..a23c108b4a14 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/lib/main.js @@ -0,0 +1,139 @@ +/** +* @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 // + +/** +* 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( [], 'forEach' ); +* // returns true +* +* @example +* var bool = hasMethod( [], 'beep' ); +* // returns false +*/ +function hasMethod( obj, method ) { + return ( typeof obj[ method ] === 'function' ); +} + +/** +* Invoke a callback function once for each array element. +* +* @private +* @param {Collection} x - input array +* @param {Function} clbk - callback function +* @param {*} thisArg - callback execution context +* @returns {void} +* +* @example +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* forEach( x, naryFunction( log, 1 ) ); +*/ +function internal( x, clbk, thisArg ) { + var i; + for ( i = 0; i < x.length; i++ ) { + clbk.call( thisArg, x[ i ], i, x ); + } +} + +/** +* Invoke a callback function once for each array element. +* +* @private +* @param {Collection} x - input array +* @param {Function} clbk - callback function +* @param {*} thisArg - callback execution context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var x = arraylike2object( toAccessorArray( [ 1, 2, 3, 4 ] ) ); +* +* accessors( x, naryFunction( log, 1 ) ); +*/ +function accessors( x, clbk, thisArg ) { + var data; + var get; + var i; + + data = x.data; + get = x.accessors[ 0 ]; + + for ( i = 0; i < data.length; i++ ) { + clbk.call( thisArg, get( data, i ), i, data ); + } +} + + +// MAIN // + +/** +* Invoke a callback function once for each array element. +* +* @private +* @param {Collection} x - input array +* @param {Function} clbk - callback function +* @param {*} thisArg - callback execution context +* @returns {void} +* +* @example +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var log = require( '@stdlib/console/log' ); +* +* var x = [ 1, 2, 3, 4 ]; +* +* forEach( x, naryFunction( log, 1 ) ); +*/ +function forEach( x, clbk, thisArg ) { + var obj; + if ( hasMethod( x, 'forEach' ) ) { + return x.forEach( clbk, thisArg ); + } + obj = arraylike2object( x ); + if ( obj.accessorProtocol ) { + return accessors( obj, clbk, thisArg ); + } + return internal( x, clbk, thisArg ); +} + + +// EXPORTS // + +module.exports = forEach; diff --git a/lib/node_modules/@stdlib/array/base/for-each/package.json b/lib/node_modules/@stdlib/array/base/for-each/package.json new file mode 100644 index 000000000000..a2201fc9468f --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/array/base/for-each", + "version": "0.0.0", + "description": "Invoke a callback function once for each array element.", + "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", + "generic", + "callback", + "foreach", + "for-each", + "map" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/for-each/test/test.js b/lib/node_modules/@stdlib/array/base/for-each/test/test.js new file mode 100644 index 000000000000..f535a892faa2 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/for-each/test/test.js @@ -0,0 +1,128 @@ +/** +* @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 AccessorArray = require( '@stdlib/array/base/accessor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var forEach = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof forEach, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function applies a callback to each indexed element in an input array (generic)', function test( t ) { + var sum; + var x; + + x = [ 1, 2, 3, 4 ]; + sum = 0; + + forEach( x, clbk ); + + t.strictEqual( sum, 10, 'returns expected value' ); + t.end(); + + function clbk( v ) { + sum += v; + } +}); + +tape( 'the function applies a callback to each indexed element in an input array (typed array)', function test( t ) { + var sum; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + sum = 0.0; + + forEach( x, clbk ); + + t.strictEqual( sum, 10.0, 'returns expected value' ); + t.end(); + + function clbk( v ) { + sum += v; + } +}); + +tape( 'the function applies a callback to each indexed element in an input array (accessors)', function test( t ) { + var sum; + var x; + + x = new AccessorArray( [ 1.0, 2.0, 3.0, 4.0 ] ); + sum = 0.0; + + forEach( x, clbk ); + + t.strictEqual( sum, 10.0, 'returns expected value' ); + t.end(); + + function clbk( v ) { + sum += v; + } +}); + +tape( 'the function applies a callback to each indexed element in an input array (array-like object)', function test( t ) { + var sum; + var x; + + x = { + 'length': 4, + '0': 1, + '1': 2, + '2': 3, + '3': 4 + }; + sum = 0; + + forEach( x, clbk ); + + t.strictEqual( sum, 10, 'returns expected value' ); + t.end(); + + function clbk( v ) { + sum += v; + } +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var ctx; + var x; + + ctx = { + 'sum': 0 + }; + x = [ 1, 2, 3, 4 ]; + + forEach( x, clbk, ctx ); + + t.strictEqual( ctx.sum, 10, 'returns expected value' ); + t.end(); + + function clbk( v ) { + this.sum += v; // eslint-disable-line no-invalid-this + } +}); From d54376d0a87e4dc2f93c39375a6a4302816587b8 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:10:03 +0500 Subject: [PATCH 2/8] docs: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/array/base/for-each/docs/types/index.d.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts index 9a23eb7d6318..2c0661ea1871 100644 --- a/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts @@ -23,19 +23,19 @@ import { Collection, AccessorArrayLike } from '@stdlib/types/array'; /** -* Callback invoked for each ndarray element. +* Callback invoked for each array element. */ type Nullary = ( this: V ) => void; /** -* Callback invoked for each ndarray element. +* Callback invoked for each array element. * * @param value - current array element */ type Unary = ( this: V, value: T ) => void; /** -* Callback invoked for each ndarray element. +* Callback invoked for each array element. * * @param value - current array element * @param index - current array element index @@ -43,7 +43,7 @@ type Unary = ( this: V, value: T ) => void; type Binary = ( this: V, value: T, indices: Array ) => void; /** -* Callback invoked for each ndarray element. +* Callback invoked for each array element. * * @param value - current array element * @param index - current array element index @@ -52,7 +52,7 @@ type Binary = ( this: V, value: T, indices: Array ) => void; type Ternary = ( this: V, value: T, indices: Array, arr: U ) => void; /** -* Callback invoked for each ndarray element. +* Callback invoked for each array element. * * @param value - current array element * @param index - current array element index From 070cee18efd50ca6a597ab251c1a41d9f2a8f8c4 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:10:34 +0500 Subject: [PATCH 3/8] docs: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts index 61f799964af5..90a241700909 100644 --- a/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts @@ -22,7 +22,7 @@ import forEach = require( './index' ); /** * Callback function. * -* @param v - ndarray element +* @param v - array element * @throws unexpected error */ function clbk( v: any ): void { From e81c6fd4deb64ea6580fe4e15665349cf330845c Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 19 Feb 2025 15:51:26 -0800 Subject: [PATCH 4/8] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/for-each/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/for-each/README.md b/lib/node_modules/@stdlib/array/base/for-each/README.md index 3f147aa814d6..2dd72bee602c 100644 --- a/lib/node_modules/@stdlib/array/base/for-each/README.md +++ b/lib/node_modules/@stdlib/array/base/for-each/README.md @@ -61,7 +61,7 @@ The function accepts the following arguments: To set the callback function execution context, provide a `thisArg`. - + ```javascript function accumulate( z ) { @@ -101,7 +101,7 @@ The callback function is provided the following arguments: x.forEach( fcn, thisArg ) ``` -- The function support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]) +- The function support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). From b57b376dbf83115ae5118c20e4c2154a34bae517 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 19 Feb 2025 15:59:47 -0800 Subject: [PATCH 5/8] refactor: use overloads to preserve type information Signed-off-by: Athan --- .../array/base/for-each/docs/types/index.d.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts index 2c0661ea1871..31aff2aaaf4d 100644 --- a/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts @@ -60,6 +60,25 @@ type Ternary = ( this: V, value: T, indices: Array, arr: U ) => */ type Callback = Nullary | Unary | Binary | Ternary; +/** +* Invokes a callback function once for each array element. +* +* @param x - input array +* @param fcn - callback function +* @param thisArg - callback function execution context +* +* @example +* var naryFunction = require( '@stdlib/utils/nary-function' ); +* var toAccessorArray = require( '@stdlib/array/base/to-accessor' ); +* var log = require( '@stdlib/console/log' ); +* +* var x = toAccesorArray( [ 1, 2, 3, 4] ); +* +* // Apply the callback function: +* forEach( x, naryFunction( log, 1 ) ); +*/ +declare function forEach( x: AccessorArrayLike, fcn: Callback, V>, thisArg?: ThisParameterType, V>> ): void; + /** * Invokes a callback function once for each array element. * @@ -76,7 +95,7 @@ type Callback = Nullary | Unary | Binary | Ternary( x: Collection | AccessorArrayLike, fcn: Callback, V>, thisArg?: ThisParameterType, V>> ): void; +declare function forEach( x: Collection, fcn: Callback, V>, thisArg?: ThisParameterType, V>> ): void; // EXPORTS // From 85ce93c8006915283b84e66bfbcb63c5031184ff Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 19 Feb 2025 16:01:06 -0800 Subject: [PATCH 6/8] docs: fix import Signed-off-by: Athan --- .../@stdlib/array/base/for-each/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts index 31aff2aaaf4d..f542aadad06e 100644 --- a/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/base/for-each/docs/types/index.d.ts @@ -69,7 +69,7 @@ type Callback = Nullary | Unary | Binary | Ternary Date: Wed, 19 Feb 2025 16:02:09 -0800 Subject: [PATCH 7/8] docs: fix comment Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts index 90a241700909..6584204c3ee2 100644 --- a/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/base/for-each/docs/types/test.ts @@ -33,7 +33,7 @@ function clbk( v: any ): void { // TESTS // -// The function returns a boolean... +// The function returns undefined... { forEach( [ 1, 2, 3 ], clbk ); // $ExpectType void forEach( new Float64Array( [ 1, 2, 3 ] ), clbk ); // $ExpectType void From b6b62217d459e84441e8f7a1879734b77e166c38 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 19 Feb 2025 16:04:42 -0800 Subject: [PATCH 8/8] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/for-each/lib/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/for-each/lib/main.js b/lib/node_modules/@stdlib/array/base/for-each/lib/main.js index a23c108b4a14..f890d2074b8f 100644 --- a/lib/node_modules/@stdlib/array/base/for-each/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/for-each/lib/main.js @@ -46,7 +46,7 @@ function hasMethod( obj, method ) { } /** -* Invoke a callback function once for each array element. +* Invokes a callback function once for each array element. * * @private * @param {Collection} x - input array @@ -70,7 +70,7 @@ function internal( x, clbk, thisArg ) { } /** -* Invoke a callback function once for each array element. +* Invokes a callback function once for each array element. * * @private * @param {Collection} x - input array @@ -105,7 +105,7 @@ function accessors( x, clbk, thisArg ) { // MAIN // /** -* Invoke a callback function once for each array element. +* Invokes a callback function once for each array element. * * @private * @param {Collection} x - input array