@metascraper/helpers 5.29.3 → 5.29.7

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 (3) hide show
  1. package/README.md +11 -8
  2. package/index.js +38 -24
  3. package/package.json +5 -7
package/README.md CHANGED
@@ -1,9 +1,12 @@
1
- # metascraper-helpers
2
-
3
- [![npm](https://img.shields.io/npm/v/@metascraper/helpers.svg?style=flat-square)](https://www.npmjs.com/package/@metascraper/helpers)
4
- [![Dependency Status](https://david-dm.org/microlinkhq/metascraper.svg?path=packages/metascraper-helpers&style=flat-square)](https://david-dm.org/microlinkhq/metascraper?path=packages/metascraper-helpers)
5
-
6
- > Collection of helper functions used by metascraper.
1
+ <div align="center">
2
+ <br>
3
+ <img style="width: 500px; margin:3rem 0 1.5rem;" src="https://metascraper.js.org/static/logo-banner.png" alt="metascraper">
4
+ <br>
5
+ <br>
6
+ <p align="center"><strong>metascraper-helpers</strong>: Collection of helper functions used by metascraper.</p>
7
+ <p align="center">See our <a href="https://metascraper.js.org" target='_blank' rel='noopener noreferrer'>website</a> for more information.</p>
8
+ <br>
9
+ </div>
7
10
 
8
11
  ## Install
9
12
 
@@ -13,7 +16,7 @@ $ npm install @metascraper/helpers --save
13
16
 
14
17
  ## License
15
18
 
16
- **metascraper-helpers** © [Microlink](https://microlink.io), Released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
19
+ **metascraper-helpers** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
17
20
  Authored and maintained by [Microlink](https://microlink.io) with help from [contributors](https://github.com/microlinkhq/metascraper/contributors).
18
21
 
19
- > [microlink.io](https://microlink.io) · GitHub [@microlink.io](https://github.com/microlinkhq) · Twitter [@microlinkhq](https://twitter.com/microlinkhq)
22
+ > [microlink.io](https://microlink.io) · GitHub [microlinkhq](https://github.com/microlinkhq) · Twitter [@microlinkhq](https://twitter.com/microlinkhq)
package/index.js CHANGED
@@ -150,9 +150,19 @@ const titleize = (src, opts = {}) => {
150
150
  return title
151
151
  }
152
152
 
153
- const $filter = ($, domNodes, fn = $filter.fn) => {
154
- const el = domNodes.filter((i, el) => fn($(el))).first()
155
- return fn(el)
153
+ const $filter = ($, matchedEl, fn = $filter.fn) => {
154
+ let matched
155
+
156
+ matchedEl.each(function () {
157
+ const result = fn($(this))
158
+
159
+ if (result) {
160
+ matched = result
161
+ return false
162
+ }
163
+ })
164
+
165
+ return matched
156
166
  }
157
167
 
158
168
  $filter.fn = el => condenseWhitespace(el.text())
@@ -227,7 +237,7 @@ const author = (value, opts) =>
227
237
  isAuthor(value) ? getAuthor(value, opts) : undefined
228
238
 
229
239
  const url = (value, { url = '' } = {}) => {
230
- if (isEmpty(value)) return undefined
240
+ if (isEmpty(value)) return
231
241
 
232
242
  try {
233
243
  const absoluteUrl = normalizeUrl(url, value)
@@ -242,7 +252,7 @@ const getISODate = date =>
242
252
 
243
253
  const date = value => {
244
254
  if (isDate(value)) return value.toISOString()
245
- if (!(isString(value) || isNumber(value))) return undefined
255
+ if (!(isString(value) || isNumber(value))) return
246
256
 
247
257
  // remove whitespace for easier parsing
248
258
  if (isString(value)) value = condenseWhitespace(value)
@@ -278,7 +288,7 @@ const date = value => {
278
288
  }
279
289
 
280
290
  const lang = input => {
281
- if (isEmpty(input) || !isString(input)) return undefined
291
+ if (isEmpty(input) || !isString(input)) return
282
292
  const key = toLower(condenseWhitespace(input))
283
293
  if (input.length === 3) return iso6393[key]
284
294
  const lang = toLower(key.substring(0, 2))
@@ -397,34 +407,38 @@ const composeRule = rule => ({ from, to = from, ...opts }) => async ({
397
407
  const has = value =>
398
408
  value !== undefined && !Number.isNaN(value) && hasValues(value)
399
409
 
400
- const domLoaded = dom =>
401
- new Promise(resolve =>
402
- dom.window.document.readyState === 'interactive' ||
403
- dom.window.document.readyState === 'complete'
404
- ? resolve()
405
- : dom.window.document.addEventListener('DOMContentLoaded', resolve)
406
- )
407
-
408
- const loadIframe = (url, html) =>
410
+ const loadIframe = (url, $, { timeout = 5000 } = {}) =>
409
411
  new Promise(resolve => {
410
- const dom = new JSDOM(html, {
412
+ const dom = new JSDOM($.html(), {
411
413
  url,
412
414
  virtualConsole: new VirtualConsole(),
413
415
  runScripts: 'dangerously',
414
416
  resources: 'usable'
415
417
  })
416
418
 
417
- const getIframe = () => dom.window.document.querySelector('iframe')
419
+ const done = (html = '') => resolve($.load(html))
420
+
421
+ const listen = (element, method, fn) =>
422
+ element[`${method}EventListener`]('load', fn, {
423
+ capture: true,
424
+ once: true,
425
+ passive: true
426
+ })
418
427
 
419
- const load = iframe =>
420
- iframe
421
- ? iframe.addEventListener('load', () => resolve(iframe.contentWindow))
422
- : resolve()
428
+ const iframe = dom.window.document.querySelector('iframe')
429
+ if (!iframe) return done()
423
430
 
424
- const iframe = getIframe()
425
- if (iframe) return load(iframe)
431
+ const timer = setTimeout(() => {
432
+ listen(iframe, 'remove', load)
433
+ done()
434
+ }, timeout)
435
+
436
+ function load () {
437
+ clearTimeout(timer)
438
+ done(iframe.contentDocument.documentElement.outerHTML)
439
+ }
426
440
 
427
- domLoaded(dom).then(() => load(getIframe()))
441
+ listen(iframe, 'add', load)
428
442
  })
429
443
 
430
444
  module.exports = {
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.29.3",
5
+ "version": "5.29.7",
6
6
  "main": "index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -45,10 +45,8 @@
45
45
  "video-extensions": "~1.2.0"
46
46
  },
47
47
  "devDependencies": {
48
- "cheerio": "latest",
49
- "mocha": "latest",
50
- "should": "latest",
51
- "snap-shot": "latest"
48
+ "ava": "latest",
49
+ "cheerio": "latest"
52
50
  },
53
51
  "engines": {
54
52
  "node": ">= 12"
@@ -57,11 +55,11 @@
57
55
  "index.js"
58
56
  ],
59
57
  "scripts": {
60
- "test": "NODE_PATH=.. TZ=UTC NODE_ENV=test mocha test"
58
+ "test": "NODE_PATH=.. TZ=UTC NODE_ENV=test ava --timeout 30s"
61
59
  },
62
60
  "license": "MIT",
63
61
  "publishConfig": {
64
62
  "access": "public"
65
63
  },
66
- "gitHead": "e3d5ef5add6fec690b1234e6aaa273e1a658c256"
64
+ "gitHead": "78434709c910cca5d8df3871c0b06131cce5df45"
67
65
  }