Skip to content

Commit 755425a

Browse files
committed
feat: add assert/is-ndarray-like-with-data-type
1 parent 4b0d2ab commit 755425a

File tree

10 files changed

+698
-0
lines changed

10 files changed

+698
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
# isndarrayLikeWithDataType
22+
23+
> Test if a value is an [ndarray-like object][@stdlib/assert/is-ndarray-like] having a specified [data type][@stdlib/ndarray/dtypes].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isndarrayLikeWithDataType = require( '@stdlib/assert/is-ndarray-like-with-data-type' );
31+
```
32+
33+
#### isndarrayLikeWithDataType( value, dtype )
34+
35+
Tests if a value is an [ndarray-like object][@stdlib/assert/is-ndarray-like] having a specified [data type][@stdlib/ndarray/dtypes].
36+
37+
```javascript
38+
var ndarray = require( '@stdlib/ndarray/ctor' );
39+
40+
var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
41+
var bool = isndarrayLikeWithDataType( arr, 'generic' );
42+
// returns true
43+
```
44+
45+
</section>
46+
47+
<!-- /.usage -->
48+
49+
<section class="examples">
50+
51+
## Examples
52+
53+
<!-- eslint no-undef: "error" -->
54+
55+
```javascript
56+
var ndarray = require( '@stdlib/ndarray/ctor' );
57+
var isndarrayLikeWithDataType = require( '@stdlib/assert/is-ndarray-like-with-data-type' );
58+
59+
var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
60+
var bool = isndarrayLikeWithDataType( arr, 'generic' );
61+
// returns true
62+
63+
bool = isndarrayLikeWithDataType( [ 1, 2, 3, 4 ], 'generic' );
64+
// returns false
65+
66+
bool = isndarrayLikeWithDataType( {}, 'generic' );
67+
// returns false
68+
69+
bool = isndarrayLikeWithDataType( null, 'generic' );
70+
// returns false
71+
```
72+
73+
</section>
74+
75+
<!-- /.examples -->
76+
77+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
78+
79+
<section class="related">
80+
81+
</section>
82+
83+
<!-- /.related -->
84+
85+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
86+
87+
<section class="links">
88+
89+
[@stdlib/assert/is-ndarray-like]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-ndarray-like
90+
91+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
92+
93+
</section>
94+
95+
<!-- /.links -->
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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 ndarray = require( '@stdlib/ndarray/ctor' );
26+
var noop = require( '@stdlib/utils/noop' );
27+
var pkg = require( './../package.json' ).name;
28+
var isndarrayLikeWithDataType = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::true,ndarray', function benchmark( b ) {
34+
var strides;
35+
var offset;
36+
var buffer;
37+
var values;
38+
var shape;
39+
var order;
40+
var bool;
41+
var arr;
42+
var i;
43+
44+
buffer = [ 0, 0, 0, 0 ];
45+
shape = [ 2, 2 ];
46+
strides = [ 2, 1 ];
47+
offset = 0;
48+
order = 'row-major';
49+
50+
arr = ndarray( 'generic', buffer, shape, strides, offset, order );
51+
52+
values = [
53+
arr,
54+
arr,
55+
arr,
56+
arr,
57+
arr
58+
];
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
bool = isndarrayLikeWithDataType( values[ i%values.length ], 'generic' );
63+
if ( typeof bool !== 'boolean' ) {
64+
b.fail( 'should return a boolean' );
65+
}
66+
}
67+
b.toc();
68+
if ( !isBoolean( bool ) ) {
69+
b.fail( 'should return a boolean' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( pkg+'::true,ndarray_like', function benchmark( b ) {
76+
var values;
77+
var bool;
78+
var arr;
79+
var i;
80+
81+
arr = {
82+
'data': [ 0, 0, 0, 0 ],
83+
'shape': [ 2, 2 ],
84+
'strides': [ 2, 1 ],
85+
'offset': 0,
86+
'order': 'row-major',
87+
'ndims': 2,
88+
'dtype': 'generic',
89+
'length': 4,
90+
'flags': {},
91+
'get': noop,
92+
'set': noop
93+
};
94+
95+
values = [
96+
arr,
97+
arr,
98+
arr,
99+
arr,
100+
arr
101+
];
102+
103+
b.tic();
104+
for ( i = 0; i < b.iterations; i++ ) {
105+
bool = isndarrayLikeWithDataType( values[ i%values.length ], 'generic' );
106+
if ( typeof bool !== 'boolean' ) {
107+
b.fail( 'should return a boolean' );
108+
}
109+
}
110+
b.toc();
111+
if ( !isBoolean( bool ) ) {
112+
b.fail( 'should return a boolean' );
113+
}
114+
b.pass( 'benchmark finished' );
115+
b.end();
116+
});
117+
118+
bench( pkg+'::false', function benchmark( b ) {
119+
var values;
120+
var bool;
121+
var i;
122+
123+
values = [
124+
5,
125+
'beep',
126+
true,
127+
false,
128+
null,
129+
[ 1, 2, 3 ],
130+
{}
131+
];
132+
133+
b.tic();
134+
for ( i = 0; i < b.iterations; i++ ) {
135+
bool = isndarrayLikeWithDataType( values[ i%values.length ], 'generic' );
136+
if ( typeof bool !== 'boolean' ) {
137+
b.fail( 'should return a boolean' );
138+
}
139+
}
140+
b.toc();
141+
if ( !isBoolean( bool ) ) {
142+
b.fail( 'should return a boolean' );
143+
}
144+
b.pass( 'benchmark finished' );
145+
b.end();
146+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
{{alias}}( value, dtype )
3+
Tests if a value is an ndarray-like object having a specified data type.
4+
5+
Parameters
6+
----------
7+
value: any
8+
Value to test.
9+
10+
dtype: any
11+
Data type value.
12+
13+
Returns
14+
-------
15+
bool: boolean
16+
Boolean indicating whether a value is an ndarray-like object having a
17+
specified data type.
18+
19+
Examples
20+
--------
21+
> var M = {};
22+
> M.data = [ 0, 0, 0, 0 ];
23+
> M.ndims = 2;
24+
> M.shape = [ 2, 2 ];
25+
> M.strides = [ 2, 1 ];
26+
> M.offset = 0;
27+
> M.order = 'row-major';
28+
> M.dtype = 'generic';
29+
> M.length = 4;
30+
> M.flags = {};
31+
> M.get = function get( i, j ) {};
32+
> M.set = function set( i, j ) {};
33+
> var bool = {{alias}}( M, 'generic' )
34+
true
35+
> bool = {{alias}}( [ 1, 2, 3, 4 ], 'generic' )
36+
false
37+
> bool = {{alias}}( 3.14, 'generic' )
38+
false
39+
> bool = {{alias}}( {}, 'generic' )
40+
false
41+
42+
See Also
43+
--------
44+
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 an ndarray-like object having a specified data type.
23+
*
24+
* @param v - value to test
25+
* @param dtype - data type
26+
* @returns boolean indicating if a value is an ndarray-like object having a specified data type
27+
*
28+
* @example
29+
* var ndarray = require( '@stdlib/ndarray/ctor' );
30+
*
31+
* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
32+
*
33+
* var bool = isndarrayLikeWithDataType( arr, 'generic' );
34+
* // returns true
35+
*
36+
* bool = isndarrayLikeWithDataType( [], 'generic' );
37+
* // returns false
38+
*/
39+
declare function isndarrayLikeWithDataType( v: any, dtype: any ): boolean;
40+
41+
42+
// EXPORTS //
43+
44+
export = isndarrayLikeWithDataType;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 @typescript-eslint/no-invalid-this */
20+
21+
import isndarrayLikeWithDataType = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The function returns a boolean...
27+
{
28+
const ndarray = {
29+
'data': [ 2, 1, 1, 2 ],
30+
'ndims': 2,
31+
'shape': [ 2, 2 ],
32+
'strides': [ 2, 1 ],
33+
'offset': 0,
34+
'order': 'row-major',
35+
'dtype': 'generic',
36+
'length': 4,
37+
'flags': {},
38+
'get': function get( i: number, j: number ): number {
39+
const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j );
40+
return this.data[ idx ];
41+
},
42+
'set': function set( i: number, j: number, v: number ): number {
43+
const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j );
44+
this.data[ idx ] = v;
45+
return v;
46+
}
47+
};
48+
isndarrayLikeWithDataType( ndarray, 'generic' ); // $ExpectType boolean
49+
isndarrayLikeWithDataType( [], 'generic' ); // $ExpectType boolean
50+
isndarrayLikeWithDataType( false, 'generic' ); // $ExpectType boolean
51+
}
52+
53+
// The compiler throws an error if the function is provided an unsupported number of arguments...
54+
{
55+
isndarrayLikeWithDataType(); // $ExpectError
56+
isndarrayLikeWithDataType( null ); // $ExpectError
57+
isndarrayLikeWithDataType( null, 'generic', {} ); // $ExpectError
58+
}

0 commit comments

Comments
 (0)