Skip to content

Commit 0fb4869

Browse files
authored
feat: add assert/is-ragged-nested-array (#1368)
Add support for checking if array-like objects are ragged and nested. PR-URL: #1368 Fixes: #1347 Signed-off-by: Pranavchiku <goswami.4@iitj.ac.in> Signed-off-by: Athan Reines <kgryte@gmail.com> Co-authored-by: Athan Reines<kgryte@gmail.com> Reviewed-by: Pranavchiku <goswami.4@iitj.ac.in> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 4253607 commit 0fb4869

File tree

10 files changed

+627
-0
lines changed

10 files changed

+627
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
# isRaggedNestedArray
22+
23+
> Test if a value is a ragged nested array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isRaggedNestedArray = require( '@stdlib/assert/is-ragged-nested-array' );
31+
```
32+
33+
#### isRaggedNestedArray( value )
34+
35+
Tests if a value is a ragged nested array.
36+
37+
```javascript
38+
var bool = isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5 ] ] );
39+
// returns true
40+
41+
bool = isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
42+
// returns false
43+
```
44+
45+
</section>
46+
47+
<!-- /.usage -->
48+
49+
<section class="examples">
50+
51+
## Examples
52+
53+
<!-- eslint-disable no-empty-function, no-restricted-syntax -->
54+
55+
<!-- eslint no-undef: "error" -->
56+
57+
```javascript
58+
var isRaggedNestedArray = require( '@stdlib/assert/is-ragged-nested-array' );
59+
60+
var bool = isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5 ] ] );
61+
// returns true
62+
63+
bool = isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
64+
// returns false
65+
66+
bool = isRaggedNestedArray( 'beep' );
67+
// returns false
68+
69+
bool = isRaggedNestedArray( null );
70+
// returns false
71+
72+
bool = isRaggedNestedArray( void 0 );
73+
// returns false
74+
75+
bool = isRaggedNestedArray( 5 );
76+
// returns false
77+
78+
bool = isRaggedNestedArray( true );
79+
// returns false
80+
81+
bool = isRaggedNestedArray( {} );
82+
// returns false
83+
84+
bool = isRaggedNestedArray( function noop() {} );
85+
// returns false
86+
```
87+
88+
</section>
89+
90+
<!-- /.examples -->
91+
92+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
93+
94+
<section class="related">
95+
96+
</section>
97+
98+
<!-- /.related -->
99+
100+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
101+
102+
<section class="links">
103+
104+
</section>
105+
106+
<!-- /.links -->
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 pkg = require( './../package.json' ).name;
25+
var isRaggedNestedArray = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg+'::ragged_array', function benchmark( b ) {
31+
var bool;
32+
var arr;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
arr = [ [ i, i+1, i+2 ], [ i-1, i-2 ] ];
38+
bool = isRaggedNestedArray( arr );
39+
if ( typeof bool !== 'boolean' ) {
40+
b.fail( 'should return a boolean' );
41+
}
42+
}
43+
b.toc();
44+
if ( !bool ) {
45+
b.fail( 'should return true' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
50+
51+
bench( pkg+'::non_ragged_array', function benchmark( b ) {
52+
var bool;
53+
var arr;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
arr = [ [ i, i+1], [i-1, i ] ];
59+
bool = isRaggedNestedArray( arr );
60+
if ( typeof bool !== 'boolean' ) {
61+
b.fail( 'should return a boolean' );
62+
}
63+
}
64+
b.toc();
65+
if ( bool ) {
66+
b.fail( 'should return false' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
71+
72+
bench( pkg+'::non_array', function benchmark( b ) {
73+
var bool;
74+
var i;
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
bool = isRaggedNestedArray( i );
79+
if ( typeof bool !== 'boolean' ) {
80+
b.fail( 'should return a boolean' );
81+
}
82+
}
83+
b.toc();
84+
if ( bool ) {
85+
b.fail( 'should return false' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( value )
3+
Tests if a value is a ragged nested array.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating whether a value is a ragged nested array.
14+
15+
Examples
16+
--------
17+
> var bool = {{alias}}( [ [ 1, 2, 3 ], [ 4, 5 ] ] )
18+
true
19+
> bool = {{alias}}( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] )
20+
false
21+
> bool = {{alias}}( 'beep' )
22+
false
23+
24+
See Also
25+
--------
26+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 a value is a ragged nested array.
23+
*
24+
* @param value - value to test
25+
* @returns boolean indicating if a value is a ragged nested array
26+
*
27+
* @example
28+
* var bool = isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5 ] ] );
29+
* // returns true
30+
*
31+
* @example
32+
* var bool = isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
33+
* // returns false
34+
*
35+
* @example
36+
* var bool = isRaggedNestedArray( 'beep' );
37+
* // returns false
38+
*/
39+
declare function isRaggedNestedArray( value: any ): boolean;
40+
41+
42+
// EXPORTS //
43+
44+
export = isRaggedNestedArray;
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+
import isRaggedNestedArray = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5 ] ] ); // $ExpectType boolean
27+
isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); // $ExpectType boolean
28+
isRaggedNestedArray( 'beep' ); // $ExpectType boolean
29+
}
30+
31+
// The compiler throws an error if the function is provided an unsupported number of arguments...
32+
{
33+
isRaggedNestedArray(); // $ExpectError
34+
isRaggedNestedArray( [], 123 ); // $ExpectError
35+
}
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+
/* eslint-disable no-empty-function, no-restricted-syntax */
20+
21+
'use strict';
22+
23+
var isRaggedNestedArray = require( './../lib' );
24+
25+
console.log( isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5 ] ] ) );
26+
// => true
27+
28+
console.log( isRaggedNestedArray( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ) );
29+
// => false
30+
31+
console.log( isRaggedNestedArray( 'beep' ) );
32+
// => false
33+
34+
console.log( isRaggedNestedArray( null ) );
35+
// => false
36+
37+
console.log( isRaggedNestedArray( void 0 ) );
38+
// => false
39+
40+
console.log( isRaggedNestedArray( 5 ) );
41+
// => false
42+
43+
console.log( isRaggedNestedArray( true ) );
44+
// => false
45+
46+
console.log( isRaggedNestedArray( {} ) );
47+
// => false
48+
49+
console.log( isRaggedNestedArray( function noop() {} ) );
50+
// => false

0 commit comments

Comments
 (0)