Skip to content

Instantly share code, notes, and snippets.

View dorianbayart's full-sized avatar

Dorian Bayart dorianbayart

View GitHub Profile
@dorianbayart
dorianbayart / fibonacci.js
Created June 2, 2024 22:17
Calculate big Fibonacci numbers with BigInt and memoization in JavaScript
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
@dorianbayart
dorianbayart / youtube-dl.sh
Last active March 19, 2024 09:44
Easily download a video using youtube-dl
#!/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
@dorianbayart
dorianbayart / getOrPost.js
Created October 26, 2022 22:50
Utils - An async function to Get or Post on url depending on a query parameter
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)
@dorianbayart
dorianbayart / debounce.js
Created October 26, 2022 22:43
Utils - Debounce function
// Utils - Debounce function
let debounceTimer
function debounce(func, timeout = 500) {
return (...args) => {
clearTimeout(debounceTimer)
debounceTimer = setTimeout(() => { func.apply(this, args) }, timeout)
}
}
// Use it
@dorianbayart
dorianbayart / generateColorFromString.js
Created October 26, 2022 22:36
Generate a color from a string - Calculate a Hash of a String and use it to generate HSL(A) color
// 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