Skip to content

Commit

Permalink
Refactor url helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 20, 2017
1 parent 13a5498 commit 9da1325
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
5 changes: 1 addition & 4 deletions packages/metascraper-author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ const removeBy = value => value.replace(REGEX_BY, '')

const wrap = rule => ({ htmlDom }) => {
const value = rule(htmlDom)

if (!isString(value)) return
if (isUrl(value)) return
return titleize(removeBy(value))
return isString(value) && !isUrl(value, {relative: false}) && titleize(removeBy(value))
}

/**
Expand Down
19 changes: 14 additions & 5 deletions packages/metascraper-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@ const isRelativeUrl = require('is-relative-url')
const { resolve: resolveUrl } = require('url')
const sanetizeUrl = require('normalize-url')
const smartquotes = require('smartquotes')
const { flow, isNil } = require('lodash')
const toTitle = require('to-title-case')
const urlRegex = require('url-regex')
const { flow } = require('lodash')

const isUrl = value => urlRegex().test(value)
const isUrl = (url, {relative = true} = {}) => {
if (isNil(url)) return false
if (!relative) return urlRegex().test(url)
return isRelativeUrl(url) || urlRegex().test(url)
}

const normalizeUrl = url => sanetizeUrl(url, { stripWWW: false })

const getAbsoluteUrl = (url, baseUrl) =>
isRelativeUrl(url) ? resolveUrl(baseUrl, url) : url
const getAbsoluteUrl = (baseUrl, relativePath = '') => (
isRelativeUrl(relativePath)
? resolveUrl(baseUrl, relativePath)
: relativePath
)

const getUrl = (url, baseUrl) => normalizeUrl(getAbsoluteUrl(url, baseUrl))
const getUrl = (baseUrl, relativePath) => (
normalizeUrl(getAbsoluteUrl(baseUrl, relativePath))
)

const createTitle = flow([condenseWhitespace, smartquotes])

Expand Down
9 changes: 4 additions & 5 deletions packages/metascraper-image/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { getUrl } = require('@metascraper/helpers')
const { getUrl, isUrl } = require('@metascraper/helpers')

/**
* Wrap a rule with validation and formatting logic.
Expand All @@ -9,10 +9,9 @@ const { getUrl } = require('@metascraper/helpers')
* @return {Function} wrapped
*/

const wrap = rule => ({ htmlDom, url: baseUrl }) => {
const url = rule(htmlDom)
if (!url) return
return getUrl(url, baseUrl)
const wrap = rule => ({ htmlDom, url }) => {
const value = rule(htmlDom)
return isUrl(value) && getUrl(url, value)
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/metascraper-logo-favicon/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { getUrl } = require('@metascraper/helpers')

module.exports = () => ({
logo: [({ htmlDom: $, meta, url }) => `${url}/favicon.ico`]
logo: [({ htmlDom: $, meta, url }) => getUrl(url, `favicon.ico`)]
})
3 changes: 3 additions & 0 deletions packages/metascraper-logo-favicon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"clearbit",
"metascraper"
],
"dependencies": {
"@metascraper/helpers": "~3.2.0"
},
"devDependencies": {
"standard": "latest"
},
Expand Down
19 changes: 5 additions & 14 deletions packages/metascraper-logo/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
'use strict'

const {
flow,
chain,
first,
isString,
concat,
toNumber,
split
} = require('lodash')
const { getUrl } = require('@metascraper/helpers')
const { flow, chain, first, concat, toNumber, split } = require('lodash')
const { getUrl, isUrl } = require('@metascraper/helpers')

const getSize = flow([str => split(str, 'x'), first, toNumber])

Expand Down Expand Up @@ -46,10 +38,9 @@ const sizeSelectors = [
* @return {Function} wrapped
*/

const wrap = rule => ({ htmlDom, url: baseUrl }) => {
const url = rule(htmlDom)
if (!isString(url)) return
return getUrl(url, baseUrl)
const wrap = rule => ({ htmlDom, url }) => {
const value = rule(htmlDom)
return isUrl(value) && getUrl(url, value)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/metascraper-url/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { isString } = require('lodash')
const { getUrl, isUrl } = require('@metascraper/helpers')
const { isString } = require('lodash')

/**
* Wrap a rule with validation and formatting logic.
Expand All @@ -12,7 +12,7 @@ const { getUrl, isUrl } = require('@metascraper/helpers')

const wrap = rule => ({ htmlDom, url }) => {
const value = rule(htmlDom, url)
return isUrl(value) ? getUrl(value) : url
return isUrl(value) ? getUrl(value, url) : url
}

/**
Expand Down

0 comments on commit 9da1325

Please sign in to comment.