Skip to content

Commit 8d6f33c

Browse files
maniksharma17stdlib-botshubhexistsPlaneshifterkgryte
authored
feat: add utils/parse-ndjson
PR-URL: #1394 Closes: #1075 --------- Signed-off-by: Manik Sharma <maniksharma.rke@gmail.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Co-authored-by: Shubham Singh <shubh622005@gmail.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 80fdd70 commit 8d6f33c

File tree

10 files changed

+830
-0
lines changed

10 files changed

+830
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
# parseNDJSON
22+
23+
> Parse a string containing serialized newline-delimited [JSON][json] (NDJSON).
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var parseNDJSON = require( '@stdlib/utils/parse-ndjson' );
31+
```
32+
33+
#### parseNDJSON( str\[, reviver] )
34+
35+
Parses a `string` as `newline-delimited JSON`.
36+
37+
```javascript
38+
var out = parseNDJSON( '{"beep":"boop"}\n{"example":42}' );
39+
// returns [ { 'beep': 'boop' }, { 'example': 42 } ]
40+
```
41+
42+
To transform the `string` being parsed, provide a `reviver`.
43+
44+
```javascript
45+
function reviver( key, value ) {
46+
if ( key === '' || key === 'beep' ) {
47+
return ( typeof value === 'string' ) ? value.toUpperCase() : value;
48+
}
49+
return ( typeof value === 'number' ) ? value * 2 : value;
50+
}
51+
52+
var str = '{"beep":"boop"}\n{"value": 20}\n{"numbers": [1,2,3]}';
53+
var out = parseNDJSON( str, reviver );
54+
// returns [ { 'beep' : 'BOOP' }, { 'value': 40 }, { 'numbers': [ 2, 4, 6 ] } ]
55+
```
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<section class="notes">
62+
63+
## Notes
64+
65+
- In contrast to the native [`JSON.parse()`][json-parse], this implementation parses `string` as `newline-delimited JSON` and returns an array of parsed JSONs.
66+
67+
```javascript
68+
var out = JSON.parse( '{"beep":"boop"}\n{"foo":"baz"}' );
69+
// throws <SyntaxError>
70+
71+
out = parseNDJSON( '{"beep":"boop"}\n{"foo":"baz"}' );
72+
// returns [ { 'beep': 'boop' }, { 'foo': 'baz' } ]
73+
```
74+
75+
76+
- In contrast to the native [`JSON.parse()`][json-parse], this implementation throws a TypeError if provided any value which is not a `string`.
77+
78+
```javascript
79+
var out = JSON.parse( null );
80+
// returns null
81+
82+
out = parseNDJSON( null );
83+
// throws <TypeError>
84+
```
85+
86+
87+
- In contrast to the native [`JSON.parse()`][json-parse], this implementation does **not** throw a SyntaxError if unable to parse a string as newline-delimited JSON.
88+
89+
```javascript
90+
var out = parseNDJSON( '{"beep":boop}' );
91+
// returns <SyntaxError>
92+
93+
out = JSON.parse( '{"beep":boop}' );
94+
// throws <SyntaxError>
95+
```
96+
97+
98+
- In contrast to the native [`JSON.parse()`][json-parse], this implementation throws a TypeError if provided a reviver argument which is not a function.
99+
100+
```javascript
101+
var out = JSON.parse( '{"a":"b"}', [] );
102+
// returns { 'a': 'b' }
103+
104+
out = parseNDJSON( '{"a":"b"}', [] );
105+
// throws <TypeError>
106+
```
107+
108+
109+
</section>
110+
111+
<!-- /.notes -->
112+
113+
<section class="examples">
114+
115+
## Examples
116+
117+
<!-- eslint no-undef: "error" -->
118+
119+
```javascript
120+
var parseNDJSON = require( '@stdlib/utils/parse-ndjson' );
121+
122+
var out = parseNDJSON( '{"name":"John"}\n{"name":"Doe"}' );
123+
// returns [ { 'name': 'John' }, { 'name': 'Doe' } ]
124+
125+
function reviver( key, value ) {
126+
if ( key === 'name' ) {
127+
return value.toUpperCase();
128+
}
129+
return value;
130+
}
131+
132+
out = parseNDJSON( '{"name":"John"}\n{"name":"Doe"}', reviver );
133+
// returns [ { 'name': 'JOHN' }, { 'name': 'DOE' } ]
134+
135+
out = parseNDJSON( '{"name":John}\n{"name":Doe}' );
136+
// returns <SyntaxError>
137+
138+
out = parseNDJSON( ' ' );
139+
// returns []
140+
141+
out = parseNDJSON( '{}' );
142+
// returns [ {} ]
143+
144+
out = parseNDJSON( '{"name":"Eve"}\n42\ntrue\n[1,2,3]' );
145+
// returns [ { 'name': 'Eve' }, 42, true, [ 1, 2, 3 ] ]
146+
147+
out = parseNDJSON( '{"name":"John"}\r\n{"name":"Doe"}' );
148+
// returns [ { 'name': 'John' }, { 'name': 'Doe' } ]
149+
150+
out = parseNDJSON( '{"name":"John"}\n{"name":"Doe"}\n' );
151+
// returns [ { 'name': 'John' }, { 'name': 'Doe' } ]
152+
```
153+
154+
</section>
155+
156+
<!-- /.examples -->
157+
158+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
159+
160+
<section class="related">
161+
162+
</section>
163+
164+
<!-- /.related -->
165+
166+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
167+
168+
<section class="links">
169+
170+
[json]: http://www.json.org/
171+
172+
[json-parse]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
173+
174+
</section>
175+
176+
<!-- /.links -->
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var fromCodePoint = require( '@stdlib/string/from-code-point' );
25+
var pkg = require( './../package.json' ).name;
26+
var parseNDJSON = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var str;
33+
var out;
34+
var i;
35+
var j;
36+
37+
b.tic();
38+
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
// Generate an NDJSON string with a changing property key in each line:
41+
str = '{"beep":"boop","'+fromCodePoint( 97 + (i%26) ) + '":true}\n{"example":' + i + '}';
42+
out = parseNDJSON( str );
43+
44+
if ( out !== out ) {
45+
b.fail( 'should return an array of JSON objects' );
46+
}
47+
}
48+
49+
b.toc();
50+
51+
for ( j = 0; j < out.length; j++ ) {
52+
if ( out[j] instanceof Error ) {
53+
b.fail( 'should return an array of JSON objects' );
54+
}
55+
}
56+
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
// TODO: Add benchmarks with different sized NDJSON strings
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
{{alias}}( str[, reviver] )
3+
Attempts to parse a string as newline-delimited JSON (NDJSON).
4+
5+
Function behavior differs from `JSON.parse()` as follows:
6+
7+
- Returns array of parsed JSON
8+
- Throws a `TypeError` if provided any value which is not a string.
9+
- Returns a `SyntaxError` if unable to parse a string as NDJSON.
10+
- Throws a `TypeError` if provided a `reviver` argument which
11+
is not a function.
12+
13+
Parameters
14+
----------
15+
str: string
16+
String to parse as newline-delimited JSON.
17+
18+
reviver: Function (optional)
19+
Transformation function.
20+
21+
Returns
22+
-------
23+
out: Array | Error
24+
Array of parsed values or an error.
25+
26+
Examples
27+
--------
28+
> var obj = '{"beep":"boop"}\n{"example":42}\n{"data":[1,2,3]}';
29+
> var result = {{alias}}( obj )
30+
[ { 'beep': 'boop' }, { 'example': 42 }, { 'data': [ 1, 2, 3 ] } ]
31+
32+
// Provide a reviver:
33+
> function reviver( key, value ) {
34+
... if ( key === '' || key === 'beep' ) {
35+
... return ( typeof value === 'string' )
36+
... ? value.toUpperCase() : value;
37+
... };
38+
... return typeof value === 'number' ? value * 2 : value;
39+
... };
40+
> var ndjsonString = '{"beep":"boop"}\n{"example":42}\n{"data":[1,2,3]}';
41+
> var resultWithReviver = {{alias}}( ndjsonString, reviver )
42+
[ { 'beep': 'boop' }, { 'example': 84 }, { 'data': [ 2, 4, 6 ] } ]
43+
44+
See Also
45+
--------
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
/**
22+
* Parses a string as newline-delimited JSON (NDJSON).
23+
*
24+
* @param str - input string containing NDJSON
25+
* @param reviver - transformation function applied to each line
26+
* @returns array of parsed values or an error
27+
*
28+
* @example
29+
* var arr = parseNDJSON( '{"beep":"boop"}\\n{"example":42}\\n{"data":[1,2,3]}' );
30+
* // returns [ { 'beep': 'boop' }, { 'example': 42 }, { 'data': [ 1, 2, 3 ] } ]
31+
*/
32+
33+
declare function parseNDJSON( str: string, reviver?: Function ): Array<any> | Error;
34+
35+
36+
// EXPORTS //
37+
38+
export = parseNDJSON;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import parseNDJSON = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of parsed values...
25+
{
26+
const validNDJSON = '{"beep":"boop"}\n{"example":42}\n{"data":[1,2,3]}';
27+
parseNDJSON( validNDJSON ); // $ExpectType any[] | Error
28+
}
29+
30+
// The function does not compile if the argument is a value other than a string...
31+
{
32+
parseNDJSON( true ); // $ExpectError
33+
parseNDJSON( false ); // $ExpectError
34+
parseNDJSON( 5 ); // $ExpectError
35+
parseNDJSON( [] ); // $ExpectError
36+
parseNDJSON( {} ); // $ExpectError
37+
parseNDJSON( ( x: number ): number => x ); // $ExpectError
38+
}
39+
40+
// The function does not compile if the second argument is a value other than a function...
41+
{
42+
parseNDJSON( '{"beep":"boop"}\n{"example":42}', true ); // $ExpectError
43+
parseNDJSON( '{"beep":"boop"}\n{"example":42}', false ); // $ExpectError
44+
parseNDJSON( '{"beep":"boop"}\n{"example":42}', 5 ); // $ExpectError
45+
parseNDJSON( '{"beep":"boop"}\n{"example":42}', [] ); // $ExpectError
46+
parseNDJSON( '{"beep":"boop"}\n{"example":42}', {} ); // $ExpectError
47+
parseNDJSON( '{"beep":"boop"}\n{"example":42}', 'baz' ); // $ExpectError
48+
}
49+
50+
// The compiler throws an error if the function is provided an unsupported number of arguments...
51+
{
52+
parseNDJSON( ); // $ExpectError
53+
parseNDJSON( '{"beep":"boop"}', 'baz', 'foo' ); // $ExpectError
54+
}

0 commit comments

Comments
 (0)