Skip to content

Commit

Permalink
Merge pull request #45 from ndaidong/v6.x.x
Browse files Browse the repository at this point in the history
v6.0.0rc3
  • Loading branch information
ndaidong authored Jul 28, 2022
2 parents fb75425 + ba5bdee commit 169981a
Show file tree
Hide file tree
Showing 39 changed files with 2,447 additions and 1,363 deletions.
109 changes: 15 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# feed-reader

Load and parse RSS/ATOM data from given feed url.
Load and parse ATOM/RSS/JSON data from given feed url.

[![NPM](https://badge.fury.io/js/feed-reader.svg)](https://badge.fury.io/js/feed-reader)
![CI test](https://github.com/ndaidong/feed-reader/workflows/ci-test/badge.svg)
Expand Down Expand Up @@ -33,13 +33,11 @@ read(url).then((feed) => {
## APIs

- [.read(String url)](#readstring-url)
- [The events](#the-events)
- [.resetEvents()](#reset-event-listeners)
- [Configuration methods](#configuration-methods)

### read(String url)

Load and extract feed data from given RSS/ATOM source. Return a Promise object.
Load and extract feed data from given RSS/ATOM/JSON source. Return a Promise object.

Example:

Expand All @@ -62,6 +60,7 @@ const getFeedData = async (url) => {

getFeedData('https://news.google.com/rss')
getFeedData('https://news.google.com/atom')
getFeedData('https://adactio.com/journal/feed.json')
```

Feed data object retuned by `read()` method should look like below:
Expand All @@ -86,96 +85,6 @@ Feed data object retuned by `read()` method should look like below:
}
```

### The events

Since v6.0.0, `feed-reader` supports event-driven pattern for easier writing code with more control.

- `onSuccess(Function callback)`
- `onError(Function callback)`
- `onComplete(Function callback)`

The following example will explain better than any word:

```js
import { read, onSuccess, onError, onComplete } from 'feed-reader'

onSuccess((url, feed) => {
console.log(`Feed data from ${url} has been parsed successfully`)
console.log('`feed` is always an object that contains feed data')
console.log(feed)
})

onError((url, err) => {
console.log(`Error occurred while processing ${url}`)
console.log('There is a message and reason:')
console.log(err)
})

onComplete((url, result, error) => {
console.log(`Finish processing ${url}`)
console.log('`result` may be feed data or null')
console.log(result)
console.log('`error` may be an error object or null')
if (error) {
console.log(error.message)
console.log(error.reason)
}
})

read('https://news.google.com/rss')
read('https://google.com')
```

We can mix both style together, for example to handle the error:

```js
import { read, onError } from 'feed-reader'

onError((url, err) => {
console.log(`Error occurred while processing ${url}`)
console.log('There is a message and reason:')
console.log(err)
})

const getFeedData = async (url) => {
const result = await read(url)
// `result` may be feed data or null
return result
}

getFeedData('https://news.google.com/rss')
````

In almost cases, using just `onComplete` is enough:

```js
import { read, onComplete } from 'feed-reader'
onComplete((url, result, error) => {
console.log(`Finish processing ${url}`)
if (result) {
// save feed data
console.log(result)
}
if (error) {
// handle error info
console.log(error)
}
})
read('https://news.google.com/rss')
````
#### Reset event listeners
Use method `resetEvents()` when you want to clear registered listeners from all events.
```js
import { resetEvents } from 'feed-reader'

resetEvents()
````

### Configuration methods

#### `setRequestOptions(Object requestOptions)`
Expand All @@ -188,6 +97,18 @@ Return current request options.

Default values can be found [here](https://github.com/ndaidong/feed-reader/blob/main/src/config.js#L5).

#### `setReaderOptions(Object readerOptions)`

To change default reader options.

- `descriptionMaxLen`: Number, max num of chars for description (default: `210`)
- `includeFullContent`: Boolean, add `content` to entry if available (default: `false`)
- `convertPubDateToISO`: Boolean, reformat published date to [ISO standard](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) (default: `true`)

#### `getReaderOptions()`

Return current reader options.


## Test

Expand Down
6 changes: 3 additions & 3 deletions build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ const cjsFile = `./dist/cjs/${pkg.name}.js`
const cjsPkg = JSON.parse(readFileSync('./dist/cjs/package.json'))

describe('Validate commonjs version output', () => {
test(`Check if ${cjsFile} created`, () => {
test(`check if ${cjsFile} created`, () => {
expect(existsSync(cjsFile)).toBeTruthy()
})
test('Check if cjs package info updated', () => {
test('check if cjs package info updated', () => {
expect(cjsPkg.name).toEqual(`${pkg.name}-cjs`)
expect(cjsPkg.version).toEqual(pkg.version)
})
const constent = readFileSync(cjsFile, 'utf8')
const lines = constent.split('\n')
test('Check if file meta contains package info', () => {
test('check if file meta contains package info', () => {
expect(lines[0].includes(`${pkg.name}@${pkg.version}`)).toBeTruthy()
expect(lines[0].includes(pkg.author)).toBeTruthy()
expect(lines[0].includes(pkg.license)).toBeTruthy()
Expand Down
Loading

0 comments on commit 169981a

Please sign in to comment.