@ohos-ports/stdlib-fs 0.0.0-stage → 0.4.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +1 -0
  3. package/README.md +230 -2
  4. package/SECURITY.md +5 -0
  5. package/append-file/README.md +239 -0
  6. package/append-file/bin/cli +110 -0
  7. package/append-file/docs/repl.txt +72 -0
  8. package/append-file/docs/types/index.d.ts +133 -0
  9. package/append-file/docs/types/test.ts +193 -0
  10. package/append-file/docs/usage.txt +10 -0
  11. package/append-file/etc/cli_opts.json +22 -0
  12. package/append-file/lib/async.js +58 -0
  13. package/append-file/lib/index.js +61 -0
  14. package/append-file/lib/sync.js +58 -0
  15. package/append-file/package.json +69 -0
  16. package/close/README.md +172 -0
  17. package/close/docs/repl.txt +45 -0
  18. package/close/docs/types/index.d.ts +135 -0
  19. package/close/docs/types/test.ts +89 -0
  20. package/close/lib/index.js +73 -0
  21. package/close/lib/main.js +68 -0
  22. package/close/lib/sync.js +65 -0
  23. package/close/package.json +64 -0
  24. package/docs/types/index.d.ts +502 -0
  25. package/exists/README.md +252 -0
  26. package/exists/bin/cli +83 -0
  27. package/exists/docs/repl.txt +42 -0
  28. package/exists/docs/types/index.d.ts +112 -0
  29. package/exists/docs/types/test.ts +87 -0
  30. package/exists/docs/usage.txt +8 -0
  31. package/exists/etc/cli_opts.json +14 -0
  32. package/exists/lib/async.js +85 -0
  33. package/exists/lib/index.js +66 -0
  34. package/exists/lib/sync.js +62 -0
  35. package/exists/package.json +69 -0
  36. package/lib/index.js +186 -0
  37. package/open/README.md +204 -0
  38. package/open/docs/repl.txt +65 -0
  39. package/open/docs/types/index.d.ts +150 -0
  40. package/open/docs/types/test.ts +129 -0
  41. package/open/lib/defaults.json +4 -0
  42. package/open/lib/index.js +63 -0
  43. package/open/lib/main.js +64 -0
  44. package/open/lib/sync.js +68 -0
  45. package/open/package.json +64 -0
  46. package/package.json +67 -4
  47. package/read-dir/README.md +232 -0
  48. package/read-dir/bin/cli +83 -0
  49. package/read-dir/docs/repl.txt +44 -0
  50. package/read-dir/docs/types/index.d.ts +99 -0
  51. package/read-dir/docs/types/test.ts +85 -0
  52. package/read-dir/docs/usage.txt +8 -0
  53. package/read-dir/etc/cli_opts.json +14 -0
  54. package/read-dir/lib/async.js +57 -0
  55. package/read-dir/lib/index.js +62 -0
  56. package/read-dir/lib/sync.js +52 -0
  57. package/read-dir/package.json +69 -0
  58. package/read-file/README.md +246 -0
  59. package/read-file/bin/cli +91 -0
  60. package/read-file/docs/repl.txt +68 -0
  61. package/read-file/docs/types/index.d.ts +138 -0
  62. package/read-file/docs/types/test.ts +147 -0
  63. package/read-file/docs/usage.txt +10 -0
  64. package/read-file/etc/cli_opts.json +21 -0
  65. package/read-file/lib/index.js +61 -0
  66. package/read-file/lib/main.js +59 -0
  67. package/read-file/lib/sync.js +61 -0
  68. package/read-file/package.json +68 -0
  69. package/read-file-list/README.md +197 -0
  70. package/read-file-list/bin/cli +100 -0
  71. package/read-file-list/docs/repl.txt +81 -0
  72. package/read-file-list/docs/types/index.d.ts +166 -0
  73. package/read-file-list/docs/types/test.ts +166 -0
  74. package/read-file-list/docs/usage.txt +10 -0
  75. package/read-file-list/etc/cli_opts.json +21 -0
  76. package/read-file-list/lib/async.js +202 -0
  77. package/read-file-list/lib/clear_pending.js +47 -0
  78. package/read-file-list/lib/clear_timeout.js +23 -0
  79. package/read-file-list/lib/config.json +4 -0
  80. package/read-file-list/lib/delay.js +47 -0
  81. package/read-file-list/lib/index.js +66 -0
  82. package/read-file-list/lib/sync.js +94 -0
  83. package/read-file-list/package.json +72 -0
  84. package/read-json/README.md +176 -0
  85. package/read-json/docs/repl.txt +72 -0
  86. package/read-json/docs/types/index.d.ts +170 -0
  87. package/read-json/docs/types/test.ts +175 -0
  88. package/read-json/lib/async.js +114 -0
  89. package/read-json/lib/index.js +65 -0
  90. package/read-json/lib/sync.js +89 -0
  91. package/read-json/package.json +66 -0
  92. package/read-ndjson/README.md +166 -0
  93. package/read-ndjson/docs/repl.txt +72 -0
  94. package/read-ndjson/docs/types/index.d.ts +170 -0
  95. package/read-ndjson/docs/types/test.ts +179 -0
  96. package/read-ndjson/lib/async.js +114 -0
  97. package/read-ndjson/lib/index.js +65 -0
  98. package/read-ndjson/lib/sync.js +89 -0
  99. package/read-ndjson/package.json +68 -0
  100. package/read-wasm/README.md +158 -0
  101. package/read-wasm/docs/repl.txt +58 -0
  102. package/read-wasm/docs/types/index.d.ts +153 -0
  103. package/read-wasm/docs/types/test.ts +127 -0
  104. package/read-wasm/lib/async.js +106 -0
  105. package/read-wasm/lib/index.js +67 -0
  106. package/read-wasm/lib/sync.js +86 -0
  107. package/read-wasm/package.json +67 -0
  108. package/rename/README.md +259 -0
  109. package/rename/bin/cli +89 -0
  110. package/rename/docs/repl.txt +80 -0
  111. package/rename/docs/types/index.d.ts +109 -0
  112. package/rename/docs/types/test.ts +111 -0
  113. package/rename/docs/usage.txt +7 -0
  114. package/rename/etc/cli_opts.json +15 -0
  115. package/rename/lib/index.js +60 -0
  116. package/rename/lib/main.js +51 -0
  117. package/rename/lib/sync.js +53 -0
  118. package/rename/package.json +73 -0
  119. package/resolve-parent-path/README.md +204 -0
  120. package/resolve-parent-path/bin/cli +85 -0
  121. package/resolve-parent-path/docs/repl.txt +58 -0
  122. package/resolve-parent-path/docs/types/index.d.ts +128 -0
  123. package/resolve-parent-path/docs/types/test.ts +117 -0
  124. package/resolve-parent-path/docs/usage.txt +9 -0
  125. package/resolve-parent-path/etc/cli_opts.json +17 -0
  126. package/resolve-parent-path/lib/async.js +116 -0
  127. package/resolve-parent-path/lib/index.js +58 -0
  128. package/resolve-parent-path/lib/sync.js +83 -0
  129. package/resolve-parent-path/lib/validate.js +67 -0
  130. package/resolve-parent-path/package.json +78 -0
  131. package/resolve-parent-path-by/README.md +220 -0
  132. package/resolve-parent-path-by/docs/repl.txt +105 -0
  133. package/resolve-parent-path-by/docs/types/index.d.ts +220 -0
  134. package/resolve-parent-path-by/docs/types/test.ts +225 -0
  135. package/resolve-parent-path-by/lib/async.js +156 -0
  136. package/resolve-parent-path-by/lib/index.js +67 -0
  137. package/resolve-parent-path-by/lib/sync.js +98 -0
  138. package/resolve-parent-path-by/lib/validate.js +67 -0
  139. package/resolve-parent-path-by/package.json +76 -0
  140. package/resolve-parent-paths/README.md +228 -0
  141. package/resolve-parent-paths/bin/cli +93 -0
  142. package/resolve-parent-paths/docs/repl.txt +84 -0
  143. package/resolve-parent-paths/docs/types/index.d.ts +145 -0
  144. package/resolve-parent-paths/docs/types/test.ts +196 -0
  145. package/resolve-parent-paths/docs/usage.txt +10 -0
  146. package/resolve-parent-paths/etc/cli_opts.json +18 -0
  147. package/resolve-parent-paths/lib/index.js +60 -0
  148. package/resolve-parent-paths/lib/main.js +415 -0
  149. package/resolve-parent-paths/lib/sync.js +231 -0
  150. package/resolve-parent-paths/lib/validate.js +81 -0
  151. package/resolve-parent-paths/package.json +78 -0
  152. package/unlink/README.md +233 -0
  153. package/unlink/bin/cli +81 -0
  154. package/unlink/docs/repl.txt +62 -0
  155. package/unlink/docs/types/index.d.ts +105 -0
  156. package/unlink/docs/types/test.ts +85 -0
  157. package/unlink/docs/usage.txt +7 -0
  158. package/unlink/etc/cli_opts.json +15 -0
  159. package/unlink/lib/index.js +60 -0
  160. package/unlink/lib/main.js +50 -0
  161. package/unlink/lib/sync.js +52 -0
  162. package/unlink/package.json +70 -0
  163. package/write-file/README.md +250 -0
  164. package/write-file/bin/cli +110 -0
  165. package/write-file/docs/repl.txt +74 -0
  166. package/write-file/docs/types/index.d.ts +133 -0
  167. package/write-file/docs/types/test.ts +193 -0
  168. package/write-file/docs/usage.txt +10 -0
  169. package/write-file/etc/cli_opts.json +22 -0
  170. package/write-file/lib/index.js +60 -0
  171. package/write-file/lib/main.js +58 -0
  172. package/write-file/lib/sync.js +58 -0
  173. package/write-file/package.json +68 -0
