Skip to content

Commit bd8ce00

Browse files
committed
Add Typescript definitions
1 parent 82a20b6 commit bd8ce00

File tree

12 files changed

+415
-0
lines changed

12 files changed

+415
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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="node"/>
22+
/// <reference types="@stdlib/types"/>
23+
24+
import { Buffer } from 'buffer';
25+
import { Collection } from '@stdlib/types/object';
26+
27+
/**
28+
* Allocates a buffer using an octet array.
29+
*
30+
* @param arr - octet array
31+
* @returns new `Buffer` instance
32+
*
33+
* @example
34+
* var fromArray = require( `@stdlib/buffer/from-array` );
35+
*
36+
* var buf = fromArray( [ 1, 2, 3, 4 ] );
37+
* // returns <Buffer>
38+
*/
39+
declare function fromArray( arr: Collection ): Buffer;
40+
41+
42+
// EXPORTS //
43+
44+
export = fromArray;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 fromArray = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a buffer...
25+
{
26+
fromArray( [ 1, 2, 3, 4 ] ); // $ExpectType Buffer
27+
}
28+
29+
// The compiler throws an error if the function is provided a first argument which is not a collection...
30+
{
31+
fromArray( 123 ); // $ExpectError
32+
fromArray( false ); // $ExpectError
33+
fromArray( true ); // $ExpectError
34+
fromArray( null ); // $ExpectError
35+
fromArray( {} ); // $ExpectError
36+
}
37+
38+
// The compiler throws an error if the function is provided an invalid number of arguments...
39+
{
40+
fromArray(); // $ExpectError
41+
fromArray( [ 1, 2, 3, 4 ], 4 ); // $ExpectError
42+
}

