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
import { useEffect } from "react"; | |
/** | |
* A hook that prevents closing or refreshing if there are unsaved changes, | |
* offering the user a prompt to confirm before closing the window. | |
* @param promptBeforeClosingWindow A boolean indicating if we should prompt before closing | |
*/ | |
export function useBeforeClosingWindowPrompt(promptBeforeClosingWindow: boolean) { | |
useEffect((): (() => void) => { | |
function handleBeforeUnload(e: BeforeUnloadEvent): 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
// The best way to split up your npm deps into smaller chunks | |
module.exports = (env) => { | |
const config = { | |
// ... rest of config options | |
optimization: { | |
runtimeChunk: "single", | |
splitChunks: { | |
cacheGroups: { | |
vendors: { |
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
async function downloadObjectAsJSONFile(object: any, filename: string): Promise<void> { | |
if(!filename.endsWith('.json')) { | |
filename = `${filename}.json` | |
} | |
const json = JSON.stringify(object) | |
const blob = new Blob([json],{ type:'application/json' }) | |
const href = await URL.createObjectURL(blob) | |
const link = document.createElement('a') | |
link.href = href | |
link.download = filename |
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
async function downloadTextAsFile(text: string, filename: string): Promise<void> { | |
if (!filename.endsWith(".txt")) { | |
filename = `${filename}.txt`; | |
} | |
const blob = new Blob([text], { type: "text/plain" }); | |
const href = await URL.createObjectURL(blob); | |
const link = document.createElement("a"); | |
link.href = href; | |
link.download = filename; | |
link.style.position = "absolute"; |
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
document.querySelectorAll('style, link[rel="stylesheet"]').forEach(el => el.parentNode.removeChild(el)) | |
document.querySelectorAll('[style]').forEach(el => el.style = null) |
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
function parseQueryString(query) { | |
query = query.substring(query.indexOf('?') + 1); | |
let re = /([^&=]+)=?([^&]*)/g; | |
let decodeRE = /\+/g; | |
let decode = function(str) { | |
return decodeURIComponent(str.replace(decodeRE, ' ')); | |
}; |
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
const not = fn => (...args) => !fn.apply(null, args) |
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
function csvStringToArrOfObjs(csvString) { | |
const rowsWithColumNames = csvString.split('\n') | |
const columnNames = rowsWithColumNames[0].split(',').map(columnName => columnName.replace('\r', '')) | |
const rows = rowsWithColumNames.slice(1) | |
return rows.map(row => { | |
const obj = {} | |
const parts = row.split(',').map(columnName => columnName.replace('\r', '')) | |
columnNames.forEach((columnName, index) => { | |
obj[columnName] = parts[index] | |
}) |
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
// Newer and preferred way | |
function copyToClipboard(text) { | |
navigator.clipboard.writeText(text) | |
} | |
// Support on old browsers | |
function copyToClipboard(text) { | |
const tmpInput = document.createElement('input') | |
tmpInput.value = text | |
tmpInput.type = 'text' |
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
const readline = require("readline"); | |
const question = (query) => | |
new Promise((resolve) => { | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
rl.question(`${query}\n`, (value) => { | |
rl.close(); |
NewerOlder