-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add fs/read-ndjson
#2969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add fs/read-ndjson
#2969
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,5 +45,6 @@ | |
".wat", | ||
".woff", | ||
".yml", | ||
".xml" | ||
".xml", | ||
".ndjson" | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
<!-- | ||
@license Apache-2.0 | ||
Copyright (c) 2018 The Stdlib Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
# Read JSON | ||
yaswanthkosuru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
> Read a file as [JSON][json]. | ||
yaswanthkosuru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var readNDJSON = require( '@stdlib/fs/read-ndjson' ); | ||
``` | ||
|
||
<a name="read-ndjson"></a> | ||
|
||
#### readNDJSON( file\[, options], clbk ) | ||
|
||
Asynchronously reads a file as [JSON][json]. | ||
|
||
```javascript | ||
var join = require( 'path' ).join; | ||
|
||
readNDJSON( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ), onJSON ); | ||
|
||
function onJSON( error, data ) { | ||
if ( error ) { | ||
throw error; | ||
} | ||
console.dir( data ); | ||
} | ||
``` | ||
|
||
The function accepts the following `options`: | ||
|
||
- **encoding**: file encoding. | ||
- **flag**: file status flag. | ||
- **reviver**: [JSON][json] transformation `function`. | ||
|
||
The `options` parameter may also be a `string` specifying the file `encoding`. | ||
|
||
```javascript | ||
var join = require( 'path' ).join; | ||
|
||
readNDJSON( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ), 'utf8', onJSON ); | ||
|
||
function onJSON( error, data ) { | ||
if ( error ) { | ||
throw error; | ||
} | ||
console.dir( data ); | ||
} | ||
``` | ||
|
||
#### readNDJSON.sync( file\[, options] ) | ||
|
||
Synchronously reads a file as [JSON][json]. | ||
|
||
```javascript | ||
var join = require( 'path' ).join; | ||
var instanceOf = require( '@stdlib/assert/instance-of' ); | ||
|
||
var out = readNDJSON.sync( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ) ); | ||
if ( instanceOf( out, Error ) ) { | ||
throw out; | ||
} | ||
console.dir( out ); | ||
``` | ||
|
||
The function accepts the same `options` as [`readJSON()`](#read-json) above. | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- 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]. | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var join = require( 'path' ).join; | ||
var readNDJSON = require( '@stdlib/fs/read-ndjson' ); | ||
|
||
var file = join( __dirname, 'examples', 'fixtures', 'file.ndjson' ); | ||
|
||
// Synchronously read file contents... | ||
var data = readNDJSON.sync( file, 'utf8' ); | ||
// returns <Object> | ||
|
||
data = readNDJSON.sync( 'beepboop', { | ||
'encoding': 'utf8' | ||
}); | ||
// returns <Error> | ||
|
||
// Asynchronously read file contents... | ||
readNDJSON( file, onJSON ); | ||
readNDJSON( 'beepboop', onJSON ); | ||
|
||
function onJSON( error, data ) { | ||
if ( error ) { | ||
if ( error.code === 'ENOENT' ) { | ||
console.error( 'JSON file does not exist.' ); | ||
} else { | ||
throw error; | ||
} | ||
} else { | ||
console.log( 'Package description: %s', data.description ); | ||
} | ||
} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
* * * | ||
|
||
## See Also | ||
|
||
- <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> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[json]: http://www.json.org/ | ||
|
||
[bom]: https://en.wikipedia.org/wiki/Byte_order_mark | ||
|
||
<!-- <related-links> --> | ||
|
||
[@stdlib/fs/read-file]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fs/read-file | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
80 changes: 80 additions & 0 deletions
80
lib/node_modules/@stdlib/fs/read-ndjson/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var resolve = require( 'path' ).resolve; | ||
var bench = require( '@stdlib/bench' ); | ||
var instanceOf = require( '@stdlib/assert/instance-of' ); | ||
var pkg = require( './../package.json' ).name; | ||
var readNDJSON = require( './../lib' ); | ||
|
||
|
||
// VARIABLES // | ||
|
||
var FILE = resolve( __dirname, '..', 'test/fixtures/good.ndjson' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var i; | ||
|
||
i = 0; | ||
b.tic(); | ||
|
||
return next(); | ||
|
||
function next() { | ||
i += 1; | ||
if ( i <= b.iterations ) { | ||
return readNDJSON( FILE, done ); | ||
} | ||
b.toc(); | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
} | ||
|
||
function done( error ) { | ||
if ( error ) { | ||
b.fail( error.message ); | ||
} | ||
next(); | ||
} | ||
}); | ||
|
||
bench( pkg+':sync', function benchmark( b ) { | ||
var out; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = readNDJSON.sync( FILE ); | ||
if ( out instanceof Error ) { | ||
b.fail( out.message ); | ||
} | ||
} | ||
b.toc(); | ||
if ( instanceOf( out, Error ) ) { | ||
b.fail( 'something went wrong' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
{{alias}}( file[, options], clbk ) | ||
Asynchronously reads a file as JSON. | ||
|
||
Parameters | ||
---------- | ||
file: string|Buffer|integer | ||
Filename or file descriptor. | ||
|
||
options: Object|string (optional) | ||
Options. If a string, the value is the encoding. | ||
|
||
options.encoding: string|null (optional) | ||
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. | ||
|
||
options.flag: string (optional) | ||
Flag. Default: 'r'. | ||
|
||
options.reviver: Function (optional) | ||
JSON transformation function. | ||
|
||
clbk: Function | ||
Callback to invoke upon reading file contents. | ||
|
||
Examples | ||
-------- | ||
> function onRead( error, data ) { | ||
... if ( error ) { | ||
... console.error( error.message ); | ||
... } else { | ||
... console.log( data ); | ||
... } | ||
... }; | ||
> {{alias}}( './beep/boop.json', onRead ); | ||
|
||
|
||
{{alias}}.sync( file[, options] ) | ||
Synchronously reads a file as JSON. | ||
|
||
Parameters | ||
---------- | ||
file: string|Buffer|integer | ||
Filename or file descriptor. | ||
|
||
options: Object|string (optional) | ||
Options. If a string, the value is the encoding. | ||
|
||
options.encoding: string|null (optional) | ||
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. | ||
|
||
options.flag: string (optional) | ||
Flag. Default: 'r'. | ||
|
||
options.reviver: Function (optional) | ||
JSON transformation function. | ||
|
||
Returns | ||
------- | ||
out: Error|JSON | ||
File contents. | ||
|
||
Examples | ||
-------- | ||
> var out = {{alias}}.sync( './beep/boop.json' ); | ||
|
||
See Also | ||
-------- | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.