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

Use destructuring more and minor tweaks #57

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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