@@ -0,0 +1,176 @@
1
+ <!--
2
+
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2018 The Stdlib Authors.
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+
19
+ -->
20
+
21
+ # Read JSON
22
+
23
+ > Read a file as [JSON][json].
24
+
25
+ <section class="usage">
26
+
27
+ ## Usage
28
+
29
+ ```javascript
30
+ var readJSON = require( '@stdlib/fs/read-json' );
31
+ ```
32
+
33
+ <a name="read-json"></a>
34
+
35
+ #### readJSON( file\[, options], clbk )
36
+
37
+ Asynchronously reads a file as [JSON][json].
38
+
39
+ ```javascript
40
+ var join = require( 'path' ).join;
41
+
42
+ readJSON( join( __dirname, 'package.json' ), onJSON );
43
+
44
+ function onJSON( error, data ) {
45
+ if ( error ) {
46
+ throw error;
47
+ }
48
+ console.dir( data );
49
+ }
50
+ ```
51
+
52
+ The function accepts the following `options`:
53
+
54
+ - **encoding**: file encoding.
55
+ - **flag**: file status flag.
56
+ - **reviver**: [JSON][json] transformation `function`.
57
+
58
+ The `options` parameter may also be a `string` specifying the file `encoding`.
59
+
60
+ ```javascript
61
+ var join = require( 'path' ).join;
62
+
63
+ readJSON( join( __dirname, 'package.json' ), 'utf8', onJSON );
64
+
65
+ function onJSON( error, data ) {
66
+ if ( error ) {
67
+ throw error;
68
+ }
69
+ console.dir( data );
70
+ }
71
+ ```
72
+
73
+ #### readJSON.sync( file\[, options] )
74
+
75
+ Synchronously reads a file as [JSON][json].
76
+
77
+ ```javascript
78
+ var join = require( 'path' ).join;
79
+ var instanceOf = require( '@stdlib/assert/instance-of' );
80
+
81
+ var out = readJSON.sync( join( __dirname, 'package.json' ) );
82
+ if ( instanceOf( out, Error ) ) {
83
+ throw out;
84
+ }
85
+ console.dir( out );
86
+ ```
87
+
88
+ The function accepts the same `options` as [`readJSON()`](#read-json) above.
89
+
90
+ </section>
91
+
92
+ <!-- /.usage -->
93
+
94
+ <section class="notes">
95
+
96
+ ## Notes
97
+
98
+ - If the `encoding` option is set to `utf8` and the file has a UTF-8 [byte order mark][bom] (BOM), the byte order mark is **removed** before attempting to parse as [JSON][json].
99
+
100
+ </section>
101
+
102
+ <!-- /.notes -->
103
+
104
+ <section class="examples">
105
+
106
+ ## Examples
107
+
108
+ <!-- eslint no-undef: "error" -->
109
+
110
+ ```javascript
111
+ var join = require( 'path' ).join;
112
+ var readJSON = require( '@stdlib/fs/read-json' );
113
+
114
+ var file = join( __dirname, 'package.json' );
115
+
116
+ // Synchronously read file contents...
117
+ var data = readJSON.sync( file, 'utf8' );
118
+ // returns <Object>
119
+
120
+ data = readJSON.sync( 'beepboop', {
121
+ 'encoding': 'utf8'
122
+ });
123
+ // returns <Error>
124
+
125
+ // Asynchronously read file contents...
126
+ readJSON( file, onJSON );
127
+ readJSON( 'beepboop', onJSON );
128
+
129
+ function onJSON( error, data ) {
130
+ if ( error ) {
131
+ if ( error.code === 'ENOENT' ) {
132
+ console.error( 'JSON file does not exist.' );
133
+ } else {
134
+ throw error;
135
+ }
136
+ } else {
137
+ console.log( 'Package description: %s', data.description );
138
+ }
139
+ }
140
+ ```
141
+
142
+ </section>
143
+
144
+ <!-- /.examples -->
145
+
146
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
147
+
148
+ <section class="related">
149
+
150
+ * * *
151
+
152
+ ## See Also
153
+
154
+ - <span class="package-name">[`@stdlib/fs/read-file`][@stdlib/fs/read-file]</span><span class="delimiter">: </span><span class="description">read the entire contents of a file.</span>
155
+
156
+ </section>
157
+
158
+ <!-- /.related -->
159
+
160
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161
+
162
+ <section class="links">
163
+
164
+ [json]: http://www.json.org/
165
+
166
+ [bom]: https://en.wikipedia.org/wiki/Byte_order_mark
167
+
168
+ <!-- <related-links> -->
169
+
170
+ [@stdlib/fs/read-file]: https://github.com/stdlib-js/fs/tree/main/read-file
171
+
172
+ <!-- </related-links> -->
173
+
174
+ </section>
175
+
176
+ <!-- /.links -->
@@ -0,0 +1,72 @@
1
+
2
+ {{alias}}( file[, options], clbk )
3
+ Asynchronously reads a file as JSON.
4
+
5
+ Parameters
6
+ ----------
7
+ file: string|Buffer|integer
8
+ Filename or file descriptor.
9
+
10
+ options: Object|string (optional)
11
+ Options. If a string, the value is the encoding.
12
+
13
+ options.encoding: string|null (optional)
14
+ Encoding. If the encoding option is set to `utf8` and the file has a
15
+ UTF-8 byte order mark (BOM), the byte order mark is *removed* before
16
+ attempting to parse as JSON. Default: null.
17
+
18
+ options.flag: string (optional)
19
+ Flag. Default: 'r'.
20
+
21
+ options.reviver: Function (optional)
22
+ JSON transformation function.
23
+
24
+ clbk: Function
25
+ Callback to invoke upon reading file contents.
26
+
27
+ Examples
28
+ --------
29
+ > function onRead( error, data ) {
30
+ ... if ( error ) {
31
+ ... console.error( error.message );
32
+ ... } else {
33
+ ... console.log( data );
34
+ ... }
35
+ ... };
36
+ > {{alias}}( './beep/boop.json', onRead );
37
+
38
+
39
+ {{alias}}.sync( file[, options] )
40
+ Synchronously reads a file as JSON.
41
+
42
+ Parameters
43
+ ----------
44
+ file: string|Buffer|integer
45
+ Filename or file descriptor.
46
+
47
+ options: Object|string (optional)
48
+ Options. If a string, the value is the encoding.
49
+
50
+ options.encoding: string|null (optional)
51
+ Encoding. If the encoding option is set to `utf8` and the file has a
52
+ UTF-8 byte order mark (BOM), the byte order mark is *removed* before
53
+ attempting to parse as JSON. Default: null.
54
+
55
+ options.flag: string (optional)
56
+ Flag. Default: 'r'.
57
+
58
+ options.reviver: Function (optional)
59
+ JSON transformation function.
60
+
61
+ Returns
62
+ -------
63
+ out: Error|JSON
64
+ File contents.
65
+
66
+ Examples
67
+ --------
68
+ > var out = {{alias}}.sync( './beep/boop.json' );
69
+
70
+ See Also
71
+ --------
72
+
@@ -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: 4.1
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 data - file contents
59
+ */
60
+ type Callback<T> = ( err: Error | null, data: T ) => 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
+ <T = unknown>( file: string | Buffer | number, options: Options | string, clbk: Callback<T> ): void;
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
+ <T = unknown>( file: string | Buffer | number, clbk: Callback<T> ): 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 = readJSON.sync( resolve( __dirname, '..', 'package.json' ) );
125
+ * if ( instanceOf( out, Error ) ) {
126
+ * throw out;
127
+ * }
128
+ * console.dir( out );
129
+ */
130
+ sync<T = unknown>( file: string | Buffer | number, options?: Options | string ): T | Error;
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;
@@ -0,0 +1,175 @@
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
+ /**
22
+ * Callback function.
23
+ *
24
+ * @param error - error object
25
+ * @param data - results
26
+ */
27
+ function onLoad( error: Error | null, data?: Array<string> | Buffer ): void {
28
+ if ( error || !data ) {
29
+ throw error;
30
+ }
31
+ }
32
+
33
+
34
+ // TESTS //
35
+
36
+ // The function does not have a return value...
37
+ {
38
+ readJSON( 'package.json', onLoad ); // $ExpectType void
39
+ }
40
+
41
+ // The compiler throws an error if the function is provided a first argument which is not a string, buffer, or file descriptor...
42
+ {
43
+ readJSON( false, onLoad ); // $ExpectError
44
+ readJSON( true, onLoad ); // $ExpectError
45
+ readJSON( null, onLoad ); // $ExpectError
46
+ readJSON( undefined, onLoad ); // $ExpectError
47
+ readJSON( [], onLoad ); // $ExpectError
48
+ readJSON( {}, onLoad ); // $ExpectError
49
+ readJSON( ( x: number ): number => x, onLoad ); // $ExpectError
50
+ }
51
+
52
+ // The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature...
53
+ {
54
+ readJSON( '/path/to/package.json', 'abc' ); // $ExpectError
55
+ readJSON( '/path/to/package.json', 1 ); // $ExpectError
56
+ readJSON( '/path/to/package.json', false ); // $ExpectError
57
+ readJSON( '/path/to/package.json', true ); // $ExpectError
58
+ readJSON( '/path/to/package.json', null ); // $ExpectError
59
+ readJSON( '/path/to/package.json', undefined ); // $ExpectError
60
+ readJSON( '/path/to/package.json', [] ); // $ExpectError
61
+ readJSON( '/path/to/package.json', {} ); // $ExpectError
62
+ readJSON( '/path/to/package.json', ( x: number ): number => x ); // $ExpectError
63
+ }
64
+
65
+ // The compiler throws an error if the function is provided an options argument which is not an object or string...
66
+ {
67
+ readJSON( 'package.json', false, onLoad ); // $ExpectError
68
+ readJSON( 'package.json', true, onLoad ); // $ExpectError
69
+ readJSON( 'package.json', null, onLoad ); // $ExpectError
70
+ readJSON( 'package.json', undefined, onLoad ); // $ExpectError
71
+ readJSON( 'package.json', 123, onLoad ); // $ExpectError
72
+ readJSON( 'package.json', [], onLoad ); // $ExpectError
73
+ readJSON( 'package.json', ( x: number ): number => x, onLoad ); // $ExpectError
74
+ }
75
+
76
+ // The compiler throws an error if the function is provided an `encoding` option which is not a string or null...
77
+ {
78
+ readJSON( 'package.json', { 'encoding': 123 }, onLoad ); // $ExpectError
79
+ readJSON( 'package.json', { 'encoding': true }, onLoad ); // $ExpectError
80
+ readJSON( 'package.json', { 'encoding': false }, onLoad ); // $ExpectError
81
+ readJSON( 'package.json', { 'encoding': [] }, onLoad ); // $ExpectError
82
+ readJSON( 'package.json', { 'encoding': {} }, onLoad ); // $ExpectError
83
+ readJSON( 'package.json', { 'encoding': ( x: number ): number => x }, onLoad ); // $ExpectError
84
+ }
85
+
86
+ // The compiler throws an error if the function is provided a `flag` option which is not a string...
87
+ {
88
+ readJSON( 'package.json', { 'flag': 123 }, onLoad ); // $ExpectError
89
+ readJSON( 'package.json', { 'flag': true }, onLoad ); // $ExpectError
90
+ readJSON( 'package.json', { 'flag': false }, onLoad ); // $ExpectError
91
+ readJSON( 'package.json', { 'flag': null }, onLoad ); // $ExpectError
92
+ readJSON( 'package.json', { 'flag': [] }, onLoad ); // $ExpectError
93
+ readJSON( 'package.json', { 'flag': {} }, onLoad ); // $ExpectError
94
+ readJSON( 'package.json', { 'flag': ( x: number ): number => x }, onLoad ); // $ExpectError
95
+ }
96
+
97
+ // The compiler throws an error if the function is provided a `reviver` option which is not a function...
98
+ {
99
+ readJSON( 'package.json', { 'reviver': 123 }, onLoad ); // $ExpectError
100
+ readJSON( 'package.json', { 'reviver': 'abc' }, onLoad ); // $ExpectError
101
+ readJSON( 'package.json', { 'reviver': true }, onLoad ); // $ExpectError
102
+ readJSON( 'package.json', { 'reviver': false }, onLoad ); // $ExpectError
103
+ readJSON( 'package.json', { 'reviver': null }, onLoad ); // $ExpectError
104
+ readJSON( 'package.json', { 'reviver': [] }, onLoad ); // $ExpectError
105
+ readJSON( 'package.json', { 'reviver': {} }, onLoad ); // $ExpectError
106
+ }
107
+
108
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
109
+ {
110
+ readJSON(); // $ExpectError
111
+ readJSON( 'C:\\foo\\bar\\baz\\package.json' ); // $ExpectError
112
+ }
113
+
114
+ // Attached to main export is a `sync` method which returns results or an error...
115
+ {
116
+ readJSON.sync<Array<string>>( 'package.json' ); // $ExpectType Error | string[]
117
+ }
118
+
119
+ // The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor...
120
+ {
121
+ readJSON.sync( false ); // $ExpectError
122
+ readJSON.sync( true ); // $ExpectError
123
+ readJSON.sync( null ); // $ExpectError
124
+ readJSON.sync( undefined ); // $ExpectError
125
+ readJSON.sync( [] ); // $ExpectError
126
+ readJSON.sync( {} ); // $ExpectError
127
+ readJSON.sync( ( x: number ): number => x ); // $ExpectError
128
+ }
129
+
130
+ // The compiler throws an error if the `sync` method is provided an options argument which is not an object or string...
131
+ {
132
+ readJSON.sync( 'package.json', null ); // $ExpectError
133
+ readJSON.sync( 'package.json', true ); // $ExpectError
134
+ readJSON.sync( 'package.json', false ); // $ExpectError
135
+ readJSON.sync( 'package.json', 123 ); // $ExpectError
136
+ readJSON.sync( 'package.json', [] ); // $ExpectError
137
+ readJSON.sync( 'package.json', ( x: number ): number => x ); // $ExpectError
138
+ }
139
+
140
+ // The compiler throws an error if the `sync` method is provided an `encoding` option which is not a string or null...
141
+ {
142
+ readJSON.sync( 'package.json', { 'encoding': 123 } ); // $ExpectError
143
+ readJSON.sync( 'package.json', { 'encoding': true } ); // $ExpectError
144
+ readJSON.sync( 'package.json', { 'encoding': false } ); // $ExpectError
145
+ readJSON.sync( 'package.json', { 'encoding': [] } ); // $ExpectError
146
+ readJSON.sync( 'package.json', { 'encoding': {} } ); // $ExpectError
147
+ readJSON.sync( 'package.json', { 'encoding': ( x: number ): number => x } ); // $ExpectError
148
+ }
149
+
150
+ // The compiler throws an error if the `sync` method is provided a `flag` option which is not a string...
151
+ {
152
+ readJSON.sync( 'package.json', { 'flag': 123 } ); // $ExpectError
153
+ readJSON.sync( 'package.json', { 'flag': true } ); // $ExpectError
154
+ readJSON.sync( 'package.json', { 'flag': false } ); // $ExpectError
155
+ readJSON.sync( 'package.json', { 'flag': null } ); // $ExpectError
156
+ readJSON.sync( 'package.json', { 'flag': [] } ); // $ExpectError
157
+ readJSON.sync( 'package.json', { 'flag': {} } ); // $ExpectError
158
+ readJSON.sync( 'package.json', { 'flag': ( x: number ): number => x } ); // $ExpectError
159
+ }
160
+
161
+ // The compiler throws an error if the `sync` method is provided a `reviver` option which is not a function...
162
+ {
163
+ readJSON.sync( 'package.json', { 'reviver': 'abc' } ); // $ExpectError
164
+ readJSON.sync( 'package.json', { 'reviver': 123 } ); // $ExpectError
165
+ readJSON.sync( 'package.json', { 'reviver': true } ); // $ExpectError
166
+ readJSON.sync( 'package.json', { 'reviver': false } ); // $ExpectError
167
+ readJSON.sync( 'package.json', { 'reviver': null } ); // $ExpectError
168
+ readJSON.sync( 'package.json', { 'reviver': [] } ); // $ExpectError
169
+ readJSON.sync( 'package.json', { 'reviver': {} } ); // $ExpectError
170
+ }
171
+
172
+ // The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
173
+ {
174
+ readJSON.sync(); // $ExpectError
175
+ }
@@ -0,0 +1,114 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
24
+ var isObject = require( '@stdlib/assert/is-plain-object' );
25
+ var isFunction = require( '@stdlib/assert/is-function' );
26
+ var readFile = require( './../../read-file' );
27
+ var removeBOM = require( '@stdlib/string/remove-utf8-bom' );
28
+ var parseJSON = require( '@ohos-ports/stdlib-utils/parse-json' );
29
+ var instanceOf = require( '@stdlib/assert/instance-of' );
30
+ var format = require( '@stdlib/string/format' );
31
+
32
+
33
+ // MAIN //
34
+
35
+ /**
36
+ * Asynchronously reads a file as JSON.
37
+ *
38
+ * @param {(string|Buffer|integer)} file - file path or file descriptor
39
+ * @param {(Options|string)} [options] - options
40
+ * @param {(string|null)} [options.encoding] - file encoding
41
+ * @param {string} [options.flag] - file status flag
42
+ * @param {Function} [options.reviver] - JSON reviver
43
+ * @param {Callback} clbk - callback
44
+ * @throws {TypeError} options argument must be either a string or an object
45
+ * @throws {TypeError} callback argument must be a function
46
+ *
47
+ * @example
48
+ * var resolve = require( 'path' ).resolve;
49
+ *
50
+ * readJSON( resolve( __dirname, '..', 'package.json' ), onJSON );
51
+ *
52
+ * function onJSON( error, data ) {
53
+ * if ( error ) {
54
+ * throw error;
55
+ * }
56
+ * console.dir( data );
57
+ * }
58
+ */
59
+ function readJSON( file, options, clbk ) {
60
+ var opts;
61
+ var done;
62
+ if ( arguments.length < 3 ) {
63
+ opts = {};
64
+ done = options;
65
+ } else {
66
+ if ( isString( options ) ) {
67
+ opts = {
68
+ 'encoding': options
69
+ };
70
+ } else {
71
+ if ( !isObject( options ) ) {
72
+ throw new TypeError( format( 'invalid argument. Options argument must be either a string or an object. Value: `%s`.', options ) );
73
+ }
74
+ opts = options;
75
+ }
76
+ done = clbk;
77
+ }
78
+ if ( !isFunction( done ) ) {
79
+ throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );
80
+ }
81
+ readFile( file, opts, onRead );
82
+
83
+ /**
84
+ * Callback invoked upon reading a file.
85
+ *
86
+ * @private
87
+ * @param {(Error|null)} error - error object
88
+ * @param {(Buffer|string)} file - file contents
89
+ * @returns {void}
90
+ */
91
+ function onRead( error, file ) {
92
+ if ( error ) {
93
+ return done( error );
94
+ }
95
+ file = file.toString();
96
+ if ( opts.encoding === 'utf8' ) {
97
+ file = removeBOM( file );
98
+ }
99
+ if ( opts.reviver ) {
100
+ file = parseJSON( file, opts.reviver );
101
+ } else {
102
+ file = parseJSON( file );
103
+ }
104
+ if ( instanceOf( file, Error ) ) {
105
+ return done( file );
106
+ }
107
+ done( null, file );
108
+ }
109
+ }
110
+
111
+
112
+ // EXPORTS //
113
+
114
+ module.exports = readJSON;