Skip to content

Commit 2592ba8

Browse files
committed
Add Typescript definition
1 parent 0301c9f commit 2592ba8

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 reading the contents of a directory.
27+
*/
28+
type Nullary = () => void;
29+
30+
/**
31+
* Callback invoked after reading the contents of a directory.
32+
*
33+
* @param err - error argument
34+
*/
35+
type Unary = ( err: Error ) => void;
36+
37+
/**
38+
* Callback invoked after reading the contents of a directory.
39+
*
40+
* @param err - error argument
41+
* @param data - directory contents
42+
*/
43+
type Binary = ( err: Error, data: Array<string> ) => void;
44+
45+
/**
46+
* Callback invoked after reading the contents of a directory.
47+
*
48+
* @param err - error argument
49+
* @param data - directory contents
50+
*/
51+
type Callback = Nullary | Unary | Binary;
52+
53+
/**
54+
* Interface for reading the contents of a directory.
55+
*/
56+
interface ReadDir {
57+
/**
58+
* Asynchronously reads the contents of a directory.
59+
*
60+
* @param path - directory path
61+
* @param clbk - callback to invoke after reading directory contents
62+
*
63+
* @example
64+
* function onRead( error, data ) {
65+
* if ( error ) {
66+
* console.error( error );
67+
* } else {
68+
* console.log( data );
69+
* }
70+
* }
71+
* readDir( __dirname, onRead );
72+
*/
73+
( path: string | Buffer, clbk: Callback ): void;
74+
75+
/**
76+
* Synchronously reads the contents of a directory.
77+
*
78+
* @param path - directory path
79+
* @returns directory contents or an error
80+
*
81+
* @example
82+
* var out = readDir.sync( __dirname );
83+
* if ( out instanceof Error ) {
84+
* throw out;
85+
* }
86+
* console.log( out );
87+
*/
88+
sync( path: string | Buffer ): Array<string> | Error;
89+
}
90+
91+
/**
92+
* Asynchronously reads the contents of a directory.
93+
*
94+
* @param path - directory path
95+
* @param clbk - callback to invoke after reading directory contents
96+
*
97+
* @example
98+
* function onRead( error, data ) {
99+
* if ( error ) {
100+
* console.error( error );
101+
* } else {
102+
* console.log( data );
103+
* }
104+
* }
105+
* readDir( __dirname, onRead );
106+
*
107+
* @example
108+
* var out = readDir.sync( __dirname );
109+
* if ( out instanceof Error ) {
110+
* throw out;
111+
* }
112+
* console.log( out );
113+
*/
114+
declare var readDir: ReadDir;
115+
116+
117+
// EXPORTS //
118+
119+
export = readDir;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 readDir = require( './index' );
20+
21+
const done = ( error: Error, data: Array<string> ) => {
22+
if ( error || !data.length ) {
23+
throw error;
24+
}
25+
};
26+
27+
28+
// TESTS //
29+
30+
// The function does not have a return value...
31+
{
32+
readDir( '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+
readDir( 123, done ); // $ExpectError
38+
readDir( false, done ); // $ExpectError
39+
readDir( true, done ); // $ExpectError
40+
readDir( null, done ); // $ExpectError
41+
readDir( undefined, done ); // $ExpectError
42+
readDir( [], done ); // $ExpectError
43+
readDir( {}, done ); // $ExpectError
44+
readDir( ( x: number ): number => x, done ); // $ExpectError
45+
}
46+
47+
// The compiler throws an error if the function is provided a second argument which is not a function with the expected signature...
48+
{
49+
readDir( '/var/log/', 1 ); // $ExpectError
50+
readDir( '/var/log/', false ); // $ExpectError
51+
readDir( '/var/log/', true ); // $ExpectError
52+
readDir( '/var/log/', null ); // $ExpectError
53+
readDir( '/var/log/', undefined ); // $ExpectError
54+
readDir( '/var/log/', [] ); // $ExpectError
55+
readDir( '/var/log/', {} ); // $ExpectError
56+
readDir( '/var/log/', ( x: number ): number => x ); // $ExpectError
57+
}
58+
59+
// The compiler throws an error if the function is provided an unsupported number of arguments...
60+
{
61+
readDir(); // $ExpectError
62+
readDir( 'C:\\foo\\bar\\baz' ); // $ExpectError
63+
}
64+
65+
// Attached to main export is a `sync` method which returns a string array...
66+
{
67+
readDir.sync( 'beepboop' ); // $ExpectType Error | string[]
68+
}
69+
70+
// The compiler throws an error if the `sync` method is provided a first argument which is not a string...
71+
{
72+
readDir.sync( 123 ); // $ExpectError
73+
readDir.sync( false ); // $ExpectError
74+
readDir.sync( true ); // $ExpectError
75+
readDir.sync( null ); // $ExpectError
76+
readDir.sync( undefined ); // $ExpectError
77+
readDir.sync( [] ); // $ExpectError
78+
readDir.sync( {} ); // $ExpectError
79+
readDir.sync( ( x: number ): number => x ); // $ExpectError
80+
}
81+
82+
// The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
83+
{
84+
readDir.sync(); // $ExpectError
85+
}

0 commit comments

Comments
 (0)