-
-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
assert/is-same-typed-array-like
PR-URL: #2939 Closes: #2887 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Jaysukh Makvana <jaysukhmakvana2004@gmail.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<!-- | ||
@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. | ||
--> | ||
|
||
# isSameArrayLike | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
> Test if two arguments are both typed-array-like objects and have the [same values][@stdlib/assert/is-same-value]. | ||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var isSameTypedArrayLike = require( '@stdlib/assert/is-same-typed-array-like' ); | ||
``` | ||
|
||
#### isSameTypedArrayLike( v1, v2 ) | ||
|
||
Tests if two arguments are both typed-array-like objects and have the [same values][@stdlib/assert/is-same-value]. | ||
|
||
```javascript | ||
var Int8Array = require( '@stdlib/array/int8' ); | ||
var Int16Array = require( '@stdlib/array/int16' ); | ||
|
||
var x = new Int8Array( [ 1.0, 2.0 ] ); | ||
var y = new Int16Array( [ 1.0, 2.0 ] ); | ||
var bool = isSameTypedArrayLike( x, y ); | ||
// returns true | ||
|
||
bool = isSameTypedArrayLike( x, new Int8Array( [ -1.0, 2.0 ] ) ); | ||
// returns false | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var Int8Array = require( '@stdlib/array/int8' ); | ||
var Int16Array = require( '@stdlib/array/int16' ); | ||
var isSameTypedArrayLike = require( '@stdlib/assert/is-same-typed-array-like' ); | ||
|
||
var x = new Int8Array( [ 1.0, 2.0, 3.0 ] ); | ||
var y = new Int16Array( [ 1.0, 2.0, 3.0 ] ); | ||
var out = isSameTypedArrayLike( x, y ); | ||
// returns true | ||
|
||
x = new Int8Array( [ 1.0, 2.0, 3.0 ] ); | ||
y = new Int16Array( [ 1.0, 2.0, 4.0 ] ); | ||
out = isSameTypedArrayLike( x, y ); | ||
// returns false | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[@stdlib/assert/is-same-value]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-same-value | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* @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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; | ||
var pow = require( '@stdlib/math/base/special/pow' ); | ||
var Int8Array = require( '@stdlib/array/int8' ); | ||
var pkg = require( './../package.json' ).name; | ||
var isSameTypedArrayLike = require( './../lib' ); | ||
|
||
|
||
// FUNCTIONS // | ||
|
||
/** | ||
* Creates a benchmark function. | ||
* | ||
* @private | ||
* @param {PositiveInteger} len - array length | ||
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var x = new Int8Array( len ); | ||
var y = new Int8Array( len ); | ||
return benchmark; | ||
|
||
/** | ||
* Benchmark function. | ||
* | ||
* @private | ||
* @param {Benchmark} b - benchmark instance | ||
*/ | ||
function benchmark( b ) { | ||
var bool; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
bool = isSameTypedArrayLike( x, y ); | ||
if ( typeof bool !== 'boolean' ) { | ||
b.fail( 'should return a boolean' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isBoolean( bool ) ) { | ||
b.fail( 'should return a boolean' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
} | ||
} | ||
|
||
|
||
// 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+':len='+len, f ); | ||
} | ||
} | ||
|
||
main(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
{{alias}}( v1, v2 ) | ||
Tests if two arguments are both typed-array-like objects and have the same | ||
values. | ||
|
||
Parameters | ||
---------- | ||
v1: any | ||
First input value. | ||
|
||
v2: any | ||
Second input value. | ||
|
||
Returns | ||
------- | ||
bool: boolean | ||
Boolean indicating whether two arguments are both typed-array-like | ||
objects and have the same values. | ||
|
||
Examples | ||
-------- | ||
> var Int8Array = require( '@stdlib/array/int8' ); | ||
> var Int16Array = require( '@stdlib/array/int16' ); | ||
> var x = new Int8Array( [ 1.0, 2.0, 3.0 ] ); | ||
> var y = new Int16Array( [ 1.0, 2.0, 3.0 ] ); | ||
> var bool = {{alias}}( x, y ) | ||
true | ||
|
||
> x = new Int8Array( [ 1.0, 2.0, 4.0 ] ); | ||
> y = new Int8Array( [ 1.0, 2.0, 3.0 ] ); | ||
> bool = {{alias}}( x, y ) | ||
false | ||
|
||
See Also | ||
-------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* @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. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/** | ||
* Tests if two arguments are both typed-array-like objects and have the same values. | ||
* | ||
* @param v1 - first input value | ||
* @param v2 - second input value | ||
* @returns boolean indicating whether the two arguments are both typed-array-like objects with the same values | ||
* | ||
* @example | ||
* var Int8Array = require( '@stdlib/array/int8' ); | ||
* var Int16Array = require( '@stdlib/array/int16' ); | ||
* var x = new Int8Array( [ 1.0, 2.0, 3.0 ] ); | ||
This comment has been minimized.
Sorry, something went wrong.
kgryte
Member
|
||
* var y = new Int16Array( [ 1.0, 2.0, 3.0 ] ); | ||
* | ||
* var out = isSameTypedArrayLike( x, y ); | ||
* // returns true | ||
* | ||
* @example | ||
* var Int8Array = require( '@stdlib/array/int8' ); | ||
* var x = new Int8Array( [ 1.0, 2.0, 3.0 ] ); | ||
* var y = new Int8Array( [ 1.0, 2.0, 4.0 ] ); | ||
* | ||
* var out = isSameTypedArrayLike( x, y ); | ||
* // returns false | ||
*/ | ||
declare function isSameTypedArrayLike( v1: any, v2: any ): boolean; | ||
Check warning on line 45 in lib/node_modules/@stdlib/assert/is-same-typed-array-like/docs/types/index.d.ts GitHub Actions / Lint Changed Files
|
||
|
||
|
||
// EXPORTS // | ||
|
||
export = isSameTypedArrayLike; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* @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. | ||
*/ | ||
|
||
import Int16Array = require( '@stdlib/array/int16' ); | ||
import Int8Array = require( '@stdlib/array/int8' ); | ||
import isSameTypedArrayLike = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a boolean... | ||
{ | ||
isSameTypedArrayLike( new Int8Array( [ 1.0, 2.0, 3.0 ] ), new Int16Array( [ 1.0, 2.0, 3.0 ] ) ); // $ExpectType boolean | ||
isSameTypedArrayLike( new Int8Array( [ 1.0, 2.0, 3.0 ] ), new Int8Array( [ 1.0, 2.0, 4.0 ] ) ); // $ExpectType boolean | ||
isSameTypedArrayLike( 3.14, 3.14 ); // $ExpectType boolean | ||
isSameTypedArrayLike( null, null ); // $ExpectType boolean | ||
isSameTypedArrayLike( 'beep', 'boop' ); // $ExpectType boolean | ||
} | ||
|
||
// The compiler throws an error if the function is provided an unsupported number of arguments... | ||
{ | ||
isSameTypedArrayLike(); // $ExpectError | ||
isSameTypedArrayLike( 3.14 ); // $ExpectError | ||
isSameTypedArrayLike( 'beep', 'beep', 3.14 ); // $ExpectError | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @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'; | ||
|
||
var Int8Array = require( '@stdlib/array/int8' ); | ||
var Int16Array = require( '@stdlib/array/int16' ); | ||
var isSameTypedArrayLike = require( './../lib' ); | ||
|
||
var x = new Int8Array( [ 1.0, 2.0, 3.0 ] ); | ||
var y = new Int16Array( [ 1.0, 2.0, 3.0 ] ); | ||
var out = isSameTypedArrayLike( x, y ); | ||
console.log( out ); | ||
// => true | ||
|
||
x = new Int8Array( [ 1.0, 2.0, 3.0 ] ); | ||
y = new Int16Array( [ 1.0, 2.0, 4.0 ] ); | ||
out = isSameTypedArrayLike( x, y ); | ||
console.log( out ); | ||
// => false |
1 comment
on commit 4927336
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report
Package | Statements | Branches | Functions | Lines |
---|---|---|---|---|
assert/is-same-typed-array-like |
|
|
|
|
The above coverage report was generated for the changes in this push.
@Planeshifter This heading is incorrect.