-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (35 loc) · 1.11 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
'use strict';
const { AtomFeed } = require('./lib/feed/atom');
const { Document } = require('./lib/xml/document');
const { InvalidFeedError } = require('./lib/errors/invalid-feed');
const { RssFeed } = require('./lib/feed/rss');
/**
* @type {{[key: string]: typeof import('./lib/feed/base').Feed}}}
*/
const DATA_PROVIDER_BY_ROOT_ELEMENT = {
feed: AtomFeed,
rdf: RssFeed,
rss: RssFeed
};
/**
* Create a Feed from an XML string.
*
* @param {string} xmlString
* A string of XML.
* @returns {import('./lib/feed/base').Feed}
* Returns a feed representation of the XML string.
* @throws {InvalidFeedError}
* Throws an invalid feed error if an unrecoverable issue is found with the feed.
*/
function parseFeed(xmlString) {
const xml = Document.fromString(xmlString);
// Decide on a data provider based on whether a root element is present
for (const [rootElement, Provider] of Object.entries(DATA_PROVIDER_BY_ROOT_ELEMENT)) {
if (xml.hasElementWithName(rootElement)) {
return new Provider(xml);
}
}
// No root feed element was found
throw new InvalidFeedError();
}
exports.parseFeed = parseFeed;