From 7c59bd355ca571e905932ae47dac89e2e6bcc51b Mon Sep 17 00:00:00 2001 From: DhruvArvindSingh Date: Wed, 20 Nov 2024 00:23:57 +0530 Subject: [PATCH 1/5] Added slice method in fixed-endian-factory/lib/main.js --- .../array/fixed-endian-factory/lib/main.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index f87d12884fe..caed77c9697 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -692,6 +692,44 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli return out.join( ',' ); }); + /** + * return sliced TypedArray. + * + * @private + * @name slice + * @memberof TypedArray.prototype + * @type {Function} + * @throws {TypeError} `this` must be a typed array instance + * @returns {TypedArray} string representation + */ + + setReadOnly(TypedArray.prototype, 'slice', function slice( value1, value2 = this._length ) { + var out; + var buf; + var i; + var temp; + var nargs; + nargs = arguments.length; + if ( nargs <= 0 || nargs > 2 ) { + throw new TypeError('invalid arguments.'); + } + if ( !isTypedArray( this ) ) { + throw new TypeError(format('invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[dtype[0]], CTOR_NAME)); + } + temp = []; + buf = this._buffer; + for (i = value1; i < value2; i++) { + temp.push( buf[GETTER]( i * BYTES_PER_ELEMENT, this._isLE ) ); + } + + if (this._isLE) { + out = new TypedArray( 'little-endian' , temp); + } else { + out = new TypedArray( 'big-endian' , temp); + } + return out; + }); + return TypedArray; /** From 2aa24d2a7dcb506c4279ef32f03604a3110ee5f7 Mon Sep 17 00:00:00 2001 From: DhruvArvindSingh Date: Wed, 20 Nov 2024 02:09:33 +0530 Subject: [PATCH 2/5] Debugged slice method and Added test.slice.js and benchmark.slice.js files --- .../benchmark/benchmark.slice.js | 55 +++++ .../array/fixed-endian-factory/lib/main.js | 9 +- .../fixed-endian-factory/test/test.slice.js | 192 ++++++++++++++++++ 3 files changed, 253 insertions(+), 3 deletions(-) create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.slice.js create mode 100644 lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.slice.js diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.slice.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.slice.js new file mode 100644 index 00000000000..1d41ba18144 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.slice.js @@ -0,0 +1,55 @@ +/** +* @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 pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var Float64ArrayFE = factory( 'float64' ); + + +// MAIN // + +bench( pkg+':slice', function benchmark( b ) { + var out; + 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++ ) { + out = arr.slice(1,2); + if ( out instanceof Float64ArrayFE === false ) { + b.fail( 'should return a TypedArray' ); + } + } + b.toc(); + if ( out instanceof Float64ArrayFE === false ) { + b.fail( 'should return a TypedArray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index caed77c9697..0a7436e5411 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -710,11 +710,14 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli var temp; var nargs; nargs = arguments.length; - if ( nargs <= 0 || nargs > 2 ) { - throw new TypeError('invalid arguments.'); + if ( (nargs <= 0 || nargs > 2 ) && !isNonNegativeInteger( value1 ) && !isNonNegativeInteger( value2 ) ) { + throw new TypeError( 'invalid arguments.' ); + } + if ( value1 >= this._length || value2 > this._length || value1 >= value2 ) { + throw new RangeError( 'invalid range of arguments.' ); } if ( !isTypedArray( this ) ) { - throw new TypeError(format('invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[dtype[0]], CTOR_NAME)); + throw new TypeError(format('invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0]], CTOR_NAME)); } temp = []; buf = this._buffer; diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.slice.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.slice.js new file mode 100644 index 00000000000..3f18943ff16 --- /dev/null +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.slice.js @@ -0,0 +1,192 @@ +/** +* @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 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 `slice` method', function test( t ) { + var ctor = factory('float64'); + t.strictEqual( hasOwnProp( ctor.prototype, 'slice'), true, 'returns expected value'); + t.strictEqual( isFunction( ctor.prototype.slice), true, 'returns expected value'); + t.end(); +}); + +tape( 'the method throws an error if invoked with single invalid argument or option' , function test( t ) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64'); + arr = new ctor( 'little-endian', [0,1,2,3,4]); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() { }, + 10, + 7, + 3.5 + ]; + for (i = 0; i < values.length; i++) { + t.throws( badValue(values[i]), TypeError, 'throws an error when provided arg1' + values[i]); + } + t.end(); + + function badValue(arg1) { + return function badValue() { + return arr.slice( arg1 ); + }; + } +}); + +tape( 'the method throws an error if provided single argument which is not a non-negative integer' , function test(t) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian' , 10); + + values = [ + '5', + 4, + -4, + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for (i = 0; i < values.length; i++) { + t.throws( badValue(values[i]) , TypeError || RangeError , ' throws an error when provided ' + values[i]); + } + t.end(); + + function badValue(value) { + return function badValue() { + return arr.slice(value); + }; + } +}); + +tape( 'the method throws an range error if provided two invalid argument ranges ', function test(t) { + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian' , [0,1,2,3,4,5] ); + + var values1 = [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10 + ] + var values2 = [ + 10, + 9, + 8, + 7, + 6, + 5, + 4, + 3, + 2, + 1 + ] + for( i = 0; i < 10; i++ ){ + t.throws( arr.slice( values1[i], values2[i] ), TypeError , 'throws an error when provided arg1 = ' + values1[i] + ', arg2 = ' + values2[i] ); + } + + t.end(); +}); + +tape('the method returns same array with instance with the parent array ', function test( t ) { + var ctor; + var arr; + var narr; + var value1; + var value2; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian' , [1.0, 2.0, 3.0, 4.0,5.2] ); + + value1 = [ + 0, + 1, + 2, + 3, + 4 + ]; + value1 = [ + 1, + 2, + 3, + 4, + 5 + ]; + + for( i=0 ;i < 5; i++ ){ + + narr = arr.slice( value1[0], value2[0] ); + t.strictEqual( narr instanceof ctor, true , 'returns expected value' ); + } + t.end(); + +}); From 763cef2693bc92e9b5e4ef003db72132a9244eb6 Mon Sep 17 00:00:00 2001 From: DhruvArvindSingh Date: Wed, 20 Nov 2024 02:18:09 +0530 Subject: [PATCH 3/5] Added slice documentation in fixed-endian-factory/README.md --- .../array/fixed-endian-factory/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md index 84beae70a57..a515a79bb47 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/README.md @@ -461,6 +461,22 @@ var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] ); var str = arr.toString(); // returns '1,2,3' ``` +#### TypedArrayFE.prototype.slice() + +Returns a Sliced Typed Array. + +```javascript +var Float64ArrayFE = fixedEndianFactory( 'float64' ); + +var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] ); + +var str = arr.slice(1,3); +// returns '[2.0,3.0]' + +str instanceof Float64ArrayFE; +// returns true +``` + @@ -472,6 +488,7 @@ var str = arr.toString(); * * * + ## Notes - A returned constructor supports the following byte orders: From 274405f1c4868de3541c3217f8875ccb5d0d2439 Mon Sep 17 00:00:00 2001 From: DhruvArvindSingh Date: Wed, 20 Nov 2024 06:11:12 +0530 Subject: [PATCH 4/5] feat: add slice method to array/fixed-endian-factory --- .../array/fixed-endian-factory/lib/main.js | 19 +- .../fixed-endian-factory/test/test.slice.js | 269 ++++++++---------- 2 files changed, 131 insertions(+), 157 deletions(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js index 0a7436e5411..b3b4cb7fadd 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js @@ -703,14 +703,21 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli * @returns {TypedArray} string representation */ - setReadOnly(TypedArray.prototype, 'slice', function slice( value1, value2 = this._length ) { + setReadOnly( TypedArray.prototype, 'slice', function slice() { + var value1; + var value2; + var nargs; + var temp; var out; var buf; var i; - var temp; - var nargs; + value1 = arguments[0]; + value2 = arguments[1]; nargs = arguments.length; - if ( (nargs <= 0 || nargs > 2 ) && !isNonNegativeInteger( value1 ) && !isNonNegativeInteger( value2 ) ) { + if (value2 === null || nargs === 1) { + value2 = this._length; + } + if ( nargs < 0 || nargs > 2 || !isNonNegativeInteger( value1 ) || !isNonNegativeInteger( value2 ) ) { throw new TypeError( 'invalid arguments.' ); } if ( value1 >= this._length || value2 > this._length || value1 >= value2 ) { @@ -726,9 +733,9 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli } if (this._isLE) { - out = new TypedArray( 'little-endian' , temp); + out = new TypedArray( 'little-endian', temp); } else { - out = new TypedArray( 'big-endian' , temp); + out = new TypedArray( 'big-endian', temp); } return out; }); diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.slice.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.slice.js index 3f18943ff16..b49c7fcc7b8 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.slice.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.slice.js @@ -20,173 +20,140 @@ // MODULES // -var tape = require('tape'); -var hasOwnProp = require('@stdlib/assert/has-own-property'); -var isFunction = require('@stdlib/assert/is-function'); -var factory = require('./../lib'); +var tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); +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(); + 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(); + 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 `slice` method', function test( t ) { - var ctor = factory('float64'); - t.strictEqual( hasOwnProp( ctor.prototype, 'slice'), true, 'returns expected value'); - t.strictEqual( isFunction( ctor.prototype.slice), true, 'returns expected value'); - t.end(); + var ctor = factory('float64'); + t.strictEqual( hasOwnProp( ctor.prototype, 'slice'), true, 'returns expected value'); + t.strictEqual( isFunction( ctor.prototype.slice), true, 'returns expected value'); + t.end(); }); -tape( 'the method throws an error if invoked with single invalid argument or option' , function test( t ) { - var values; - var ctor; - var arr; - var i; - - ctor = factory( 'float64'); - arr = new ctor( 'little-endian', [0,1,2,3,4]); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [], - function noop() { }, - 10, - 7, - 3.5 - ]; - for (i = 0; i < values.length; i++) { - t.throws( badValue(values[i]), TypeError, 'throws an error when provided arg1' + values[i]); - } - t.end(); - - function badValue(arg1) { - return function badValue() { - return arr.slice( arg1 ); - }; - } -}); - -tape( 'the method throws an error if provided single argument which is not a non-negative integer' , function test(t) { - var values; - var ctor; - var arr; - var i; - - ctor = factory( 'float64' ); - arr = new ctor( 'little-endian' , 10); - - values = [ - '5', - 4, - -4, - 3.14, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - for (i = 0; i < values.length; i++) { - t.throws( badValue(values[i]) , TypeError || RangeError , ' throws an error when provided ' + values[i]); - } - t.end(); - - function badValue(value) { - return function badValue() { - return arr.slice(value); - }; - } +tape( 'the method throws an error if provided single argument which is not a non-negative integer', function test(t) { + var values; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', 10); + + values = [ + '5', + -4, + 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]); + } + function badValue( value ) { + return function badValue() { + return arr.slice( value ); + }; + } + t.end(); }); tape( 'the method throws an range error if provided two invalid argument ranges ', function test(t) { - var ctor; - var arr; - var i; - - ctor = factory( 'float64' ); - arr = new ctor( 'little-endian' , [0,1,2,3,4,5] ); - - var values1 = [ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10 - ] - var values2 = [ - 10, - 9, - 8, - 7, - 6, - 5, - 4, - 3, - 2, - 1 - ] - for( i = 0; i < 10; i++ ){ - t.throws( arr.slice( values1[i], values2[i] ), TypeError , 'throws an error when provided arg1 = ' + values1[i] + ', arg2 = ' + values2[i] ); - } - - t.end(); + var values1; + var values2; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [0, 1, 2, 3, 4] ); + + values1 = [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10 + ]; + values2 = [ + 10, + 9, + 8, + 7, + 6, + 5, + 4, + 3, + 2, + 1 + ]; + for ( i = 0; i < 10; i++ ) { + t.throws( badValue( values1[i], values2[i] ), RangeError, 'throws an error when provided arg1 = ' + values1[i] + ', arg2 = ' + values2[i] ); + } + function badValue( v1, v2 ) { + return function badValue() { + return arr.slice( v1, v2 ); + }; + } + + t.end(); }); -tape('the method returns same array with instance with the parent array ', function test( t ) { - var ctor; - var arr; - var narr; - var value1; - var value2; - var i; - - ctor = factory( 'float64' ); - arr = new ctor( 'little-endian' , [1.0, 2.0, 3.0, 4.0,5.2] ); - - value1 = [ - 0, - 1, - 2, - 3, - 4 - ]; - value1 = [ - 1, - 2, - 3, - 4, - 5 - ]; - - for( i=0 ;i < 5; i++ ){ - - narr = arr.slice( value1[0], value2[0] ); - t.strictEqual( narr instanceof ctor, true , 'returns expected value' ); - } - t.end(); - +tape('the method returns same type array with instance with the parent array ', function test( t ) { + var value1; + var value2; + var narr; + var ctor; + var arr; + var i; + + ctor = factory( 'float64' ); + arr = new ctor( 'little-endian', [1.0, 2.0, 3.0, 4.0, 5.2] ); + + value1 = [ + 0, + 1, + 2, + 3, + 4 + ]; + value2 = [ + 1, + 2, + 3, + 4, + 5 + ]; + + for ( i=0; i < 5; i++ ) { + narr = arr.slice( value1[0], value2[0] ); + t.strictEqual( narr instanceof ctor, true, 'returns expected value' ); + } + t.end(); }); From 8b4abd9f51466279563d87f71823e8f13291ac79 Mon Sep 17 00:00:00 2001 From: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Date: Wed, 20 Nov 2024 03:02:07 +0000 Subject: [PATCH 5/5] fix: resolve lint errors --- .../array/fixed-endian-factory/benchmark/benchmark.slice.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.slice.js b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.slice.js index 1d41ba18144..cd53e7e7666 100644 --- a/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.slice.js +++ b/lib/node_modules/@stdlib/array/fixed-endian-factory/benchmark/benchmark.slice.js @@ -41,13 +41,13 @@ bench( pkg+':slice', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = arr.slice(1,2); + out = arr.slice(1, 2); if ( out instanceof Float64ArrayFE === false ) { b.fail( 'should return a TypedArray' ); } } b.toc(); - if ( out instanceof Float64ArrayFE === false ) { + if ( out instanceof Float64ArrayFE === false ) { b.fail( 'should return a TypedArray' ); } b.pass( 'benchmark finished' );