All files / src Feed.js

100% Statements 16/16
50% Branches 3/6
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 551x 1x               2x 2x   2x     1x                               3x 3x                 2x   2x 2x 2x 2x   2x         1x  
const Parser = require('rss-parser');
const stampit = require('stampit');
 
/**
 * Cleans an item
 * @param {Object} item
 * @return {Object}
 */
function cleanItem(item) {
  const cleanedItem = Object.assign({}, item);
  cleanedItem.title = item.title.replace(/\bhttps?:\/\/\S+/gi, '');
 
  return cleanedItem;
}
 
const Feed = stampit({
  props: {
    config: undefined,
    parser: undefined,
    items: undefined,
    url: undefined,
    title: undefined,
    description: undefined,
  },
 
  /**
   * Creates a new Feed from a feed configuration object
   * @param { Object } Feed configuration object
   * @return { void }
   */
  init({ feedConfig }) {
    this.config = feedConfig;
    this.parser = new Parser();
  },
 
  methods: {
    /**
     * Promises a feed with items embedded
     * @return { Promise<Feed> }
     */
    async resolve() {
      const feedObject = await this.parser.parseURL(this.config.url);
 
      this.items = feedObject.items.map(item => cleanItem(item));
      this.title = this.config.title || feedObject.title;
      this.description = this.config.description || feedObject.description;
      this.url = this.config.url || feedObject.feedUrl;
 
      return this;
    },
  },
});
 
module.exports = Feed;