Skip to content

Commit 6adb0c3

Browse files
committed
Add Typescript definition
1 parent 6e52135 commit 6adb0c3

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 { TypedArray } from '@stdlib/types/array';
24+
25+
/**
26+
* JSON representation of typed array.
27+
*/
28+
interface JSONRepresentation {
29+
/**
30+
* Typed array type.
31+
*/
32+
type: 'Float64Array' | 'Float32Array' | 'Int32Array' | 'Uint32Array' | 'Int16Array' | 'Uint16Array' | 'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray'; // tslint:disable-line:max-line-length
33+
34+
/**
35+
* Typed array data as a generic array.
36+
*/
37+
data: Array<number>;
38+
}
39+
40+
/**
41+
* Returns a JSON representation of a typed array.
42+
*
43+
* ## Notes
44+
*
45+
* - We build a JSON object representing a typed array similar to how Node.js `Buffer` objects are represented. See [Buffer][1].
46+
*
47+
* [1]: https://nodejs.org/api/buffer.html#buffer_buf_tojson
48+
*
49+
* @param arr - typed array to serialize
50+
* @returns JSON representation
51+
*
52+
* @example
53+
* var Float64Array = require( `@stdlib/array/float64` );
54+
*
55+
* var arr = new Float64Array( [ 5.0, 3.0 ] );
56+
* var json = toJSON( arr );
57+
* // returns { 'type': 'Float64Array', 'data': [ 5.0, 3.0 ] }
58+
*/
59+
declare function toJSON( arr: TypedArray ): JSONRepresentation;
60+
61+
62+
// EXPORTS //
63+
64+
export = toJSON;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 toJSON = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a JSON representation...
25+
{
26+
const x = new Float64Array( 10 );
27+
28+
toJSON( x ); // $ExpectType JSONRepresentation
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not array-like...
32+
{
33+
toJSON( 'abc' ); // $ExpectError
34+
toJSON( 123 ); // $ExpectError
35+
toJSON( true ); // $ExpectError
36+
toJSON( false ); // $ExpectError
37+
toJSON( {} ); // $ExpectError
38+
toJSON( [] ); // $ExpectError
39+
toJSON( null ); // $ExpectError
40+
toJSON( undefined ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided an unsupported number of arguments...
44+
{
45+
const x = new Float64Array( 10 );
46+
47+
toJSON(); // $ExpectError
48+
toJSON( x, 3 ); // $ExpectError
49+
}

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