Skip to content

Commit

Permalink
refactor(rss): use getters for lazy compution
Browse files Browse the repository at this point in the history
BREAKING: no more summary and content
  • Loading branch information
z0al committed Jul 8, 2019
1 parent 7d93ef3 commit 5c87723
Show file tree
Hide file tree
Showing 5 changed files with 2,433 additions and 6,682 deletions.
2 changes: 0 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export interface Item {
// Normalized
id?: string;
title?: string;
summary?: string;
content?: string;
image?: string;
published?: string;
updated?: string;
Expand Down
93 changes: 43 additions & 50 deletions src/parser/rss/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class RSSParser extends Parser {

// Normalize?
if (normalize) {
this.normalize(node);
node = this.normalize(node);
}

return node;
Expand Down Expand Up @@ -381,7 +381,7 @@ class RSSParser extends Parser {
* @returns
* @memberof Parser
*/
query(node, names) {
static query(node, names) {
for (const name of names) {
// e.g atom:link => prefix=atom, local=link
let [prefix, local] = name.trim().split(':');
Expand Down Expand Up @@ -446,56 +446,49 @@ class RSSParser extends Parser {
* @memberof Parser
*/
normalize(node) {
const id = this.query(node, ['guid', 'atom:id']);
if (id) {
node.id = id.value || '';
}

const title = this.query(node, ['title', 'atom:title']);
if (title) {
node.title = title.value || '';
}

const summary = this.query(node, [
'description',
'atom:summary',
'atom:subtitle'
]);
if (summary) {
node.summary = summary.value || '';
}

const content = this.query(node, ['content:encoded', 'atom:content']);
if (content) {
node.content = content.value || '';
}

const published = this.query(node, ['pubDate', 'atom:published']);
if (published) {
node.published = published.value || '';
}

const updated = this.query(node, ['lastBuildDate', 'atom:updated']);
if (updated) {
node.updated = updated.value || '';
}

const image = this.query(node, ['image', 'atom:logo']);
if (image) {
// RSS
if (image.meta.has('url')) {
const url = image.meta.get('url');
if (!(url instanceof Array)) {
node.image = url.value || '';
const self = RSSParser;

return {
...node,

get id() {
const id = self.query(node, ['guid', 'atom:id']);
return id && id.value;
},

get title() {
const title = self.query(node, ['title', 'atom:title']);
return title && title.value;
},

get published() {
const published = self.query(node, ['pubDate', 'atom:published']);
return published && published.value;
},

get updated() {
const updated = self.query(node, ['lastBuildDate', 'atom:updated']);
return updated && updated.value;
},

get image() {
const image = self.query(node, ['image', 'atom:logo']);

if (image) {
// RSS
if (image.meta.has('url')) {
const url = image.meta.get('url');
if (!(url instanceof Array)) {
return url.value;
}
}
// Atom
else {
return image.value;
}
}
}
// Atom
else {
node.image = image.value || '';
}
}

return node;
};
}

/**
Expand Down
21 changes: 10 additions & 11 deletions src/parser/rss/tests/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
// Ours
import Parser from '..';

describe('parser.query', () => {
let node, parser;
describe('Parser.query', () => {
let node;

beforeEach(() => {
node = { attrs: new Map(), meta: new Map(), ns: '' };
parser = new Parser();
});

test('simple term', () => {
Expand All @@ -21,7 +20,7 @@ describe('parser.query', () => {

node.meta.set('title', title);

expect(parser.query(node, ['title'])).toBe(title);
expect(Parser.query(node, ['title'])).toBe(title);
});

test('namespaced prefix', () => {
Expand All @@ -33,8 +32,8 @@ describe('parser.query', () => {
};
node.meta.set('title', title);

expect(parser.query(node, ['title'])).toBeUndefined();
expect(parser.query(node, ['atom:title'])).toBe(title);
expect(Parser.query(node, ['title'])).toBeUndefined();
expect(Parser.query(node, ['atom:title'])).toBe(title);

const link = {
attrs: new Map(),
Expand All @@ -44,7 +43,7 @@ describe('parser.query', () => {
};

node.meta.set('atom:link', link);
expect(parser.query(node, ['atom:link', 'title'])).toBe(link);
expect(Parser.query(node, ['atom:link', 'title'])).toBe(link);
});

test('duplicated keys', () => {
Expand All @@ -65,8 +64,8 @@ describe('parser.query', () => {

node.meta.set('title', titles);

expect(parser.query(node, ['title'])).toBe(titles[0]);
expect(parser.query(node, ['atom:title'])).toBe(titles[1]);
expect(Parser.query(node, ['title'])).toBe(titles[0]);
expect(Parser.query(node, ['atom:title'])).toBe(titles[1]);
});

test('filter by attribute', () => {
Expand All @@ -87,7 +86,7 @@ describe('parser.query', () => {

node.meta.set('link', links);

expect(parser.query(node, ['atom:link'])).toBe(links[0]);
expect(parser.query(node, ['atom:link[rel=self]'])).toBe(links[1]);
expect(Parser.query(node, ['atom:link'])).toBe(links[0]);
expect(Parser.query(node, ['atom:link[rel=self]'])).toBe(links[1]);
});
});
2 changes: 0 additions & 2 deletions src/parser/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
* @property {string} [value]
* @property {string} [id]
* @property {string} [title]
* @property {string} [summary]
* @property {string} [content]
* @property {string} [image]
* @property {string} [published]
* @property {string} [updated]
Expand Down
Loading

0 comments on commit 5c87723

Please sign in to comment.