Skip to content

Commit d95e986

Browse files
committed
Add Typescript definitions
1 parent 7ecfcfb commit d95e986

File tree

2 files changed

+339
-0
lines changed

2 files changed

+339
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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. If the encoding option is set to `utf8` and the file has a UTF-8 byte order mark (BOM), the byte order mark is *removed* before attempting to parse as JSON (default: null).
31+
*/
32+
encoding?: string | null;
33+
34+
/**
35+
* Flag (default: 'r').
36+
*/
37+
flag?: string;
38+
39+
/**
40+
* JSON transformation function.
41+
*/
42+
reviver?: Reviver;
43+
}
44+
45+
/**
46+
* JSON transformation function.
47+
*
48+
* @param key - key name
49+
* @param value - corresponding value
50+
* @returns updated value
51+
*/
52+
type Reviver = ( key: string, value: any ) => any;
53+
54+
/**
55+
* Callback invoked upon reading a file.
56+
*
57+
* @param err - error object
58+
* @param file - file contents
59+
*/
60+
type Callback = ( err: Error | null, file: Buffer | string ) => void;
61+
62+
/**
63+
* Interface for reading a file as JSON.
64+
*/
65+
interface ReadJSON {
66+
/**
67+
* Asynchronously reads a file as JSON.
68+
*
69+
* @param file - file path or file descriptor
70+
* @param options - options
71+
* @param options.encoding - file encoding
72+
* @param options.flag - file status flag
73+
* @param options.reviver - JSON reviver
74+
* @param clbk - callback
75+
*
76+
* @example
77+
* var resolve = require( `path` ).resolve;
78+
*
79+
* readJSON( resolve( __dirname, '..', 'package.json' ), 'utf-8', onJSON );
80+
*
81+
* function onJSON( error, data ) {
82+
* if ( error ) {
83+
* throw error;
84+
* }
85+
* console.dir( data );
86+
* }
87+
*/
88+
( file: string | Buffer | number, options: Options | string, clbk: Callback ): void; // tslint-disable-line max-line-length
89+
90+
/**
91+
* Asynchronously reads a file as JSON.
92+
*
93+
* @param file - file path or file descriptor
94+
* @param clbk - callback
95+
*
96+
* @example
97+
* var resolve = require( 'path' ).resolve;
98+
*
99+
* readJSON( resolve( __dirname, '..', 'package.json' ), onJSON );
100+
*
101+
* function onJSON( error, data ) {
102+
* if ( error ) {
103+
* throw error;
104+
* }
105+
* console.dir( data );
106+
* }
107+
*/
108+
( file: string | Buffer | number, clbk: Callback ): void;
109+
110+
/**
111+
* Synchronously reads a file as JSON.
112+
*
113+
* @param file - file path or file descriptor
114+
* @param options - options
115+
* @param options.encoding - file encoding
116+
* @param options.flag - file status flag
117+
* @param options.reviver - JSON reviver
118+
* @returns JSON or an error
119+
*
120+
* @example
121+
* var resolve = require( `path` ).resolve;
122+
* var instanceOf = require( '@stdlib/assert/instance-of' );
123+
*
124+
* var out = readJSONSync( resolve( __dirname, '..', 'package.json' ) );
125+
* if ( instanceOf( out, Error ) ) {
126+
* throw out;
127+
* }
128+
* console.dir( out );
129+
*/
130+
sync( file: string | Buffer | number, options?: Options | string ): string | Error; // tslint-disable-line max-line-length
131+
}
132+
133+
/**
134+
* Asynchronously reads a file as JSON.
135+
*
136+
* @param file - file path or file descriptor
137+
* @param options - options
138+
* @param options.encoding - file encoding
139+
* @param options.flag - file status flag
140+
* @param options.reviver - JSON reviver
141+
* @param clbk - callback
142+
*
143+
* @example
144+
* var resolve = require( `path` ).resolve;
145+
*
146+
* readJSON( resolve( __dirname, '..', 'package.json' ), 'utf-8', onJSON );
147+
*
148+
* function onJSON( error, data ) {
149+
* if ( error ) {
150+
* throw error;
151+
* }
152+
* console.dir( data );
153+
* }
154+
*
155+
* @example
156+
* var resolve = require( `path` ).resolve;
157+
* var instanceOf = require( `@stdlib/assert/instance-of` );
158+
*
159+
* var out = readJSON.sync( resolve( __dirname, '..', 'package.json' ) );
160+
* if ( instanceOf( out, Error ) ) {
161+
* throw out;
162+
* }
163+
* console.dir( out );
164+
*/
165+
declare var readJSON: ReadJSON;
166+
167+
168+
// EXPORTS //
169+
170+
export = readJSON;
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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 readJSON = 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+
readJSON( 'package.json', 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+
readJSON( false, onLoad ); // $ExpectError
38+
readJSON( true, onLoad ); // $ExpectError
39+
readJSON( null, onLoad ); // $ExpectError
40+
readJSON( undefined, onLoad ); // $ExpectError
41+
readJSON( [], onLoad ); // $ExpectError
42+
readJSON( {}, onLoad ); // $ExpectError
43+
readJSON( ( 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+
readJSON( '/path/to/package.json', 'abc' ); // $ExpectError
49+
readJSON( '/path/to/package.json', 1 ); // $ExpectError
50+
readJSON( '/path/to/package.json', false ); // $ExpectError
51+
readJSON( '/path/to/package.json', true ); // $ExpectError
52+
readJSON( '/path/to/package.json', null ); // $ExpectError
53+
readJSON( '/path/to/package.json', undefined ); // $ExpectError
54+
readJSON( '/path/to/package.json', [] ); // $ExpectError
55+
readJSON( '/path/to/package.json', {} ); // $ExpectError
56+
readJSON( '/path/to/package.json', ( 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+
readJSON( 'package.json', false, onLoad ); // $ExpectError
62+
readJSON( 'package.json', true, onLoad ); // $ExpectError
63+
readJSON( 'package.json', null, onLoad ); // $ExpectError
64+
readJSON( 'package.json', undefined, onLoad ); // $ExpectError
65+
readJSON( 'package.json', 123, onLoad ); // $ExpectError
66+
readJSON( 'package.json', [], onLoad ); // $ExpectError
67+
readJSON( 'package.json', ( 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+
readJSON( 'package.json', { 'encoding': 123 }, onLoad ); // $ExpectError
73+
readJSON( 'package.json', { 'encoding': true }, onLoad ); // $ExpectError
74+
readJSON( 'package.json', { 'encoding': false }, onLoad ); // $ExpectError
75+
readJSON( 'package.json', { 'encoding': [] }, onLoad ); // $ExpectError
76+
readJSON( 'package.json', { 'encoding': {} }, onLoad ); // $ExpectError
77+
readJSON( 'package.json', { '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+
readJSON( 'package.json', { 'flag': 123 }, onLoad ); // $ExpectError
83+
readJSON( 'package.json', { 'flag': true }, onLoad ); // $ExpectError
84+
readJSON( 'package.json', { 'flag': false }, onLoad ); // $ExpectError
85+
readJSON( 'package.json', { 'flag': null }, onLoad ); // $ExpectError
86+
readJSON( 'package.json', { 'flag': [] }, onLoad ); // $ExpectError
87+
readJSON( 'package.json', { 'flag': {} }, onLoad ); // $ExpectError
88+
readJSON( 'package.json', { 'flag': ( x: number ): number => x }, onLoad ); // $ExpectError
89+
}
90+
91+
// The compiler throws an error if the function is provided a `reviver` option which is not a function...
92+
{
93+
readJSON( 'package.json', { 'reviver': 123 }, onLoad ); // $ExpectError
94+
readJSON( 'package.json', { 'reviver': 'abc' }, onLoad ); // $ExpectError
95+
readJSON( 'package.json', { 'reviver': true }, onLoad ); // $ExpectError
96+
readJSON( 'package.json', { 'reviver': false }, onLoad ); // $ExpectError
97+
readJSON( 'package.json', { 'reviver': null }, onLoad ); // $ExpectError
98+
readJSON( 'package.json', { 'reviver': [] }, onLoad ); // $ExpectError
99+
readJSON( 'package.json', { 'reviver': {} }, onLoad ); // $ExpectError
100+
}
101+
102+
// The compiler throws an error if the function is provided an unsupported number of arguments...
103+
{
104+
readJSON(); // $ExpectError
105+
readJSON( 'C:\\foo\\bar\\baz\\package.json' ); // $ExpectError
106+
}
107+
108+
// Attached to main export is a `sync` method which returns a string or an error...
109+
{
110+
readJSON.sync( 'package.json' ); // $ExpectType string | Error
111+
}
112+
113+
// The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor...
114+
{
115+
readJSON.sync( false ); // $ExpectError
116+
readJSON.sync( true ); // $ExpectError
117+
readJSON.sync( null ); // $ExpectError
118+
readJSON.sync( undefined ); // $ExpectError
119+
readJSON.sync( [] ); // $ExpectError
120+
readJSON.sync( {} ); // $ExpectError
121+
readJSON.sync( ( x: number ): number => x ); // $ExpectError
122+
}
123+
124+
// The compiler throws an error if the `sync` method is provided an options argument which is not an object or string...
125+
{
126+
readJSON.sync( 'package.json', null ); // $ExpectError
127+
readJSON.sync( 'package.json', true ); // $ExpectError
128+
readJSON.sync( 'package.json', false ); // $ExpectError
129+
readJSON.sync( 'package.json', 123 ); // $ExpectError
130+
readJSON.sync( 'package.json', [] ); // $ExpectError
131+
readJSON.sync( 'package.json', ( x: number ): number => x ); // $ExpectError
132+
}
133+
134+
// The compiler throws an error if the `sync` method is provided an `encoding` option which is not a string or null...
135+
{
136+
readJSON.sync( 'package.json', { 'encoding': 123 } ); // $ExpectError
137+
readJSON.sync( 'package.json', { 'encoding': true } ); // $ExpectError
138+
readJSON.sync( 'package.json', { 'encoding': false } ); // $ExpectError
139+
readJSON.sync( 'package.json', { 'encoding': [] } ); // $ExpectError
140+
readJSON.sync( 'package.json', { 'encoding': {} } ); // $ExpectError
141+
readJSON.sync( 'package.json', { 'encoding': ( x: number ): number => x } ); // $ExpectError
142+
}
143+
144+
// The compiler throws an error if the `sync` method is provided a `flag` option which is not a string...
145+
{
146+
readJSON.sync( 'package.json', { 'flag': 123 } ); // $ExpectError
147+
readJSON.sync( 'package.json', { 'flag': true } ); // $ExpectError
148+
readJSON.sync( 'package.json', { 'flag': false } ); // $ExpectError
149+
readJSON.sync( 'package.json', { 'flag': null } ); // $ExpectError
150+
readJSON.sync( 'package.json', { 'flag': [] } ); // $ExpectError
151+
readJSON.sync( 'package.json', { 'flag': {} } ); // $ExpectError
152+
readJSON.sync( 'package.json', { 'flag': ( x: number ): number => x } ); // $ExpectError
153+
}
154+
155+
// The compiler throws an error if the `sync` method is provided a `reviver` option which is not a function...
156+
{
157+
readJSON.sync( 'package.json', { 'reviver': 'abc' } ); // $ExpectError
158+
readJSON.sync( 'package.json', { 'reviver': 123 } ); // $ExpectError
159+
readJSON.sync( 'package.json', { 'reviver': true } ); // $ExpectError
160+
readJSON.sync( 'package.json', { 'reviver': false } ); // $ExpectError
161+
readJSON.sync( 'package.json', { 'reviver': null } ); // $ExpectError
162+
readJSON.sync( 'package.json', { 'reviver': [] } ); // $ExpectError
163+
readJSON.sync( 'package.json', { 'reviver': {} } ); // $ExpectError
164+
}
165+
166+
// The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
167+
{
168+
readJSON.sync(); // $ExpectError
169+
}

0 commit comments

Comments
 (0)