diff --git a/lib/node_modules/@stdlib/_tools/lint/filenames/lib/extensions.json b/lib/node_modules/@stdlib/_tools/lint/filenames/lib/extensions.json index 40daa83cba82..48a7b1fd7c7a 100644 --- a/lib/node_modules/@stdlib/_tools/lint/filenames/lib/extensions.json +++ b/lib/node_modules/@stdlib/_tools/lint/filenames/lib/extensions.json @@ -27,6 +27,7 @@ ".md", ".mjs", ".mk", + ".ndjson", ".png", ".py", ".R", diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/README.md b/lib/node_modules/@stdlib/fs/read-ndjson/README.md new file mode 100644 index 000000000000..cc37df68b10c --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/README.md @@ -0,0 +1,166 @@ + + +# readNDJSON + +> Read a file as [newline-delimited JSON][ndjson]. + +
+ +## Usage + +```javascript +var readNDJSON = require( '@stdlib/fs/read-ndjson' ); +``` + + + +#### 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' ), clbk ); + +function clbk( error, data ) { + if ( error ) { + throw error; + } + console.log( 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', clbk ); + +function clbk( error, data ) { + if ( error ) { + throw error; + } + console.log( 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.log( out ); +``` + +The function accepts the same `options` as [`readNDJSON()`](#read-ndjson) above. + +
+ + + +
+ +## 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]. + +
+ + + +
+ +## Examples + + + +```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 [...] + +data = readNDJSON.sync( 'beepboop', { + 'encoding': 'utf8' +}); +// returns + +// Asynchronously read file contents... +readNDJSON( file, clbk ); +readNDJSON( 'beepboop', clbk ); + +function clbk( 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[2].description ); + } +} +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/benchmark/benchmark.js b/lib/node_modules/@stdlib/fs/read-ndjson/benchmark/benchmark.js new file mode 100644 index 000000000000..005a246e8200 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/benchmark/benchmark.js @@ -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, 'fixtures', 'file.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(); +}); diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/benchmark/fixtures/file.ndjson b/lib/node_modules/@stdlib/fs/read-ndjson/benchmark/fixtures/file.ndjson new file mode 100644 index 000000000000..070d7fddeb7e --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/benchmark/fixtures/file.ndjson @@ -0,0 +1,18 @@ +{"name":"@stdlib/fs/read-ndjson"} +{"version":"0.0.0"} +{"description":"Read a file as JSON."} +{"license":"Apache-2.0"} +{"author":{"name":"The Stdlib Authors","url":"https://github.com/stdlib-js/stdlib/graphs/contributors"}} +{"contributors":[{"name":"The Stdlib Authors","url":"https://github.com/stdlib-js/stdlib/graphs/contributors"}]} +{"main":"./lib"} +{"directories":{"benchmark":"./benchmark","doc":"./docs","example":"./examples","lib":"./lib","test":"./test"}} +{"types":"./docs/types"} +{"scripts":{}} +{"homepage":"https://github.com/stdlib-js/stdlib"} +{"repository":{"type":"git","url":"git://github.com/stdlib-js/stdlib.git"}} +{"bugs":{"url":"https://github.com/stdlib-js/stdlib/issues"}} +{"dependencies":{}} +{"devDependencies":{}} +{"engines":{"node":">=0.10.0","npm":">2.7.0"}} +{"os":["aix","darwin","freebsd","linux","macos","openbsd","sunos","win32","windows"]} +{"keywords":["stdlib","stdfs","fs","readfile","readfilesync","path","async","sync","file","read","open","filesystem","json"]} diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/docs/repl.txt b/lib/node_modules/@stdlib/fs/read-ndjson/docs/repl.txt new file mode 100644 index 000000000000..b94cdcff6619 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/docs/repl.txt @@ -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) + 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) + JSON transformation function. + + Returns + ------- + out: Error|Array + File contents. + + Examples + -------- + > var out = {{alias}}.sync( './beep/boop.ndjson' ); + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/docs/types/index.d.ts b/lib/node_modules/@stdlib/fs/read-ndjson/docs/types/index.d.ts new file mode 100644 index 000000000000..6150e584f984 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/docs/types/index.d.ts @@ -0,0 +1,170 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Buffer } from 'buffer'; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * 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). + */ + encoding?: string | null; + + /** + * Flag (default: 'r'). + */ + flag?: string; + + /** + * JSON transformation function. + */ + reviver?: Reviver; +} + +/** +* JSON transformation function. +* +* @param key - key name +* @param value - corresponding value +* @returns updated value +*/ +type Reviver = ( key: string, value: any ) => any; + +/** +* Callback invoked upon reading a file. +* +* @param err - error object +* @param data - file contents +*/ +type Callback = ( err: Error | null, data: Array ) => void; + +/** +* Interface for reading a file as newline-delimited JSON. +*/ +interface ReadNDJSON { + /** + * Asynchronously reads a file as newline-delimited JSON. + * + * @param file - file path or file descriptor + * @param options - options + * @param options.encoding - file encoding + * @param options.flag - file status flag + * @param options.reviver - JSON reviver + * @param clbk - callback + * + * @example + * var resolve = require( 'path' ).resolve; + * + * readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), 'utf-8', clbk ); + * + * function clbk( error, data ) { + * if ( error ) { + * throw error; + * } + * console.log( data ); + * } + */ + ( file: string | Buffer | number, options: Options | string, clbk: Callback ): void; + + /** + * Asynchronously reads a file as newline-delimited newline-delimited JSON. + * + * @param file - file path or file descriptor + * @param clbk - callback + * + * @example + * var resolve = require( 'path' ).resolve; + * + * readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), clbk ); + * + * function clbk( error, data ) { + * if ( error ) { + * throw error; + * } + * console.log( data ); + * } + */ + ( file: string | Buffer | number, clbk: Callback ): void; + + /** + * Synchronously reads a file as newline-delimited JSON. + * + * @param file - file path or file descriptor + * @param options - options + * @param options.encoding - file encoding + * @param options.flag - file status flag + * @param options.reviver - JSON reviver + * @returns an array of results or an error + * + * @example + * var resolve = require( 'path' ).resolve; + * var instanceOf = require( '@stdlib/assert/instance-of' ); + * + * var out = readNDJSON.sync( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ) ); + * if ( instanceOf( out, Error ) ) { + * throw out; + * } + * console.log( out ); + */ + sync( file: string | Buffer | number, options?: Options | string ): Array | Error; +} + +/** +* Asynchronously reads a file as newline-delimited JSON. +* +* @param file - file path or file descriptor +* @param options - options +* @param options.encoding - file encoding +* @param options.flag - file status flag +* @param options.reviver - JSON reviver +* @param clbk - callback +* +* @example +* var resolve = require( 'path' ).resolve; +* +* readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), 'utf-8', clbk ); +* +* function clbk( error, data ) { +* if ( error ) { +* throw error; +* } +* console.log( data ); +* } +* +* @example +* var resolve = require( 'path' ).resolve; +* var instanceOf = require( '@stdlib/assert/instance-of' ); +* +* var out = readNDJSON.sync( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ) ); +* if ( instanceOf( out, Error ) ) { +* throw out; +* } +* console.log( out ); +*/ +declare var readNDJSON: ReadNDJSON; + + +// EXPORTS // + +export = readNDJSON; diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/docs/types/test.ts b/lib/node_modules/@stdlib/fs/read-ndjson/docs/types/test.ts new file mode 100644 index 000000000000..a483413184eb --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/docs/types/test.ts @@ -0,0 +1,179 @@ +/* +* @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. +*/ + +import readNDJSON = require( './index' ); + +/** +* Callback function. +* +* @param error - error +* @param file - file path +* @returns results +*/ +function onLoad( error: Error | null, data?: Array ): null { + if ( error || !data ) { + throw error; + } + return null; +}; + + +// TESTS // + +// The function does not have a return value... +{ + readNDJSON( 'file.ndjson', onLoad ); // $ExpectType void +} + +// The compiler throws an error if the function is provided a first argument which is not a string, buffer, or file descriptor... +{ + readNDJSON( false, onLoad ); // $ExpectError + readNDJSON( true, onLoad ); // $ExpectError + readNDJSON( null, onLoad ); // $ExpectError + readNDJSON( undefined, onLoad ); // $ExpectError + readNDJSON( [], onLoad ); // $ExpectError + readNDJSON( {}, onLoad ); // $ExpectError + readNDJSON( ( x: number ): number => x, onLoad ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature... +{ + readNDJSON( '/path/to/file.ndjson', 'abc' ); // $ExpectError + readNDJSON( '/path/to/file.ndjson', 1 ); // $ExpectError + readNDJSON( '/path/to/file.ndjson', false ); // $ExpectError + readNDJSON( '/path/to/file.ndjson', true ); // $ExpectError + readNDJSON( '/path/to/file.ndjson', null ); // $ExpectError + readNDJSON( '/path/to/file.ndjson', undefined ); // $ExpectError + readNDJSON( '/path/to/file.ndjson', [] ); // $ExpectError + readNDJSON( '/path/to/file.ndjson', {} ); // $ExpectError + readNDJSON( '/path/to/file.ndjson', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object or string... +{ + readNDJSON( 'file.ndjson', false, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', true, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', null, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', undefined, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', 123, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', [], onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', ( x: number ): number => x, onLoad ); // $ExpectError +} + +// The compiler throws an error if the function is provided an `encoding` option which is not a string or null... +{ + readNDJSON( 'file.ndjson', { 'encoding': 123 }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'encoding': true }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'encoding': false }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'encoding': [] }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'encoding': {} }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'encoding': ( x: number ): number => x }, onLoad ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `flag` option which is not a string... +{ + readNDJSON( 'file.ndjson', { 'flag': 123 }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'flag': true }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'flag': false }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'flag': null }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'flag': [] }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'flag': {} }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'flag': ( x: number ): number => x }, onLoad ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `reviver` option which is not a function... +{ + readNDJSON( 'file.ndjson', { 'reviver': 123 }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'reviver': 'abc' }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'reviver': true }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'reviver': false }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'reviver': null }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'reviver': [] }, onLoad ); // $ExpectError + readNDJSON( 'file.ndjson', { 'reviver': {} }, onLoad ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + readNDJSON(); // $ExpectError + readNDJSON( 'C:\\foo\\bar\\baz\\file.ndjson' ); // $ExpectError +} + +// Attached to main export is a `sync` method which returns results or an error... +{ + readNDJSON.sync( 'file.ndjson' ); // $ExpectType Error | unknown[] + readNDJSON.sync( 'file.ndjson' ); // $ExpectType Error | string[] + readNDJSON.sync( 'file.ndjson' ); // $ExpectType Error | number[] +} + +// The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor... +{ + readNDJSON.sync( false ); // $ExpectError + readNDJSON.sync( true ); // $ExpectError + readNDJSON.sync( null ); // $ExpectError + readNDJSON.sync( undefined ); // $ExpectError + readNDJSON.sync( [] ); // $ExpectError + readNDJSON.sync( {} ); // $ExpectError + readNDJSON.sync( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided an options argument which is not an object or string... +{ + readNDJSON.sync( 'file.ndjson', null ); // $ExpectError + readNDJSON.sync( 'file.ndjson', true ); // $ExpectError + readNDJSON.sync( 'file.ndjson', false ); // $ExpectError + readNDJSON.sync( 'file.ndjson', 123 ); // $ExpectError + readNDJSON.sync( 'file.ndjson', [] ); // $ExpectError + readNDJSON.sync( 'file.ndjson', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided an `encoding` option which is not a string or null... +{ + readNDJSON.sync( 'file.ndjson', { 'encoding': 123 } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'encoding': true } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'encoding': false } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'encoding': [] } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'encoding': {} } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'encoding': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided a `flag` option which is not a string... +{ + readNDJSON.sync( 'file.ndjson', { 'flag': 123 } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'flag': true } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'flag': false } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'flag': null } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'flag': [] } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'flag': {} } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'flag': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided a `reviver` option which is not a function... +{ + readNDJSON.sync( 'file.ndjson', { 'reviver': 'abc' } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'reviver': 123 } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'reviver': true } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'reviver': false } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'reviver': null } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'reviver': [] } ); // $ExpectError + readNDJSON.sync( 'file.ndjson', { 'reviver': {} } ); // $ExpectError +} + +// The compiler throws an error if the `sync` method is provided an unsupported number of arguments... +{ + readNDJSON.sync(); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/examples/fixtures/file.ndjson b/lib/node_modules/@stdlib/fs/read-ndjson/examples/fixtures/file.ndjson new file mode 100644 index 000000000000..f5862392a31f --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/examples/fixtures/file.ndjson @@ -0,0 +1,18 @@ +{"name":"@stdlib/fs/read-ndjson"} +{"version":"0.0.0"} +{"description":"Read a file as newline-delimited JSON."} +{"license":"Apache-2.0"} +{"author":{"name":"The Stdlib Authors","url":"https://github.com/stdlib-js/stdlib/graphs/contributors"}} +{"contributors":[{"name":"The Stdlib Authors","url":"https://github.com/stdlib-js/stdlib/graphs/contributors"}]} +{"main":"./lib"} +{"directories":{"benchmark":"./benchmark","doc":"./docs","example":"./examples","lib":"./lib","test":"./test"}} +{"types":"./docs/types"} +{"scripts":{}} +{"homepage":"https://github.com/stdlib-js/stdlib"} +{"repository":{"type":"git","url":"git://github.com/stdlib-js/stdlib.git"}} +{"bugs":{"url":"https://github.com/stdlib-js/stdlib/issues"}} +{"dependencies":{}} +{"devDependencies":{}} +{"engines":{"node":">=0.10.0","npm":">2.7.0"}} +{"os":["aix","darwin","freebsd","linux","macos","openbsd","sunos","win32","windows"]} +{"keywords":["stdlib","stdfs","fs","readfile","readfilesync","path","async","sync","file","read","open","filesystem","json"]} diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/examples/index.js b/lib/node_modules/@stdlib/fs/read-ndjson/examples/index.js new file mode 100644 index 000000000000..d6ba773282b5 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/examples/index.js @@ -0,0 +1,49 @@ +/** +* @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'; + +var resolve = require( 'path' ).resolve; +var readNDJSON = require( './../lib' ); + +var file = resolve( __dirname, 'fixtures', 'file.ndjson' ); + +// Synchronously read a newline-delimited JSON file... +var data1 = readNDJSON.sync( file, 'utf8' ); +console.log( data1 ); + +var data2 = readNDJSON.sync( 'beepboop', { + 'encoding': 'utf8' +}); +console.log( data2 ); + +// Asynchronously read a newline-delimited JSON file... +readNDJSON( file, clbk ); +readNDJSON( 'beepboop', clbk ); + +function clbk( 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[2].description ); + } +} diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/lib/async.js b/lib/node_modules/@stdlib/fs/read-ndjson/lib/async.js new file mode 100644 index 000000000000..a362c8fb4fcd --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/lib/async.js @@ -0,0 +1,114 @@ +/** +* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isObject = require( '@stdlib/assert/is-plain-object' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var readFile = require( '@stdlib/fs/read-file' ); +var removeBOM = require( '@stdlib/string/remove-utf8-bom' ); +var parseNDJSON = require( '@stdlib/utils/parse-ndjson' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Asynchronously reads a file as newline-delimited JSON. +* +* @param {(string|Buffer|integer)} file - file path or file descriptor +* @param {(Options|string)} [options] - options +* @param {(string|null)} [options.encoding] - file encoding +* @param {string} [options.flag] - file status flag +* @param {Function} [options.reviver] - JSON reviver +* @param {Callback} clbk - callback +* @throws {TypeError} options argument must be either a string or an object +* @throws {TypeError} callback argument must be a function +* +* @example +* var resolve = require( 'path' ).resolve; +* +* readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), onNDJSON ); +* +* function onNDJSON( error, data ) { +* if ( error ) { +* throw error; +* } +* console.dir( data ); +* } +*/ +function readNDJSON( file, options, clbk ) { + var opts; + var done; + if ( arguments.length < 3 ) { + opts = {}; + done = options; + } else { + if ( isString( options ) ) { + opts = { + 'encoding': options + }; + } else { + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be either a string or an object. Value: `%s`.', options ) ); + } + opts = options; + } + done = clbk; + } + if ( !isFunction( done ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) ); + } + readFile( file, opts, onRead ); + + /** + * Callback invoked upon reading a file. + * + * @private + * @param {(Error|null)} error - error object + * @param {(Buffer|string)} file - file contents + * @returns {void} + */ + function onRead( error, file ) { + if ( error ) { + return done( error ); + } + file = file.toString(); + if ( opts.encoding === 'utf8' ) { + file = removeBOM( file ); + } + if ( opts.reviver ) { + file = parseNDJSON( file, opts.reviver ); + } else { + file = parseNDJSON( file ); + } + if ( instanceOf( file, Error ) ) { + return done( file ); + } + done( null, file ); + } +} + + +// EXPORTS // + +module.exports = readNDJSON; diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/lib/index.js b/lib/node_modules/@stdlib/fs/read-ndjson/lib/index.js new file mode 100644 index 000000000000..0d5d4e075d01 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/lib/index.js @@ -0,0 +1,65 @@ +/** +* @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'; + +/** +* Read a file as newline-delimited JSON. +* +* @module @stdlib/fs/read-ndjson +* +* @example +* var resolve = require( 'path' ).resolve; +* var readNDJSON = require( '@stdlib/fs/read-ndjson' ); +* +* function clbk( error, data ) { +* if ( error ) { +* throw error; +* } +* console.log( data ); +* } +* +* readNDJSON( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ), clbk ); +* +* @example +* var resolve = require( 'path' ).resolve; +* var instanceOf = require( '@stdlib/assert/instance-of' ); +* var readNDJSON = require( '@stdlib/fs/read-ndjson' ); +* +* var out = readNDJSON.sync( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ) ); +* if ( instanceOf( out, Error ) ) { +* throw out; +* } +* console.log( out ); +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './async.js' ); +var sync = require( './sync.js' ); + + +// MAIN // + +setReadOnly( main, 'sync', sync ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/lib/sync.js b/lib/node_modules/@stdlib/fs/read-ndjson/lib/sync.js new file mode 100644 index 000000000000..f26137fdbba3 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/lib/sync.js @@ -0,0 +1,89 @@ +/** +* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isObject = require( '@stdlib/assert/is-plain-object' ); +var readFile = require( '@stdlib/fs/read-file' ).sync; +var removeBOM = require( '@stdlib/string/remove-utf8-bom' ); +var parseNDJSON = require( '@stdlib/utils/parse-ndjson' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Synchronously reads a file as newline-delimited JSON. +* +* @param {(string|Buffer|integer)} file - file path or file descriptor +* @param {(Options|string)} [options] - options +* @param {(string|null)} [options.encoding] - file encoding +* @param {string} [options.flag] - file status flag +* @param {Function} [options.reviver] - JSON reviver +* @throws {TypeError} options argument must be either a string or an object +* @returns {(Array|Error)} array of results or an error +* +* @example +* var resolve = require( 'path' ).resolve; +* var instanceOf = require( '@stdlib/assert/instance-of' ); +* +* var out = readNDJSONSync( resolve( __dirname, '..', 'examples', 'fixtures', 'file.ndjson' ) ); +* if ( instanceOf( out, Error ) ) { +* throw out; +* } +* console.dir( out ); +*/ +function readNDJSONSync( file, options ) { + var opts; + var f; + if ( arguments.length > 1 ) { + if ( isString( options ) ) { + opts = { + 'encoding': options + }; + } else { + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be either a string or an object. Value: `%s`.', options ) ); + } + opts = options; + } + } else { + opts = {}; + } + f = readFile( file, opts ); + if ( instanceOf( f, Error ) ) { + return f; + } + f = f.toString(); + if ( opts.encoding === 'utf8' ) { + f = removeBOM( f ); + } + if ( opts.reviver ) { + return parseNDJSON( f, opts.reviver ); + } + return parseNDJSON( f ); +} + + +// EXPORTS // + +module.exports = readNDJSONSync; diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/package.json b/lib/node_modules/@stdlib/fs/read-ndjson/package.json new file mode 100644 index 000000000000..2501aedf808a --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/fs/read-ndjson", + "version": "0.0.0", + "description": "Read a file as newline-delimited JSON.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdfs", + "fs", + "readfile", + "readfilesync", + "path", + "async", + "sync", + "file", + "read", + "open", + "filesystem", + "json", + "ndjson", + "newline-delimited" + ] +} diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/test/fixtures/bad.ndjson.txt b/lib/node_modules/@stdlib/fs/read-ndjson/test/fixtures/bad.ndjson.txt new file mode 100644 index 000000000000..d85a3a1f850b --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/test/fixtures/bad.ndjson.txt @@ -0,0 +1 @@ +{'bee: b'beep;} diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/test/fixtures/bom.ndjson.txt b/lib/node_modules/@stdlib/fs/read-ndjson/test/fixtures/bom.ndjson.txt new file mode 100644 index 000000000000..11bc1d99114d --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/test/fixtures/bom.ndjson.txt @@ -0,0 +1,4 @@ +{"beep": "boop"} +{"bop": [1, 2, 3]} +{"bool": true} +{"bap": null} diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/test/fixtures/good.ndjson b/lib/node_modules/@stdlib/fs/read-ndjson/test/fixtures/good.ndjson new file mode 100644 index 000000000000..78a79b1885cf --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/test/fixtures/good.ndjson @@ -0,0 +1,4 @@ +{"beep": "boop"} +{"bop": [1, 2, 3]} +{"bool": true} +{"bap": null} diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/test/test.async.js b/lib/node_modules/@stdlib/fs/read-ndjson/test/test.async.js new file mode 100644 index 000000000000..f5e52054c0d5 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/test/test.async.js @@ -0,0 +1,356 @@ +/** +* @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 join = require( 'path' ).join; +var tape = require( 'tape' ); +var readFile = require( '@stdlib/fs/read-file' ).sync; +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var noop = require( '@stdlib/utils/noop' ); +var parseJSON = require( '@stdlib/utils/parse-json' ); +var trim = require( '@stdlib/string/trim' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var readNDJSON = require( './../lib/async.js' ); + + +// VARIABLES // + +// Don't run tests in the browser...for now... +var opts = { + 'skip': IS_BROWSER // FIXME +}; + + +// FIXTURES // + +var goodJSON = join( __dirname, 'fixtures', 'good.ndjson' ); +var badJSON = join( __dirname, 'fixtures', 'bad.ndjson.txt' ); +var bomJSON = join( __dirname, 'fixtures', 'bom.ndjson.txt' ); + + +// FUNCTIONS // + +/** +* Loads a newline-delimited JSON file. +* +* @param {string} file - file path +* @returns {Array} results +*/ +function loadFile( file ) { + return trim( readFile( file, 'utf8' ) ).split( '\n' ).map( naryFunction( parseJSON, 1 ) ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof readNDJSON, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an options argument which is neither a string nor object', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + readNDJSON( goodJSON, value, noop ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + readNDJSON( goodJSON, value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (encoding)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + readNDJSON( goodJSON, 'utf8', value ); + }; + } +}); + +tape( 'the function throws an error if provided a callback argument which is not a function (options object)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + readNDJSON( goodJSON, {}, value ); + }; + } +}); + +tape( 'if the function encounters an error when attempting to read a file, the function returns the error', opts, function test( t ) { + readNDJSON( 'beepboopbapbop', onRead ); + + function onRead( error ) { + if ( error ) { + t.ok( true, error.message ); + } else { + t.ok( false, 'expected an error' ); + } + t.end(); + } +}); + +tape( 'if the function encounters an error when attempting to read a file, the function returns the error (encoding)', opts, function test( t ) { + readNDJSON( 'beepboopbapbop', 'utf8', onRead ); + + function onRead( error ) { + if ( error ) { + t.ok( true, error.message ); + } else { + t.ok( false, 'expected an error' ); + } + t.end(); + } +}); + +tape( 'if the function encounters an error when attempting to read a file, the function returns the error (options object)', opts, function test( t ) { + readNDJSON( 'beepboopbapbop', { + 'encoding': 'utf8' + }, onRead ); + + function onRead( error ) { + if ( error ) { + t.ok( true, error.message ); + } else { + t.ok( false, 'expected an error' ); + } + t.end(); + } +}); + +tape( 'if the function encounters an error when attempting to parse file contents as newline-delimited JSON, the function returns the error', opts, function test( t ) { + readNDJSON( badJSON, onRead ); + + function onRead( error ) { + if ( error ) { + t.ok( true, error.message ); + } else { + t.ok( false, 'expected an error' ); + } + t.end(); + } +}); + +tape( 'if the function encounters an error when attempting to parse file contents as newline-delimited JSON, the function returns the error (encoding)', opts, function test( t ) { + readNDJSON( badJSON, 'utf8', onRead ); + + function onRead( error ) { + if ( error ) { + t.ok( true, error.message ); + } else { + t.ok( false, 'expected an error' ); + } + t.end(); + } +}); + +tape( 'if the function encounters an error when attempting to parse file contents as newline-delimited JSON, the function returns the error (options object)', opts, function test( t ) { + readNDJSON( badJSON, { + 'encoding': 'utf8' + }, onRead ); + + function onRead( error ) { + if ( error ) { + t.ok( true, error.message ); + } else { + t.ok( false, 'expected an error' ); + } + t.end(); + } +}); + +tape( 'the function reads a file as newline-delimited JSON', opts, function test( t ) { + var expected = loadFile( goodJSON ); + + readNDJSON( goodJSON, onRead ); + + function onRead( error, actual ) { + if ( error ) { + t.ok( false, error.message ); + } + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + } +}); + +tape( 'the function reads a file as newline-delimited JSON using provided options (encoding)', opts, function test( t ) { + var expected = loadFile( goodJSON ); + + readNDJSON( goodJSON, 'utf8', onRead ); + + function onRead( error, actual ) { + if ( error ) { + t.ok( false, error.message ); + } + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + } +}); + +tape( 'the function reads a file as newline-delimited JSON using provided options (options object)', opts, function test( t ) { + var expected = loadFile( goodJSON ); + + readNDJSON( goodJSON, { + 'encoding': 'utf8' + }, onRead ); + + function onRead( error, actual ) { + if ( error ) { + t.ok( false, error.message ); + } + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + } +}); + +tape( 'the function supports reading a UTF-8 encoded file having a byte order mark (BOM) as newline-delimited JSON', opts, function test( t ) { + var expected = loadFile( goodJSON ); + + readNDJSON( bomJSON, { + 'encoding': 'utf8' + }, onRead ); + + function onRead( error, actual ) { + if ( error ) { + t.ok( false, error.message ); + } + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + } +}); + +tape( 'the function supports providing a JSON reviver', opts, function test( t ) { + var expected = [ + { + 'beep': 'boop' + }, + { + 'bop': [ 1, 2, 3 ] + }, + { + 'bool': true + }, + { + 'bap': 'woot' + } + ]; + readNDJSON( goodJSON, { + 'reviver': reviver + }, onRead ); + + function onRead( error, actual ) { + if ( error ) { + t.ok( false, error.message ); + } + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + } + + function reviver( key, value ) { + if ( key === 'bap' ) { + return 'woot'; + } + return value; + } +}); diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/test/test.js b/lib/node_modules/@stdlib/fs/read-ndjson/test/test.js new file mode 100644 index 000000000000..94b2cd2af0c4 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/test/test.js @@ -0,0 +1,38 @@ +/** +* @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 tape = require( 'tape' ); +var readNDJSON = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof readNDJSON, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a function to synchronously read a file as newline-delimited JSON', function test( t ) { + t.strictEqual( typeof readNDJSON.sync, 'function', 'has `sync` method' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/fs/read-ndjson/test/test.sync.js b/lib/node_modules/@stdlib/fs/read-ndjson/test/test.sync.js new file mode 100644 index 000000000000..a6a996060890 --- /dev/null +++ b/lib/node_modules/@stdlib/fs/read-ndjson/test/test.sync.js @@ -0,0 +1,214 @@ +/** +* @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 join = require( 'path' ).join; +var tape = require( 'tape' ); +var readFile = require( '@stdlib/fs/read-file' ).sync; +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var parseJSON = require( '@stdlib/utils/parse-json' ); +var trim = require( '@stdlib/string/trim' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var readNDJSON = require( './../lib/sync.js' ); + + +// VARIABLES // + +// Don't run tests in the browser...for now... +var opts = { + 'skip': IS_BROWSER // FIXME +}; + + +// FIXTURES // + +var goodJSON = join( __dirname, 'fixtures', 'good.ndjson' ); +var badJSON = join( __dirname, 'fixtures', 'bad.ndjson.txt' ); +var bomJSON = join( __dirname, 'fixtures', 'bom.ndjson.txt' ); + + +// FUNCTIONS // + +/** +* Loads a newline-delimited JSON file. +* +* @param {string} file - file path +* @returns {Array} results +*/ +function loadFile( file ) { + return trim( readFile( file, 'utf8' ) ).split( '\n' ).map( naryFunction( parseJSON, 1 ) ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof readNDJSON, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an options argument which is neither a string nor object', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + readNDJSON( goodJSON, value ); + }; + } +}); + +tape( 'if the function encounters an error when attempting to read a file, the function returns the error', opts, function test( t ) { + var out = readNDJSON( 'beepboopbapbop' ); + t.strictEqual( instanceOf( out, Error ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if the function encounters an error when attempting to read a file, the function returns the error (options)', opts, function test( t ) { + var out; + + out = readNDJSON( 'beepboopbapbop', 'utf8' ); + t.strictEqual( instanceOf( out, Error ), true, 'returns expected value' ); + + out = readNDJSON( 'beepboopbapbop', { + 'encoding': 'utf8' + }); + t.strictEqual( instanceOf( out, Error ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if the function encounters an error when attempting to parse file contents as newline-delimited JSON, the function returns the error', opts, function test( t ) { + var out = readNDJSON( badJSON ); + t.strictEqual( instanceOf( out, Error ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'if the function encounters an error when attempting to parse file contents as newline-delimited JSON, the function returns the error (options)', opts, function test( t ) { + var out; + + out = readNDJSON( badJSON, 'utf8' ); + t.strictEqual( instanceOf( out, Error ), true, 'returns expected value' ); + + out = readNDJSON( badJSON, { + 'encoding': 'utf8' + }); + t.strictEqual( instanceOf( out, Error ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reads a file as newline-delimited JSON', opts, function test( t ) { + var expected; + var actual; + + expected = loadFile( goodJSON ); + actual = readNDJSON( goodJSON ); + + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reads a file as newline-delimited JSON using provided options', opts, function test( t ) { + var expected; + var actual; + + // String options: + expected = loadFile( goodJSON ); + actual = readNDJSON( goodJSON, 'utf8' ); + + t.deepEqual( expected, actual, 'returns expected value' ); + + // Object options: + actual = readNDJSON( goodJSON, { + 'encoding': 'utf8' + }); + + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports reading a UTF-8 encoded file having a byte order mark (BOM) as newline-delimited JSON', opts, function test( t ) { + var expected; + var actual; + + expected = loadFile( goodJSON ); + actual = readNDJSON( bomJSON, { + 'encoding': 'utf8' + }); + + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a JSON reviver', opts, function test( t ) { + var expected; + var actual; + + expected = [ + { + 'beep': 'boop' + }, + { + 'bop': [ 1, 2, 3 ] + }, + { + 'bool': true + }, + { + 'bap': 'woot' + } + ]; + actual = readNDJSON( goodJSON, { + 'reviver': reviver + }); + + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); + + function reviver( key, value ) { + if ( key === 'bap' ) { + return 'woot'; + } + return value; + } +});