Comparing version
15
index.js
@@ -60,13 +60,19 @@ /*! | ||
const validTypes = Object.keys(types); | ||
const typeArray = Array.isArray(type) ? type : [type]; | ||
if (!validTypes.includes(type)) { | ||
throw new Error(`Invalid \`type\` value "${type}". Choose one of: ${validTypes.join(', ')}`); | ||
for (const type of typeArray) { | ||
if (!validTypes.includes(type)) { | ||
throw new Error(`Invalid \`type\` value "${type}". Choose one of: ${validTypes.join(', ')}`); | ||
} | ||
} | ||
const chosenType = types[type]; | ||
const chosenTypes = typeArray.map(type => ({...types[type], type})); | ||
const selector = chosenTypes.map(type => type.selector).join(', '); | ||
const $ = cheerio.load(src); | ||
return Array.prototype.map.call($(chosenType.selector), element => { | ||
return Array.prototype.map.call($(selector), element => { | ||
const $element = $(element); | ||
const chosenType = chosenTypes.find(type => $element.is(type.selector)); | ||
let value = ''; | ||
@@ -82,2 +88,3 @@ if (chosenType.method && $element[chosenType.method]) { | ||
$el: $element, | ||
type: chosenType.type, | ||
value | ||
@@ -84,0 +91,0 @@ }; |
{ | ||
"name": "oust", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": "Addy Osmani <addyosmani@gmail.com> (https://addyosmani.com/)", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -73,2 +73,8 @@ # oust [](https://github.com/addyosmani/oust/actions?workflow=Tests) [](https://david-dm.org/addyosmani/oust) [](https://david-dm.org/addyosmani/oust?type=dev) | ||
#### Extract preload and stylesheet references combined | ||
```js | ||
const hrefs = oust(htmlString, ['preload', 'stylesheets']); | ||
``` | ||
#### Extract cheerio elements alongside the value | ||
@@ -75,0 +81,0 @@ |
18713
2.52%115
5.5%151
4.14%