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 / proxy-wrapper.js
Last active June 28, 2022 21:10
Javascript Proxy template
const proxify = object =>
!['function', 'object'].includes(typeof object)
? object
: new Proxy(object, {
/* A trap for a function call */
apply(target, thisArg, args) {
console.log('--- apply() ---');
console.dir(object);
console.log('target:');
@zazaulola
zazaulola / LICENSE.txt
Last active August 25, 2021 17:20 — forked from diafygi/LICENSE.txt
Base58 Encoder/Decoder - slightly over 140 bytes, but still pretty useful
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
/**
* Convert DateTime to timestamp
* @param {Number} y Full year
* @param {Number} M Month (1..12)
* @param {Number} d Date (1..31)
* @param {Number} h Hour (0..23)
* @param {Number} m Minute (0..59)
* @param {Number} s Second (0..59)
* @param {Number} l Millisecond (0..999)
* @oaram {Number} z Time zone
function at(obj, path, val = undefined) {
// If path is an Array,
if (Array.isArray(path)) {
// it returns the mapped array for each result of the path
return path.map((path) => at(obj, path, val));
}
// Uniting several RegExps into one
const rx = new RegExp(
[
/(?:^(?:\.\s*)?([_a-zA-Z][_a-zA-Z0-9]*))/,
@zazaulola
zazaulola / jump.html
Created August 29, 2021 21:30
Parcel jump animation
<div id="parcel">📦</div>
<style>
#parcel {
position: absolute;
font-size: 4rem;
animation: jump 1.2s infinite;
}
@keyframes jump {
0% { transform: translate(-50%, -50%) scale(1.25, 0.75); }
50% { transform: translate(-50%, -150%) scale(1, 1); }
@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]}`)),
@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 / 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 / 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 / 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 ||