Skip to content

Instantly share code, notes, and snippets.

View nestarz's full-sized avatar
🏠
Working from home

Elias Rhouzlane nestarz

🏠
Working from home
View GitHub Profile
@nestarz
nestarz / globalQueue.js
Last active December 20, 2021 12:59
Promise queue with concurrency control
const queue = (id, concurrency = 1) => {
const d = { running: 0, queue: [] };
const c = (globalThis._RQ ?? {})[id] ?? d;
globalThis._RQ = { ...(globalThis._RQ ?? {}), [id]: c };
const inc = (fn) => (c.running++, fn());
return (task) =>
(c.running < concurrency
? Promise.resolve(inc(task))
: new Promise((res) => c.queue.push(() => res(inc(task))))
).finally(
@nestarz
nestarz / rm-dependencies.js
Created September 6, 2021 22:16
Remove Unused Dependencies
import acorn from "acorn";
import glob from "glob";
import esbuild from "esbuild";
import fs from "fs/promises";
import { resolve, dirname } from "path";
const isNode = (node) => typeof node?.type === "string";
const getChilds = (node) =>
Object.keys(node)
@nestarz
nestarz / queue.js
Created August 27, 2021 19:05
queue function and execute them one by one at fixed interval
const queue = (fn, delay) => {
const queue = []
let curr = null
setInterval(() => {
curr = queue.shift()
}, delay)
return (...args) => {
const id = Math.random()
@nestarz
nestarz / deploy.sh
Last active July 14, 2021 20:20
Self Hosted Deploy Solution Git Hooks post-update
#!/bin/bash
if [ $(git rev-parse --is-bare-repository) = true ]
then
REPOSITORY_BASENAME=$(basename "$PWD")
else
REPOSITORY_BASENAME=$(basename $(readlink -nf "$PWD"/..))
fi
REPOSITORY_BASENAME=${REPOSITORY_BASENAME%.git}
@nestarz
nestarz / script.sh
Created April 11, 2021 14:12
update caddy using caddyfile
curl localhost:2019/load \
-X POST \
-H "Content-Type: application/json" \
-d "$(caddy adapt)"
@nestarz
nestarz / stdintofile.sh
Created April 10, 2021 21:39
How to redirect stdin to file in bash
echo "$(</dev/stdin)" > myfile.txt
# terminate your input with ENTER + ctrl-d
@nestarz
nestarz / unlike.js
Created February 17, 2021 15:57
Unlike all Facebook page
// go to https://www.facebook.com/pages/?category=liked&ref=bookmarks
let pages = []
const unlike_all = () => {
const all = [...document
.querySelectorAll('[aria-label="Liked"],[aria-label="Followed"],[aria-label="Following"]')
]
pages = [...new Set([...pages, ...all.map((a) => a.parentNode.parentNode.querySelector("span").innerText.trim(" "))])]
@nestarz
nestarz / index.html
Last active June 27, 2021 09:06
TS/JSX Transformer using Service Worker and Sucrase
<script>
navigator.serviceWorker
.register("/sw.js")
.then(() => console.log("ServiceWorker registration successful"))
.catch((err) => console.log("ServiceWorker registration failed: ", err));
</script>
<script type="module" src="index.jsx"></script>
<div id="root"></div>
@nestarz
nestarz / Chart.jsx
Created August 21, 2020 10:34
Chart.js 3.x + React
import React, { useState, useEffect, useRef, memo } from "react";
import {
Chart,
Line,
LineController,
LinearScale,
LogarithmicScale,
PieController,
Point,
PolarAreaController,
@nestarz
nestarz / ddd.js
Last active July 1, 2020 18:29
Similar p5 API for creative drawing
const cssColor = (...color) =>
`rgb${color.length === 4 ? "a" : ""}(${(color.length === 1
? Array(3).fill(color[0])
: color
).join(",")})`;
const createP5Api = (ctx, frameCount) => ({
context: ctx,
width: ctx.canvas.width,
height: ctx.canvas.height,