Skip to content

Commit 2cd60df

Browse files
committed
feat: tests readme and logic added
1 parent 92b2038 commit 2cd60df

File tree

3 files changed

+284
-0
lines changed

3 files changed

+284
-0
lines changed

lib/node_modules/@stdlib/array/fixed-endian-factory/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,26 @@ var str = arr.toString();
695695
// returns '1,2,3'
696696
```
697697

698+
<a name="method-with"></a>
699+
700+
#### TypedArrayFE.prototype.with( index, value )
701+
702+
Returns a new typed array with the element at a provided index replaced with a provided value.
703+
704+
```javascript
705+
var Float64ArrayFE = fixedEndianFactory( 'float64' );
706+
707+
var arr = new Float64ArrayFE( 'little-endian', [ 1.0, 2.0, 3.0 ] );
708+
// returns <Float64ArrayFE>
709+
710+
var out = arr.with( 0, 0.0 );
711+
// returns <Float64ArrayFE>
712+
713+
var v = out.get( 0 );
714+
// returns 0.0
715+
716+
```
717+
698718
</section>
699719

700720
<!-- /.usage -->

lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
2626
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
27+
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
2728
var isCollection = require( '@stdlib/assert/is-collection' );
2829
var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
2930
var isObject = require( '@stdlib/assert/is-object' );
@@ -875,6 +876,49 @@ function factory( dtype ) { // eslint-disable-line max-lines-per-function, stdli
875876
return out.join( ',' );
876877
});
877878

879+
/**
880+
* Returns a new typed array with the element at a provided index replaced with a provided value.
881+
*
882+
* @name with
883+
* @memberof TypedArray.prototype
884+
* @type {Function}
885+
* @param {integer} index - element index
886+
* @param {number} value - new value
887+
* @throws {TypeError} `this` must be a boolean array
888+
* @throws {TypeError} first argument must be an integer
889+
* @throws {RangeError} index argument is out-of-bounds
890+
* @throws {TypeError} second argument must be a number
891+
* @returns {TypedArray} new typed array
892+
*/
893+
setReadOnly( TypedArray.prototype, 'with', function indexOf( index, value ) {
894+
var outbuf;
895+
var buf;
896+
var out;
897+
var len;
898+
899+
if ( !isTypedArray( this ) ) {
900+
throw new TypeError( format( 'invalid invocation. `this` is not %s %s.', CHAR2ARTICLE[ dtype[0] ], CTOR_NAME ) );
901+
}
902+
if ( !isInteger( index ) ) {
903+
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', index ) );
904+
}
905+
len = this._length;
906+
buf = this._buffer;
907+
if ( index < 0 ) {
908+
index += len;
909+
}
910+
if ( index < 0 || index >= len ) {
911+
throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%s`.', index ) );
912+
}
913+
if ( !isNumber( value ) ) {
914+
throw new TypeError( format( 'invalid argument. Second argument must be a number. Value: `%s`.', value ) );
915+
}
916+
out = new this.constructor( flag2byteOrder( this._isLE ), buf.buffer );
917+
outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
918+
outbuf[ SETTER ]( index * BYTES_PER_ELEMENT, value, this._isLE );
919+
return out;
920+
});
921+
878922
return TypedArray;
879923

