Skip to content

Commit ead614f

Browse files
committed
Add Typescript definition
1 parent 8384f78 commit ead614f

File tree

4 files changed

+289
-0
lines changed

4 files changed

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

lib/node_modules/@stdlib/fs/read-file/lib/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var readfile = require( 'fs' ).readFile;
3030
*
3131
* @param {(string|Buffer|integer)} file - file path or file descriptor
3232
* @param {(Object|string)} [options] - options
33+
* @param {(string|null)} [options.encoding] - file encoding
34+
* @param {string} [options.flag] - file status flag
3335
* @param {Function} clbk - callback to invoke after reading file contents
3436
*
3537
* @example

lib/node_modules/@stdlib/fs/read-file/lib/sync.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var readfileSync = require( 'fs' ).readFileSync; // eslint-disable-line no-sync
3030
*
3131
* @param {(string|Buffer|integer)} file - file path or file descriptor
3232
* @param {(Object|string)} [options] - options
33+
* @param {(string|null)} [options.encoding] - file encoding
34+
* @param {string} [options.flag] - file status flag
3335
* @returns {(Buffer|string|Error)} file contents or an error
3436
*
3537
* @example

0 commit comments

Comments
 (0)