-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
40 lines (35 loc) · 967 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const { isValidJSON } = require('./utilities/validation');
const { parseBibtex, parseToBibtex } = require('./parsers/bibtexParsers');
/**
* @function - Converts JSON to BibTeX
* @param {string} content
* @returns {Promise}
*/
const jsonToBibtex = (data, property) => new Promise((resolve, reject) => {
const valid = isValidJSON(data.toString());
if (valid) {
const parsedData = parseToBibtex(data.toString(), property);
resolve(parsedData);
} else {
const invalid = new Error('Invalid JSON');
reject(invalid.message);
}
});
/**
* @function - Converts BibTeX to JSON
* @param {string} content
* @returns {Promise}
*/
const bibtexToJSON = (content) => new Promise((resolve, reject) => {
if (content) {
const parsedData = parseBibtex(content);
resolve(parsedData);
} else {
const error = new Error('Data passed in is invalid');
reject(error.message);
}
});
module.exports = {
bibtexToJSON,
jsonToBibtex,
};