880924
/**
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var tape = require( 'tape' );
24+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var isFunction = require( '@stdlib/assert/is-function' );
26+
var factory = require( './../lib' );
27+
28+
29+
// TESTS //
30+
31+
tape( 'main export is a function', function test( t ) {
32+
t.ok( true, __filename );
33+
t.strictEqual( typeof factory, 'function', 'main export is a function' );
34+
t.end();
35+
});
36+
37+
tape( 'the function returns a function', function test( t ) {
38+
var ctor = factory( 'float64' );
39+
t.strictEqual( isFunction( ctor ), true, 'returns expected value' );
40+
t.end();
41+
});
42+
43+
tape( 'attached to the prototype of the returned function is a `with` method', function test( t ) {
44+
var ctor = factory( 'float64' );
45+
t.strictEqual( hasOwnProp( ctor.prototype, 'with' ), true, 'returns expected value' );
46+
t.strictEqual( isFunction( ctor.prototype.with ), true, 'returns expected value' );
47+
t.end();
48+
});
49+
50+
tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) {
51+
var values;
52+
var ctor;
53+
var arr;
54+
var i;
55+
56+
ctor = factory( 'float64' );
57+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
58+
59+
values = [
60+
'5',
61+
5,
62+
NaN,
63+
true,
64+
false,
65+
null,
66+
void 0,
67+
{},
68+
[],
69+
function noop() {}
70+
];
71+
for ( i = 0; i < values.length; i++ ) {
72+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
73+
}
74+
t.end();
75+
76+
function badValue( value ) {
77+
return function badValue() {
78+
return arr.with.call( value, 0, 0.0 );
79+
};
80+
}
81+
});
82+
83+
tape( 'the method throws an error if provided a first argument which is not an integer', function test( t ) {
84+
var values;
85+
var ctor;
86+
var arr;
87+
var i;
88+
89+
ctor = factory( 'float64' );
90+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
91+
92+
values = [
93+
'5',
94+
3.14,
95+
NaN,
96+
true,
97+
false,
98+
null,
99+
void 0,
100+
{},
101+
[],
102+
function noop() {}
103+
];
104+
105+
for ( i = 0; i < values.length; i++ ) {
106+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
107+
}
108+
t.end();
109+
110+
function badValue( value ) {
111+
return function badValue() {
112+
return arr.with( value, 0.0 );
113+
};
114+
}
115+
});
116+
117+
tape( 'the method throws an error if provided a first argument which is not in bounds', function test( t ) {
118+
var values;
119+
var ctor;
120+
var arr;
121+
var i;
122+
123+
ctor = factory( 'float64' );
124+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
125+
126+
values = [
127+
-6,
128+
-9,
129+
10,
130+
20
131+
];
132+
133+
for ( i = 0; i < values.length; i++ ) {
134+
t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] );
135+
}
136+
t.end();
137+
138+
function badValue( value ) {
139+
return function badValue() {
140+
return arr.with( value, 0.0 );
141+
};
142+
}
143+
});
144+
145+
tape( 'the method throws an error if provided a second argument which is not a number', function test( t ) {
146+
var values;
147+
var ctor;
148+
var arr;
149+
var i;
150+
151+
ctor = factory( 'float64' );
152+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
153+
154+
values = [
155+
'5',
156+
true,
157+
false,
158+
null,
159+
void 0,
160+
{},
161+
[],
162+
function noop() {}
163+
];
164+
165+
for ( i = 0; i < values.length; i++ ) {
166+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
167+
}
168+
t.end();
169+
170+
function badValue( value ) {
171+
return function badValue() {
172+
return arr.with( 0, value );
173+
};
174+
}
175+
});
176+
177+
tape( 'the method does not change the array length', function test( t ) {
178+
var ctor;
179+
var arr;
180+
var out;
181+
182+
ctor = factory( 'float64' );
183+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
184+
out = arr.with( 0, 4.0 );
185+
186+
t.strictEqual( out.length, 5, 'returns expected value' );
187+
t.end();
188+
});
189+
190+
tape( 'the method returns a new boolean array with the element at a provided index replaced with a provided value', function test( t ) {
191+
var expected;
192+
var actual;
193+
var ctor;
194+
var arr;
195+
196+
ctor = factory( 'float64' );
197+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
198+
expected = new ctor( 'little-endian', [ 0.0, 2.0, 3.0, 4.0, 5.0 ] );
199+
actual = arr.with( 0, 0.0 );
200+
t.strictEqual( actual instanceof ctor, true, 'returns expected value' );
201+
t.deepEqual( actual, expected, 'returns expected value' );
202+
t.notEqual( actual, arr, 'returns new instance' );
203+
t.end();
204+
});
205+
206+
tape( 'the method supports negative indices', function test( t ) {
207+
var expected;
208+
var actual;
209+
var ctor;
210+
var arr;
211+
212+
ctor = factory( 'float64' );
213+
arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
214+
expected = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 0.0 ] );
215+
actual = arr.with( -5, 0.0 );
216+
t.strictEqual( actual instanceof ctor, true, 'returns expected value' );
217+
t.deepEqual( actual, expected, 'returns expected value' );
218+
t.notEqual( actual, arr, 'returns new instance' );
219+
t.end();
220+
});

0 commit comments

Comments
 (0)