Skip to content

Commit

Permalink
Support async logic into rules set
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Feb 13, 2018
1 parent a4c6c54 commit 06fa950
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/metascraper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"metascraper-title": "^3.8.0",
"metascraper-url": "^3.8.0",
"metascraper-video": "^3.8.0",
"p-reduce": "~1.0.0",
"resolve-from": "~4.0.0",
"sanitize-html": "~1.17.0"
},
Expand Down
13 changes: 7 additions & 6 deletions packages/metascraper/src/get-data.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
'use strict'

const { reduce, isEmpty } = require('lodash')
const { isEmpty } = require('lodash')
const pReduce = require('p-reduce')

const getValue = ({ htmlDom, url, conditions, meta }) => {
const getValue = async ({ htmlDom, url, conditions, meta }) => {
const size = conditions.length
let index = -1
let value

while (isEmpty(value) && index++ < size - 1) {
value = conditions[index]({ htmlDom, url, meta })
value = await conditions[index]({ htmlDom, url, meta })
}

return value
}

const getData = ({ rules, htmlDom, url }) =>
reduce(
pReduce(
rules,
(acc, [propName, conditions]) => {
const value = getValue({ htmlDom, url, conditions, meta: acc })
async (acc, [propName, conditions]) => {
const value = await getValue({ htmlDom, url, conditions, meta: acc })
acc[propName] = !isEmpty(value) ? value : null
return acc
},
Expand Down

0 comments on commit 06fa950

Please sign in to comment.