Skip to content

Commit 162e284

Browse files
committed
Add Typescript definition
1 parent 42bfbed commit 162e284

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
import { Order } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Generates a stride array from an array shape.
28+
*
29+
* @param shape - array shape
30+
* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
31+
* @returns array strides
32+
*
33+
* @example
34+
* var s = shape2strides( [ 3, 2 ], 'row-major' );
35+
* // returns [ 2, 1 ]
36+
*
37+
* s = shape2strides( [ 3, 2 ], 'column-major' );
38+
* // returns [ 1, 3 ]
39+
*/
40+
declare function shape2strides( shape: ArrayLike<number>, order: Order ): Array<number>; // tslint-disable-line max-line-length
41+
42+
43+
// EXPORTS //
44+
45+
export = shape2strides;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 shape2strides = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of numbers...
25+
{
26+
shape2strides( [ 3, 2, 1 ], 'row-major' ); // $ExpectType number[]
27+
}
28+
29+
// The function does not compile if provided a first argument that is not an array-like object containing numbers...
30+
{
31+
shape2strides( true, 'row-major' ); // $ExpectError
32+
shape2strides( false, 'row-major' ); // $ExpectError
33+
shape2strides( null, 'row-major' ); // $ExpectError
34+
shape2strides( undefined, 'row-major' ); // $ExpectError
35+
shape2strides( '5', 'row-major' ); // $ExpectError
36+
shape2strides( [ '1', '2' ], 'row-major' ); // $ExpectError
37+
shape2strides( {}, 'row-major' ); // $ExpectError
38+
shape2strides( ( x: number ): number => x, 'row-major' ); // $ExpectError
39+
}
40+
41+
// The function does not compile if provided a second argument that is not a recognized order...
42+
{
43+
shape2strides( [ 2, 3 ], true ); // $ExpectError
44+
shape2strides( [ 2, 3 ], false ); // $ExpectError
45+
shape2strides( [ 2, 3 ], null ); // $ExpectError
46+
shape2strides( [ 2, 3 ], undefined ); // $ExpectError
47+
shape2strides( [ 2, 3 ], '5' ); // $ExpectError
48+
shape2strides( [ 2, 3 ], [ '1', '2' ] ); // $ExpectError
49+
shape2strides( [ 2, 3 ], {} ); // $ExpectError
50+
shape2strides( [ 2, 3 ], ( x: number ): number => x ); // $ExpectError
51+
}
52+
53+
// The function does not compile if provided insufficient arguments...
54+
{
55+
shape2strides(); // $ExpectError
56+
shape2strides( [ 3, 2 ] ); // $ExpectError
57+
}

lib/node_modules/@stdlib/ndarray/base/shape2strides/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"src": "./src",
2424
"test": "./test"
2525
},
26+
"types": "./docs/types",
2627
"scripts": {},
2728
"homepage": "https://github.com/stdlib-js/stdlib",
2829
"repository": {

0 commit comments

Comments
 (0)