unprint 0.16.2 → 0.16.3

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.
package/README.md CHANGED
@@ -59,6 +59,9 @@ The selector can be a CSS selector, an XPath selector starting with `/` or `(`,
59
59
  #### Querying multiple elements
60
60
  Most methods can be used in plural, returning an array of results, i.e. `query.elements()`, `query.dates()`.
61
61
 
62
+ Options
63
+ * `filterDuplicates`: When an array of selectors results in the same element being selected multiple times, ensure each element is only returned once, default `true`.
64
+
62
65
  #### Query an element
63
66
  * `query.element([selector], [options])`
64
67
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unprint",
3
- "version": "0.16.2",
3
+ "version": "0.16.3",
4
4
  "description": "Simplify common web scraping tasks while staying in control of the data.",
5
5
  "main": "src/app.js",
6
6
  "scripts": {
package/src/app.js CHANGED
@@ -102,14 +102,20 @@ function queryElement(context, selectors, _customOptions) {
102
102
  return target || null;
103
103
  }
104
104
 
105
- function queryElements(context, selectors, _customOptions) {
105
+ function queryElements(context, selectors, customOptions = {}) {
106
106
  if (!selectors) {
107
107
  return context.element;
108
108
  }
109
109
 
110
- const targets = [].concat(selectors).reduce((acc, selector) => acc || getElements(context, selector, false), null);
110
+ const options = customOptions;
111
+ const targets = [].concat(selectors).reduce((acc, selector) => acc.concat(getElements(context, selector, false)), []).filter(Boolean);
111
112
 
112
- return targets || [];
113
+ if (options.filterDuplicates === false) {
114
+ return targets || [];
115
+ }
116
+
117
+ // findIndex always finds first index, if current index is not the first index, it's a dupe
118
+ return targets.filter((target, index, array) => index === array.findIndex((dupe) => target === dupe));
113
119
  }
114
120
 
115
121
  function queryExistence(context, selector, customOptions) {
package/tests/init.js CHANGED
@@ -47,6 +47,7 @@ async function initTest() {
47
47
  console.log('number indexed', res.context.query.number('.number', { match: /(\d+)/, matchIndex: 1 }));
48
48
  console.log('data', res.context.query.json('#json'));
49
49
  console.log('items', res.context.query.contents('.item'));
50
+ console.log('items css xpath array', res.context.query.contents(['.item', '//li[contains(@class, "number")]']));
50
51
  console.log('link', res.context.query.url('#link'));
51
52
  console.log('links', res.context.query.urls('.link'));
52
53
  console.log('text', res.context.query.text('.text'));