@metascraper/helpers 5.34.10 → 5.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +33 -24
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -218,6 +218,15 @@ const isAudioExtension = url => isMediaTypeExtension(url, AUDIO)
218
218
 
219
219
  const isImageExtension = url => isMediaTypeExtension(url, IMAGE)
220
220
 
221
+ const isContentType =
222
+ extensions =>
223
+ ({ type = '' } = {}) =>
224
+ extensions.some(extension => type.endsWith(extension))
225
+
226
+ const isVideoContentType = isContentType(Object.keys(videoExtensions))
227
+
228
+ const isAudioContentType = isContentType(Object.keys(audioExtensions))
229
+
221
230
  const extension = (str = '') => {
222
231
  const url = urlObject(
223
232
  str,
@@ -323,11 +332,7 @@ const jsonld = memoizeOne(
323
332
  .map(function () {
324
333
  try {
325
334
  const el = $(this)
326
- const json = JSON.parse(
327
- $(el)
328
- .contents()
329
- .text()
330
- )
335
+ const json = JSON.parse($(el).contents().text())
331
336
 
332
337
  const { '@graph': graph, ...props } = json
333
338
  if (!graph) return json
@@ -364,15 +369,16 @@ const image = (value, opts) => {
364
369
 
365
370
  const logo = image
366
371
 
367
- const video = (value, opts) => {
372
+ const media = (urlValidator, contentTypeValidator) => (value, opts) => {
368
373
  const urlValue = url(value, opts)
369
- return isVideoUrl(urlValue, opts) ? urlValue : undefined
374
+ return urlValidator(urlValue, opts) || contentTypeValidator(opts)
375
+ ? urlValue
376
+ : undefined
370
377
  }
371
378
 
372
- const audio = (value, opts) => {
373
- const urlValue = url(value, opts)
374
- return isAudioUrl(urlValue, opts) ? urlValue : undefined
375
- }
379
+ const video = media(isVideoUrl, isVideoContentType)
380
+
381
+ const audio = media(isAudioUrl, isAudioContentType)
376
382
 
377
383
  const validator = {
378
384
  audio,
@@ -403,19 +409,22 @@ const findRule = async (rules, args) => {
403
409
  return value
404
410
  }
405
411
 
406
- const toRule = (mapper, opts) => rule => async ({ htmlDom, url }) => {
407
- const value = await rule(htmlDom, url)
408
- return mapper(value, { url, ...opts })
409
- }
410
-
411
- const composeRule = rule => ({ from, to = from, ...opts }) => async ({
412
- htmlDom,
413
- url
414
- }) => {
415
- const data = await rule(htmlDom, url)
416
- const value = get(data, from)
417
- return invoke(validator, to, value, { url, ...opts })
418
- }
412
+ const toRule =
413
+ (mapper, opts) =>
414
+ rule =>
415
+ async ({ htmlDom, url }) => {
416
+ const value = await rule(htmlDom, url)
417
+ return mapper(value, { url, ...opts })
418
+ }
419
+
420
+ const composeRule =
421
+ rule =>
422
+ ({ from, to = from, ...opts }) =>
423
+ async ({ htmlDom, url }) => {
424
+ const data = await rule(htmlDom, url)
425
+ const value = get(data, from)
426
+ return invoke(validator, to, value, { url, ...opts })
427
+ }
419
428
 
420
429
  const has = value =>
421
430
  value !== undefined && !Number.isNaN(value) && hasValues(value)
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@metascraper/helpers",
3
3
  "description": "Collection of helper functions used by metascraper",
4
4
  "homepage": "https://nicedoc.io/microlinkhq/metascraper/packages/metascraper-helpers",
5
- "version": "5.34.10",
5
+ "version": "5.35.0",
6
6
  "main": "index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -62,5 +62,5 @@
62
62
  "publishConfig": {
63
63
  "access": "public"
64
64
  },
65
- "gitHead": "42dac0d9f4d96b3b9de566df01eebb92e4440df6"
65
+ "gitHead": "4050798d28b1118e3bb8058bf17b182bb84b7c5b"
66
66
  }