Skip to content

Commit d6ddfcd

Browse files
committed
Add Typescript definition
1 parent 6ae6481 commit d6ddfcd

2 files changed

Lines changed: 200 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)