From 991fb750e2bef942b7b33b805bdfb7e6dbaaf063 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 18 Nov 2024 10:00:54 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 65 ++++++ CONTRIBUTORS | 3 + README.md | 4 +- bool/README.md | 9 +- fixed-endian-factory/README.md | 59 +++++ .../benchmark/benchmark.for_each.js | 61 ++++++ .../benchmark/benchmark.for_each.length.js | 105 +++++++++ fixed-endian-factory/lib/main.js | 27 +++ fixed-endian-factory/test/test.for_each.js | 207 ++++++++++++++++++ 9 files changed, 531 insertions(+), 9 deletions(-) create mode 100644 fixed-endian-factory/benchmark/benchmark.for_each.js create mode 100644 fixed-endian-factory/benchmark/benchmark.for_each.length.js create mode 100644 fixed-endian-factory/test/test.for_each.js diff --git a/CHANGELOG.md b/CHANGELOG.md index fef95d68..c70bc62f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,71 @@ > Package changelog. +
+ +## Unreleased (2024-11-18) + +
+ +### Packages + +
+ +#### [@stdlib/array/fixed-endian-factory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/fixed-endian-factory) + +
+ +
+ +##### Features + +- [`956a462`](https://github.com/stdlib-js/stdlib/commit/956a4624c788689b1bca285856b987ea3aa32eb6) - add `forEach` method + +
+ + + +
+ +
+ + + +
+ + + +
+ +### Contributors + +A total of 1 person contributed to this release. Thank you to this contributor: + +- Athan Reines + +
+ + + +
+ +### Commits + +
+ +- [`956a462`](https://github.com/stdlib-js/stdlib/commit/956a4624c788689b1bca285856b987ea3aa32eb6) - **feat:** add `forEach` method _(by Athan Reines)_ +- [`de1ef8b`](https://github.com/stdlib-js/stdlib/commit/de1ef8ba5e2d7dd1363bdf826572456f49b7895c) - **docs:** fix example _(by Athan Reines)_ + +
+ +
+ + + +
+ + +
## 0.3.3 (2024-11-05) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index c9f811ce..b2ba3878 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -59,6 +59,7 @@ Mohammad Kaif <98884589+Kaif987@users.noreply.github.com> Momtchil Momtchev Muhammad Haris Naresh Jagadeesan +Neeraj Pathak NightKnight Nithin Katta <88046362+nithinkatta@users.noreply.github.com> Nourhan Hasan <109472010+TheNourhan@users.noreply.github.com> @@ -69,6 +70,7 @@ Prajwal Kulkarni Pranav Goswami Praneki <97080887+PraneGIT@users.noreply.github.com> Pratik <97464067+Pratik772846@users.noreply.github.com> +Pratyush Kumar Chouhan Priyansh <88396544+itsspriyansh@users.noreply.github.com> Pushpendra Chandravanshi RISHAV <115060907+rishav2404@users.noreply.github.com> @@ -79,6 +81,7 @@ Ridam Garg <67867319+RidamGarg@users.noreply.github.com> Robert Gislason Roman Stetsyk <25715951+romanstetsyk@users.noreply.github.com> Rutam <138517416+performant23@users.noreply.github.com> +Ruthwik Chikoti <145591715+ruthwikchikoti@users.noreply.github.com> Ryan Seal Sai Srikar Dumpeti <80447788+the-r3aper7@users.noreply.github.com> SarthakPaandey <145528240+SarthakPaandey@users.noreply.github.com> diff --git a/README.md b/README.md index 2cd6830d..946e2703 100644 --- a/README.md +++ b/README.md @@ -322,8 +322,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. [npm-image]: http://img.shields.io/npm/v/@stdlib/array.svg [npm-url]: https://npmjs.org/package/@stdlib/array -[test-image]: https://github.com/stdlib-js/array/actions/workflows/test.yml/badge.svg?branch=v0.3.3 -[test-url]: https://github.com/stdlib-js/array/actions/workflows/test.yml?query=branch:v0.3.3 +[test-image]: https://github.com/stdlib-js/array/actions/workflows/test.yml/badge.svg?branch=main +[test-url]: https://github.com/stdlib-js/array/actions/workflows/test.yml?query=branch:main [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array/main.svg [coverage-url]: https://codecov.io/github/stdlib-js/array?branch=main diff --git a/bool/README.md b/bool/README.md index db6a0349..244c5397 100644 --- a/bool/README.md +++ b/bool/README.md @@ -869,7 +869,7 @@ Invokes a function once for each array element. ```javascript function log( v, i ) { - console.log( '%s: %s', i, v.toString() ); + console.log( '%s: %s', i.toString(), v.toString() ); } var arr = new BooleanArray( 3 ); @@ -897,7 +897,7 @@ To set the function execution context, provide a `thisArg`. ```javascript function fcn( v, i ) { this.count += 1; - console.log( '%s: %s', i, v.toString() ); + console.log( '%s: %s', i.toString(), v.toString() ); } var arr = new BooleanArray( 3 ); @@ -911,11 +911,6 @@ arr.set( false, 1 ); arr.set( true, 2 ); arr.forEach( fcn, context ); -/* => - 0: 1 + 1i - 1: 2 + 2i - 2: 3 + 3i -*/ var count = context.count; // returns 3 diff --git a/fixed-endian-factory/README.md b/fixed-endian-factory/README.md index 90caef29..bb1059b1 100644 --- a/fixed-endian-factory/README.md +++ b/fixed-endian-factory/README.md @@ -306,6 +306,65 @@ var v = arr.get( 0 ); // returns 1.0 ``` + + +#### TypedArrayFE.prototype.forEach( callbackFn\[, thisArg] ) + +Invokes a function once for each array element. + +```javascript +function log( v, i ) { + console.log( '%s: %s', i.toString(), v.toString() ); +} + +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', 3 ); + +arr.set( 1.5, 0 ); +arr.set( 2.5, 1 ); +arr.set( 3.5, 2 ); + +arr.forEach( log ); +/* => + 0: 1.5 + 1: 2.5 + 2: 3.5 +*/ +``` + +The invoked function is provided three arguments: + +- **value**: current array element. +- **index**: current array element index. +- **arr**: the array on which this method was called. + +To set the function execution context, provide a `thisArg`. + +```javascript +function fcn( v, i ) { + this.count += 1; + console.log( '%s: %s', i.toString(), v.toString() ); +} + +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', 3 ); + +var context = { + 'count': 0 +}; + +arr.set( 1.0, 0 ); +arr.set( 2.0, 1 ); +arr.set( 3.0, 2 ); + +arr.forEach( fcn, context ); + +var count = context.count; +// returns 3 +``` + #### TypedArrayFE.prototype.get( i ) diff --git a/fixed-endian-factory/benchmark/benchmark.for_each.js b/fixed-endian-factory/benchmark/benchmark.for_each.js new file mode 100644 index 00000000..0bb9137f --- /dev/null +++ b/fixed-endian-factory/benchmark/benchmark.for_each.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// MAIN // + +bench( pkg+':forEach', function benchmark( b ) { + var arr; + var i; + + arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 2.0, 1.0 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr.forEach( check ); + if ( arr.length !== 4 ) { + b.fail( 'should not change an array length' ); + } + } + b.toc(); + if ( arr.length !== 4 ) { + b.fail( 'should not change an array length' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function check( v ) { + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } +}); diff --git a/fixed-endian-factory/benchmark/benchmark.for_each.length.js b/fixed-endian-factory/benchmark/benchmark.for_each.length.js new file mode 100644 index 00000000..e1579ef7 --- /dev/null +++ b/fixed-endian-factory/benchmark/benchmark.for_each.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var zeroTo = require( './../../zero-to' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var arr = new Float64ArrayFE( 'little-endian', 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++ ) { + arr.forEach( callback ); + if ( arr.length !== len ) { + b.fail( 'should not change an array length' ); + } + } + b.toc(); + if ( arr.length !== len ) { + b.fail( 'should not change an array length' ); + } + b.pass( 'benchmark finished' ); + b.end(); + + function callback( value ) { + if ( isnan( value ) ) { + throw new Error( 'something went wrong' ); + } + } + } +} + + +// 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+':forEach:len='+len, f ); + } +} + +main(); diff --git a/fixed-endian-factory/lib/main.js b/fixed-endian-factory/lib/main.js index 490d4eed..11c555b6 100644 --- a/fixed-endian-factory/lib/main.js +++ b/fixed-endian-factory/lib/main.js @@ -507,6 +507,33 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli */ setReadOnly( TypedArray.prototype, 'BYTES_PER_ELEMENT', TypedArray.BYTES_PER_ELEMENT ); + /** + * Invokes a function once for each array element. + * + * @name forEach + * @memberof TypedArray.prototype + * @type {Function} + * @param {Function} fcn - function to invoke + * @param {*} [thisArg] - function invocation context + * @throws {TypeError} `this` must be a typed array instance + * @throws {TypeError} first argument must be a function + */ + setReadOnly( TypedArray.prototype, 'forEach', function forEach( fcn, thisArg ) { + var buf; + var i; + + if ( !isTypedArray( this ) ) { + throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) ); + } + if ( !isFunction( fcn ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) ); + } + buf = this._buffer; + for ( i = 0; i < this._length; i++ ) { + fcn.call( thisArg, buf[ GETTER ]( i*BYTES_PER_ELEMENT, this._isLE ), i, this ); + } + }); + /** * Returns an array element. * diff --git a/fixed-endian-factory/test/test.for_each.js b/fixed-endian-factory/test/test.for_each.js new file mode 100644 index 00000000..e21247a5 --- /dev/null +++ b/fixed-endian-factory/test/test.for_each.js @@ -0,0 +1,207 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var factory = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( isFunction( ctor ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'attached to the prototype of the returned function is a `forEach` method', function test( t ) { + var ctor = factory( 'float64' ); + t.strictEqual( hasOwnProp( ctor.prototype, 'forEach' ), true, 'returns expected value' ); + t.strictEqual( isFunction( ctor.prototype.forEach ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.forEach.call( value, fcn ); + }; + } + + function fcn( v ) { + if ( isnan( v ) ) { + t.fail( 'should not be NaN' ); + } + } +}); + +tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.forEach( value ); + }; + } +}); + +tape( 'the method should not invoke a provided callback function if operating on an empty array', function test( t ) { + var ctor; + var arr; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 0 ); + arr.forEach( fcn ); + + t.end(); + + function fcn() { + t.fail( 'should not be invoked' ); + } +}); + +tape( 'the method returns `undefined`', function test( t ) { + var ctor; + var arr; + var out; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); + out = arr.forEach( fcn ); + + t.strictEqual( out, void 0, 'returns expected value' ); + t.end(); + + function fcn( v ) { + if ( isnan( v ) ) { + t.fail( 'should not be NaN' ); + } + } +}); + +tape( 'the method invokes a provided function for each element in an array', function test( t ) { + var indices; + var values; + var arrays; + var ctor; + var arr; + + indices = []; + values = []; + arrays = []; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); + arr.forEach( fcn ); + + t.deepEqual( values, [ 1.0, 2.0, 3.0, 4.0 ], 'returns expected value' ); + t.deepEqual( indices, [ 0, 1, 2, 3 ], 'returns expected value' ); + t.strictEqual( arrays[ 0 ], arr, 'returns expected value' ); + t.strictEqual( arrays[ 1 ], arr, 'returns expected value' ); + t.strictEqual( arrays[ 2 ], arr, 'returns expected value' ); + t.strictEqual( arrays[ 3 ], arr, 'returns expected value' ); + + t.end(); + + function fcn( v, i, arr ) { + values.push( v ); + indices.push( i ); + arrays.push( arr ); + } +}); + +tape( 'the method supports providing an execution context', function test( t ) { + var ctor; + var ctx; + var arr; + + ctor = factory( 'float64' ); + + ctx = { + 'count': 0 + }; + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); + arr.forEach( fcn, ctx ); + + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function fcn() { + this.count += 1; // eslint-disable-line no-invalid-this + } +});