Skip to content

Commit 8dde7c1

Browse files
committed
Add Typescript definition
1 parent dcba08f commit 8dde7c1

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
// tslint:disable:unified-signatures
22+
23+
/// <reference types="@stdlib/types"/>
24+
25+
import { ArrayLike, TypedArray } from '@stdlib/types/array';
26+
import ArrayBuffer = require( '@stdlib/array/buffer' );
27+
28+
type DType = 'float64' | 'float32' | 'int32' | 'uint32' | 'int16' | 'uint16' | 'int8' | 'uint8' | 'uint8c'; // tslint:disable-line:max-line-length
29+
30+
/**
31+
* Creates a typed array.
32+
*
33+
* @param dtype - data type (default: 'float64')
34+
* @returns typed array
35+
*
36+
* @example
37+
* var arr = typedarray();
38+
* // returns <Float64Array>
39+
*
40+
* @example
41+
* var arr = typedarray( 'float32');
42+
* // returns <Float32Array>
43+
*/
44+
declare function typedarray( dtype?: DType ): TypedArray;
45+
46+
/**
47+
* Creates a typed array.
48+
*
49+
* @param length - typed array length
50+
* @param dtype - data type (default: 'float64')
51+
* @returns typed array
52+
*
53+
* @example
54+
* var arr = typedarray( 2 );
55+
* // returns <Float64Array>[ 0.0, 0.0 ]
56+
*
57+
* @example
58+
* var arr = typedarray( 2, 'float32' );
59+
* // returns <Float32Array>[ 0.0, 0.0 ]
60+
*/
61+
declare function typedarray( length: number, dtype?: DType ): TypedArray;
62+
63+
/**
64+
* Creates a typed array.
65+
*
66+
* @param typedarray - typed array from which to generate another typed array
67+
* @param dtype - data type (default: 'float64')
68+
* @returns typed array
69+
*
70+
* @example
71+
* var arr = typedarray( new Float64Array( 2 ) );
72+
* // returns <Float64Array>[ 0.0, 0.0 ]
73+
*
74+
* @example
75+
* var arr = typedarray( new Float64Array( 2 ), 'float32' );
76+
* // returns <Float32Array>[ 0.0, 0.0 ]
77+
*
78+
* @example
79+
* var arr1 = typedarray( [ 5, 3 ], 'int32' );
80+
* var arr2 = typedarray( arr1 );
81+
* // returns <Float64Array>[ 5.0, 3.0 ]
82+
*
83+
* @example
84+
* var arr1 = typedarray( [ 5, 3 ], 'int32' );
85+
* var arr2 = typedarray( arr1, 'uint32' );
86+
* // returns <Uint32Array>[ 5, 3 ]
87+
*/
88+
declare function typedarray( typedarray: TypedArray, dtype?: DType ): TypedArray; // tslint:disable-line:max-line-length
89+
90+
/**
91+
* Creates a typed array.
92+
*
93+
* @param obj - array-like object or iterable from which to generate a typed array
94+
* @param dtype - data type (default: 'float64')
95+
* @returns typed array
96+
*
97+
* @example
98+
* var arr = typedarray( [ 0.5, 0.5 ] );
99+
* // returns <Float64Array>[ 0.5, 0.5 ]
100+
*
101+
* @example
102+
* var arr = typedarray( [ 5, -3 ], 'int32' );
103+
* // returns <Int32Array>[ 5, -3 ]
104+
*/
105+
declare function typedarray( obj: ArrayLike<number> | Iterable<any>, dtype?: DType ): TypedArray; // tslint:disable-line:max-line-length
106+
107+
/**
108+
* Creates a typed array.
109+
*
110+
* @param buffer - underlying ArrayBuffer
111+
* @param dtype - data type (default: 'float64')
112+
* @returns typed array
113+
*
114+
* @example
115+
* var ArrayBuffer = require( `@stdlib/array/buffer` );
116+
*
117+
* var buf = new ArrayBuffer( 16 );
118+
* var arr = typedarray( buf );
119+
* // returns <Float64Array>[ 0.0, 0.0 ]
120+
*
121+
* @example
122+
* var ArrayBuffer = require( `@stdlib/array/buffer` );
123+
*
124+
* var buf = new ArrayBuffer( 16 );
125+
* var arr = typedarray( buf, 'float32' );
126+
* // returns <Float32Array>[ 0.0, 0.0, 0.0, 0.0 ]
127+
*/
128+
declare function typedarray( buffer: ArrayBuffer, dtype?: DType ): TypedArray;
129+
130+
/**
131+
* Creates a typed array.
132+
*
133+
* @param buffer - underlying ArrayBuffer
134+
* @param byteOffset - integer byte offset specifying the location of the first typed array element (default: 0)
135+
* @param dtype - data type (default: 'float64')
136+
* @returns typed array
137+
*
138+
* @example
139+
* var ArrayBuffer = require( `@stdlib/array/buffer` );
140+
*
141+
* var buf = new ArrayBuffer( 16 );
142+
* var arr = typedarray( buf, 8 );
143+
* // returns <Float64Array>[ 0.0 ]
144+
*
145+
* @example
146+
* var ArrayBuffer = require( `@stdlib/array/buffer` );
147+
*
148+
* var buf = new ArrayBuffer( 16 );
149+
* var arr = typedarray( buf, 8, 'float32' );
150+
* // returns <Float32Array>[ 0.0, 0.0 ]
151+
*/
152+
declare function typedarray( buffer: ArrayBuffer, byteOffset?: number, dtype?: DType ): TypedArray; // tslint:disable-line:max-line-length
153+
154+
/**
155+
* Creates a typed array.
156+
*
157+
* @param buffer - underlying ArrayBuffer
158+
* @param byteOffset - integer byte offset specifying the location of the first typed array element (default: 0)
159+
* @param length - view length; if not provided, the view spans from the byteOffset to the end of the underlying ArrayBuffer
160+
* @param dtype - data type (default: 'float64')
161+
* @returns typed array
162+
*
163+
* @example
164+
* var ArrayBuffer = require( `@stdlib/array/buffer` );
165+
*
166+
* var buf = new ArrayBuffer( 32 );
167+
* var arr = typedarray( buf, 8, 2 );
168+
* // returns <Float64Array>[ 0.0, 0.0 ]
169+
*
170+
* @example
171+
* var ArrayBuffer = require( `@stdlib/array/buffer` );
172+
*
173+
* var buf = new ArrayBuffer( 32 );
174+
* var arr = typedarray( buf, 8, 2, 'int32' );
175+
* // returns <Int32Array>[ 0, 0 ]
176+
*/
177+
declare function typedarray( buffer: ArrayBuffer, byteOffset?: number, length?: number, dtype?: DType ): TypedArray; // tslint:disable-line:max-line-length
178+
179+
180+
// EXPORTS //
181+
182+
export = typedarray;
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+
import typedarray = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a typed array..
25+
{
26+
typedarray(); // $ExpectType TypedArray
27+
typedarray( 'float32' ); // $ExpectType TypedArray
28+
typedarray( 10, 'float32' ); // $ExpectType TypedArray
29+
typedarray( [ 1, 2, 3 ], 'int32' ); // $ExpectType TypedArray
30+
}
31+
32+
// The compiler throws an error if the function is provided a first argument which is not a data type, number, array-like object, or typed array...
33+
{
34+
typedarray( true ); // $ExpectError
35+
typedarray( false ); // $ExpectError
36+
typedarray( {} ); // $ExpectError
37+
typedarray( null ); // $ExpectError
38+
typedarray( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided an unsupported number of arguments...
42+
{
43+
const buf = new ArrayBuffer( 32 );
44+
typedarray( buf, 8, 2, 'int32', {} ); // $ExpectError
45+
}

lib/node_modules/@stdlib/array/typed/package.json

Lines changed: 1 addition & 0 deletions
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)