Skip to content

Instantly share code, notes, and snippets.

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

zazaulola

🏠
Working from home
View GitHub Profile
@zazaulola
zazaulola / stackoverflow-date-outdate-marker.user.js
Created October 20, 2021 03:49
Stackoverflow date outdate marker
// ==UserScript==
// @name Stackoverflow dates
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Stackoverflow dates outdate marker
// @author You
// @include /^https:\/\/(.+\.)?stackoverflow\.com/
// @icon https://www.google.com/s2/favicons?domain=stackoverflow.com
// @grant none
// ==/UserScript==
@zazaulola
zazaulola / point-in-polygon.js
Created October 18, 2021 07:07
check the coordinates of a point in that it is inside a polygon
function inside(point, vs) {
// ray-casting algorithm based on
// https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html/pnpoly.html
// https://stackoverflow.com/questions/22521982/check-if-point-is-inside-a-polygon/29915728#29915728
// https://github.com/substack/point-in-polygon
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
function getSelector(el){
let nodes = [],c,e,d=el.ownerDocument,h=d.documentElement;
do{if (el.id){ nodes.push(`#${el.id}`); break;
}else{ if (el==h) c=0; else for (c=1,e=el;e=e.previousElementSibling;c++);
nodes.push(c?`:nth-child(${c})`:el.tagName.toLowerCase());
} } while ((el=el.parentNode)&&el!=d); return nodes.reverse().join` > `;
}
@zazaulola
zazaulola / calendar.js
Last active September 11, 2024 10:09
Nano-calendar on javascript without elements rendering (data only)
// Calendar for month `m` of the year `y` with Sunday as first day of week
const calS = (y,m) => {
let[r,d,w,l]=[[],1,0,!(y%4)-!(y%1e2)+!(y%4e2)],
c=((0|23*m/9)+(m<3?y--:y-2)+5+(0|y/4)-(0|y/1e2)+(0|y/4e2))%7;
for(;d<29+(0x3bbeecc+16*l>>m*2&3);++d,c=++c%7,w+=!c)
(r[w]||(r[w]=[,,,,,,,]))[c]=d;
return r
}
// Calendar for month `m` of the year `y` with Monday as first day of week
@zazaulola
zazaulola / nano-queue.js
Last active October 11, 2021 22:50
Queue, thread, stack, task, job, flow, stream
const Queue = () => {
let idle = true;
const stack = [],
fx = (...job) => {
stack.push(...job);
if(idle)
(async () => {
idle = false;
while(stack.length)
await stack.shift()();
@zazaulola
zazaulola / flood-fill.js
Created September 29, 2021 22:07
flood-fill algorithm pattern on javascript
function floodFill(image, sr, sc, newColor) {
const current = image[sr][sc];
if (current != newColor) {
fill(image, sr, sc, newColor, current);
}
return image;
function fill(image, sr, sc, newColor, current) {
if (
sr < 0 ||
@zazaulola
zazaulola / derelative.js
Last active October 29, 2021 09:00
SVG path d attribute derelative
function derelativePathD(d,fixed){
let steps = d
.replace(/[,\s]+/g,' ')
.replace(/-\./g,'-0.')
.replace(/(?<=\.\d+)\./g,' 0.')
.split(/([MmLlAaCcSsQqTtVvHhZz])/g)
.splice(1)
.map((v,i) => !(i%2) ? v : v
.split(/\s+/g)
.map(v=>v.split(/(-?[\d]*(?:\.\d+)?(?:e-?\d{1,2})?)/g)
@zazaulola
zazaulola / html-beautifier.js
Created September 14, 2021 09:21
Simple HTML-string beautifier (works in browser)
function beautify(
/* HTML-string */ html,
/* new line chars */ nl = '\n',
/* indent chars */ tab = '\t',
/* disabled format tags */ disabled = /^(code|pre|em|strong|span|b|i|u|sup|sub|a)$/ig,
/* without closed tags */ single = /^(area|base|br|col|command|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/gi
){
return escape(
stringify(
new DOMParser() /* parse HTML */
@zazaulola
zazaulola / middleware-router.js
Last active September 8, 2021 01:36
Primitive middleware router without timeout
// Router is a function that returns async function.
const Router = (...chain) =>
Object.assign(
// That async function as object have one method push()
async (ctx) => {
// When it calls that function it runs recursive algorithm
return await (async function run(idx) {
// Which calls each function in the chain
let res = await ("AsyncFunction" !== chain[idx].constructor.name
? new Promise((res) => chain[idx](ctx, res))
@zazaulola
zazaulola / middleware-router-with-timeout-detection.js
Last active September 8, 2021 01:38
Primitive middleware router with timeout detection
const stepTimeout = 505;
const Router = (...chain) =>
Object.assign(
async (ctx) => {
await (async function run(idx) {
if ("function" == typeof chain[idx]) {
await new Promise((res, rej) => {
const to = setTimeout(
() => rej(new Error(`Step [${idx}] timeout on \n${chain[idx]}`)),