@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.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +230 -2
- package/SECURITY.md +5 -0
- package/append-file/README.md +239 -0
- package/append-file/bin/cli +110 -0
- package/append-file/docs/repl.txt +72 -0
- package/append-file/docs/types/index.d.ts +133 -0
- package/append-file/docs/types/test.ts +193 -0
- package/append-file/docs/usage.txt +10 -0
- package/append-file/etc/cli_opts.json +22 -0
- package/append-file/lib/async.js +58 -0
- package/append-file/lib/index.js +61 -0
- package/append-file/lib/sync.js +58 -0
- package/append-file/package.json +69 -0
- package/close/README.md +172 -0
- package/close/docs/repl.txt +45 -0
- package/close/docs/types/index.d.ts +135 -0
- package/close/docs/types/test.ts +89 -0
- package/close/lib/index.js +73 -0
- package/close/lib/main.js +68 -0
- package/close/lib/sync.js +65 -0
- package/close/package.json +64 -0
- package/docs/types/index.d.ts +502 -0
- package/exists/README.md +252 -0
- package/exists/bin/cli +83 -0
- package/exists/docs/repl.txt +42 -0
- package/exists/docs/types/index.d.ts +112 -0
- package/exists/docs/types/test.ts +87 -0
- package/exists/docs/usage.txt +8 -0
- package/exists/etc/cli_opts.json +14 -0
- package/exists/lib/async.js +85 -0
- package/exists/lib/index.js +66 -0
- package/exists/lib/sync.js +62 -0
- package/exists/package.json +69 -0
- package/lib/index.js +186 -0
- package/open/README.md +204 -0
- package/open/docs/repl.txt +65 -0
- package/open/docs/types/index.d.ts +150 -0
- package/open/docs/types/test.ts +129 -0
- package/open/lib/defaults.json +4 -0
- package/open/lib/index.js +63 -0
- package/open/lib/main.js +64 -0
- package/open/lib/sync.js +68 -0
- package/open/package.json +64 -0
- package/package.json +67 -4
- package/read-dir/README.md +232 -0
- package/read-dir/bin/cli +83 -0
- package/read-dir/docs/repl.txt +44 -0
- package/read-dir/docs/types/index.d.ts +99 -0
- package/read-dir/docs/types/test.ts +85 -0
- package/read-dir/docs/usage.txt +8 -0
- package/read-dir/etc/cli_opts.json +14 -0
- package/read-dir/lib/async.js +57 -0
- package/read-dir/lib/index.js +62 -0
- package/read-dir/lib/sync.js +52 -0
- package/read-dir/package.json +69 -0
- package/read-file/README.md +246 -0
- package/read-file/bin/cli +91 -0
- package/read-file/docs/repl.txt +68 -0
- package/read-file/docs/types/index.d.ts +138 -0
- package/read-file/docs/types/test.ts +147 -0
- package/read-file/docs/usage.txt +10 -0
- package/read-file/etc/cli_opts.json +21 -0
- package/read-file/lib/index.js +61 -0
- package/read-file/lib/main.js +59 -0
- package/read-file/lib/sync.js +61 -0
- package/read-file/package.json +68 -0
- package/read-file-list/README.md +197 -0
- package/read-file-list/bin/cli +100 -0
- package/read-file-list/docs/repl.txt +81 -0
- package/read-file-list/docs/types/index.d.ts +166 -0
- package/read-file-list/docs/types/test.ts +166 -0
- package/read-file-list/docs/usage.txt +10 -0
- package/read-file-list/etc/cli_opts.json +21 -0
- package/read-file-list/lib/async.js +202 -0
- package/read-file-list/lib/clear_pending.js +47 -0
- package/read-file-list/lib/clear_timeout.js +23 -0
- package/read-file-list/lib/config.json +4 -0
- package/read-file-list/lib/delay.js +47 -0
- package/read-file-list/lib/index.js +66 -0
- package/read-file-list/lib/sync.js +94 -0
- package/read-file-list/package.json +72 -0
- package/read-json/README.md +176 -0
- package/read-json/docs/repl.txt +72 -0
- package/read-json/docs/types/index.d.ts +170 -0
- package/read-json/docs/types/test.ts +175 -0
- package/read-json/lib/async.js +114 -0
- package/read-json/lib/index.js +65 -0
- package/read-json/lib/sync.js +89 -0
- package/read-json/package.json +66 -0
- package/read-ndjson/README.md +166 -0
- package/read-ndjson/docs/repl.txt +72 -0
- package/read-ndjson/docs/types/index.d.ts +170 -0
- package/read-ndjson/docs/types/test.ts +179 -0
- package/read-ndjson/lib/async.js +114 -0
- package/read-ndjson/lib/index.js +65 -0
- package/read-ndjson/lib/sync.js +89 -0
- package/read-ndjson/package.json +68 -0
- package/read-wasm/README.md +158 -0
- package/read-wasm/docs/repl.txt +58 -0
- package/read-wasm/docs/types/index.d.ts +153 -0
- package/read-wasm/docs/types/test.ts +127 -0
- package/read-wasm/lib/async.js +106 -0
- package/read-wasm/lib/index.js +67 -0
- package/read-wasm/lib/sync.js +86 -0
- package/read-wasm/package.json +67 -0
- package/rename/README.md +259 -0
- package/rename/bin/cli +89 -0
- package/rename/docs/repl.txt +80 -0
- package/rename/docs/types/index.d.ts +109 -0
- package/rename/docs/types/test.ts +111 -0
- package/rename/docs/usage.txt +7 -0
- package/rename/etc/cli_opts.json +15 -0
- package/rename/lib/index.js +60 -0
- package/rename/lib/main.js +51 -0
- package/rename/lib/sync.js +53 -0
- package/rename/package.json +73 -0
- package/resolve-parent-path/README.md +204 -0
- package/resolve-parent-path/bin/cli +85 -0
- package/resolve-parent-path/docs/repl.txt +58 -0
- package/resolve-parent-path/docs/types/index.d.ts +128 -0
- package/resolve-parent-path/docs/types/test.ts +117 -0
- package/resolve-parent-path/docs/usage.txt +9 -0
- package/resolve-parent-path/etc/cli_opts.json +17 -0
- package/resolve-parent-path/lib/async.js +116 -0
- package/resolve-parent-path/lib/index.js +58 -0
- package/resolve-parent-path/lib/sync.js +83 -0
- package/resolve-parent-path/lib/validate.js +67 -0
- package/resolve-parent-path/package.json +78 -0
- package/resolve-parent-path-by/README.md +220 -0
- package/resolve-parent-path-by/docs/repl.txt +105 -0
- package/resolve-parent-path-by/docs/types/index.d.ts +220 -0
- package/resolve-parent-path-by/docs/types/test.ts +225 -0
- package/resolve-parent-path-by/lib/async.js +156 -0
- package/resolve-parent-path-by/lib/index.js +67 -0
- package/resolve-parent-path-by/lib/sync.js +98 -0
- package/resolve-parent-path-by/lib/validate.js +67 -0
- package/resolve-parent-path-by/package.json +76 -0
- package/resolve-parent-paths/README.md +228 -0
- package/resolve-parent-paths/bin/cli +93 -0
- package/resolve-parent-paths/docs/repl.txt +84 -0
- package/resolve-parent-paths/docs/types/index.d.ts +145 -0
- package/resolve-parent-paths/docs/types/test.ts +196 -0
- package/resolve-parent-paths/docs/usage.txt +10 -0
- package/resolve-parent-paths/etc/cli_opts.json +18 -0
- package/resolve-parent-paths/lib/index.js +60 -0
- package/resolve-parent-paths/lib/main.js +415 -0
- package/resolve-parent-paths/lib/sync.js +231 -0
- package/resolve-parent-paths/lib/validate.js +81 -0
- package/resolve-parent-paths/package.json +78 -0
- package/unlink/README.md +233 -0
- package/unlink/bin/cli +81 -0
- package/unlink/docs/repl.txt +62 -0
- package/unlink/docs/types/index.d.ts +105 -0
- package/unlink/docs/types/test.ts +85 -0
- package/unlink/docs/usage.txt +7 -0
- package/unlink/etc/cli_opts.json +15 -0
- package/unlink/lib/index.js +60 -0
- package/unlink/lib/main.js +50 -0
- package/unlink/lib/sync.js +52 -0
- package/unlink/package.json +70 -0
- package/write-file/README.md +250 -0
- package/write-file/bin/cli +110 -0
- package/write-file/docs/repl.txt +74 -0
- package/write-file/docs/types/index.d.ts +133 -0
- package/write-file/docs/types/test.ts +193 -0
- package/write-file/docs/usage.txt +10 -0
- package/write-file/etc/cli_opts.json +22 -0
- package/write-file/lib/index.js +60 -0
- package/write-file/lib/main.js +58 -0
- package/write-file/lib/sync.js +58 -0
- package/write-file/package.json +68 -0
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
/**
|
|
22
|
+
* Read a file as JSON.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/fs/read-json
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var resolve = require( 'path' ).resolve;
|
|
28
|
+
* var readJSON = require( '@stdlib/fs/read-json' );
|
|
29
|
+
*
|
|
30
|
+
* function onJSON( error, data ) {
|
|
31
|
+
* if ( error ) {
|
|
32
|
+
* throw error;
|
|
33
|
+
* }
|
|
34
|
+
* console.dir( data );
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* readJSON( resolve( __dirname, '..', 'package.json' ), onJSON );
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* var resolve = require( 'path' ).resolve;
|
|
41
|
+
* var instanceOf = require( '@stdlib/assert/instance-of' );
|
|
42
|
+
* var readJSON = require( '@stdlib/fs/read-json' );
|
|
43
|
+
*
|
|
44
|
+
* var out = readJSON.sync( resolve( __dirname, '..', 'package.json' ) );
|
|
45
|
+
* if ( instanceOf( out, Error ) ) {
|
|
46
|
+
* throw out;
|
|
47
|
+
* }
|
|
48
|
+
* console.dir( out );
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
// MODULES //
|
|
52
|
+
|
|
53
|
+
var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
|
|
54
|
+
var readJSON = require( './async.js' );
|
|
55
|
+
var sync = require( './sync.js' );
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
// MAIN //
|
|
59
|
+
|
|
60
|
+
setReadOnly( readJSON, 'sync', sync );
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
// EXPORTS //
|
|
64
|
+
|
|
65
|
+
module.exports = readJSON;
|
|
@@ -0,0 +1,89 @@
|
|
|
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 readFile = require( './../../read-file' ).sync;
|
|
26
|
+
var removeBOM = require( '@stdlib/string/remove-utf8-bom' );
|
|
27
|
+
var parseJSON = require( '@ohos-ports/stdlib-utils/parse-json' );
|
|
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 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 {(JSON|Error)} JSON or an error
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* var resolve = require( 'path' ).resolve;
|
|
47
|
+
* var instanceOf = require( '@stdlib/assert/instance-of' );
|
|
48
|
+
*
|
|
49
|
+
* var out = readJSONSync( resolve( __dirname, '..', 'package.json' ) );
|
|
50
|
+
* if ( instanceOf( out, Error ) ) {
|
|
51
|
+
* throw out;
|
|
52
|
+
* }
|
|
53
|
+
* console.dir( out );
|
|
54
|
+
*/
|
|
55
|
+
function readJSONSync( 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 parseJSON( f, opts.reviver );
|
|
82
|
+
}
|
|
83
|
+
return parseJSON( f );
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
// EXPORTS //
|
|
88
|
+
|
|
89
|
+
module.exports = readJSONSync;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/fs/read-json",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Read a file as 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
|
+
]
|
|
66
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2024 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
|
+
# readNDJSON
|
|
22
|
+
|
|
23
|
+
> Read a file as [newline-delimited JSON][ndjson].
|
|
24
|
+
|
|
25
|
+
<section class="usage">
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
var readNDJSON = require( '@stdlib/fs/read-ndjson' );
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
<a name="read-ndjson"></a>
|
|
34
|
+
|
|
35
|
+
#### readNDJSON( file\[, options], clbk )
|
|
36
|
+
|
|
37
|
+
Asynchronously reads a file as [newline-delimited JSON][ndjson].
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
var join = require( 'path' ).join;
|
|
41
|
+
|
|
42
|
+
readNDJSON( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ), clbk );
|
|
43
|
+
|
|
44
|
+
function clbk( error, data ) {
|
|
45
|
+
if ( error ) {
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
console.log( 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
|
+
readNDJSON( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ), 'utf8', clbk );
|
|
64
|
+
|
|
65
|
+
function clbk( error, data ) {
|
|
66
|
+
if ( error ) {
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
console.log( data );
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### readNDJSON.sync( file\[, options] )
|
|
74
|
+
|
|
75
|
+
Synchronously reads a file as [newline-delimited JSON][ndjson].
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
var join = require( 'path' ).join;
|
|
79
|
+
var instanceOf = require( '@stdlib/assert/instance-of' );
|
|
80
|
+
|
|
81
|
+
var out = readNDJSON.sync( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ) );
|
|
82
|
+
if ( instanceOf( out, Error ) ) {
|
|
83
|
+
throw out;
|
|
84
|
+
}
|
|
85
|
+
console.log( out );
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The function accepts the same `options` as [`readNDJSON()`](#read-ndjson) 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 [newline-delimited JSON][ndjson].
|
|
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 readNDJSON = require( '@stdlib/fs/read-ndjson' );
|
|
113
|
+
|
|
114
|
+
var file = join( __dirname, 'examples', 'fixtures', 'file.ndjson' );
|
|
115
|
+
|
|
116
|
+
// Synchronously read file contents...
|
|
117
|
+
var data = readNDJSON.sync( file, 'utf8' );
|
|
118
|
+
// returns [...]
|
|
119
|
+
|
|
120
|
+
data = readNDJSON.sync( 'beepboop', {
|
|
121
|
+
'encoding': 'utf8'
|
|
122
|
+
});
|
|
123
|
+
// returns <Error>
|
|
124
|
+
|
|
125
|
+
// Asynchronously read file contents...
|
|
126
|
+
readNDJSON( file, clbk );
|
|
127
|
+
readNDJSON( 'beepboop', clbk );
|
|
128
|
+
|
|
129
|
+
function clbk( 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[2].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
|
+
</section>
|
|
151
|
+
|
|
152
|
+
<!-- /.related -->
|
|
153
|
+
|
|
154
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
155
|
+
|
|
156
|
+
<section class="links">
|
|
157
|
+
|
|
158
|
+
[json]: http://www.json.org/
|
|
159
|
+
|
|
160
|
+
[ndjson]: http://www.ndjson.org/
|
|
161
|
+
|
|
162
|
+
[bom]: https://en.wikipedia.org/wiki/Byte_order_mark
|
|
163
|
+
|
|
164
|
+
</section>
|
|
165
|
+
|
|
166
|
+
<!-- /.links -->
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}( file[, options], clbk )
|
|
3
|
+
Asynchronously reads a file as newline-delimited 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 newline-delimited 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.ndjson', onRead );
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
{{alias}}.sync( file[, options] )
|
|
40
|
+
Synchronously reads a file as newline-delimited 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 newline-delimited 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|Array
|
|
64
|
+
File contents.
|
|
65
|
+
|
|
66
|
+
Examples
|
|
67
|
+
--------
|
|
68
|
+
> var out = {{alias}}.sync( './beep/boop.ndjson' );
|
|
69
|
+
|
|
70
|
+
See Also
|
|
71
|
+
--------
|
|
72
|
+
|
|
@@ -0,0 +1,170 @@
|
|
|
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
|
+
// 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 newline-delimited 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: Array<T> ) => void;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Interface for reading a file as newline-delimited JSON.
|
|
64
|
+
*/
|
|
65
|
+
interface ReadNDJSON {
|
|
66
|
+
/**
|
|
67
|
+
* Asynchronously reads a file as newline-delimited 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
|
+
* readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), 'utf-8', clbk );
|
|
80
|
+
*
|
|
81
|
+
* function clbk( error, data ) {
|
|
82
|
+
* if ( error ) {
|
|
83
|
+
* throw error;
|
|
84
|
+
* }
|
|
85
|
+
* console.log( 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 newline-delimited newline-delimited 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
|
+
* readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), clbk );
|
|
100
|
+
*
|
|
101
|
+
* function clbk( error, data ) {
|
|
102
|
+
* if ( error ) {
|
|
103
|
+
* throw error;
|
|
104
|
+
* }
|
|
105
|
+
* console.log( data );
|
|
106
|
+
* }
|
|
107
|
+
*/
|
|
108
|
+
<T = unknown>( file: string | Buffer | number, clbk: Callback<T> ): void;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Synchronously reads a file as newline-delimited 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 an array of results or an error
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* var resolve = require( 'path' ).resolve;
|
|
122
|
+
* var instanceOf = require( '@stdlib/assert/instance-of' );
|
|
123
|
+
*
|
|
124
|
+
* var out = readNDJSON.sync( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ) );
|
|
125
|
+
* if ( instanceOf( out, Error ) ) {
|
|
126
|
+
* throw out;
|
|
127
|
+
* }
|
|
128
|
+
* console.log( out );
|
|
129
|
+
*/
|
|
130
|
+
sync<T = unknown>( file: string | Buffer | number, options?: Options | string ): Array<T> | Error;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Asynchronously reads a file as newline-delimited 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
|
+
* readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), 'utf-8', clbk );
|
|
147
|
+
*
|
|
148
|
+
* function clbk( error, data ) {
|
|
149
|
+
* if ( error ) {
|
|
150
|
+
* throw error;
|
|
151
|
+
* }
|
|
152
|
+
* console.log( data );
|
|
153
|
+
* }
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* var resolve = require( 'path' ).resolve;
|
|
157
|
+
* var instanceOf = require( '@stdlib/assert/instance-of' );
|
|
158
|
+
*
|
|
159
|
+
* var out = readNDJSON.sync( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ) );
|
|
160
|
+
* if ( instanceOf( out, Error ) ) {
|
|
161
|
+
* throw out;
|
|
162
|
+
* }
|
|
163
|
+
* console.log( out );
|
|
164
|
+
*/
|
|
165
|
+
declare var readNDJSON: ReadNDJSON;
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
// EXPORTS //
|
|
169
|
+
|
|
170
|
+
export = readNDJSON;
|