Skip to content

Commit

Permalink
chore: avoid null
Browse files Browse the repository at this point in the history
use undefined instead.

Related: sindresorhus/meta#7
  • Loading branch information
Kikobeats committed Aug 1, 2021
1 parent adf62cb commit ccab48a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 30 deletions.
5 changes: 1 addition & 4 deletions packages/metascraper-clearbit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const appendQuery = (data, query) => {
const createClearbit = ({ gotOpts, logoOpts } = {}) =>
asyncMemoizeOne(async url => {
const domain = getDomain(url)

try {
const { body } = await got(ENDPOINT, {
...DEFAULT_GOT_OPTS,
Expand All @@ -36,9 +35,7 @@ const createClearbit = ({ gotOpts, logoOpts } = {}) =>
body.find(item => domain === item.domain),
logoOpts
)
} catch (err) {
return null
}
} catch (_) {}
})

module.exports = opts => {
Expand Down
38 changes: 18 additions & 20 deletions packages/metascraper-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const isUrl = (url, { relative = false } = {}) =>
const urlObject = (...args) => {
try {
return new URL(...args)
} catch (err) {
} catch (_) {
return { toString: () => '' }
}
}
Expand All @@ -128,9 +128,7 @@ const sanetizeUrl = (url, opts) =>
const normalizeUrl = (baseUrl, relativePath, opts) => {
try {
return sanetizeUrl(absoluteUrl(baseUrl, relativePath), opts)
} catch (_) {
return null
}
} catch (_) {}
}

const removeBy = flow([
Expand Down Expand Up @@ -207,7 +205,7 @@ const extension = (str = '') => {
}

const description = (value, opts) =>
isString(value) && getDescription(value, opts)
isString(value) ? getDescription(value, opts) : undefined

const getDescription = (
str,
Expand All @@ -217,23 +215,25 @@ const getDescription = (
return titleize(description, opts)
}

const publisher = value => isString(value) && condenseWhitespace(value)
const publisher = value =>
isString(value) ? condenseWhitespace(value) : undefined

const author = (value, opts) => isAuthor(value) && getAuthor(value, opts)
const author = (value, opts) =>
isAuthor(value) ? getAuthor(value, opts) : undefined

const url = (value, { url = '' } = {}) => {
if (isEmpty(value)) return null
if (isEmpty(value)) return undefined

try {
const absoluteUrl = normalizeUrl(url, value)
if (isUrl(absoluteUrl)) return absoluteUrl
} catch (_) {}

return isUri(value) ? value : null
return isUri(value) ? value : undefined
}

const getISODate = date =>
date && !Number.isNaN(date.getTime()) && date.toISOString()
date && !Number.isNaN(date.getTime()) ? date.toISOString() : undefined

const date = value => {
if (isDate(value)) return value.toISOString()
Expand Down Expand Up @@ -281,7 +281,7 @@ const lang = input => {
}

const title = (value, { removeSeparator = false, ...opts } = {}) =>
isString(value) && titleize(value, { removeSeparator, ...opts })
isString(value) ? titleize(value, { removeSeparator, ...opts }) : undefined

const isMime = (contentType, type) => {
const ext = mimeTypes.extension(contentType)
Expand All @@ -301,9 +301,7 @@ const jsonld = memoizeOne(
.contents()
.text()
)
} catch (err) {
return undefined
}
} catch (_) {}
})
.get()
.filter(Boolean),
Expand All @@ -324,19 +322,21 @@ const $jsonld = propName => $ => {

const image = (value, opts) => {
const urlValue = url(value, opts)
return !isAudioUrl(urlValue, opts) && !isVideoUrl(urlValue, opts) && urlValue
return !isAudioUrl(urlValue, opts) && !isVideoUrl(urlValue, opts)
? urlValue
: undefined
}

const logo = image

const video = (value, opts) => {
const urlValue = url(value, opts)
return isVideoUrl(urlValue, opts) && urlValue
return isVideoUrl(urlValue, opts) ? urlValue : undefined
}

const audio = (value, opts) => {
const urlValue = url(value, opts)
return isAudioUrl(urlValue, opts) && urlValue
return isAudioUrl(urlValue, opts) ? urlValue : undefined
}

const validator = {
Expand Down Expand Up @@ -383,9 +383,7 @@ const composeRule = rule => ({ from, to = from, ...opts }) => async ({
}

const has = value =>
value === null || value === false || value === 0 || Number.isNaN(value)
? false
: hasValues(value)
value !== undefined && !Number.isNaN(value) && hasValues(value)

const domLoaded = dom =>
new Promise(resolve =>
Expand Down
4 changes: 2 additions & 2 deletions packages/metascraper-iframe/src/from-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const getOembedUrl = memoizeOne(

const fromHTML = gotOpts => async ({ htmlDom, url, iframe }) => {
const oembedUrl = getOembedUrl(url, htmlDom)
if (!oembedUrl) return null
if (!oembedUrl) return

const oembedUrlObj = new URL(oembedUrl)
forEach(iframe, (value, key) =>
oembedUrlObj.searchParams.append(key.toLowerCase(), value)
)
const { value } = await pReflect(got(oembedUrlObj.toString(), gotOpts).json())
return get(value, 'html', null)
return get(value, 'html')
}

fromHTML.test = (...args) => !!getOembedUrl(...args)
Expand Down
2 changes: 1 addition & 1 deletion packages/metascraper-iframe/src/from-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const fromProvider = gotOpts => async ({ url, iframe }) => {
const { value } = await pReflect(
fetchProvider(provider, url, iframe, gotOpts)
)
return get(value, 'html', null)
return get(value, 'html')
}

fromProvider.test = url => !!findProvider(url)
Expand Down
4 changes: 1 addition & 3 deletions packages/metascraper-logo-favicon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ const createGetLogo = gotOpts => async url => {
...gotOpts
})
return logo(logoUrl)
} catch (err) {
return null
}
} catch (_) {}
}

/**
Expand Down

0 comments on commit ccab48a

Please sign in to comment.