Skip to content

Commit 717f960

Browse files
committed
Add Typescript definition
1 parent 80fa1b9 commit 717f960

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns a boolean indicating if a buffer length is compatible with provided ndarray meta data.
27+
*
28+
* @param len - buffer length
29+
* @param shape - array shape
30+
* @param strides - stride array
31+
* @param offset - index offset
32+
* @returns boolean indicating if a buffer length is compatible
33+
*
34+
* @example
35+
* var shape = [ 2, 2 ];
36+
* var strides = [ 2, 1 ];
37+
* var offset = 0;
38+
*
39+
* var bool = isBufferLengthCompatible( 4, shape, strides, offset );
40+
* // returns true
41+
*
42+
* @example
43+
* var shape = [ 2, 2 ];
44+
* var strides = [ 2, 1 ];
45+
* var offset = 2;
46+
*
47+
* var bool = isBufferLengthCompatible( 4, shape, strides, offset );
48+
* // returns false
49+
*/
50+
declare function isBufferLengthCompatible( len: number, shape: ArrayLike<number>, strides: ArrayLike<number>, offset: number ): boolean; // tslint-disable-line max-line-length
51+
52+
53+
// EXPORTS //
54+
55+
export = isBufferLengthCompatible;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 isBufferLengthCompatible = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isBufferLengthCompatible( 4, [ 2, 2 ], [ 2, 1 ], 0 ); // $ExpectType boolean
27+
}
28+
29+
// The function does not compile if provided a first argument which is not a number...
30+
{
31+
const shape = [ 2, 2 ];
32+
const strides = [ 2, 1 ];
33+
const offset = 0;
34+
isBufferLengthCompatible( true, shape, strides, offset ); // $ExpectError
35+
isBufferLengthCompatible( false, shape, strides, offset ); // $ExpectError
36+
isBufferLengthCompatible( null, shape, strides, offset ); // $ExpectError
37+
isBufferLengthCompatible( undefined, shape, strides, offset ); // $ExpectError
38+
isBufferLengthCompatible( '5', shape, strides, offset ); // $ExpectError
39+
isBufferLengthCompatible( [ '1', '2' ], shape, strides, offset ); // $ExpectError
40+
isBufferLengthCompatible( {}, shape, strides, offset ); // $ExpectError
41+
isBufferLengthCompatible( ( x: number ): number => x, shape, strides, offset ); // $ExpectError
42+
}
43+
44+
// The function does not compile if provided a second argument which is not an array-like object containing numbers...
45+
{
46+
const len = 4;
47+
const strides = [ 2, 1 ];
48+
const offset = 0;
49+
isBufferLengthCompatible( len, true, strides, offset ); // $ExpectError
50+
isBufferLengthCompatible( len, false, strides, offset ); // $ExpectError
51+
isBufferLengthCompatible( len, null, strides, offset ); // $ExpectError
52+
isBufferLengthCompatible( len, undefined, strides, offset ); // $ExpectError
53+
isBufferLengthCompatible( len, '5', strides, offset ); // $ExpectError
54+
isBufferLengthCompatible( len, [ '1', '2' ], strides, offset ); // $ExpectError
55+
isBufferLengthCompatible( len, {}, strides, offset ); // $ExpectError
56+
isBufferLengthCompatible( len, ( x: number ): number => x, strides, offset ); // $ExpectError
57+
}
58+
59+
// The function does not compile if provided a third argument which is not an array-like object containing numbers...
60+
{
61+
const len = 4;
62+
const shape = [ 2, 2 ];
63+
const offset = 0;
64+
isBufferLengthCompatible( len, shape, true, offset ); // $ExpectError
65+
isBufferLengthCompatible( len, shape, false, offset ); // $ExpectError
66+
isBufferLengthCompatible( len, shape, null, offset ); // $ExpectError
67+
isBufferLengthCompatible( len, shape, undefined, offset ); // $ExpectError
68+
isBufferLengthCompatible( len, shape, '5', offset ); // $ExpectError
69+
isBufferLengthCompatible( len, shape, [ '1', '2' ], offset ); // $ExpectError
70+
isBufferLengthCompatible( len, shape, {}, offset ); // $ExpectError
71+
isBufferLengthCompatible( len, shape, ( x: number ): number => x, offset ); // $ExpectError
72+
}
73+
74+
// The function does not compile if provided a fourth argument which is not a number...
75+
{
76+
const len = 4;
77+
const shape = [ 2, 2 ];
78+
const strides = [ 2, 1 ];
79+
isBufferLengthCompatible( len, shape, strides, true ); // $ExpectError
80+
isBufferLengthCompatible( len, shape, strides, false ); // $ExpectError
81+
isBufferLengthCompatible( len, shape, strides, null ); // $ExpectError
82+
isBufferLengthCompatible( len, shape, strides, undefined ); // $ExpectError
83+
isBufferLengthCompatible( len, shape, strides, '5' ); // $ExpectError
84+
isBufferLengthCompatible( len, shape, strides, [ '1', '2' ] ); // $ExpectError
85+
isBufferLengthCompatible( len, shape, strides, {} ); // $ExpectError
86+
isBufferLengthCompatible( len, shape, strides, ( x: number ): number => x ); // $ExpectError
87+
}
88+
89+
// The function does not compile if provided insufficient arguments...
90+
{
91+
isBufferLengthCompatible(); // $ExpectError
92+
isBufferLengthCompatible( 4 ); // $ExpectError
93+
isBufferLengthCompatible( 4, [ 2, 2 ] ); // $ExpectError
94+
isBufferLengthCompatible( 4, [ 2, 2 ], [ 2, 1 ] ); // $ExpectError
95+
}

lib/node_modules/@stdlib/ndarray/base/assert/is-buffer-length-compatible/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {

0 commit comments

Comments
 (0)