-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
63 lines (49 loc) · 1.74 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function castDollarStringToNumber(dollarString) {
return castNumberStringToNumber(dollarString.replace('$', ''));
}
function castPercentStringToNumber(percentString) {
return +percentString.replace('%', '') / 100;
}
function castNumberStringToNumber(numberString) {
return +numberString.replaceAll(',', '')
}
function formatDate(date) {
return date.toISOString().slice(0, 10)
}
const dollarStringParser = (value, header) => ({ [header]: castDollarStringToNumber(value) });
const numberStringParser = (value, header) => ({ [header]: castNumberStringToNumber(value) });
const percentStringParser = (value, header) => ({ [header]: castPercentStringToNumber(value) });
async function wait(ms) {
return new Promise((resolve) => setTimeout(() => resolve(), ms));
}
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
function totalSoldMoreThan(number) {
return item => item.total_sold > number;
}
function isNotEmpty(field) {
return item => Boolean(item[field]);
}
function matches(item, field, regexp) {
const fieldValue = (item[field] ?? '').toString();
console.log('matches', regexp, fieldValue, regexp.test(fieldValue));
return regexp.test(fieldValue);
}
function fieldNotMatches(field, regexp) {
return item => !matches(item, field, regexp);
}
const filterRules = [
totalSoldMoreThan(5),
isNotEmpty('mpn'),
fieldNotMatches('mpn', /does not apply/ig),
fieldNotMatches('mpn', /null/ig),
fieldNotMatches('mpn', /,/ig),
fieldNotMatches('mpn', / and /ig),
fieldNotMatches('mpn', /\//ig),
]
// Sold price range - sold_price_range
// Sell-through - sell_through
function toSnakeCase(inputString) {
return inputString.replace(/(\s+|[^\w+])/gi, '_').toLowerCase();
}