Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: avoid null #441

Merged
merged 2 commits into from Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/metascraper-clearbit/index.js
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
36 changes: 18 additions & 18 deletions packages/metascraper-helpers/index.js
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,7 +301,7 @@ const jsonld = memoizeOne(
.contents()
.text()
)
} catch (err) {
} catch (_) {
return undefined
}
})
Expand All @@ -324,19 +324,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 +385,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
40 changes: 21 additions & 19 deletions packages/metascraper-helpers/test/index.js
Expand Up @@ -30,7 +30,9 @@ const {

describe('metascraper-helpers', () => {
it('.normalizeUrl', () => {
should(normalizeUrl('https://example.com', 'javascript:false')).be.null()
should(
normalizeUrl('https://example.com', 'javascript:false')
).be.undefined()

should(
normalizeUrl(
Expand All @@ -55,13 +57,13 @@ describe('metascraper-helpers', () => {
})

it('.url', () => {
should(url()).be.null()
should(url(null)).be.null()
should(url('')).be.null()
should(url('', { url: 'https://kikobeats.com/' })).be.null()
should(url('paco')).be.null()
should(url(NaN, { url: 'https://kikobeats.com' })).be.null()
should(url('http://<foo>', { url: 'https://kikobeats.com' })).be.null()
should(url()).be.undefined()
should(url(null)).be.undefined()
should(url('')).be.undefined()
should(url('', { url: 'https://kikobeats.com/' })).be.undefined()
should(url('paco')).be.undefined()
should(url(NaN, { url: 'https://kikobeats.com' })).be.undefined()
should(url('http://<foo>', { url: 'https://kikobeats.com' })).be.undefined()

should(url('blog', { url: 'https://kikobeats.com/' })).be.equal(
'https://kikobeats.com/blog'
Expand Down Expand Up @@ -360,7 +362,7 @@ describe('metascraper-helpers', () => {
should(date()).be.equal(undefined)
should(date(undefined)).be.equal(undefined)
should(date(null)).be.equal(undefined)
should(date('null')).be.equal(null)
should(date('null')).be.equal(undefined)
should(date('Jun 20')).be.equal('2021-06-20T12:00:00.000Z')
should(date('Jun 20 2018')).be.equal('2018-06-20T12:00:00.000Z')
should(date('Jun 2018')).be.equal('2018-06-01T12:00:00.000Z')
Expand All @@ -369,7 +371,7 @@ describe('metascraper-helpers', () => {
should(date(1594767608 * 1000)).be.equal('2020-07-14T23:00:08.000Z')
should(date(1594767608 * 1000000)).be.equal('2020-07-14T23:00:08.000Z')
should(date(1594767608 * 1000000000)).be.equal('2020-07-14T23:00:08.000Z')
should(date('11 juil. 2019')).be.equal(null)
should(date('11 juil. 2019')).be.equal(undefined)
const now = new Date()
should(date(now)).be.equal(now.toISOString())
})
Expand All @@ -380,6 +382,12 @@ describe('.has', () => {
it('true', () => {
should(has(true)).be.true()
})
it('0', () => {
should(has(0)).be.true()
})
it('null', () => {
should(has(null)).be.true()
})
it('foo', () => {
should(has('foo')).be.true()
})
Expand All @@ -392,21 +400,15 @@ describe('.has', () => {
it('{foo: "bar"}', () => {
should(has({ foo: 'bar' })).be.true()
})
it('false', () => {
should(has(false)).be.true()
})
})

describe('false', () => {
it('false', () => {
should(has(false)).be.false()
})
it('0', () => {
should(has(0)).be.false()
})
it("''", () => {
should(has('')).be.false()
})
it('null', () => {
should(has(null)).be.false()
})
it('undefined', () => {
should(has(undefined)).be.false()
})
Expand Down
4 changes: 2 additions & 2 deletions packages/metascraper-iframe/src/from-html.js
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
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
Expand Up @@ -74,9 +74,7 @@ const createGetLogo = gotOpts => async url => {
...gotOpts
})
return logo(logoUrl)
} catch (err) {
return null
}
} catch (_) {}
}

/**
Expand Down