Skip to content

Commit

Permalink
Merge pull request #57 from addyosmani/xmr/dev
Browse files Browse the repository at this point in the history
Use destructuring more and minor tweaks
  • Loading branch information
bezoerb committed Sep 12, 2023
2 parents 02f5fbd + 2447039 commit 5210b92
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ const srcs = oust.raw(htmlString, '...');

/*
-> [
{value: '...', $el: '...'},
{value: '...', $el: '...'},
{$el: '...', type: '...', value: '...'},
{$el: '...', type: '...', value: '...'},
...
]
*/
Expand Down
32 changes: 12 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const types = {
},
};

function oust(src, type, raw = false) {
function oust(src, type, raw) {
if (typeof src !== 'string' || !type) {
throw new Error('`src` and `type` required');
}
Expand All @@ -65,33 +65,25 @@ function oust(src, type, raw = false) {
}
}

const chosenTypes = typeArray.map(type => ({...types[type], type}));
const selector = chosenTypes.map(type => type.selector).join(', ');
const $ = cheerio.load(src);
const chosenTypes = typeArray.map(type => ({...types[type], type}));
const $selector = $(chosenTypes.map(type => type.selector).join(', '));

return Array.prototype.map.call($(selector), element => {
const $element = $(element);
const chosenType = chosenTypes.find(type => $element.is(type.selector));

return [...$selector].map(element => {
const $el = $(element);
const {type, method, attribute} = chosenTypes.find(type => $el.is(type.selector));
let value = '';
if (chosenType.method && $element[chosenType.method]) {
value = $element[chosenType.method]();
} else if (chosenType.attribute) {
value = $element.attr(chosenType.attribute);
}

if (raw === true) {
return {
$el: $element,
type: chosenType.type,
value,
};
if (method && $el[method]) {
value = $el[method]();
} else if (attribute) {
value = $el.attr(attribute);
}

return value;
return raw ? {$el, type, value} : value;
});
}

module.exports = (src, type) => oust(src, type);
module.exports = (src, type) => oust(src, type, false);

module.exports.raw = (src, type) => oust(src, type, true);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"space": 2,
"rules": {
"capitalized-comments": "off",
"unicorn/prefer-module": "off"
"unicorn/prefer-module": "off",
"unicorn/prevent-abbreviations": "off"
}
}
}
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('should return an array of stylesheet link hrefs', () => {
assert.equal(links, expected);
});

test('should return an array of refs when passed a HTML string', () => {
test('should return an array of refs when passed an HTML string', () => {
const fixture = '<html><link rel="stylesheet" href="styles/main.css"></html>';
const links = oust(fixture, 'stylesheets');
const expected = ['styles/main.css'];
Expand Down

0 comments on commit 5210b92

Please sign in to comment.