@iebh/reflib 2.3.1 → 2.3.2
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.
package/modules/endnoteXml.js
CHANGED
|
@@ -1,19 +1,8 @@
|
|
|
1
1
|
import camelCase from '../shared/camelCase.js';
|
|
2
2
|
import Emitter from '../shared/emitter.js';
|
|
3
3
|
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
let XMLParser;
|
|
7
|
-
if (typeof window === 'undefined') {
|
|
8
|
-
// We are in node env
|
|
9
|
-
import('htmlparser2/lib/WritableStream').then(module => {
|
|
10
|
-
XMLParser = module.WritableStream;
|
|
11
|
-
});
|
|
12
|
-
} else {
|
|
13
|
-
// Handle or ignore in browser environment
|
|
14
|
-
console.log("WritableStream not loaded in browser.");
|
|
15
|
-
}
|
|
16
|
-
|
|
4
|
+
// This import is overwritten by the 'browser' field in package.json with the shimmed version
|
|
5
|
+
import { WritableStream as XMLParser } from 'htmlparser2/lib/WritableStream';
|
|
17
6
|
|
|
18
7
|
/**
|
|
19
8
|
* @see modules/inhterface.js
|
|
@@ -42,59 +31,6 @@ export function readStream(stream) {
|
|
|
42
31
|
*/
|
|
43
32
|
let textAppend = false;
|
|
44
33
|
|
|
45
|
-
class XMLParserBrowser {
|
|
46
|
-
constructor(passedParserOptions) {
|
|
47
|
-
// Stores XML in text form once read
|
|
48
|
-
this.text = "";
|
|
49
|
-
this.emitter = emitter;
|
|
50
|
-
// Add event listeners to mimic htmlparser2 behavior in the browser
|
|
51
|
-
this.emitter.on('opentag', passedParserOptions.onopentag);
|
|
52
|
-
this.emitter.on('closetag', passedParserOptions.onclosetag);
|
|
53
|
-
this.emitter.on('text', passedParserOptions.ontext);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
write(data) {
|
|
57
|
-
// CF: TODO: Parse data as it comes in chunks for better memory efficiency
|
|
58
|
-
this.text += data;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
end() {
|
|
62
|
-
this.parseXML(this.text);
|
|
63
|
-
// Free memory
|
|
64
|
-
this.text = ''
|
|
65
|
-
this.emitter.emit('end');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
parseXML(xmlString) {
|
|
69
|
-
let parser = new DOMParser();
|
|
70
|
-
let doc = parser.parseFromString(xmlString, 'application/xml');
|
|
71
|
-
this.traverseNode(doc.documentElement);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
traverseNode(node) {
|
|
75
|
-
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
76
|
-
let name = camelCase(node.nodeName);
|
|
77
|
-
let attrs = Array.from(node.attributes).reduce((acc, attr) => {
|
|
78
|
-
acc[attr.name] = attr.value;
|
|
79
|
-
return acc;
|
|
80
|
-
}, {});
|
|
81
|
-
|
|
82
|
-
this.emitter.emit('opentag', name, attrs);
|
|
83
|
-
|
|
84
|
-
for (let child of node.childNodes) {
|
|
85
|
-
this.traverseNode(child);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
this.emitter.emit('closetag', name);
|
|
89
|
-
} else if (node.nodeType === Node.TEXT_NODE) {
|
|
90
|
-
let text = node.nodeValue.trim();
|
|
91
|
-
if (text) {
|
|
92
|
-
this.emitter.emit('text', text);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
34
|
/**
|
|
99
35
|
* The options/callbacks for the parser
|
|
100
36
|
* @type {Object}
|
|
@@ -168,18 +104,7 @@ export function readStream(stream) {
|
|
|
168
104
|
// Queue up the parser in the next tick (so we can return the emitter first)
|
|
169
105
|
setTimeout(() => {
|
|
170
106
|
|
|
171
|
-
if (stream.
|
|
172
|
-
// We are on the node.js client
|
|
173
|
-
console.log('Loading EndNote library as node.js')
|
|
174
|
-
let parser = new XMLParserBrowser(parserOptions);
|
|
175
|
-
stream.on('data', ()=> emitter.emit('progress', stream.bytesRead))
|
|
176
|
-
stream.pipe(parser)
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
else if (typeof stream.pipe === 'function') {
|
|
181
|
-
// We are on the node.js client
|
|
182
|
-
console.log('Loading EndNote library as node.js')
|
|
107
|
+
if (typeof stream.pipe === 'function') {
|
|
183
108
|
let parser = new XMLParser(parserOptions);
|
|
184
109
|
stream.on('data', ()=> emitter.emit('progress', stream.bytesRead))
|
|
185
110
|
stream.pipe(parser)
|
|
@@ -187,7 +112,7 @@ export function readStream(stream) {
|
|
|
187
112
|
}
|
|
188
113
|
|
|
189
114
|
else {
|
|
190
|
-
console.error('Error
|
|
115
|
+
console.error('Error with stream, check "streamEmitter.js" if on browser')
|
|
191
116
|
}
|
|
192
117
|
|
|
193
118
|
})
|
package/modules/json.js
CHANGED
|
@@ -1,45 +1,7 @@
|
|
|
1
1
|
import Emitter from '../shared/emitter.js';
|
|
2
|
+
// This import is overwritten by the 'browser' field in package.json with the shimmed version
|
|
2
3
|
import JSONStream from 'JSONStream';
|
|
3
4
|
|
|
4
|
-
class BrowserJSONStream {
|
|
5
|
-
constructor() {
|
|
6
|
-
this.text = '';
|
|
7
|
-
this.emitter = Emitter();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
write(data) {
|
|
11
|
-
// CF: TODO: Parse data as it comes in chunks for better memory efficiency
|
|
12
|
-
this.text += data
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
end() {
|
|
16
|
-
try {
|
|
17
|
-
// Parse this.text as JSON
|
|
18
|
-
const jsonArray = JSON.parse(this.text);
|
|
19
|
-
// Free memory
|
|
20
|
-
this.text = ''
|
|
21
|
-
|
|
22
|
-
// For each entry in the json array (as ref):
|
|
23
|
-
jsonArray.forEach(ref => {
|
|
24
|
-
this.emitter.emit('ref', {
|
|
25
|
-
recNumber: this.recNumber++,
|
|
26
|
-
...ref,
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
// Finished
|
|
31
|
-
this.emitter.emit('end');
|
|
32
|
-
} catch (e) {
|
|
33
|
-
console.error('Error parsing final JSON:', e);
|
|
34
|
-
this.emitter.emit('error', e);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
on(event, listener) {
|
|
39
|
-
this.emitter.on(event, listener);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
5
|
/**
|
|
44
6
|
* @see modules/interface.js
|
|
45
7
|
*/
|
|
@@ -51,34 +13,21 @@ export function readStream(stream) {
|
|
|
51
13
|
setTimeout(()=> {
|
|
52
14
|
stream.on('data', ()=> emitter.emit('progress', stream.bytesRead));
|
|
53
15
|
|
|
54
|
-
if (stream.
|
|
55
|
-
// On browser
|
|
56
|
-
console.log('Parsing JSON natively in browser');
|
|
57
|
-
const browserJSONStream = new BrowserJSONStream();
|
|
58
|
-
browserJSONStream.on('ref', (data) => {
|
|
59
|
-
emitter.emit('ref', data);
|
|
60
|
-
});
|
|
61
|
-
browserJSONStream.on('end', () => emitter.emit('end'));
|
|
62
|
-
browserJSONStream.on('error', (error) => emitter.emit('error', error));
|
|
63
|
-
stream.pipe(browserJSONStream);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
else if (typeof stream.pipe === 'function') {
|
|
16
|
+
if (typeof stream.pipe === 'function') {
|
|
67
17
|
// On node.js
|
|
68
18
|
console.log('Parsing JSON with node.js library')
|
|
69
19
|
const nodeJSONStream = JSONStream.parse('*')
|
|
70
|
-
nodeJSONStream
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
.on('
|
|
76
|
-
.on('error', emitter.emit.bind('error'));
|
|
20
|
+
nodeJSONStream.on('data', ref => emitter.emit('ref', {
|
|
21
|
+
recNumber: recNumber++,
|
|
22
|
+
...ref,
|
|
23
|
+
}))
|
|
24
|
+
nodeJSONStream.on('end', ()=> emitter.emit('end'))
|
|
25
|
+
nodeJSONStream.on('error', emitter.emit.bind('error'));
|
|
77
26
|
stream.pipe(nodeJSONStream)
|
|
78
27
|
}
|
|
79
28
|
|
|
80
29
|
else {
|
|
81
|
-
console.error('Error
|
|
30
|
+
console.error('Error with stream, check "streamEmitter.js" if on browser')
|
|
82
31
|
}
|
|
83
32
|
});
|
|
84
33
|
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import Emitter from '../../shared/emitter.js';
|
|
2
|
+
|
|
3
|
+
export default class BrowserJSONStream {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.text = '';
|
|
6
|
+
this.emitter = Emitter();
|
|
7
|
+
this.recNumber = 1;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
write(data) {
|
|
11
|
+
// CF: TODO: Parse data as it comes in chunks for better memory efficiency
|
|
12
|
+
this.text += data;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
end() {
|
|
16
|
+
try {
|
|
17
|
+
// Parse this.text as JSON
|
|
18
|
+
const jsonArray = JSON.parse(this.text);
|
|
19
|
+
// Free memory
|
|
20
|
+
this.text = '';
|
|
21
|
+
|
|
22
|
+
// For each entry in the json array (as ref):
|
|
23
|
+
jsonArray.forEach(ref => {
|
|
24
|
+
this.emitter.emit('data', ref);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Finished
|
|
28
|
+
this.emitter.emit('end');
|
|
29
|
+
} catch (e) {
|
|
30
|
+
console.error('Error parsing final JSON:', e);
|
|
31
|
+
this.emitter.emit('error', e);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
on(event, listener) {
|
|
36
|
+
this.emitter.on(event, listener);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static parse() {
|
|
40
|
+
return new BrowserJSONStream();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import camelCase from '../../shared/camelCase.js';
|
|
2
|
+
import Emitter from '../../shared/emitter.js';
|
|
3
|
+
|
|
4
|
+
export class WritableStream {
|
|
5
|
+
constructor(passedParserOptions) {
|
|
6
|
+
// Stores XML in text form once read
|
|
7
|
+
this.text = "";
|
|
8
|
+
this.emitter = Emitter();
|
|
9
|
+
// Add event listeners to mimic htmlparser2 behavior in the browser
|
|
10
|
+
this.emitter.on('opentag', passedParserOptions.onopentag);
|
|
11
|
+
this.emitter.on('closetag', passedParserOptions.onclosetag);
|
|
12
|
+
this.emitter.on('text', passedParserOptions.ontext);
|
|
13
|
+
this.emitter.on('end', passedParserOptions.onend);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
write(data) {
|
|
17
|
+
// CF: TODO: Parse data as it comes in chunks for better memory efficiency
|
|
18
|
+
this.text += data;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
end() {
|
|
22
|
+
this.parseXML(this.text);
|
|
23
|
+
// Free memory
|
|
24
|
+
this.text = ''
|
|
25
|
+
this.emitter.emit('end');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
parseXML(xmlString) {
|
|
29
|
+
let parser = new DOMParser();
|
|
30
|
+
let doc = parser.parseFromString(xmlString, 'application/xml');
|
|
31
|
+
this.traverseNode(doc.documentElement);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
traverseNode(node) {
|
|
35
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
36
|
+
let name = camelCase(node.nodeName);
|
|
37
|
+
let attrs = Array.from(node.attributes).reduce((acc, attr) => {
|
|
38
|
+
acc[attr.name] = attr.value;
|
|
39
|
+
return acc;
|
|
40
|
+
}, {});
|
|
41
|
+
|
|
42
|
+
this.emitter.emit('opentag', name, attrs);
|
|
43
|
+
|
|
44
|
+
for (let child of node.childNodes) {
|
|
45
|
+
this.traverseNode(child);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.emitter.emit('closetag', name);
|
|
49
|
+
} else if (node.nodeType === Node.TEXT_NODE) {
|
|
50
|
+
let text = node.nodeValue.trim();
|
|
51
|
+
if (text) {
|
|
52
|
+
this.emitter.emit('text', text);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iebh/reflib",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.2",
|
|
4
4
|
"description": "Reference / Citation reference library utilities",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "eslint lib modules shared test",
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
"./*": "./lib/*.js"
|
|
40
40
|
},
|
|
41
41
|
"browser": {
|
|
42
|
-
"htmlparser2/lib/WritableStream":
|
|
43
|
-
"htmlparser2/lib/esm/WritableStream":
|
|
44
|
-
"JSONStream":
|
|
42
|
+
"htmlparser2/lib/WritableStream": "./modules/shims/WritableStream-browser.js",
|
|
43
|
+
"htmlparser2/lib/esm/WritableStream": "./modules/shims/WritableStream-browser.js",
|
|
44
|
+
"JSONStream": "./modules/shims/JSONStream-browser.js"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@momsfriendlydevco/eslint-config": "^1.0.9",
|