This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as React from 'react'; | |
| interface Props { | |
| condition: boolean; | |
| wrapper: Function; | |
| children: React.ReactNode; | |
| } | |
| export const ConditionalWrapper: React.FC<Props> = ({ condition, wrapper, children }) => { | |
| return condition ? wrapper(children) : children; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useRef, useLayoutEffect, useState } from 'react'; | |
| interface ResponsiveWrapperProps { | |
| defaultWidth?: number; | |
| defaultHeight?: number; | |
| fixedWidth?: number; | |
| fixedHeight?: number; | |
| heightRatio?: number; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState, useEffect } from 'react'; | |
| const getDimensions = () => { | |
| const { innerWidth: width, innerHeight: height } = window; | |
| return { | |
| width, | |
| height | |
| }; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState, useEffect } from "react"; | |
| export function useReducedMotion() { | |
| const [matches, setMatch] = useState( | |
| window.matchMedia("(prefers-reduced-motion: reduce)").matches | |
| ); | |
| useEffect(() => { | |
| const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)"); | |
| const handleChange = () => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function csvToJson(csv, columnSeparator = ',', rowSeparator = '\n') { | |
| const inferType = (str) => { | |
| if (!str) { | |
| return; | |
| } else if (!isNaN(str) || !isNaN(str.replace(',', '.'))) { | |
| return parseFloat(str.replace(',', '.')); | |
| } else if (Date.parse(str.replace(/"/g, ''))) { | |
| return new Date(str.replace(/"/g, '')); | |
| } else { | |
| return str.replace(/"/g, ''); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function dateRange(startDate, endDate, steps = 1) { | |
| const dateArray = []; | |
| let currentDate = new Date(startDate); | |
| while (currentDate <= new Date(endDate)) { | |
| dateArray.push(new Date(currentDate)); | |
| // Use UTC date to prevent problems with time zones and DST | |
| currentDate.setUTCDate(currentDate.getUTCDate() + steps); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function sleep(milliseconds) { | |
| return new Promise(resolve => { | |
| setTimeout(() => { | |
| resolve(true); | |
| }, milliseconds); | |
| }); | |
| } | |
| // await sleep(1000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function confidence(data, level = '95', key = 'value') { | |
| const zValues = { '80': 1.28, '90': 1.645, '95': 1.96, '98': 2.33, '99': 2.58 }; | |
| const zValue = zValues[level]; | |
| const stdDevValue = stdDev(data, key); | |
| return zValue * (stdDevValue / Math.sqrt(data.length)) | |
| } | |
| function stdDev(data, key = 'value') { | |
| const avg = data.reduce((sum, curr) => sum + curr[key], 0) / data.length; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function reproNumber(data, steps = 4, key = 'value') { | |
| return data.map((obj, i) => { | |
| const currentDays = data.slice(i - steps, i); | |
| const previousDays = data.slice(i - steps * 2, i - steps); | |
| const currentDaysSum = currentDays.reduce((sum, curr) => { return sum + curr[key]; }, 0); | |
| const previousDaysSum = previousDays.reduce((sum, curr) => { return sum + curr[key]; }, 0); | |
| return Object.assign({}, obj, { | |
| r: currentDaysSum / previousDaysSum | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sma(data, steps = 7, key = 'value') { | |
| return data.map((obj, index) => { | |
| const offset = index - Math.floor(steps / 2); | |
| const window = data.slice(offset, steps + offset); | |
| return Object.assign({}, obj, { | |
| average: window.reduce((sum, curr) => { | |
| return curr[key] ? sum + curr[key] : null; | |
| }, 0) / window.length | |
| }); | |
| }).filter(d => d.average); |