Skip to content

Commit 7b129a5

Browse files
committed
Add Typescript definition
1 parent c8ccb22 commit 7b129a5

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
/// <reference types="node"/>
23+
24+
import { ArrayLike } from '@stdlib/types/array';
25+
import { Order } from '@stdlib/types/ndarray';
26+
import { Buffer } from 'buffer';
27+
28+
29+
/**
30+
* Converts an ndarray buffer to a generic array (which may include nested arrays).
31+
*
32+
* @param buffer - data buffer
33+
* @param shape - array shape
34+
* @param strides - array strides
35+
* @param offset - index offset
36+
* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
37+
* @returns array (which may include nested arrays)
38+
*
39+
* @example
40+
* var buffer = [ 1, 2, 3, 4 ];
41+
* var shape = [ 2, 2 ];
42+
* var order = 'row-major';
43+
* var strides = [ 2, 1 ];
44+
* var offset = 0;
45+
*
46+
* var out = ndarray2array( buffer, shape, strides, offset, order );
47+
* // returns [ [ 1, 2 ], [ 3, 4 ] ]
48+
*/
49+
declare function ndarray2array( buffer: ArrayLike<any> | Buffer, shape: ArrayLike<number>, strides: ArrayLike<number>, offset: number, order: Order ): Array<any>; // tslint-disable-line max-line-length
50+
51+
52+
// EXPORTS //
53+
54+
export = ndarray2array;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 ndarray2array = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array...
25+
{
26+
ndarray2array( [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); // $ExpectType any[]
27+
ndarray2array( [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'column-major' ); // $ExpectType any[]
28+
}
29+
30+
// The function does not compile if provided a first argument which is not an array-like object or buffer...
31+
{
32+
const shape = [ 2, 2 ];
33+
const strides = [ 2, 1 ];
34+
const offset = 0;
35+
const order = 'row-major';
36+
ndarray2array( 123, shape, strides, offset, order ); // $ExpectError
37+
ndarray2array( true, shape, strides, offset, order ); // $ExpectError
38+
ndarray2array( false, shape, strides, offset, order ); // $ExpectError
39+
ndarray2array( null, shape, strides, offset, order ); // $ExpectError
40+
ndarray2array( undefined, shape, strides, offset, order ); // $ExpectError
41+
}
42+
43+
// The function does not compile if provided a second argument which is not an array-like object containing numbers...
44+
{
45+
const buffer = [ 1, 2, 3, 4 ];
46+
const strides = [ 2, 1 ];
47+
const offset = 0;
48+
const order = 'row-major';
49+
ndarray2array( buffer, true, strides, offset, order ); // $ExpectError
50+
ndarray2array( buffer, false, strides, offset, order ); // $ExpectError
51+
ndarray2array( buffer, null, strides, offset, order ); // $ExpectError
52+
ndarray2array( buffer, undefined, strides, offset, order ); // $ExpectError
53+
ndarray2array( buffer, '5', strides, offset, order ); // $ExpectError
54+
ndarray2array( buffer, [ '1', '2' ], strides, offset, order ); // $ExpectError
55+
ndarray2array( buffer, {}, strides, offset, order ); // $ExpectError
56+
ndarray2array( buffer, ( x: number ): number => x, strides, offset, order ); // $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 buffer = [ 1, 2, 3, 4 ];
62+
const shape = [ 2, 2 ];
63+
const offset = 0;
64+
const order = 'row-major';
65+
ndarray2array( buffer, shape, true, offset, order ); // $ExpectError
66+
ndarray2array( buffer, shape, false, offset, order ); // $ExpectError
67+
ndarray2array( buffer, shape, null, offset, order ); // $ExpectError
68+
ndarray2array( buffer, shape, undefined, offset, order ); // $ExpectError
69+
ndarray2array( buffer, shape, '5', offset, order ); // $ExpectError
70+
ndarray2array( buffer, shape, [ '1', '2' ], offset, order ); // $ExpectError
71+
ndarray2array( buffer, shape, {}, offset, order ); // $ExpectError
72+
ndarray2array( buffer, shape, ( x: number ): number => x, offset, order ); // $ExpectError
73+
}
74+
75+
// The function does not compile if provided a fourth argument which is not a number...
76+
{
77+
const buffer = [ 1, 2, 3, 4 ];
78+
const shape = [ 2, 2 ];
79+
const strides = [ 2, 1 ];
80+
const order = 'row-major';
81+
ndarray2array( buffer, shape, strides, true, order ); // $ExpectError
82+
ndarray2array( buffer, shape, strides, false, order ); // $ExpectError
83+
ndarray2array( buffer, shape, strides, null, order ); // $ExpectError
84+
ndarray2array( buffer, shape, strides, undefined, order ); // $ExpectError
85+
ndarray2array( buffer, shape, strides, '5', order ); // $ExpectError
86+
ndarray2array( buffer, shape, strides, [ '1', '2' ], order ); // $ExpectError
87+
ndarray2array( buffer, shape, strides, {}, order ); // $ExpectError
88+
ndarray2array( buffer, shape, strides, ( x: number ): number => x, order ); // $ExpectError
89+
}
90+
91+
// The function does not compile if provided a fifth argument which is not a known array order...
92+
{
93+
const buffer = [ 1, 2, 3, 4 ];
94+
const shape = [ 2, 2 ];
95+
const strides = [ 2, 1 ];
96+
const offset = 0;
97+
ndarray2array( buffer, shape, strides, offset, true ); // $ExpectError
98+
ndarray2array( buffer, shape, strides, offset, false ); // $ExpectError
99+
ndarray2array( buffer, shape, strides, offset, null ); // $ExpectError
100+
ndarray2array( buffer, shape, strides, offset, undefined ); // $ExpectError
101+
ndarray2array( buffer, shape, strides, offset, '5' ); // $ExpectError
102+
ndarray2array( buffer, shape, strides, offset, [ '1', '2' ] ); // $ExpectError
103+
ndarray2array( buffer, shape, strides, offset, {} ); // $ExpectError
104+
ndarray2array( buffer, shape, strides, offset, ( x: number ): number => x ); // $ExpectError
105+
}
106+
107+
// The function does not compile if provided insufficient arguments...
108+
{
109+
const buffer = [ 1, 2, 3, 4 ];
110+
const shape = [ 2, 2 ];
111+
const strides = [ 2, 1 ];
112+
const offset = 0;
113+
ndarray2array(); // $ExpectError
114+
ndarray2array( buffer ); // $ExpectError
115+
ndarray2array( buffer, shape ); // $ExpectError
116+
ndarray2array( buffer, shape, strides ); // $ExpectError
117+
ndarray2array( buffer, shape, strides, offset ); // $ExpectError
118+
}

lib/node_modules/@stdlib/ndarray/base/to-array/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)