Skip to content

Commit

Permalink
feat: expose item-level get helper
Browse files Browse the repository at this point in the history
  • Loading branch information
z0al committed Jul 8, 2019
1 parent 5c87723 commit 65a0e20
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export interface Item {
ns: string;
value?: string;

// Helpers
get: (selectors: string[]) => Item;

// Normalized
id?: string;
title?: string;
Expand Down
24 changes: 16 additions & 8 deletions src/parser/rss/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class RSSParser extends Parser {
* @returns
* @memberof Parser
*/
static query(node, names) {
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,33 +446,41 @@ class RSSParser extends Parser {
* @memberof Parser
*/
normalize(node) {
const self = RSSParser;

return {
...node,

/**
* Attribute selector
*
* @param {string[]} selectors
*/
get: selectors => {
return this.query(node, selectors);
},

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

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

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

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

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

if (image) {
// RSS
Expand Down
19 changes: 10 additions & 9 deletions src/parser/rss/tests/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import Parser from '..';

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

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

test('simple term', () => {
Expand All @@ -20,7 +21,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 @@ -32,8 +33,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 @@ -43,7 +44,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 @@ -64,8 +65,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 @@ -86,7 +87,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]);
});
});
1 change: 1 addition & 0 deletions src/parser/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @property {Map<string, string>} attrs
* @property {Map<string, Item | Item[]>} meta
* @property {string} ns
* @property {(s: string[])=> Item} [get]
* @property {string} [value]
* @property {string} [id]
* @property {string} [title]
Expand Down
Loading

0 comments on commit 65a0e20

Please sign in to comment.