Skip to content

Commit 819d2e4

Browse files
Planeshifterkgryte
andauthored
feat: add data type maps and replace use of overloads
PR-URL: stdlib-js#1317 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 39b8176 commit 819d2e4

File tree

22 files changed

+239
-3197
lines changed

22 files changed

+239
-3197
lines changed

lib/node_modules/@stdlib/array/convert/docs/types/index.d.ts

Lines changed: 2 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -20,175 +20,7 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { AnyArray, Collection, Complex128Array, Complex64Array, DataType } from '@stdlib/types/array';
24-
25-
/**
26-
* Converts an array to a `Float64Array`.
27-
*
28-
* @param x - array to convert
29-
* @param dtype - output data type
30-
* @returns output array
31-
*
32-
* @example
33-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
34-
* var out = convert( arr, 'float64' );
35-
* // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
36-
*/
37-
declare function convert( x: Collection, dtype: 'float64' ): Float64Array;
38-
39-
/**
40-
* Converts an array to a `Float32Array`.
41-
*
42-
* @param x - array to convert
43-
* @param dtype - output data type
44-
* @returns output array
45-
*
46-
* @example
47-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
48-
* var out = convert( arr, 'float32' );
49-
* // returns <Float32Array>[ 1.0, 2.0, 3.0, 4.0 ]
50-
*/
51-
declare function convert( x: Collection, dtype: 'float32' ): Float32Array;
52-
53-
/**
54-
* Converts an array to an `Int32Array`.
55-
*
56-
* @param x - array to convert
57-
* @param dtype - output data type
58-
* @returns output array
59-
*
60-
* @example
61-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
62-
* var out = convert( arr, 'int32' );
63-
* // returns <Int32Array>[ 1, 2, 3, 4 ]
64-
*/
65-
declare function convert( x: Collection, dtype: 'int32' ): Int32Array;
66-
67-
/**
68-
* Converts an array to an `Int16Array`.
69-
*
70-
* @param x - array to convert
71-
* @param dtype - output data type
72-
* @returns output array
73-
*
74-
* @example
75-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
76-
* var out = convert( arr, 'int16' );
77-
* // returns <Int16Array>[ 1, 2, 3, 4 ]
78-
*/
79-
declare function convert( x: Collection, dtype: 'int16' ): Int16Array;
80-
81-
/**
82-
* Converts an array to an `Int8Array`.
83-
*
84-
* @param x - array to convert
85-
* @param dtype - output data type
86-
* @returns output array
87-
*
88-
* @example
89-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
90-
* var out = convert( arr, 'int8' );
91-
* // returns <Int8Array>[ 1, 2, 3, 4 ]
92-
*/
93-
declare function convert( x: Collection, dtype: 'int8' ): Int8Array;
94-
95-
/**
96-
* Converts an array to a `Uint32Array`.
97-
*
98-
* @param x - array to convert
99-
* @param dtype - output data type
100-
* @returns output array
101-
*
102-
* @example
103-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
104-
* var out = convert( arr, 'uint32' );
105-
* // returns <Uint32Array>[ 1, 2, 3, 4 ]
106-
*/
107-
declare function convert( x: Collection, dtype: 'uint32' ): Uint32Array;
108-
109-
/**
110-
* Converts an array to a `Uint16Array`.
111-
*
112-
* @param x - array to convert
113-
* @param dtype - output data type
114-
* @returns output array
115-
*
116-
* @example
117-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
118-
* var out = convert( arr, 'uint16' );
119-
* // returns <Uint16Array>[ 1, 2, 3, 4 ]
120-
*/
121-
declare function convert( x: Collection, dtype: 'uint16' ): Uint16Array;
122-
123-
/**
124-
* Converts an array to a `Uint8Array`.
125-
*
126-
* @param x - array to convert
127-
* @param dtype - output data type
128-
* @returns output array
129-
*
130-
* @example
131-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
132-
* var out = convert( arr, 'uint8' );
133-
* // returns <Uint8Array>[ 1, 2, 3, 4 ]
134-
*/
135-
declare function convert( x: Collection, dtype: 'uint8' ): Uint8Array;
136-
137-
/**
138-
* Converts an array to a `Uint8ClampedArray`.
139-
*
140-
* @param x - array to convert
141-
* @param dtype - output data type
142-
* @returns output array
143-
*
144-
* @example
145-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
146-
* var out = convert( arr, 'uint8c' );
147-
* // returns <Uint8ClampedArray>[ 1, 2, 3, 4 ]
148-
*/
149-
declare function convert( x: Collection, dtype: 'uint8c' ): Uint8ClampedArray;
150-
151-
/**
152-
* Converts an array to a `Complex128Array`.
153-
*
154-
* @param x - array to convert
155-
* @param dtype - output data type
156-
* @returns output array
157-
*
158-
* @example
159-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
160-
* var out = convert( arr, 'complex128' );
161-
* // returns <Complex128>
162-
*/
163-
declare function convert( x: Collection, dtype: 'complex128' ): Complex128Array;
164-
165-
/**
166-
* Converts an array to a `Complex64Array`.
167-
*
168-
* @param x - array to convert
169-
* @param dtype - output data type
170-
* @returns output array
171-
*
172-
* @example
173-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
174-
* var out = convert( arr, 'complex64' );
175-
* // returns <Complex64>
176-
*/
177-
declare function convert( x: Collection, dtype: 'complex64' ): Complex64Array;
178-
179-
/**
180-
* Converts an array to an `Array`.
181-
*
182-
* @param x - array to convert
183-
* @param dtype - output data type
184-
* @returns output array
185-
*
186-
* @example
187-
* var arr = [ 1.0, 2.0, 3.0, 4.0 ];
188-
* var out = convert( arr, 'generic' );
189-
* // returns [ 1.0, 2.0, 3.0, 4.0 ]
190-
*/
191-
declare function convert( x: Collection, dtype: 'generic' ): Array<any>;
23+
import { Collection, DataTypeMap } from '@stdlib/types/array';
19224

19325
/**
19426
* Converts an array to an array of a different data type.
@@ -202,7 +34,7 @@ declare function convert( x: Collection, dtype: 'generic' ): Array<any>;
20234
* var out = convert( arr, 'float64' );
20335
* // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
20436
*/
205-
declare function convert( x: Collection, dtype: DataType ): AnyArray;
37+
declare function convert<T, U extends keyof DataTypeMap<T>>( x: Collection<T>, dtype: U ): DataTypeMap<T>[U];
20638

20739

20840
// EXPORTS //

lib/node_modules/@stdlib/array/convert/docs/types/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import convert = require( './index' );
3434
convert( [ 1.0, 2.0, 3.0, 4.0 ], 'uint8c' ); // $ExpectType Uint8ClampedArray
3535
convert( [ 1.0, 2.0, 3.0, 4.0 ], 'complex128' ); // $ExpectType Complex128Array
3636
convert( [ 1.0, 2.0, 3.0, 4.0 ], 'complex64' ); // $ExpectType Complex64Array
37+
convert( [ 1.0, 2.0, 3.0, 4.0 ], 'generic' ); // $ExpectType number[]
38+
convert( [ 'a', 'b', 'c', 'd' ], 'generic' ); // $ExpectType string[]
3739
}
3840

3941
// The compiler throws an error if the function is provided a first argument which is not array-like...

0 commit comments

Comments
 (0)