-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctions.js
49 lines (48 loc) · 1.65 KB
/
functions.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
const distance = require("jaro-winkler");
module.exports = {
getBazaarProduct: (query, products) => {
let resultMatch;
let itemResults = [];
for (const key in products) {
itemResults.push({...products[key]});
}
for (const product of itemResults) {
if (product.name.toLowerCase() == query) {
resultMatch = product;
}
product.tagMatches = 0;
product.distance = distance(product.name, query, {caseSensitive: false});
for (const part of query.split(" ")) {
for (const tag of product.tag) {
if (tag == part) {
product.tagMatches++;
}
}
}
}
itemResults = itemResults.sort((a, b) => {
if (a.tagMatches > b.tagMatches) return -1;
if (a.tagMatches < b.tagMatches) return 1;
if (a.distance > b.distance) return -1;
if (a.distance < b.distance) return 1;
});
if (!resultMatch) {
resultMatch = itemResults[0];
}
return resultMatch;
},
formatNumber: (number, floor, rounding = 10) => {
let roundFunc = floor ? Math.floor : Math.ceil;
if (number < 1000) {
return Math.floor(number);
} else if (number < 10000) {
return (roundFunc(number / 1000 * rounding) / rounding).toFixed(rounding.toString().length - 1) + 'K';
} else if (number < 1000000) {
return roundFunc(number / 1000) + 'K';
} else if (number < 1000000000) {
return (roundFunc(number / 1000 / 1000 * rounding) / rounding).toFixed(rounding.toString().length - 1) + 'M';
} else {
return (roundFunc(number / 1000 / 1000 / 1000 * rounding * 10) / (rounding * 10)).toFixed(rounding.toString().length) + 'B';
}
}
}