forked from hash-bang/Reflib-MEDLINE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
206 lines (193 loc) · 5.72 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var _ = require('lodash').mixin({
isStream: require('isstream'),
});
var async = require('async-chainable');
var events = require('events');
var _fieldTranslations = { // Map of MEDLINE fields to RefLib fields - See https://www.nlm.nih.gov/bsd/mms/medlineelements.html for details
'PMID': {reflib: 'recNo'},
'AB ': {reflib: 'abstract'},
'AID ': {reflib: 'doi'},
'FAU ': {reflib: 'authors', isArray: true},
'AD ': {reflib: 'affiliation', isArray: true},
'DP ': {reflib: 'date'},
'ISBN': {reflib: 'isbn'},
'JT ': {reflib: 'journal'},
'LA ': {reflib: 'language'},
'PG ': {reflib: 'pages'},
'PT ': {reflib: 'type'},
'PL ': {reflib: 'address'},
'TI ': {reflib: 'title'},
'VI ': {reflib: 'volume'},
'OT ': {reflib: 'keywords', isArray: true},
};
var _fieldTranslationsReverse = _(_fieldTranslations) // Calculate the key/val lookup - this time with the key being the reflib key
.map(function(tran, id) {
tran.medline = id;
return tran;
})
.mapKeys(function(tran) {
return tran.reflib;
})
.value();
// Lookup object of MEDLINE => RefLib types
var _typeTranslations = {
'CASE REPORTS': 'case',
'CLASSICAL ARTICLE': 'classicalWork',
'DICTIONARY': 'dictionary',
'JOURNAL ARTICLE': 'journalArticle',
'LEGAL CASES': 'legalRuleOrRegulation',
'LEGISLATION': 'legalRuleOrRegulation',
'LETTER': 'personalCommunication',
'NEWSPAPER ARTICLE': 'newspaperArticle',
'TECHNICAL REPORT': 'report',
'VIDEO-AUDIO MEDIA': 'filmOrBroadcast',
'WEBCASTS': 'web',
};
var _typeTranslationsReverse = _(_typeTranslations)
.mapValues(function(trans, id) {
return {reflib: trans, medline: id};
})
.mapKeys(function(trans) {
return trans.reflib;
})
.mapValues(function(trans) {
return trans.medline;
})
.value();
function parse(content) {
var emitter = new events.EventEmitter();
var parser = function(content) { // Perform parser in async so the function will return the emitter otherwise an error could be thrown before the emitter is ready
var ref = {};
var refField; // Currently appending ref field
(content + "\n").split("\n").forEach(function(line) {
var bits = /^(....)- (.*)$/.exec(line);
if (bits) {
if (_fieldTranslations[bits[1]]) { // Only accept known fields
refField = _fieldTranslations[bits[1]];
if (refField.isArray) {
if (!ref[refField.reflib]) {
ref[refField.reflib] = [bits[2]];
} else {
ref[refField.reflib].push(bits[2]);
}
} else {
ref[refField.reflib] = bits[2];
}
} else {
refField = null;
}
} else if (refField && _.startsWith(line, ' ')) {
if (refField.isArray) {
ref[refField.reflib][ref[refField.reflib].length-1] += ' ' + line.substr(6)
} else {
ref[refField.reflib] += ' ' + line.substr(6);
}
} else if (!line) {
if (!_.isEmpty(ref)) {
ref.type = ref.type && _typeTranslations[ref.type] ? _typeTranslations[ref.type] : 'unknown';
emitter.emit('ref', ref);
}
ref = {};
}
});
emitter.emit('end');
};
if (_.isString(content)) {
setTimeout(function() { parser(content) });
} else if (_.isBuffer(content)) {
setTimeout(function() { parser(content.toString('utf-8')) });
} else if (_.isStream(content)) {
var buffer = '';
content
.on('data', function(data) {
buffer += data.toString('utf-8');
})
.on('end', function() {
parser(buffer);
});
} else {
throw new Error('Unknown item type to parse: ' + typeof content);
}
return emitter;
};
function _pusher(stream, isLast, child, settings) {
var buffer = '';
if (child.type) child.type = _typeTranslationsReverse[child.type] || settings.defaultType;
_(child)
.omitBy(function(data, key) {
return !_fieldTranslationsReverse[key]; // Known translation?
})
.forEach(function(data, key) {
var field = _fieldTranslationsReverse[key];
if (field.isArray) {
data.map(function(item) {
buffer += field.medline + '- ' + item + '\n';
});
} else {
buffer += field.medline + '- ' + data + '\n';
}
})
stream.write(buffer + (isLast ? '' : '\n\n'));
};
function output(options) {
var settings = _.defaults(options, {
stream: null,
defaultType: 'journalArticle', // Assume this reference type if we are not provided with one
content: [],
});
async()
// Sanity checks {{{
.then(function(next) {
if (!settings.content) return next('No content has been provided');
next();
})
// }}}
// References {{{
.then(function(next) {
if (_.isFunction(settings.content)) { // Callback
var batchNo = 0;
var fetcher = function() {
settings.content(function(err, data, isLast) {
if (err) return emitter.error(err);
if (_.isArray(data) && data.length > 0) { // Callback provided array
data.forEach(function(d, dIndex) {
_pusher(settings.stream, isLast && dIndex == data.length-1, d, settings);
});
setTimeout(fetcher);
} else if(!_.isArray(data) && _.isObject(data)) { // Callback provided single ref
_pusher(settings.stream, isLast, data, settings);
setTimeout(fetcher);
} else { // End of stream
next();
}
}, batchNo++);
};
fetcher();
} else if (_.isArray(settings.content)) { // Array of refs
settings.content.forEach(function(d, dIndex) {
_pusher(settings.stream, dIndex == settings.content.length -1, d, settings);
});
next();
} else if (_.isObject(settings.content)) { // Single ref
_pusher(settings.stream, true, data, settings);
next();
}
})
// }}}
// Stream end {{{
.then(function(next) {
settings.stream.end();
next();
})
// }}}
// End {{{
.end(function(err) {
if (err) throw new Error(err);
});
// }}}
return settings.stream;
}
module.exports = {
output: output,
parse: parse,
};