Skip to content

Commit 4927336

Browse files
feat: add assert/is-same-typed-array-like
PR-URL: #2939 Closes: #2887 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Jaysukh Makvana <[email protected]>
1 parent 61912b7 commit 4927336

File tree

10 files changed

+702
-0
lines changed

10 files changed

+702
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isSameArrayLike
22+
23+
> Test if two arguments are both typed-array-like objects and have the [same values][@stdlib/assert/is-same-value].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isSameTypedArrayLike = require( '@stdlib/assert/is-same-typed-array-like' );
31+
```
32+
33+
#### isSameTypedArrayLike( v1, v2 )
34+
35+
Tests if two arguments are both typed-array-like objects and have the [same values][@stdlib/assert/is-same-value].
36+
37+
```javascript
38+
var Int8Array = require( '@stdlib/array/int8' );
39+
var Int16Array = require( '@stdlib/array/int16' );
40+
41+
var x = new Int8Array( [ 1.0, 2.0 ] );
42+
var y = new Int16Array( [ 1.0, 2.0 ] );
43+
var bool = isSameTypedArrayLike( x, y );
44+
// returns true
45+
46+
bool = isSameTypedArrayLike( x, new Int8Array( [ -1.0, 2.0 ] ) );
47+
// returns false
48+
```
49+
50+
</section>
51+
52+
<!-- /.usage -->
53+
54+
<section class="examples">
55+
56+
## Examples
57+
58+
<!-- eslint no-undef: "error" -->
59+
60+
```javascript
61+
var Int8Array = require( '@stdlib/array/int8' );
62+
var Int16Array = require( '@stdlib/array/int16' );
63+
var isSameTypedArrayLike = require( '@stdlib/assert/is-same-typed-array-like' );
64+
65+
var x = new Int8Array( [ 1.0, 2.0, 3.0 ] );
66+
var y = new Int16Array( [ 1.0, 2.0, 3.0 ] );
67+
var out = isSameTypedArrayLike( x, y );
68+
// returns true
69+
70+
x = new Int8Array( [ 1.0, 2.0, 3.0 ] );
71+
y = new Int16Array( [ 1.0, 2.0, 4.0 ] );
72+
out = isSameTypedArrayLike( x, y );
73+
// returns false
74+
```
75+
76+
</section>
77+
78+
<!-- /.examples -->
79+
80+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
81+
82+
<section class="related">
83+
84+
</section>
85+
86+
<!-- /.related -->
87+
88+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
89+
90+
<section class="links">
91+
92+
[@stdlib/assert/is-same-value]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-same-value
93+
94+
<!-- <related-links> -->
95+
96+
<!-- </related-links> -->
97+
98+
</section>
99+
100+
<!-- /.links -->
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var Int8Array = require( '@stdlib/array/int8' );
27+
var pkg = require( './../package.json' ).name;
28+
var isSameTypedArrayLike = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = new Int8Array( len );
42+
var y = new Int8Array( len );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var bool;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
bool = isSameTypedArrayLike( x, y );
58+
if ( typeof bool !== 'boolean' ) {
59+
b.fail( 'should return a boolean' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isBoolean( bool ) ) {
64+
b.fail( 'should return a boolean' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
f = createBenchmark( len );
92+
bench( pkg+':len='+len, f );
93+
}
94+
}
95+
96+
main();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( v1, v2 )
3+
Tests if two arguments are both typed-array-like objects and have the same
4+
values.
5+
6+
Parameters
7+
----------
8+
v1: any
9+
First input value.
10+
11+
v2: any
12+
Second input value.
13+
14+
Returns
15+
-------
16+
bool: boolean
17+
Boolean indicating whether two arguments are both typed-array-like
18+
objects and have the same values.
19+
20+
Examples
21+
--------
22+
> var Int8Array = require( '@stdlib/array/int8' );
23+
> var Int16Array = require( '@stdlib/array/int16' );
24+
> var x = new Int8Array( [ 1.0, 2.0, 3.0 ] );
25+
> var y = new Int16Array( [ 1.0, 2.0, 3.0 ] );
26+
> var bool = {{alias}}( x, y )
27+
true
28+
29+
> x = new Int8Array( [ 1.0, 2.0, 4.0 ] );
30+
> y = new Int8Array( [ 1.0, 2.0, 3.0 ] );
31+
> bool = {{alias}}( x, y )
32+
false
33+
34+
See Also
35+
--------
36+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Tests if two arguments are both typed-array-like objects and have the same values.
23+
*
24+
* @param v1 - first input value
25+
* @param v2 - second input value
26+
* @returns boolean indicating whether the two arguments are both typed-array-like objects with the same values
27+
*
28+
* @example
29+
* var Int8Array = require( '@stdlib/array/int8' );
30+
* var Int16Array = require( '@stdlib/array/int16' );
31+
* var x = new Int8Array( [ 1.0, 2.0, 3.0 ] );
32+
* var y = new Int16Array( [ 1.0, 2.0, 3.0 ] );
33+
*
34+
* var out = isSameTypedArrayLike( x, y );
35+
* // returns true
36+
*
37+
* @example
38+
* var Int8Array = require( '@stdlib/array/int8' );
39+
* var x = new Int8Array( [ 1.0, 2.0, 3.0 ] );
40+
* var y = new Int8Array( [ 1.0, 2.0, 4.0 ] );
41+
*
42+
* var out = isSameTypedArrayLike( x, y );
43+
* // returns false
44+
*/
45+
declare function isSameTypedArrayLike( v1: any, v2: any ): boolean;
46+
47+
48+
// EXPORTS //
49+
50+
export = isSameTypedArrayLike;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
import Int16Array = require( '@stdlib/array/int16' );
20+
import Int8Array = require( '@stdlib/array/int8' );
21+
import isSameTypedArrayLike = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The function returns a boolean...
27+
{
28+
isSameTypedArrayLike( new Int8Array( [ 1.0, 2.0, 3.0 ] ), new Int16Array( [ 1.0, 2.0, 3.0 ] ) ); // $ExpectType boolean
29+
isSameTypedArrayLike( new Int8Array( [ 1.0, 2.0, 3.0 ] ), new Int8Array( [ 1.0, 2.0, 4.0 ] ) ); // $ExpectType boolean
30+
isSameTypedArrayLike( 3.14, 3.14 ); // $ExpectType boolean
31+
isSameTypedArrayLike( null, null ); // $ExpectType boolean
32+
isSameTypedArrayLike( 'beep', 'boop' ); // $ExpectType boolean
33+
}
34+
35+
// The compiler throws an error if the function is provided an unsupported number of arguments...
36+
{
37+
isSameTypedArrayLike(); // $ExpectError
38+
isSameTypedArrayLike( 3.14 ); // $ExpectError
39+
isSameTypedArrayLike( 'beep', 'beep', 3.14 ); // $ExpectError
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
var Int8Array = require( '@stdlib/array/int8' );
22+
var Int16Array = require( '@stdlib/array/int16' );
23+
var isSameTypedArrayLike = require( './../lib' );
24+
25+
var x = new Int8Array( [ 1.0, 2.0, 3.0 ] );
26+
var y = new Int16Array( [ 1.0, 2.0, 3.0 ] );
27+
var out = isSameTypedArrayLike( x, y );
28+
console.log( out );
29+
// => true
30+
31+
x = new Int8Array( [ 1.0, 2.0, 3.0 ] );
32+
y = new Int16Array( [ 1.0, 2.0, 4.0 ] );
33+
out = isSameTypedArrayLike( x, y );
34+
console.log( out );
35+
// => false

0 commit comments

Comments
 (0)