@iebh/reflib 2.1.4 → 2.2.0

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/lib/browser.js CHANGED
@@ -1,9 +1,27 @@
1
- import {identifyFormat} from './identifyFormat.js';
1
+ import {downloadFile} from './downloadFile.js';
2
2
  import {formats} from './formats.js';
3
3
  import {getModule} from './getModule.js';
4
+ import {identifyFormat} from './identifyFormat.js';
4
5
  import {readStream} from './readStream.js';
5
6
  import {uploadFile} from './uploadFile.js';
6
7
  import {writeStream} from './writeStream.js';
7
8
 
8
- export {identifyFormat, formats, getModule, readStream, uploadFile, writeStream};
9
- export default {identifyFormat, formats, getModule, readStream, uploadFile, writeStream};
9
+ export {
10
+ downloadFile,
11
+ formats,
12
+ getModule,
13
+ identifyFormat,
14
+ readStream,
15
+ uploadFile,
16
+ writeStream,
17
+ };
18
+
19
+ export default {
20
+ downloadFile,
21
+ formats,
22
+ getModule,
23
+ identifyFormat,
24
+ readStream,
25
+ uploadFile,
26
+ writeStream,
27
+ };
package/lib/default.js CHANGED
@@ -6,5 +6,22 @@ import {readFile} from './readFile.js';
6
6
  import {writeStream} from './writeStream.js';
7
7
  import {writeFile} from './writeFile.js';
8
8
 
9
- export {identifyFormat, formats, getModule, readStream, readFile, writeStream, writeFile};
10
- export default {identifyFormat, formats, getModule, readStream, readFile, writeStream, writeFile};
9
+ export {
10
+ formats,
11
+ getModule,
12
+ identifyFormat,
13
+ readFile,
14
+ readStream,
15
+ writeFile,
16
+ writeStream,
17
+ };
18
+
19
+ export default {
20
+ formats,
21
+ getModule,
22
+ identifyFormat,
23
+ readFile,
24
+ readStream,
25
+ writeFile,
26
+ writeStream,
27
+ };
@@ -0,0 +1,90 @@
1
+ import {identifyFormat} from './identifyFormat.js';
2
+ import {writeStream} from './writeStream.js';
3
+
4
+ /**
5
+ * Prompt the user for a file to save as and Write a given array of citations
6
+ *
7
+ * @param {Array<Ref>} Collection of references to write
8
+ *
9
+ * @param {Object} [options] Additional options when prompting the user
10
+ * @param {String} [options.filename='References.xml'] The filename to download
11
+ * @param {Object} [options.writeStreamOptions] Additional options for the subsequent `writeStream` internal call
12
+ *
13
+ * @returns {Promise} A promise which will resolve when the file download has completed
14
+ */
15
+ export function downloadFile(refs, options) {
16
+ let settings = {
17
+ filename: 'References.xml',
18
+ writeStreamOptions: {},
19
+ ...options,
20
+ };
21
+
22
+ return Promise.resolve()
23
+ // Identify module from filename {{{
24
+ .then(()=> {
25
+ let module = identifyFormat(settings.filename);
26
+ if (!module) throw new Error(`Unsupported Reflib filename "${settings.filename}"`);
27
+ return module;
28
+ })
29
+ // }}}
30
+ // Create stream {{{
31
+ .then(async (module) => {
32
+ let blobData = [];
33
+ let writableStream = new WritableStream({
34
+ write(chunk) {
35
+ blobData.push(chunk);
36
+ },
37
+ });
38
+
39
+ let writer = writableStream.getWriter();
40
+
41
+ // Map end->close to keep browser compatibility with Node
42
+ writer.end = async (cb) => {
43
+ await writer.close();
44
+ cb();
45
+ };
46
+
47
+ let streamer = await writeStream(module.id, writer, settings.writeStreamOptions);
48
+
49
+ return {blobData, streamer};
50
+ })
51
+ // }}}
52
+ // Flush all references into the stream {{{
53
+ .then(async ({blobData, streamer}) => {
54
+ // Start stream
55
+ await streamer.start();
56
+
57
+ // Write all references as a promise chain
58
+ await refs.reduce((promiseChain, ref) =>
59
+ promiseChain.then(()=>
60
+ streamer.write(ref)
61
+ )
62
+ , Promise.resolve());
63
+
64
+ // End stream
65
+ await streamer.end();
66
+
67
+ return blobData;
68
+ })
69
+ // }}}
70
+ // Convert blobData array to a Blob {{{
71
+ .then(blobData => {
72
+ return new Blob(blobData);
73
+ })
74
+ // }}}
75
+ // Download Blob
76
+ .then(blob => {
77
+ let url = URL.createObjectURL(blob);
78
+ let aEl = document.createElement('a');
79
+ aEl.href = url;
80
+ aEl.download = settings.filename;
81
+ document.body.appendChild(aEl);
82
+ aEl.click();
83
+
84
+ // Clean up DOM
85
+ document.body.removeChild(aEl);
86
+ URL.revokeObjectURL(url);
87
+ // }}}
88
+ })
89
+ // }}}
90
+ }
package/lib/writeFile.js CHANGED
@@ -4,9 +4,12 @@ import {writeStream} from './writeStream.js';
4
4
 
5
5
  /**
6
6
  * Write a file to disk via the appropriate module
7
+ *
7
8
  * @param {string} path The file path to write, the module to use will be determined from this
9
+ * @param {Array<Ref>} Collection of references to write
8
10
  * @param {Object} [options] Additional options to pass to the file writer
9
11
  * @param {string} [options.module] The module to use if overriding from the file path
12
+ *
10
13
  * @returns {Promise} A promise which resolves when the operation has completed
11
14
  */
12
15
  export function writeFile(path, refs, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iebh/reflib",
3
- "version": "2.1.4",
3
+ "version": "2.2.0",
4
4
  "description": "Reference / Citation reference library utilities",
5
5
  "scripts": {
6
6
  "lint": "eslint lib modules shared test",