UNPKG

feed-reader

Version:

To read and normalize RSS/ATOM/JSON feed data

29 lines (22 loc) 735 B
// utils / xmlparser import { hasProperty, isString } from 'bellajs' import { XMLValidator, XMLParser } from 'fast-xml-parser' export const isRSS = (data = {}) => { return hasProperty(data, 'rss') && hasProperty(data.rss, 'channel') } export const isAtom = (data = {}) => { return hasProperty(data, 'feed') && hasProperty(data.feed, 'entry') } export const validate = (xml) => { return (!isString(xml) || !xml.length) ? false : XMLValidator.validate(xml) === true } export const xml2obj = (xml = '', extraOptions = {}) => { const options = { ...extraOptions, ignoreAttributes: false, attributeNamePrefix: '@_' } const parser = new XMLParser(options) const jsonObj = parser.parse(xml) return jsonObj }