Skip to content

Commit

Permalink
feat: add slice method to array/fixed-endian-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
DhruvArvindSingh committed Nov 20, 2024
1 parent 763cef2 commit 274405f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 157 deletions.
19 changes: 13 additions & 6 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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;
});
Expand Down
269 changes: 118 additions & 151 deletions lib/node_modules/@stdlib/array/fixed-endian-factory/test/test.slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 274405f

Please sign in to comment.