This file contains 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
// react | |
import { useCallback, useEffect, useRef } from 'react'; | |
// props | |
export interface UseIdleProps { | |
events?: string[]; | |
onIdle: () => void; | |
onPrompt?: () => void; | |
promptTimeout?: number; | |
timeout: number; |
This file contains 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
// react | |
import { useCallback, useEffect, useRef, useState } from 'react'; | |
// props | |
export interface UseFetchParams { | |
isEnabled?: boolean; | |
isLazy?: boolean; | |
onComplete?: () => void; | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
onError?: (error: any) => void; |
This file contains 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
export const isDate = date => { | |
const dateTest = new Date(date); | |
return ( | |
dateTest && Object.prototype.toString.call(dateTest) === '[object Date]' && !isNaN(dateTest) | |
); | |
}; | |
export const isNumber = value => { | |
return typeof value === 'number' && !Number.isNaN(value); | |
}; |
This file contains 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
export const merge = (current, updates) => { | |
for (const key of Object.keys(updates)) { | |
if (!current.hasOwnProperty(key) || typeof updates[key] !== 'object') { | |
current[key] = updates[key]; | |
} else { | |
merge(current[key], updates[key]); | |
} | |
} | |
return current; | |
}; |
This file contains 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
export const capitalize = sentence => { | |
const words = sentence.split(' '); | |
for (let i = 0; i < words.length; i++) { | |
if (words[i]) { | |
words[i] = words[i][0].toUpperCase() + words[i].substr(1).toLowerCase(); | |
} | |
} | |
return words.join(' '); | |
}; |
This file contains 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
export const toCamelCase = string => { | |
if (!string) { | |
return ''; | |
} | |
let newString = string.toLowerCase(); | |
newString = newString.replace(/ /g, '_').toLowerCase(); | |
newString = newString.replace(/_([a-z])/g, g => { | |
return g[1].toUpperCase(); |
This file contains 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
// https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-123.php | |
export const toKebabCase = string => { | |
if (!string) { | |
return ''; | |
} | |
const newString = string | |
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) | |
.map(x => x.toLowerCase()) | |
.join('-'); |
This file contains 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
// https://stackoverflow.com/a/31536517/2262604 | |
export const jsonToCsv = json => { | |
const replacer = (key, value) => (value === null ? '' : value); | |
const header = Object.keys(json[0]); | |
const csv = [ | |
header.join(','), | |
...json.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(',')), | |
].join('\r\n'); | |
return csv; | |
}; |
This file contains 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
// https://stackoverflow.com/a/68146412/2262604 | |
export const downloadBlob = ({ content, filename, contentType }) => { | |
// create blob | |
const blob = new Blob([content], { | |
type: contentType || 'text/csv;charset=utf-8;', | |
}); | |
const url = URL.createObjectURL(blob); | |
// create link to download it | |
const anchor = document.createElement('a'); |