Skip to content

Commit 1ed5dd2

Browse files
committed
Add Typescript definition
1 parent 8861372 commit 1ed5dd2

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/**
22+
* ndarray data type.
23+
*/
24+
type ndarrayDataType = 'binary' | 'float32' | 'float64' | 'generic' | 'int16' | 'int32' | 'int8' | 'uint16' | 'uint32' | 'uint8' | 'uint8c'; // tslint-disable-line max-line-length
25+
26+
/**
27+
* ndarray casting mode.
28+
*/
29+
type ndarrayCastingMode = 'none' | 'equiv' | 'safe' | 'same-kind' | 'unsafe';
30+
31+
/**
32+
* Returns a boolean indicating if a provided ndarray data type can be cast to another ndarray data type according to a specified casting mode.
33+
*
34+
* @param from - ndarray data type
35+
* @param to - ndarray data type
36+
* @param casting - ndarray casting mode
37+
* @returns boolean indicating if a data type can be cast to another data type
38+
*
39+
* @example
40+
* var bool = isAllowedCast( 'float32', 'float64', 'safe' );
41+
* // returns true
42+
*
43+
* bool = isAllowedCast( 'float64', 'int32', 'safe' );
44+
* // returns false
45+
*/
46+
declare function isAllowedCast( from: ndarrayDataType, to: ndarrayDataType, casting: ndarrayCastingMode ): boolean; // tslint-disable-line max-line-length
47+
48+
49+
// EXPORTS //
50+
51+
export = isAllowedCast;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 isAllowedCast = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isAllowedCast( 'float32', 'float64', 'safe' ); // $ExpectType boolean
27+
isAllowedCast( 'float64', 'int32', 'safe' ); // $ExpectType boolean
28+
}
29+
30+
// The function does not compile if provided a first argument which is not a known ndarray data type...
31+
{
32+
isAllowedCast( 'abc', 'int32', 'safe' ); // $ExpectError
33+
isAllowedCast( true, 'int32', 'safe' ); // $ExpectError
34+
isAllowedCast( false, 'int32', 'safe' ); // $ExpectError
35+
isAllowedCast( null, 'int32', 'safe' ); // $ExpectError
36+
isAllowedCast( undefined, 'int32', 'safe' ); // $ExpectError
37+
isAllowedCast( 123, 'int32', 'safe' ); // $ExpectError
38+
isAllowedCast( [], 'int32', 'safe' ); // $ExpectError
39+
isAllowedCast( {}, 'int32', 'safe' ); // $ExpectError
40+
isAllowedCast( ( x: number ): number => x, 'int32', 'safe' ); // $ExpectError
41+
}
42+
43+
// The function does not compile if provided a second argument which is not a known ndarray data type...
44+
{
45+
isAllowedCast( 'float64', 'abc', 'safe' ); // $ExpectError
46+
isAllowedCast( 'float64', true, 'safe' ); // $ExpectError
47+
isAllowedCast( 'float64', false, 'safe' ); // $ExpectError
48+
isAllowedCast( 'float64', null, 'safe' ); // $ExpectError
49+
isAllowedCast( 'float64', undefined, 'safe' ); // $ExpectError
50+
isAllowedCast( 'float64', 123, 'safe' ); // $ExpectError
51+
isAllowedCast( 'float64', [], 'safe' ); // $ExpectError
52+
isAllowedCast( 'float64', {}, 'safe' ); // $ExpectError
53+
isAllowedCast( 'float64', ( x: number ): number => x, 'safe' ); // $ExpectError
54+
}
55+
56+
// The function does not compile if provided a third argument which is not a known ndarray casting mode...
57+
{
58+
isAllowedCast( 'float64', 'int32', 'abc' ); // $ExpectError
59+
isAllowedCast( 'float64', 'int32', true ); // $ExpectError
60+
isAllowedCast( 'float64', 'int32', false ); // $ExpectError
61+
isAllowedCast( 'float64', 'int32', null ); // $ExpectError
62+
isAllowedCast( 'float64', 'int32', undefined ); // $ExpectError
63+
isAllowedCast( 'float64', 'int32', 123 ); // $ExpectError
64+
isAllowedCast( 'float64', 'int32', [] ); // $ExpectError
65+
isAllowedCast( 'float64', 'int32', {} ); // $ExpectError
66+
isAllowedCast( 'float64', 'int32', ( x: number ): number => x ); // $ExpectError
67+
}
68+
69+
// The compiler throws an error if the function is provided an unsupported number of arguments...
70+
{
71+
isAllowedCast(); // $ExpectError
72+
isAllowedCast( 'float64' ); // $ExpectError
73+
isAllowedCast( 'float64', 'int32' ); // $ExpectError
74+
}

lib/node_modules/@stdlib/ndarray/base/assert/is-allowed-data-type-cast/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)