unprint 0.10.6 → 0.10.7

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.7",
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,17 @@ 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
+ const numberString = options.separator === ','
198
+ ? rawNumberString.replace(',', '.')
199
+ : rawNumberString.replace(',', '');
200
+
197
201
  if (numberString && options.match) {
198
- return Number(numberString.match(options.match)?.[options.matchIndex]?.replace(',', '.')) || null;
202
+ return Number(numberString.match(options.match)?.[options.matchIndex]) || null;
199
203
  }
200
204
 
201
205
  if (numberString) {
202
- return Number(numberString.replace(',', '.')) || null;
206
+ return Number(numberString) || null;
203
207
  }
204
208
 
205
209
  return null;
@@ -211,6 +215,7 @@ function queryNumber(context, selector, customOptions) {
211
215
  const options = {
212
216
  match: defaultNumberRegexp,
213
217
  matchIndex: 0,
218
+ separator: '.',
214
219
  ...customOptions,
215
220
  };
216
221
 
@@ -223,6 +228,7 @@ function queryNumbers(context, selector, customOptions) {
223
228
  const options = {
224
229
  match: defaultNumberRegexp,
225
230
  matchIndex: 0,
231
+ separator: '.',
226
232
  ...customOptions,
227
233
  };
228
234
 
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'));