Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 30, 2018
1 parent e7475c2 commit 7da0187
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const { concat, isEmpty, reduce, get, findIndex } = require('lodash')
const { getUrl } = require('@metascraper/helpers')
const cheerio = require('cheerio')
Expand Down Expand Up @@ -39,32 +41,27 @@ const includes = (collection, fn) => findIndex(collection, fn) !== -1

const getLink = ({ url, el, attribute }) => {
const attr = get(el, `attribs.${attribute}`, '')
if (isEmpty(attr)) return null

return Object.assign({
url: attr,
normalizeUrl: getUrl(url, attr)
})
return isEmpty(attr)
? null
: Object.assign({
url: attr,
normalizeUrl: getUrl(url, attr)
})
}

const getLinksByAttribute = ({ selector, attribute, url, whitelist }) => {
return reduceSelector(
selector,
(acc, el) => {
const link = getLink({ url, el, attribute })

if (isEmpty(link)) return acc

const isAlreadyAdded = includes(
acc,
item => item.normalizeUrl === link.normalizeUrl
)
if (isAlreadyAdded) return acc

const match = whitelist && matcher([link.normalizeUrl], whitelist)
if (isEmpty(match)) acc.push(link)

return acc
return isEmpty(match) ? concat(acc, link) : acc
},
[]
)
Expand All @@ -82,7 +79,6 @@ module.exports = ({ html = '', url = '', whitelist = false } = {}) => {
url,
whitelist
})

return concat(acc, links)
},
[]
Expand Down

0 comments on commit 7da0187

Please sign in to comment.