Skip to content

Commit 02e40e6

Browse files
committed
Add Typescript definition
1 parent 365abfe commit 02e40e6

File tree

2 files changed

+279
-0
lines changed

2 files changed

+279
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
* Callback invoked after after opening a file.
27+
*
28+
* @param err - error argument
29+
* @param fd - file descriptor
30+
*/
31+
type Callback = ( err: Error, fd: number ) => void;
32+
33+
/**
34+
* Interface for opening a file.
35+
*/
36+
interface Open {
37+
/**
38+
* Asynchronously opens a file.
39+
*
40+
* @param path - file path
41+
* @param flags - file system flags (default: 'r')
42+
* @param mode - file mode (default: 0o666)
43+
* @param clbk - callback to invoke after opening a file
44+
*
45+
* @example
46+
* var closeSync = require( `@stdlib/fs/close` ).sync;
47+
*
48+
* function onOpen( error, fd ) {
49+
* if ( error ) {
50+
* throw error;
51+
* }
52+
* closeSync( fd );
53+
* }
54+
* open( __filename, 'r+', 438, onOpen );
55+
*/
56+
( path: string | Buffer, flags: string | number, mode: number, clbk: Callback ): void; // tslint-disable-line max-line-length
57+
58+
/**
59+
* Asynchronously opens a file.
60+
*
61+
* @param path - file path
62+
* @param flags - file system flags (default: 'r')
63+
* @param clbk - callback to invoke after opening a file
64+
*
65+
* @example
66+
* var closeSync = require( `@stdlib/fs/close` ).sync;
67+
*
68+
* function onOpen( error, fd ) {
69+
* if ( error ) {
70+
* throw error;
71+
* }
72+
* closeSync( fd );
73+
* }
74+
* open( __filename, 'r+', onOpen );
75+
*/
76+
( path: string | Buffer, flags: string | number, clbk: Callback ): void;
77+
78+
/**
79+
* Asynchronously opens a file.
80+
*
81+
* @param path - file path
82+
* @param clbk - callback to invoke after opening a file
83+
*
84+
* @example
85+
* var closeSync = require( `@stdlib/fs/close` ).sync;
86+
*
87+
* function onOpen( error, fd ) {
88+
* if ( error ) {
89+
* throw error;
90+
* }
91+
* closeSync( fd );
92+
* }
93+
* open( __filename, onOpen );
94+
*/
95+
( path: string | Buffer, clbk: Callback ): void;
96+
97+
/**
98+
* Synchronously opens a file.
99+
*
100+
* @param path - file path
101+
* @param flags - file system flags (default: 'r')
102+
* @param mode - file mode (default: 0o666)
103+
* @returns file descriptor or an error
104+
*
105+
* @example
106+
* var closeSync = require( `@stdlib/fs/close` ).sync;
107+
*
108+
* var fd = open.sync( __filename, 'r+' );
109+
* if ( fd instanceof Error ) {
110+
* throw fd;
111+
* }
112+
* closeSync( fd );
113+
*/
114+
sync( path: string | Buffer, flags?: string | number, mode?: number ): number | Error; // tslint-disable-line max-line-length
115+
}
116+
117+
/**
118+
* Asynchronously opens a file.
119+
*
120+
* @param path - file path
121+
* @param flags - file system flags (default: 'r')
122+
* @param mode - file mode (default: 0o666)
123+
* @param clbk - callback to invoke after opening a file
124+
*
125+
* @example
126+
* var closeSync = require( `@stdlib/fs/close` ).sync;
127+
*
128+
* function onOpen( error, fd ) {
129+
* if ( error ) {
130+
* throw error;
131+
* }
132+
* closeSync( fd );
133+
* }
134+
* open( __filename, onOpen );
135+
*
136+
* @example
137+
* var closeSync = require( `@stdlib/fs/close` ).sync;
138+
*
139+
* var fd = open.sync( __filename );
140+
* if ( fd instanceof Error ) {
141+
* throw fd;
142+
* }
143+
* closeSync( fd );
144+
*/
145+
declare var open: Open;
146+
147+
148+
// EXPORTS //
149+
150+
export = open;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 open = require( './index' );
20+
21+
const done = ( error: Error, fd: number ) => {
22+
if ( error || !fd ) {
23+
throw error;
24+
}
25+
};
26+
27+
28+
// TESTS //
29+
30+
// The function does not have a return value...
31+
{
32+
open( '/path/to/beepboop', done ); // $ExpectType void
33+
}
34+
35+
// The compiler throws an error if the function is provided a first argument which is not a string...
36+
{
37+
open( 123, done ); // $ExpectError
38+
open( false, done ); // $ExpectError
39+
open( true, done ); // $ExpectError
40+
open( null, done ); // $ExpectError
41+
open( undefined, done ); // $ExpectError
42+
open( [], done ); // $ExpectError
43+
open( {}, done ); // $ExpectError
44+
open( ( x: number ): number => x, done ); // $ExpectError
45+
}
46+
47+
// The compiler throws an error if the function is provided a last argument which is not a function with the expected signature...
48+
{
49+
open( '/path/to/beepboop', 'abc' ); // $ExpectError
50+
open( '/path/to/beepboop', 123 ); // $ExpectError
51+
open( '/path/to/beepboop', false ); // $ExpectError
52+
open( '/path/to/beepboop', true ); // $ExpectError
53+
open( '/path/to/beepboop', null ); // $ExpectError
54+
open( '/path/to/beepboop', undefined ); // $ExpectError
55+
open( '/path/to/beepboop', [] ); // $ExpectError
56+
open( '/path/to/beepboop', {} ); // $ExpectError
57+
open( '/path/to/beepboop', ( x: number ): number => x ); // $ExpectError
58+
}
59+
60+
// The compiler throws an error if the function is provided a `flags` argument which is not a string or number...
61+
{
62+
open( '/path/to/beepboop', false, done ); // $ExpectError
63+
open( '/path/to/beepboop', true, done ); // $ExpectError
64+
open( '/path/to/beepboop', null, done ); // $ExpectError
65+
open( '/path/to/beepboop', undefined, done ); // $ExpectError
66+
open( '/path/to/beepboop', [], done ); // $ExpectError
67+
open( '/path/to/beepboop', {}, done ); // $ExpectError
68+
open( '/path/to/beepboop', ( x: number ): number => x, done ); // $ExpectError
69+
}
70+
71+
// The compiler throws an error if the function is provided a `mode` argument which is not a number...
72+
{
73+
open( '/path/to/beepboop', 'r+', 'abc', done ); // $ExpectError
74+
open( '/path/to/beepboop', 'r+', false, done ); // $ExpectError
75+
open( '/path/to/beepboop', 'r+', true, done ); // $ExpectError
76+
open( '/path/to/beepboop', 'r+', null, done ); // $ExpectError
77+
open( '/path/to/beepboop', 'r+', undefined, done ); // $ExpectError
78+
open( '/path/to/beepboop', 'r+', [], done ); // $ExpectError
79+
open( '/path/to/beepboop', 'r+', {}, done ); // $ExpectError
80+
open( '/path/to/beepboop', 'r+', ( x: number ): number => x, done ); // $ExpectError
81+
}
82+
83+
// The compiler throws an error if the function is provided an unsupported number of arguments...
84+
{
85+
open(); // $ExpectError
86+
open( 'C:\\foo\\bar\\baz' ); // $ExpectError
87+
}
88+
89+
// Attached to main export is a `sync` method which returns a number or an error...
90+
{
91+
open.sync( '/path/to/beepboop' ); // $ExpectType number | Error
92+
}
93+
94+
// The compiler throws an error if the `sync` method is provided a first argument which is not a string...
95+
{
96+
open.sync( 123 ); // $ExpectError
97+
open.sync( false ); // $ExpectError
98+
open.sync( true ); // $ExpectError
99+
open.sync( null ); // $ExpectError
100+
open.sync( undefined ); // $ExpectError
101+
open.sync( [] ); // $ExpectError
102+
open.sync( {} ); // $ExpectError
103+
open.sync( ( x: number ): number => x ); // $ExpectError
104+
}
105+
106+
// The compiler throws an error if the `sync` method is provided a second argument which is not a string or number...
107+
{
108+
open.sync( '/path/to/beepboop', false ); // $ExpectError
109+
open.sync( '/path/to/beepboop', true ); // $ExpectError
110+
open.sync( '/path/to/beepboop', null ); // $ExpectError
111+
open.sync( '/path/to/beepboop', [] ); // $ExpectError
112+
open.sync( '/path/to/beepboop', {} ); // $ExpectError
113+
open.sync( '/path/to/beepboop', ( x: number ): number => x ); // $ExpectError
114+
}
115+
116+
// The compiler throws an error if the `sync` method is provided a third argument which is not a number...
117+
{
118+
open.sync( '/path/to/beepboop', 'r+', false ); // $ExpectError
119+
open.sync( '/path/to/beepboop', 'r+', true ); // $ExpectError
120+
open.sync( '/path/to/beepboop', 'r+', null ); // $ExpectError
121+
open.sync( '/path/to/beepboop', 'r+', [] ); // $ExpectError
122+
open.sync( '/path/to/beepboop', 'r+', {} ); // $ExpectError
123+
open.sync( '/path/to/beepboop', 'r+', ( x: number ): number => x ); // $ExpectError
124+
}
125+
126+
// The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
127+
{
128+
open.sync(); // $ExpectError
129+
}

0 commit comments

Comments
 (0)