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 App = () => { | |
const [showModal, setShowModal] = useState(false); | |
const handleKeyup = e => e.keyCode === 27 && setShowModal(false); | |
const toggleModal = () => setShowModal(!showModal); | |
useEffect(() => { | |
if (showModal) window.addEventListener('keyup', handleKeyup); | |
return () => window.removeEventListener('keyup', handleKeyup); | |
}); | |
return <div onClick={toggleModal} className="App"> |
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 Modal = ({children}) => <div className='Modal'> | |
{children} | |
</div>; |
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 factorial(n) { | |
if (n <= 1) return 1; | |
return n * factorial(n - 1); | |
} | |
console.log(factorial(5)); // 120 |
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 recursiveFn(n) { | |
if (n < 0) return true; | |
recursiveFn(n - 1); | |
} | |
recursiveFn(100000); // RangeError: Maximum call stack size exceeded |
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 traverseDOM(tree) { | |
if (isLeaf(tree)) tree.className = 'leaf'; | |
else for (const leaf of tree.childNodes) traverseDOM(leaf); | |
} |
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 trampoline(f) { | |
return function trampolined(...args) { | |
let result = f.bind(null, ...args); | |
while (typeof result === 'function') result = result(); | |
return result; | |
}; | |
} |
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 factorial = trampoline(function _factorial(n, acc = 1) { | |
if (n <= 1) return acc; | |
return () => _factorial(n - 1, n * acc); | |
}); | |
console.log(factorial(5)); |
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 recursiveFn = trampoline(function _recursiveFn(n) { | |
if (n < 0) return; | |
return () => _recursiveFn(n - 1); | |
}); | |
recursiveFn(100000); | |
console.log('No range error!'); |
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
interface ColumnOfStrings { | |
_id: string; // MongoDB id field | |
type: 'string'; // yes, it is literal 'string' for column of strings | |
values: (string | null)[]; // an array, each element is either string or 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 compress(column) { | |
column.compression = 'ZIP_JSON_STRINGIFY'; | |
column.values = zip(JSON.stringify(column.values)); | |
return column; | |
} |