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
// Implements stale-while-revalidate | |
self.addEventListener('fetch', event => { | |
const cached = caches.match(event.request); | |
const fetched = fetch(event.request); | |
const fetchedCopy = fetched.then(resp => resp.clone()); | |
// Call respondWith() with whatever we get first. | |
// If the fetch fails (e.g disconnected), wait for the cache. | |
// If there’s nothing in cache, wait for the fetch. | |
// If neither yields a response, return a 404. |
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 fibonacci = (n) => { | |
// memoization Map: stores all Fibonacci numbers | |
const lookup = new Map() | |
const fib = (n) => { | |
// if the nth number is in the memoization table, return it | |
if (lookup.has(n)) return BigInt(lookup.get(n)) | |
// base case: if n is 0 or 1, return n itself | |
if (n <= 1) return n |
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
#!/bin/bash | |
# This script downloads a video with the highest available video and audio qualities | |
# Requirements: | |
# brew install youtube-dl | |
# chmod +x youtube-dl.sh | |
# Usage: | |
# ./youtube-dl.sh |
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 get(url, query = null) { | |
if(query) { // if query => /post | |
return new Promise((resolve, reject) => { | |
fetch(url, { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ query }) | |
}) | |
.then((response) => response.json()) | |
.then(resolve) |
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
// Utils - Debounce function | |
let debounceTimer | |
function debounce(func, timeout = 500) { | |
return (...args) => { | |
clearTimeout(debounceTimer) | |
debounceTimer = setTimeout(() => { func.apply(this, args) }, timeout) | |
} | |
} | |
// Use it |
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
// Utils - Calculate a Hash of a String | |
const hashCode = (str) => { | |
let hash = 0 | |
for (var i = 0; i < str.length; i++) { | |
hash = str.charCodeAt(i) + ((hash << 5) - hash) | |
} | |
return hash | |
} | |
// Generate a HSL color using the Hash |