UNPKG

777 BJavaScriptView Raw
1import JSONStream from 'JSONStream';
2import { writeTo } from './utils';
3
4function JSONParse(data, feed) {
5 if (!this.handle) {
6 const separator = this.getParam('separator', '*');
7 this.handle = JSONStream.parse(separator);
8 this.handle.on('data', obj => feed.write(obj));
9 }
10 if (!this.isLast()) {
11 writeTo(this.handle,
12 data,
13 () => feed.end());
14 } else {
15 this.handle.end();
16 process.nextTick(() => {
17 feed.close();
18 });
19 }
20}
21
22/**
23 * Take `String` and parse JSON and generate objects
24 *
25 * @name JSONParse
26 * @param {String} [separator=*] to split at every JSONPath found
27 * @returns {Object}
28 * @see https://github.com/dominictarr/JSONStream
29 */
30export default {
31 JSONParse,
32};