lib/node_modules/@stdlib/buffer/from-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": {
Lines changed: 64 additions & 0 deletions
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="node"/>
22+
23+
import { Buffer } from 'buffer';
24+
25+
/**
26+
* Allocates a buffer from an `ArrayBuffer`.
27+
*
28+
* ## Notes
29+
*
30+
* The behavior of this function varies across Node.js versions due to changes in the underlying Node.js APIs:
31+
*
32+
* - `<6.0.0`: if provided an empty ArrayBuffer, the function returns an empty Buffer which is **not** an ArrayBuffer view.
33+
* - otherwise, the function returns a view of an ArrayBuffer without copying the underlying memory.
34+
*
35+
*
36+
* @param buf - ArrayBuffer instance
37+
* @param byteOffset - index specifying the location of the first buffer byte (default: 0)
38+
* @param length - number of buffer bytes (default: buf.byteLength)
39+
* @throws second argument must be a nonnegative integer
40+
* @throws second argument must not exceed number of bytes in input ArrayBuffer
41+
* @throws last argument must be a nonnegative integer
42+
* @throws last argument must not exceed number of bytes in input ArrayBuffer
43+
* @returns new `Buffer` instance
44+
*
45+
* @example
46+
* var ArrayBuffer = require( `@stdlib/array/buffer` );
47+
* var ab = new ArrayBuffer( 10 );
48+
*
49+
* var buf = fromArrayBuffer( ab );
50+
* // returns <Buffer>
51+
*
52+
* @example
53+
* var ArrayBuffer = require( `@stdlib/array/buffer` );
54+
* var ab = new ArrayBuffer( 10 );
55+
*
56+
* var buf = fromArrayBuffer( ab, 2, 4 );
57+
* // returns <Buffer>
58+
*/
59+
declare function fromArrayBuffer( buf: ArrayBuffer, byteOffset?: number, length?: number ): Buffer; // tslint-disable-line max-line-length
60+
61+
62+
// EXPORTS //
63+
64+
export = fromArrayBuffer;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 fromArrayBuffer = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a buffer...
25+
{
26+
const buf = new ArrayBuffer( 10 );
27+
fromArrayBuffer( buf ); // $ExpectType Buffer
28+
fromArrayBuffer( buf, 2 ); // $ExpectType Buffer
29+
fromArrayBuffer( buf, 2, 4 ); // $ExpectType Buffer
30+
}
31+
32+
// The compiler throws an error if the function is provided a first argument which is not an array buffer...
33+
{
34+
fromArrayBuffer( 'abc' ); // $ExpectError
35+
fromArrayBuffer( 123 ); // $ExpectError
36+
fromArrayBuffer( false ); // $ExpectError
37+
fromArrayBuffer( true ); // $ExpectError
38+
fromArrayBuffer( null ); // $ExpectError
39+
fromArrayBuffer( {} ); // $ExpectError
40+
fromArrayBuffer( ( x: number ): number => x ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided a second argument which is not a number...
44+
{
45+
const buf = new ArrayBuffer( 10 );
46+
fromArrayBuffer( buf, 'abc' ); // $ExpectError
47+
fromArrayBuffer( buf, false ); // $ExpectError
48+
fromArrayBuffer( buf, true ); // $ExpectError
49+
fromArrayBuffer( buf, null ); // $ExpectError
50+
fromArrayBuffer( buf, [] ); // $ExpectError
51+
fromArrayBuffer( buf, {} ); // $ExpectError
52+
fromArrayBuffer( buf, ( x: number ): number => x ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided a third argument which is not a number...
56+
{
57+
const buf = new ArrayBuffer( 10 );
58+
fromArrayBuffer( buf, 2, 'abc' ); // $ExpectError
59+
fromArrayBuffer( buf, 2, false ); // $ExpectError
60+
fromArrayBuffer( buf, 2, true ); // $ExpectError
61+
fromArrayBuffer( buf, 2, null ); // $ExpectError
62+
fromArrayBuffer( buf, 2, [] ); // $ExpectError
63+
fromArrayBuffer( buf, 2, {} ); // $ExpectError
64+
fromArrayBuffer( buf, 2, ( x: number ): number => x ); // $ExpectError
65+
}
66+
67+
// The compiler throws an error if the function is provided an invalid number of arguments...
68+
{
69+
const buf = new ArrayBuffer( 10 );
70+
fromArrayBuffer(); // $ExpectError
71+
fromArrayBuffer( buf, 2, 4, 2 ); // $ExpectError
72+
}

lib/node_modules/@stdlib/buffer/from-arraybuffer/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": {
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+
// TypeScript Version: 2.0
20+
21+
/// <reference types="node"/>
22+
23+
import { Buffer } from 'buffer';
24+
25+
/**
26+
* Copies buffer data to a new `Buffer` instance.
27+
*
28+
* @param buffer - buffer from which to copy
29+
* @returns new `Buffer` instance
30+
*
31+
* @example
32+
* var fromArray = require( `@stdlib/buffer/from-array` );
33+
*
34+
* var b1 = fromArray( [ 1, 2, 3, 4 ] );
35+
* // returns <Buffer>
36+
*
37+
* var b2 = fromBuffer( b1 );
38+
* // returns <Buffer>
39+
*/
40+
declare function fromBuffer( buffer: Buffer ): Buffer;
41+
42+
43+
// EXPORTS //
44+
45+
export = fromBuffer;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 fromArray = require( '@stdlib/buffer/from-array' );
20+
import fromBuffer = require( './index' );
21+
22+
// TESTS //
23+
24+
// The function returns a buffer...
25+
{
26+
const buf = fromArray( [ 1, 2, 3, 4 ] );
27+
fromBuffer( buf ); // $ExpectType Buffer
28+
}
29+
30+
// The compiler throws an error if the function is provided a first argument which is not a buffer...
31+
{
32+
fromBuffer( 'abc' ); // $ExpectError
33+
fromBuffer( 123 ); // $ExpectError
34+
fromBuffer( false ); // $ExpectError
35+
fromBuffer( true ); // $ExpectError
36+
fromBuffer( null ); // $ExpectError
37+
fromBuffer( {} ); // $ExpectError
38+
fromBuffer( [] ); // $ExpectError
39+
fromBuffer( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided an invalid number of arguments...
43+
{
44+
const buf = fromArray( [ 1, 2, 3, 4 ] );
45+
fromBuffer(); // $ExpectError
46+
fromBuffer( buf, 4 ); // $ExpectError
47+
}

lib/node_modules/@stdlib/buffer/from-buffer/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": {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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="node"/>
22+
23+
import { Buffer } from 'buffer';
24+
25+
/**
26+
* Allocates a buffer containing a provided string.
27+
*
28+
* @param str - input string
29+
* @param encoding - character encoding (default: 'utf8')
30+
* @throws second argument must be a valid encoding
31+
* @returns new `Buffer` instance
32+
*
33+
* @example
34+
* var buf = fromString( 'beep boop' );
35+
* // returns <Buffer>
36+
*/
37+
declare function fromString( str: string, encoding?: string ): Buffer;
38+
39+
40+
// EXPORTS //
41+
42+
export = fromString;

0 commit comments

Comments
 (0)