UNPKG

@ledgerhq/live-common

Version:
93 lines 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatPerformer = exports.format = void 0; exports.currencyFormatter = currencyFormatter; const types_1 = require("./types"); function distributedCopy(items, n) { if (!items) return []; if (items.length <= n) return items; const elements = [items[0]]; const totalItems = items.length - 2; const interval = Math.floor(totalItems / (n - 2)); for (let i = 1; i < n - 1; i++) { elements.push(items[i * interval]); } elements.push(items[items.length - 1]); return elements; } const sparklineXMagnitude = 5; const sparklineYHeight = 50; function sparklineAsSvgData(points) { const totalXSteps = sparklineXMagnitude * points.length; const min = Math.min(...points); const max = Math.max(...points); const yOffset = max - min; return { path: points .map((d, i) => { const [x, y] = [ i * sparklineXMagnitude, sparklineYHeight + 3 - ((d - min) * sparklineYHeight) / yOffset, ]; return i === 0 ? `M ${x} ${y}` : `L ${x} ${y}`; }) .join(" "), viewBox: `0 0 ${totalXSteps} ${sparklineYHeight + 3}`, isPositive: points[0] <= points[points.length - 1], }; } function currencyFormatter(data) { return data.map((currency) => (0, exports.format)(currency)); } const format = (currency) => { return { id: currency.id, ledgerIds: currency.ledgerIds, name: currency.name, image: currency.image, marketcap: currency.marketCap, marketcapRank: currency.marketCapRank, totalVolume: currency.totalVolume, high24h: currency.high24h, low24h: currency.low24h, ticker: currency.ticker, price: currency.price, priceChangePercentage: { [types_1.KeysPriceChange.hour]: currency.priceChangePercentage1h, [types_1.KeysPriceChange.day]: currency.priceChangePercentage24h, [types_1.KeysPriceChange.week]: currency.priceChangePercentage7d, [types_1.KeysPriceChange.month]: currency.priceChangePercentage30d, [types_1.KeysPriceChange.year]: currency.priceChangePercentage1y, }, marketCapChangePercentage24h: currency.marketCapChangePercentage24h, circulatingSupply: currency.circulatingSupply, totalSupply: currency.totalSupply, maxSupply: currency.maxSupply, ath: currency.allTimeHigh, athDate: new Date(currency.allTimeHighDate), atl: currency.allTimeLow, atlDate: new Date(currency.allTimeLowDate), sparklineIn7d: currency.sparkline ? sparklineAsSvgData(distributedCopy(currency.sparkline, 6 * 7)) // keep 6 points per day : undefined, chartData: {}, }; }; exports.format = format; const formatPerformer = (currency) => ({ id: currency.id, ledgerIds: currency.ledgerIds, name: currency.name, image: currency.image, ticker: currency.ticker, priceChangePercentage1h: currency.priceChangePercentage1h, priceChangePercentage24h: currency.priceChangePercentage24h, priceChangePercentage7d: currency.priceChangePercentage7d, priceChangePercentage30d: currency.priceChangePercentage30d, priceChangePercentage1y: currency.priceChangePercentage1y, price: currency.price, }); exports.formatPerformer = formatPerformer; //# sourceMappingURL=currencyFormatter.js.map