Skip to content

Commit

Permalink
Ensure get lang only for amazon urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Feb 8, 2018
1 parent 66a7424 commit 24d5a19
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/metascraper-amazon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ const SUFFIX_LANGUAGES = {
'it': 'it'
}

const wrap = rule => ({ htmlDom, url }) => isAmazonUrl(url) && rule(htmlDom)

const wrapUrl = rule => ({ htmlDom, url }) => {
const value = wrap(rule)({htmlDom, url})
return isUrl(value) && value
}

const getDomainLanguage = url => {
const {host} = new URL(url)
const suffix = host.replace('www.', '').split('.')
suffix.shift()
return SUFFIX_LANGUAGES[suffix.join('.')]
}

const createWrap = fn => rule => ({ htmlDom, url }) => {
const value = isAmazonUrl(url) && rule(htmlDom)
return !fn ? value : fn(value) && value
}

const wrap = createWrap()
const wrapUrl = createWrap(value => isUrl(value))

module.exports = () => ({
lang: [({ htmlDom: $, meta, url }) => getDomainLanguage(url)],
lang: [({ htmlDom: $, meta, url }) => isAmazonUrl(url) && getDomainLanguage(url)],
author: [
wrap($ => titleize($('.contributorNameID').text())),
wrap($ => titleize($('#bylineInfo').text())),
Expand Down

0 comments on commit 24d5a19

Please sign in to comment.