Skip to content

Commit 15e6c71

Browse files
feat: add assert/is-same-array-like-object
PR-URL: #2894 Closes: #2886 --------- Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 7bb8e20 commit 15e6c71

File tree

10 files changed

+672
-0
lines changed

10 files changed

+672
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
# isSameArrayLikeObject
22+
23+
> Test if two arguments are both [array-like objects][@stdlib/assert/is-array-like-object] and have the [same values][@stdlib/assert/is-same-value].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isSameArrayLikeObject = require( '@stdlib/assert/is-same-array-like-object' );
31+
```
32+
33+
#### isSameArrayLikeObject( v1, v2 )
34+
35+
Test if two arguments are both [array-like objects][@stdlib/assert/is-array-like-object] and have the [same values][@stdlib/assert/is-same-value].
36+
37+
```javascript
38+
var x = [ 1.0, 2.0 ];
39+
var y = [ 1.0, 2.0 ];
40+
var bool = isSameArrayLikeObject( x, y );
41+
// returns true
42+
43+
bool = isSameArrayLikeObject( x, [ -1.0, 2.0 ] );
44+
// returns false
45+
```
46+
47+
</section>
48+
49+
<!-- /.usage -->
50+
51+
<section class="notes">
52+
53+
## Notes
54+
55+
- In contrast to the strict equality operator `===`, the function distinguishes between `+0` and `-0` and treats `NaNs` as the [same value][@stdlib/assert/is-same-value].
56+
57+
</section>
58+
59+
<!-- /.notes -->
60+
61+
<section class="examples">
62+
63+
## Examples
64+
65+
<!-- eslint no-undef: "error" -->
66+
67+
```javascript
68+
var isSameArrayLikeObject = require( '@stdlib/assert/is-same-array-like-object' );
69+
70+
var x = [ 1.0, 2.0, 3.0 ];
71+
var y = [ 1.0, 2.0, 3.0 ];
72+
var out = isSameArrayLikeObject( x, y );
73+
// returns true
74+
75+
x = [ -0.0, 0.0, -0.0 ];
76+
y = [ 0.0, -0.0, 0.0 ];
77+
out = isSameArrayLikeObject( x, y );
78+
// returns false
79+
80+
x = [ NaN, NaN, NaN ];
81+
y = [ NaN, NaN, NaN ];
82+
out = isSameArrayLikeObject( x, y );
83+
// returns true
84+
```
85+
86+
</section>
87+
88+
<!-- /.examples -->
89+
90+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
91+
92+
<section class="related">
93+
94+
</section>
95+
96+
<!-- /.related -->
97+
98+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
99+
100+
<section class="links">
101+
102+
[@stdlib/assert/is-array-like-object]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-array-like-object
103+
104+
[@stdlib/assert/is-same-value]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-same-value
105+
106+
<!-- <related-links> -->
107+
108+
<!-- </related-links> -->
109+
110+
</section>
111+
112+
<!-- /.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 zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var pkg = require( './../package.json' ).name;
28+
var isSameArrayLikeObject = 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 = zeroTo( len );
42+
var y = zeroTo( 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 = isSameArrayLikeObject( 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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( v1, v2 )
3+
Tests if two arguments are both array-like objects and have the same values.
4+
5+
The function differs from the `===` operator in that the function treats
6+
`-0` and `+0` as distinct and `NaNs` as the same.
7+
8+
Parameters
9+
----------
10+
v1: any
11+
First input value.
12+
13+
v2: any
14+
Second input value.
15+
16+
Returns
17+
-------
18+
bool: boolean
19+
Boolean indicating whether two arguments are the same.
20+
21+
Examples
22+
--------
23+
> var x = [ 1.0, 2.0, 3.0 ];
24+
> var y = [ 1.0, 2.0, 3.0 ];
25+
> var bool = {{alias}}( x, y )
26+
true
27+
28+
> x = [ NaN, NaN, NaN ];
29+
> y = [ NaN, NaN, NaN ];
30+
> bool = {{alias}}( x, y )
31+
true
32+
33+
See Also
34+
--------
35+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 array-like objects and have the same values.
23+
*
24+
* ## Notes
25+
*
26+
* - The function differs from the `===` operator in that the function treats `-0` and `+0` as distinct and `NaNs` as the same.
27+
*
28+
* @param v1 - first input value
29+
* @param v2 - second input value
30+
* @returns boolean indicating whether two arguments are the same
31+
*
32+
* @example
33+
* var x = [ 1.0, 2.0, 3.0 ];
34+
* var y = [ 1.0, 2.0, 3.0 ];
35+
*
36+
* var out = isSameArrayLikeObject( x, y );
37+
* // returns true
38+
*
39+
* @example
40+
* var x = [ 1.0, 2.0, 3.0 ];
41+
* var y = [ 1.0, 2.0, 4.0 ];
42+
*
43+
* var out = isSameArrayLikeObject( x, y );
44+
* // returns false
45+
*/
46+
declare function isSameArrayLikeObject( v1: any, v2: any ): boolean;
47+
48+
49+
// EXPORTS //
50+
51+
export = isSameArrayLikeObject;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 isSameArrayLikeObject = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isSameArrayLikeObject( 3.14, 3.14 ); // $ExpectType boolean
27+
isSameArrayLikeObject( null, null ); // $ExpectType boolean
28+
isSameArrayLikeObject( 'beep', 'boop' ); // $ExpectType boolean
29+
}
30+
31+
// The compiler throws an error if the function is provided an unsupported number of arguments...
32+
{
33+
isSameArrayLikeObject(); // $ExpectError
34+
isSameArrayLikeObject( 3.14 ); // $ExpectError
35+
isSameArrayLikeObject( 'beep', 'beep', 3.14 ); // $ExpectError
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 isSameArrayLikeObject = require( './../lib' );
22+
23+
var x = [ 1.0, 2.0, 3.0 ];
24+
var y = [ 1.0, 2.0, 3.0 ];
25+
var out = isSameArrayLikeObject( x, y );
26+
console.log( out );
27+
// => true
28+
29+
x = [ -0.0, 0.0, -0.0 ];
30+
y = [ 0.0, -0.0, 0.0 ];
31+
out = isSameArrayLikeObject( x, y );
32+
console.log( out );
33+
// => false
34+
35+
x = [ NaN, NaN, NaN ];
36+
y = [ NaN, NaN, NaN ];
37+
out = isSameArrayLikeObject( x, y );
38+
console.log( out );
39+
// => true

0 commit comments

Comments
 (0)