Skip to content
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

feat: add fs/read-ndjson #2969

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
".md",
".mjs",
".mk",
".ndjson",
".png",
".py",
".R",
Expand Down
176 changes: 176 additions & 0 deletions lib/node_modules/@stdlib/fs/read-ndjson/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<!--

@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.

-->

# readNDJSON

> Read a file as [newline-delimited JSON][ndjson].

<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 [newline-delimited JSON][ndjson].

```javascript
var join = require( 'path' ).join;

readNDJSON( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ), onNDJSON );

function onNDJSON( error, data ) {
if ( error ) {
throw error;
}
console.dir( data );
}
```

The function accepts the following `options`:

- **encoding**: file encoding.
- **flag**: file status flag.
- **reviver**: [newline-delimited JSON][ndjson] 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', onNDJSON );

function onNDJSON( error, data ) {
if ( error ) {
throw error;
}
console.dir( data );
}
```

#### readNDJSON.sync( file\[, options] )

Synchronously reads a file as [newline-delimited JSON][ndjson].

```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 [`readNDJSON()`](#read-ndjson) 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 [newline-delimited JSON][ndjson].

</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, onNDJSON );
readNDJSON( 'beepboop', onNDJSON );

function onNDJSON( 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">

[ndjson]: http://www.ndjson.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 lib/node_modules/@stdlib/fs/read-ndjson/benchmark/benchmark.js
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();
});
72 changes: 72 additions & 0 deletions lib/node_modules/@stdlib/fs/read-ndjson/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

{{alias}}( file[, options], clbk )
Asynchronously reads a file as newline-delimited 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 newline-delimited JSON. Default: null.

options.flag: string (optional)
Flag. Default: 'r'.

options.reviver: Function (optional)
Newline-delimited 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.ndjson', onRead );


{{alias}}.sync( file[, options] )
Synchronously reads a file as newline-delimited 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 newline-delimited JSON. Default: null.

options.flag: string (optional)
Flag. Default: 'r'.

options.reviver: Function (optional)
Newline-delimited JSON transformation function.

Returns
-------
out: Error|newline-delimited JSON
File contents.

Examples
--------
> var out = {{alias}}.sync( './beep/boop.ndjson' );

See Also
--------

Loading
Loading