All files Feed.js

100% Statements 23/23
71.43% Branches 10/14
100% Functions 6/6
100% Lines 22/22

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 761x 1x               10x 10x   10x     1x                               6x 6x                 5x   10x 5x 5x 5x 5x   5x               5x   5x 1x 2x       5x 1x               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(feedConfig.parserOptions || {});
  },
 
  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.applyFilters();
      this.title = this.config.title || feedObject.title;
      this.description = this.config.description || feedObject.description;
      this.url = this.config.url || feedObject.feedUrl;
 
      return this;
    },
 
    /**
     * Apply filters to the items in this object
     * @return {void}
     */
    applyFilters() {
      Eif (this.items) {
        // Filter by published since
        if (this.config.publishedSince) {
          this.items = this.items
            .filter(item => new Date(item.isoDate) >= new Date(this.config.publishedSince));
        }
 
        // Apply limit
        if (this.config.limit) {
          this.items = this.items.slice(0, this.config.limit);
        }
      }
    },
 
  },
});
 
module.exports = Feed;