-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
136 lines (120 loc) · 4.24 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { motion } from "framer-motion";
export const dollarFormat = (amount, decimalCount = 2, decimal = ".", thousands = ",") => {
try {
decimalCount = Math.abs(decimalCount);
decimalCount = isNaN(decimalCount) ? 2 : decimalCount;
const negativeSign = amount < 0 ? "-" : "";
let i = parseInt(amount = Math.abs(Number(amount) || 0).toFixed(decimalCount)).toString();
let j = (i.length > 3) ? i.length % 3 : 0;
return negativeSign + (j ? i.substr(0, j) + thousands : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands) + (decimalCount ? decimal + Math.abs(amount - i).toFixed(decimalCount).slice(2) : "");
} catch (e) {
console.log(e)
return false;
}
};
export const getLastAndClosePriceFromYahoo = item => {
let lastPrice = 0;
let closePrice = 0;
if(!item) {
return {lastPrice, closePrice};
}
const { marketState } = item;
if(marketState === 'PRE') {
lastPrice = item?.preMarketPrice || item?.regularMarketPrice;
closePrice = item?.regularMarketPrice;
} else if (['POSTPOST', 'PREPRE', 'PREPARE'].includes(marketState)) {
lastPrice = item?.postMarketPrice;
closePrice = item?.regularMarketPrice;
} else if (marketState === 'CLOSED') {
lastPrice = item?.postMarketPrice;
closePrice = item?.regularMarketPreviousClose;
} else {
lastPrice = item?.regularMarketPrice;
closePrice = item?.regularMarketPreviousClose;
}
return {lastPrice, closePrice};
}
export const getPricesFromYahoo = item => {
let lastPrice = 0;
let closePrice = 0;
if(!item) {
return {lastPrice, closePrice};
}
const { marketState } = item;
if(marketState === 'PRE') {
lastPrice = item?.preMarketPrice.raw || item?.regularMarketPrice.raw;
closePrice = item?.regularMarketPrice.raw;
} else if (['POSTPOST', 'PREPRE', 'PREPARE'].includes(marketState)) {
lastPrice = item?.postMarketPrice.raw;
closePrice = item?.regularMarketPrice.raw;
} else if (marketState === 'CLOSED') {
lastPrice = item?.postMarketPrice.raw;
closePrice = item?.regularMarketPreviousClose.raw;
} else {
lastPrice = item?.regularMarketPrice.raw;
closePrice = item?.regularMarketPreviousClose.raw;
}
return {lastPrice, closePrice};
}
const spanStyle = {display: 'inline-block'};
export const differenceBetweenValues = ({oldValue = 0, newValue = 0, controls, theme = {}}) =>{
const themeTextColor = theme.text;
const variants = {
rest: { color: [themeTextColor] },
up: {color: [themeTextColor, '#0cce6b', themeTextColor], transition: {duration: 1.4}},
down: {color: [themeTextColor, '#fd1050', themeTextColor], transition: {duration: 1.4}}
};
let oldText = parseFloat(oldValue).toFixed(2);
let newText = parseFloat(newValue).toFixed(2);
let text = [];
let isDiff = false;
newText.split('').forEach(function(val, i){
if (val != oldText.charAt(i) || isDiff) {
isDiff = true;
text.push(<motion.span
style={spanStyle}
variants={variants}
animate={controls}>{val}</motion.span>);
} else {
text.push(val);
}
});
return text;
}
export const getAnimationType = (lastPrice, prevLastPrice) => {
if(!prevLastPrice || prevLastPrice === '0.00') {
return;
}
const difference = lastPrice - prevLastPrice;
let type = 'rest';
if(difference > 0) {
type = 'up';
} else if (difference < 0) {
type = 'down';
}
return type;
}
export const convertHexToRGBA = (hexCode, opacity = 1) => {
let hex = hexCode.replace('#', '');
if (hex.length === 3) {
hex = `${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}`;
}
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
return `rgba(${r},${g},${b},${opacity})`;
};
export const getPricesFromHtml = code => {
if(!code) {
return {lastPrice: 0, closePrice: 0};
}
var div = document.createElement('div');
div.innerHTML = code;
const lastPriceText = (div?.childNodes?.[2]?.childNodes?.[2]?.innerText).replace(',','') || 0;
const lastPrice = parseFloat(lastPriceText);
const changeText = div?.childNodes?.[2]?.childNodes?.[3]?.childNodes?.[0]?.innerText || 0;
return {
lastPrice,
closePrice: lastPrice - parseFloat(changeText)
};
}