@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,179 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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 readNDJSON = require( './index' );
20
+
21
+ /**
22
+ * Callback function.
23
+ *
24
+ * @param error - error
25
+ * @param file - file path
26
+ * @returns results
27
+ */
28
+ function onLoad( error: Error | null, data?: Array<null> ): null {
29
+ if ( error || !data ) {
30
+ throw error;
31
+ }
32
+ return null;
33
+ };
34
+
35
+
36
+ // TESTS //
37
+
38
+ // The function does not have a return value...
39
+ {
40
+ readNDJSON<null>( 'file.ndjson', onLoad ); // $ExpectType void
41
+ }
42
+
43
+ // The compiler throws an error if the function is provided a first argument which is not a string, buffer, or file descriptor...
44
+ {
45
+ readNDJSON( false, onLoad ); // $ExpectError
46
+ readNDJSON( true, onLoad ); // $ExpectError
47
+ readNDJSON( null, onLoad ); // $ExpectError
48
+ readNDJSON( undefined, onLoad ); // $ExpectError
49
+ readNDJSON( [], onLoad ); // $ExpectError
50
+ readNDJSON( {}, onLoad ); // $ExpectError
51
+ readNDJSON( ( x: number ): number => x, onLoad ); // $ExpectError
52
+ }
53
+
54
+ // The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature...
55
+ {
56
+ readNDJSON( '/path/to/file.ndjson', 'abc' ); // $ExpectError
57
+ readNDJSON( '/path/to/file.ndjson', 1 ); // $ExpectError
58
+ readNDJSON( '/path/to/file.ndjson', false ); // $ExpectError
59
+ readNDJSON( '/path/to/file.ndjson', true ); // $ExpectError
60
+ readNDJSON( '/path/to/file.ndjson', null ); // $ExpectError
61
+ readNDJSON( '/path/to/file.ndjson', undefined ); // $ExpectError
62
+ readNDJSON( '/path/to/file.ndjson', [] ); // $ExpectError
63
+ readNDJSON( '/path/to/file.ndjson', {} ); // $ExpectError
64
+ readNDJSON( '/path/to/file.ndjson', ( x: number ): number => x ); // $ExpectError
65
+ }
66
+
67
+ // The compiler throws an error if the function is provided an options argument which is not an object or string...
68
+ {
69
+ readNDJSON( 'file.ndjson', false, onLoad ); // $ExpectError
70
+ readNDJSON( 'file.ndjson', true, onLoad ); // $ExpectError
71
+ readNDJSON( 'file.ndjson', null, onLoad ); // $ExpectError
72
+ readNDJSON( 'file.ndjson', undefined, onLoad ); // $ExpectError
73
+ readNDJSON( 'file.ndjson', 123, onLoad ); // $ExpectError
74
+ readNDJSON( 'file.ndjson', [], onLoad ); // $ExpectError
75
+ readNDJSON( 'file.ndjson', ( x: number ): number => x, onLoad ); // $ExpectError
76
+ }
77
+
78
+ // The compiler throws an error if the function is provided an `encoding` option which is not a string or null...
79
+ {
80
+ readNDJSON( 'file.ndjson', { 'encoding': 123 }, onLoad ); // $ExpectError
81
+ readNDJSON( 'file.ndjson', { 'encoding': true }, onLoad ); // $ExpectError
82
+ readNDJSON( 'file.ndjson', { 'encoding': false }, onLoad ); // $ExpectError
83
+ readNDJSON( 'file.ndjson', { 'encoding': [] }, onLoad ); // $ExpectError
84
+ readNDJSON( 'file.ndjson', { 'encoding': {} }, onLoad ); // $ExpectError
85
+ readNDJSON( 'file.ndjson', { 'encoding': ( x: number ): number => x }, onLoad ); // $ExpectError
86
+ }
87
+
88
+ // The compiler throws an error if the function is provided a `flag` option which is not a string...
89
+ {
90
+ readNDJSON( 'file.ndjson', { 'flag': 123 }, onLoad ); // $ExpectError
91
+ readNDJSON( 'file.ndjson', { 'flag': true }, onLoad ); // $ExpectError
92
+ readNDJSON( 'file.ndjson', { 'flag': false }, onLoad ); // $ExpectError
93
+ readNDJSON( 'file.ndjson', { 'flag': null }, onLoad ); // $ExpectError
94
+ readNDJSON( 'file.ndjson', { 'flag': [] }, onLoad ); // $ExpectError
95
+ readNDJSON( 'file.ndjson', { 'flag': {} }, onLoad ); // $ExpectError
96
+ readNDJSON( 'file.ndjson', { 'flag': ( x: number ): number => x }, onLoad ); // $ExpectError
97
+ }
98
+
99
+ // The compiler throws an error if the function is provided a `reviver` option which is not a function...
100
+ {
101
+ readNDJSON( 'file.ndjson', { 'reviver': 123 }, onLoad ); // $ExpectError
102
+ readNDJSON( 'file.ndjson', { 'reviver': 'abc' }, onLoad ); // $ExpectError
103
+ readNDJSON( 'file.ndjson', { 'reviver': true }, onLoad ); // $ExpectError
104
+ readNDJSON( 'file.ndjson', { 'reviver': false }, onLoad ); // $ExpectError
105
+ readNDJSON( 'file.ndjson', { 'reviver': null }, onLoad ); // $ExpectError
106
+ readNDJSON( 'file.ndjson', { 'reviver': [] }, onLoad ); // $ExpectError
107
+ readNDJSON( 'file.ndjson', { 'reviver': {} }, onLoad ); // $ExpectError
108
+ }
109
+
110
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
111
+ {
112
+ readNDJSON(); // $ExpectError
113
+ readNDJSON( 'C:\\foo\\bar\\baz\\file.ndjson' ); // $ExpectError
114
+ }
115
+
116
+ // Attached to main export is a `sync` method which returns results or an error...
117
+ {
118
+ readNDJSON.sync( 'file.ndjson' ); // $ExpectType Error | unknown[]
119
+ readNDJSON.sync<string>( 'file.ndjson' ); // $ExpectType Error | string[]
120
+ readNDJSON.sync<number>( 'file.ndjson' ); // $ExpectType Error | number[]
121
+ }
122
+
123
+ // The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor...
124
+ {
125
+ readNDJSON.sync( false ); // $ExpectError
126
+ readNDJSON.sync( true ); // $ExpectError
127
+ readNDJSON.sync( null ); // $ExpectError
128
+ readNDJSON.sync( undefined ); // $ExpectError
129
+ readNDJSON.sync( [] ); // $ExpectError
130
+ readNDJSON.sync( {} ); // $ExpectError
131
+ readNDJSON.sync( ( x: number ): number => x ); // $ExpectError
132
+ }
133
+
134
+ // The compiler throws an error if the `sync` method is provided an options argument which is not an object or string...
135
+ {
136
+ readNDJSON.sync( 'file.ndjson', null ); // $ExpectError
137
+ readNDJSON.sync( 'file.ndjson', true ); // $ExpectError
138
+ readNDJSON.sync( 'file.ndjson', false ); // $ExpectError
139
+ readNDJSON.sync( 'file.ndjson', 123 ); // $ExpectError
140
+ readNDJSON.sync( 'file.ndjson', [] ); // $ExpectError
141
+ readNDJSON.sync( 'file.ndjson', ( x: number ): number => x ); // $ExpectError
142
+ }
143
+
144
+ // The compiler throws an error if the `sync` method is provided an `encoding` option which is not a string or null...
145
+ {
146
+ readNDJSON.sync( 'file.ndjson', { 'encoding': 123 } ); // $ExpectError
147
+ readNDJSON.sync( 'file.ndjson', { 'encoding': true } ); // $ExpectError
148
+ readNDJSON.sync( 'file.ndjson', { 'encoding': false } ); // $ExpectError
149
+ readNDJSON.sync( 'file.ndjson', { 'encoding': [] } ); // $ExpectError
150
+ readNDJSON.sync( 'file.ndjson', { 'encoding': {} } ); // $ExpectError
151
+ readNDJSON.sync( 'file.ndjson', { 'encoding': ( x: number ): number => x } ); // $ExpectError
152
+ }
153
+
154
+ // The compiler throws an error if the `sync` method is provided a `flag` option which is not a string...
155
+ {
156
+ readNDJSON.sync( 'file.ndjson', { 'flag': 123 } ); // $ExpectError
157
+ readNDJSON.sync( 'file.ndjson', { 'flag': true } ); // $ExpectError
158
+ readNDJSON.sync( 'file.ndjson', { 'flag': false } ); // $ExpectError
159
+ readNDJSON.sync( 'file.ndjson', { 'flag': null } ); // $ExpectError
160
+ readNDJSON.sync( 'file.ndjson', { 'flag': [] } ); // $ExpectError
161
+ readNDJSON.sync( 'file.ndjson', { 'flag': {} } ); // $ExpectError
162
+ readNDJSON.sync( 'file.ndjson', { 'flag': ( x: number ): number => x } ); // $ExpectError
163
+ }
164
+
165
+ // The compiler throws an error if the `sync` method is provided a `reviver` option which is not a function...
166
+ {
167
+ readNDJSON.sync( 'file.ndjson', { 'reviver': 'abc' } ); // $ExpectError
168
+ readNDJSON.sync( 'file.ndjson', { 'reviver': 123 } ); // $ExpectError
169
+ readNDJSON.sync( 'file.ndjson', { 'reviver': true } ); // $ExpectError
170
+ readNDJSON.sync( 'file.ndjson', { 'reviver': false } ); // $ExpectError
171
+ readNDJSON.sync( 'file.ndjson', { 'reviver': null } ); // $ExpectError
172
+ readNDJSON.sync( 'file.ndjson', { 'reviver': [] } ); // $ExpectError
173
+ readNDJSON.sync( 'file.ndjson', { 'reviver': {} } ); // $ExpectError
174
+ }
175
+
176
+ // The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
177
+ {
178
+ readNDJSON.sync(); // $ExpectError
179
+ }
@@ -0,0 +1,114 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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 parseNDJSON = require( '@ohos-ports/stdlib-utils/parse-ndjson' );
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 newline-delimited 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
+ * readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), onNDJSON );
51
+ *
52
+ * function onNDJSON( error, data ) {
53
+ * if ( error ) {
54
+ * throw error;
55
+ * }
56
+ * console.dir( data );
57
+ * }
58
+ */
59
+ function readNDJSON( 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 = parseNDJSON( file, opts.reviver );
101
+ } else {
102
+ file = parseNDJSON( 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 = readNDJSON;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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
+ /**
22
+ * Read a file as newline-delimited JSON.
23
+ *
24
+ * @module @stdlib/fs/read-ndjson
25
+ *
26
+ * @example
27
+ * var resolve = require( 'path' ).resolve;
28
+ * var readNDJSON = require( '@stdlib/fs/read-ndjson' );
29
+ *
30
+ * function clbk( error, data ) {
31
+ * if ( error ) {
32
+ * throw error;
33
+ * }
34
+ * console.log( data );
35
+ * }
36
+ *
37
+ * readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), clbk );
38
+ *
39
+ * @example
40
+ * var resolve = require( 'path' ).resolve;
41
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
42
+ * var readNDJSON = require( '@stdlib/fs/read-ndjson' );
43
+ *
44
+ * var out = readNDJSON.sync( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ) );
45
+ * if ( instanceOf( out, Error ) ) {
46
+ * throw out;
47
+ * }
48
+ * console.log( out );
49
+ */
50
+
51
+ // MODULES //
52
+
53
+ var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
54
+ var main = require( './async.js' );
55
+ var sync = require( './sync.js' );
56
+
57
+
58
+ // MAIN //
59
+
60
+ setReadOnly( main, 'sync', sync );
61
+
62
+
63
+ // EXPORTS //
64
+
65
+ module.exports = main;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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 readFile = require( './../../read-file' ).sync;
26
+ var removeBOM = require( '@stdlib/string/remove-utf8-bom' );
27
+ var parseNDJSON = require( '@ohos-ports/stdlib-utils/parse-ndjson' );
28
+ var instanceOf = require( '@stdlib/assert/instance-of' );
29
+ var format = require( '@stdlib/string/format' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Synchronously reads a file as newline-delimited JSON.
36
+ *
37
+ * @param {(string|Buffer|integer)} file - file path or file descriptor
38
+ * @param {(Options|string)} [options] - options
39
+ * @param {(string|null)} [options.encoding] - file encoding
40
+ * @param {string} [options.flag] - file status flag
41
+ * @param {Function} [options.reviver] - JSON reviver
42
+ * @throws {TypeError} options argument must be either a string or an object
43
+ * @returns {(Array|Error)} array of results or an error
44
+ *
45
+ * @example
46
+ * var resolve = require( 'path' ).resolve;
47
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
48
+ *
49
+ * var out = readNDJSONSync( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ) );
50
+ * if ( instanceOf( out, Error ) ) {
51
+ * throw out;
52
+ * }
53
+ * console.dir( out );
54
+ */
55
+ function readNDJSONSync( file, options ) {
56
+ var opts;
57
+ var f;
58
+ if ( arguments.length > 1 ) {
59
+ if ( isString( options ) ) {
60
+ opts = {
61
+ 'encoding': options
62
+ };
63
+ } else {
64
+ if ( !isObject( options ) ) {
65
+ throw new TypeError( format( 'invalid argument. Options argument must be either a string or an object. Value: `%s`.', options ) );
66
+ }
67
+ opts = options;
68
+ }
69
+ } else {
70
+ opts = {};
71
+ }
72
+ f = readFile( file, opts );
73
+ if ( instanceOf( f, Error ) ) {
74
+ return f;
75
+ }
76
+ f = f.toString();
77
+ if ( opts.encoding === 'utf8' ) {
78
+ f = removeBOM( f );
79
+ }
80
+ if ( opts.reviver ) {
81
+ return parseNDJSON( f, opts.reviver );
82
+ }
83
+ return parseNDJSON( f );
84
+ }
85
+
86
+
87
+ // EXPORTS //
88
+
89
+ module.exports = readNDJSONSync;
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@stdlib/fs/read-ndjson",
3
+ "version": "0.0.0",
4
+ "description": "Read a file as newline-delimited JSON.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "directories": {
18
+ "benchmark": "./benchmark",
19
+ "doc": "./docs",
20
+ "example": "./examples",
21
+ "lib": "./lib",
22
+ "test": "./test"
23
+ },
24
+ "types": "./docs/types",
25
+ "scripts": {},
26
+ "homepage": "https://github.com/stdlib-js/stdlib",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git://github.com/stdlib-js/stdlib.git"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/stdlib-js/stdlib/issues"
33
+ },
34
+ "dependencies": {},
35
+ "devDependencies": {},
36
+ "engines": {
37
+ "node": ">=0.10.0",
38
+ "npm": ">2.7.0"
39
+ },
40
+ "os": [
41
+ "aix",
42
+ "darwin",
43
+ "freebsd",
44
+ "linux",
45
+ "macos",
46
+ "openbsd",
47
+ "sunos",
48
+ "win32",
49
+ "windows"
50
+ ],
51
+ "keywords": [
52
+ "stdlib",
53
+ "stdfs",
54
+ "fs",
55
+ "readfile",
56
+ "readfilesync",
57
+ "path",
58
+ "async",
59
+ "sync",
60
+ "file",
61
+ "read",
62
+ "open",
63
+ "filesystem",
64
+ "json",
65
+ "ndjson",
66
+ "newline-delimited"
67
+ ]
68
+ }
@@ -0,0 +1,158 @@
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 WebAssembly
22
+
23
+ > Read a file as [WebAssembly][webassembly].
24
+
25
+ <section class="usage">
26
+
27
+ ## Usage
28
+
29
+ ```javascript
30
+ var readWASM = require( '@stdlib/fs/read-wasm' );
31
+ ```
32
+
33
+ <a name="read-wasm"></a>
34
+
35
+ #### readWASM( file\[, options], clbk )
36
+
37
+ Asynchronously reads a file as [WebAssembly][webassembly].
38
+
39
+ ```javascript
40
+ var join = require( 'path' ).join;
41
+
42
+ var fpath = join( __dirname, 'examples', 'fixtures', 'file.wasm' );
43
+ readWASM( fpath, onRead );
44
+
45
+ function onRead( error, buf ) {
46
+ if ( error ) {
47
+ throw error;
48
+ }
49
+ console.log( buf );
50
+ }
51
+ ```
52
+
53
+ The function accepts the following `options`:
54
+
55
+ - **flag**: file status flag.
56
+
57
+ #### readWASM.sync( file\[, options] )
58
+
59
+ Synchronously reads a file as [WebAssembly][webassembly].
60
+
61
+ ```javascript
62
+ var join = require( 'path' ).join;
63
+ var instanceOf = require( '@stdlib/assert/instance-of' );
64
+
65
+ var fpath = join( __dirname, 'examples', 'fixtures', 'file.wasm' );
66
+ var out = readWASM.sync( fpath );
67
+ if ( instanceOf( out, Error ) ) {
68
+ throw out;
69
+ }
70
+ console.log( out );
71
+ ```
72
+
73
+ The function accepts the same `options` as [`readWASM()`](#read-wasm) above.
74
+
75
+ </section>
76
+
77
+ <!-- /.usage -->
78
+
79
+ <section class="notes">
80
+
81
+ ## Notes
82
+
83
+ - In contrast to [`readFile()`][@stdlib/fs/read-file], neither function accepts an `encoding` option. As [WebAssembly][webassembly] is a binary file format, if provided an `encoding` option, the function **overrides** the option, setting the option value to `null`.
84
+ - Both functions return [WebAssembly][webassembly] file content as a [`Uint8Array`][@stdlib/array/uint8].
85
+
86
+ </section>
87
+
88
+ <!-- /.notes -->
89
+
90
+ <section class="examples">
91
+
92
+ ## Examples
93
+
94
+ <!-- eslint-disable no-undef -->
95
+
96
+ <!-- eslint no-undef: "error" -->
97
+
98
+ ```javascript
99
+ var join = require( 'path' ).join;
100
+ var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
101
+ var readWASM = require( '@stdlib/fs/read-wasm' );
102
+
103
+ var fpath = join( __dirname, 'examples', 'fixtures', 'file.wasm' );
104
+ readWASM( fpath, onRead );
105
+
106
+ function onRead( error, wasm ) {
107
+ var bool;
108
+ if ( error ) {
109
+ throw error;
110
+ }
111
+ bool = hasWebAssemblySupport();
112
+
113
+ // If WebAssembly is supported, create a WebAssembly module instance...
114
+ if ( bool ) {
115
+ wasm = new WebAssembly.Module( wasm );
116
+ wasm = new WebAssembly.Instance( wasm, {} );
117
+ console.log( wasm.exports.stdlib_hypot( 5.0, 12.0 ) );
118
+ } else {
119
+ console.log( wasm );
120
+ }
121
+ }
122
+ ```
123
+
124
+ </section>
125
+
126
+ <!-- /.examples -->
127
+
128
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
129
+
130
+ <section class="related">
131
+
132
+ * * *
133
+
134
+ ## See Also
135
+
136
+ - <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>
137
+
138
+ </section>
139
+
140
+ <!-- /.related -->
141
+
142
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
143
+
144
+ <section class="links">
145
+
146
+ [webassembly]: https://webassembly.org/
147
+
148
+ [@stdlib/fs/read-file]: https://github.com/stdlib-js/fs/tree/main/read-file
149
+
150
+ [@stdlib/array/uint8]: https://www.npmjs.com/package/@stdlib/array-uint8
151
+
152
+ <!-- <related-links> -->
153
+
154
+ <!-- </related-links> -->
155
+
156
+ </section>
157
+
158
+ <!-- /.links -->