@iebh/reflib 2.7.2 → 2.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,47 +1,47 @@
1
- /**
2
- * Generic module interface
3
- * This file serves no purpose other than to document the methods that each module can itself expose
4
- */
5
- /* eslint-disable jsdoc/require-returns-check, no-unused-vars */
6
-
7
-
8
- /**
9
- * Provide the low-level function for a module to read a stream and emit refs
10
- * @param {stream.Readable} stream Input stream to read from
11
- * @param {Object} [options] Additional options to use when parsing
12
- * @returns {EventEmitter} EventEmitter(-like) that should emit the below events
13
- *
14
- * @emits ref Emitted with a single ref object when found
15
- * @emits end Emitted when parsing has completed
16
- * @emits error Emitted when an error has been raised
17
- * @emits progress Emitted as (bytesRead) when reading a stream
18
- */
19
- export function readStream(stream, options) {
20
- // Stub
21
- }
22
-
23
-
24
- /**
25
- * Provide the low-level function for a module to write a stream
26
- * @param {stream.Writable} stream Output stream to write to
27
- * @param {array<Object>} refs Collection of refs to write
28
- * @param {Object} [options] Additional options to use when writing
29
- *
30
- * @returns {Object} An object which exposes methods to call to start, write and end the writing process. All methods MUST return a Promise
31
- * @property {function<Promise>} start Function to call when beginning to write
32
- * @property {function<Promise>} write Function called as `(ref)` when writing a single ref
33
- * @property {function<promise>} [middle] Function to call after `write()` for each reference if the reference is NOT last, this works similar to `Array.prototype.join()`
34
- * @property {function<Promise>} end Function to call when finishing writing, must resolve its Promise when the stream has closed successfully
35
- */
36
- export function writeStream(stream, refs, options) {
37
- // Stub
38
- }
39
-
40
-
41
- /**
42
- * Function to run to set up any additional data before the module is needed
43
- * This is generally used by worker functions which construct maps from collections for efficiency purposes
44
- */
45
- export function setup() {
46
- // Stub
47
- }
1
+ /**
2
+ * Generic module interface
3
+ * This file serves no purpose other than to document the methods that each module can itself expose
4
+ */
5
+ /* eslint-disable jsdoc/require-returns-check, no-unused-vars */
6
+
7
+
8
+ /**
9
+ * Provide the low-level function for a module to read a stream and emit refs
10
+ * @param {stream.Readable} stream Input stream to read from
11
+ * @param {Object} [options] Additional options to use when parsing
12
+ * @returns {EventEmitter} EventEmitter(-like) that should emit the below events
13
+ *
14
+ * @emits ref Emitted with a single ref object when found
15
+ * @emits end Emitted when parsing has completed
16
+ * @emits error Emitted when an error has been raised
17
+ * @emits progress Emitted as (bytesRead) when reading a stream
18
+ */
19
+ export function readStream(stream, options) {
20
+ // Stub
21
+ }
22
+
23
+
24
+ /**
25
+ * Provide the low-level function for a module to write a stream
26
+ * @param {stream.Writable} stream Output stream to write to
27
+ * @param {array<Object>} refs Collection of refs to write
28
+ * @param {Object} [options] Additional options to use when writing
29
+ *
30
+ * @returns {Object} An object which exposes methods to call to start, write and end the writing process. All methods MUST return a Promise
31
+ * @property {function<Promise>} start Function to call when beginning to write
32
+ * @property {function<Promise>} write Function called as `(ref)` when writing a single ref
33
+ * @property {function<promise>} [middle] Function to call after `write()` for each reference if the reference is NOT last, this works similar to `Array.prototype.join()`
34
+ * @property {function<Promise>} end Function to call when finishing writing, must resolve its Promise when the stream has closed successfully
35
+ */
36
+ export function writeStream(stream, refs, options) {
37
+ // Stub
38
+ }
39
+
40
+
41
+ /**
42
+ * Function to run to set up any additional data before the module is needed
43
+ * This is generally used by worker functions which construct maps from collections for efficiency purposes
44
+ */
45
+ export function setup() {
46
+ // Stub
47
+ }
package/modules/json.js CHANGED
@@ -1,79 +1,109 @@
1
- import Emitter from '../shared/emitter.js';
2
- // This import is overwritten by the 'browser' field in package.json with the shimmed version
3
- import JSONStream from 'JSONStream';
4
-
5
- /**
6
- * Read a JSON stream and emit references
7
- * @see modules/interface.js
8
- *
9
- * @param {Stream} stream Stream primative to encapsulate
10
- *
11
- * @returns {Object} A readable stream analogue defined in `modules/interface.js`
12
- */
13
- export function readStream(stream) {
14
- let recNumber = 1;
15
- let emitter = Emitter();
16
-
17
- // Queue up the parser in the next tick (so we can return the emitter first)
18
- setTimeout(()=> {
19
- stream.on('data', ()=> emitter.emit('progress', stream.bytesRead));
20
-
21
- if (typeof stream.pipe === 'function') {
22
- // On node.js
23
- const nodeJSONStream = JSONStream.parse('*')
24
- nodeJSONStream.on('data', ref => emitter.emit('ref', {
25
- recNumber: recNumber++,
26
- ...ref,
27
- }))
28
- nodeJSONStream.on('end', ()=> emitter.emit('end'))
29
- nodeJSONStream.on('error', emitter.emit.bind('error'));
30
- stream.pipe(nodeJSONStream)
31
- }
32
-
33
- else {
34
- console.error('Error with stream, check "streamEmitter.js" if on browser')
35
- }
36
- });
37
-
38
- return emitter;
39
- }
40
-
41
-
42
- /**
43
- * Write to a stream object
44
- *
45
- * @see modules/interface.js
46
- *
47
- * @param {Steam} [stream] The stream to write to
48
- * @param {Object} [options] Additional options to use when parsing
49
- * @param {string} [options.lineSuffix='\n'] Optional line suffix for each output line of JSON
50
- *
51
- * @returns {Object} A writable stream analogue defined in `modules/interface.js`
52
- */
53
- export function writeStream(stream, options) {
54
- let settings = {
55
- lineSuffix: '\n',
56
- ...options,
57
- };
58
-
59
- let lastRef; // Hold last refrence string in memory so we know when we've reached the end (last item shoulnd't have a closing comma)
60
-
61
- return {
62
- start: ()=> {
63
- stream.write('[\n');
64
- return Promise.resolve();
65
- },
66
- write: ref => {
67
- if (lastRef) stream.write(lastRef + ',' + settings.lineSuffix); // Flush last reference to disk with comma
68
- lastRef = JSON.stringify(ref);
69
- return Promise.resolve();
70
- },
71
- end: ()=> {
72
- if (lastRef) stream.write(lastRef + settings.lineSuffix); // Flush final reference to disk without final comma
73
- stream.write(']');
74
- return new Promise((resolve, reject) =>
75
- stream.end(err => err ? reject(err) : resolve())
76
- );
77
- },
78
- };
79
- }
1
+ import Emitter from '../shared/emitter.js';
2
+ // This import is overwritten by the 'browser' field in package.json with the shimmed version
3
+ import JSONStream from 'JSONStream';
4
+ import sortKeys from '@momsfriendlydevco/sort-keys';
5
+
6
+ /**
7
+ * Read a JSON stream and emit references
8
+ * @see modules/interface.js
9
+ *
10
+ * @param {Stream} stream Stream primative to encapsulate
11
+ *
12
+ * @returns {Object} A readable stream analogue defined in `modules/interface.js`
13
+ */
14
+ export function readStream(stream) {
15
+ let recNumber = 1;
16
+ let emitter = Emitter();
17
+
18
+ // Queue up the parser in the next tick (so we can return the emitter first)
19
+ setTimeout(()=> {
20
+ stream.on('data', ()=> emitter.emit('progress', stream.bytesRead));
21
+
22
+ if (typeof stream.pipe === 'function') {
23
+ // On node.js
24
+ const nodeJSONStream = JSONStream.parse('*')
25
+ nodeJSONStream.on('data', ref => emitter.emit('ref', {
26
+ recNumber: recNumber++,
27
+ ...ref,
28
+ }))
29
+ nodeJSONStream.on('end', ()=> emitter.emit('end'))
30
+ nodeJSONStream.on('error', emitter.emit.bind('error'));
31
+ stream.pipe(nodeJSONStream)
32
+ }
33
+
34
+ else {
35
+ console.error('Error with stream, check "streamEmitter.js" if on browser')
36
+ }
37
+ });
38
+
39
+ return emitter;
40
+ }
41
+
42
+
43
+ /**
44
+ * Write to a stream object
45
+ *
46
+ * @see modules/interface.js
47
+ *
48
+ * @param {Steam} [stream] The stream to write to
49
+ * @param {Object} [options] Additional options to use when parsing
50
+ * @param {String} [options.indent=2] The Indentation option, specify number of space indents or the literal string to indent by (i.e. `\t`)
51
+ * @param {String} [options.lineSuffix='\n'] Optional line suffix for each output line of JSON
52
+ * @param {Object} [options.sortKeys] Optional options object passed to `sort-keys` to tidy up the output. If omitted the reference is used as is
53
+ *
54
+ * @returns {Object} A writable stream analogue defined in `modules/interface.js`
55
+ */
56
+ export function writeStream(stream, options) {
57
+ let settings = {
58
+ lineSuffix: '\n',
59
+ indent: 2,
60
+ sortKeys: {
61
+ keys: {
62
+ 'recNumber': 1,
63
+ 'type': 5,
64
+ 'title': 13,
65
+ 'journal': 14,
66
+ 'authors': 15,
67
+ 'isbn': 16,
68
+ 'doi': 17,
69
+ 'edition': 21,
70
+ 'number': 22,
71
+ 'pages': 23,
72
+ 'language': 70,
73
+ 'custom1': 81,
74
+ 'custom2': 82,
75
+ 'custom3': 83,
76
+ 'custom4': 84,
77
+ 'custom5': 85,
78
+ 'custom6': 86,
79
+ 'custom7': 87,
80
+ 'researchNotes': 91,
81
+ 'notes': 92,
82
+ 'abstract': 93,
83
+ },
84
+ },
85
+ ...options,
86
+ };
87
+
88
+ let lastRef; // Hold last refrence string in memory so we know when we've reached the end (last item shoulnd't have a closing comma)
89
+
90
+ return {
91
+ start: ()=> {
92
+ stream.write('[\n');
93
+ return Promise.resolve();
94
+ },
95
+ write: rawRef => {
96
+ let ref = settings.sortKeys ? sortKeys(rawRef, settings.sortKeys) : rawRef;
97
+ if (lastRef) stream.write(lastRef + ',' + settings.lineSuffix); // Flush last reference to disk with comma
98
+ lastRef = JSON.stringify(ref, null, settings.indent);
99
+ return Promise.resolve();
100
+ },
101
+ end: ()=> {
102
+ if (lastRef) stream.write(lastRef + settings.lineSuffix); // Flush final reference to disk without final comma
103
+ stream.write(']');
104
+ return new Promise((resolve, reject) =>
105
+ stream.end(err => err ? reject(err) : resolve())
106
+ );
107
+ },
108
+ };
109
+ }