unprint 0.10.6 → 0.10.8

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
@@ -77,6 +77,7 @@ Return the text contents of an element (`.textContent`).
77
77
  Options
78
78
  * `match`: The regular expression to use to extract a number from text, default `/\d+(\.\d+)?/` for decimal numbers.
79
79
  * `matchIndex`: The index of the match result, useful for expressions containing groups or a global flag, default `0`.
80
+ * `separator`: Whether to use `.` (Europe) or `,` (US) as the decimal separator, default `.`
80
81
 
81
82
  Return the contents of the element or attribute as a Number primitive.
82
83
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unprint",
3
- "version": "0.10.6",
3
+ "version": "0.10.8",
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
@@ -193,13 +193,21 @@ function queryDatasets(context, selector, dataAttribute, customOptions) {
193
193
 
194
194
  const defaultNumberRegexp = /\d+([.,]\d+)?/;
195
195
 
196
- function matchNumberString(numberString, options) {
196
+ function matchNumberString(rawNumberString, options) {
197
+ if (!rawNumberString) {
198
+ return null;
199
+ }
200
+
201
+ const numberString = options.separator === ','
202
+ ? rawNumberString.replace(',', '.')
203
+ : rawNumberString.replace(',', '');
204
+
197
205
  if (numberString && options.match) {
198
- return Number(numberString.match(options.match)?.[options.matchIndex]?.replace(',', '.')) || null;
206
+ return Number(numberString.match(options.match)?.[options.matchIndex]) || null;
199
207
  }
200
208
 
201
209
  if (numberString) {
202
- return Number(numberString.replace(',', '.')) || null;
210
+ return Number(numberString) || null;
203
211
  }
204
212
 
205
213
  return null;
@@ -211,6 +219,7 @@ function queryNumber(context, selector, customOptions) {
211
219
  const options = {
212
220
  match: defaultNumberRegexp,
213
221
  matchIndex: 0,
222
+ separator: '.',
214
223
  ...customOptions,
215
224
  };
216
225
 
@@ -223,6 +232,7 @@ function queryNumbers(context, selector, customOptions) {
223
232
  const options = {
224
233
  match: defaultNumberRegexp,
225
234
  matchIndex: 0,
235
+ separator: '.',
226
236
  ...customOptions,
227
237
  };
228
238
 
package/tests/init.js CHANGED
@@ -24,6 +24,7 @@ async function initTest() {
24
24
  console.log('timestring', res.context.query.duration('#timestring'));
25
25
  console.log('number', res.context.query.number('.number'));
26
26
  console.log('numbers', res.context.query.numbers('.number'));
27
+ console.log('numbers comma', res.context.query.numbers('.number', { separator: ',' }));
27
28
  console.log('number indexed', res.context.query.number('.number', { match: /(\d+)/, matchIndex: 1 }));
28
29
  console.log('data', res.context.query.json('#json'));
29
30
  console.log('items', res.context.query.contents('.item'));