From c83911d0a16884defc2a3b7375338c3fe264f975 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Mon, 22 May 2023 09:35:51 +0300 Subject: [PATCH 1/4] Use destructuring --- README.md | 4 ++-- index.js | 26 +++++++++----------------- package.json | 3 ++- test/index.js | 2 +- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8afe512..8f23444 100644 --- a/README.md +++ b/README.md @@ -87,8 +87,8 @@ const srcs = oust.raw(htmlString, '...'); /* -> [ - {value: '...', $el: '...'}, - {value: '...', $el: '...'}, + {$el: '...', type: '...', value: '...'}, + {$el: '...', type: '...', value: '...'}, ... ] */ diff --git a/index.js b/index.js index baca341..240be42 100644 --- a/index.js +++ b/index.js @@ -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'); } @@ -70,28 +70,20 @@ function oust(src, type, raw = false) { const $ = cheerio.load(src); return Array.prototype.map.call($(selector), element => { - const $element = $(element); - const chosenType = chosenTypes.find(type => $element.is(type.selector)); - + 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); diff --git a/package.json b/package.json index 28c44d6..284f7e9 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,8 @@ "space": 2, "rules": { "capitalized-comments": "off", - "unicorn/prefer-module": "off" + "unicorn/prefer-module": "off", + "unicorn/prevent-abbreviations": "off" } } } diff --git a/test/index.js b/test/index.js index e9eebb2..279950b 100644 --- a/test/index.js +++ b/test/index.js @@ -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 = ''; const links = oust(fixture, 'stylesheets'); const expected = ['styles/main.css']; From b6c6d6f6d6e94da5e6b0b851a3fbf01a7e208780 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Mon, 22 May 2023 09:49:09 +0300 Subject: [PATCH 2/4] Use spread --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 240be42..8801fc7 100644 --- a/index.js +++ b/index.js @@ -65,11 +65,11 @@ function oust(src, type, raw) { } } - 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 => { + return [...$selector].map(element => { const $el = $(element); const {type, method, attribute} = chosenTypes.find(type => $el.is(type.selector)); let value = ''; From 4a407cbd11a264b98fa8966972554206db1f1684 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Fri, 25 Aug 2023 08:51:46 +0300 Subject: [PATCH 3/4] Update index.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ben Zörb --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 8801fc7..a6474e2 100644 --- a/index.js +++ b/index.js @@ -71,7 +71,7 @@ function oust(src, type, raw) { return [...$selector].map(element => { const $el = $(element); - const {type, method, attribute} = chosenTypes.find(type => $el.is(type.selector)); + const {type, method, attribute} = chosenTypes.find(type => $el.is(type.selector)) || {}; let value = ''; if (method && $el[method]) { From 24470390f29703d926a7324dba5a7e9870d113a8 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sun, 3 Sep 2023 08:45:08 +0300 Subject: [PATCH 4/4] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index a6474e2..8801fc7 100644 --- a/index.js +++ b/index.js @@ -71,7 +71,7 @@ function oust(src, type, raw) { return [...$selector].map(element => { const $el = $(element); - const {type, method, attribute} = chosenTypes.find(type => $el.is(type.selector)) || {}; + const {type, method, attribute} = chosenTypes.find(type => $el.is(type.selector)); let value = ''; if (method && $el[method]) {