Skip to content

Instantly share code, notes, and snippets.

@nestoralonso
nestoralonso / react-recursive-render-tree-state.html
Created January 13, 2020 01:18
Renders a Tree with state (a node renders children nodes that render children nodes...)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Recursive Component in React</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.11.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
</head>
@nestoralonso
nestoralonso / memoize-multiple-arguments.js
Created January 20, 2020 23:48
Memoize High Order Function that can handle multiple and nested arguments
function equals(a, b) {
if (a === b) return true;
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
if (!a || !b || (typeof a !== 'object' && typeof b !== 'object')) return a === b;
if (a.prototype !== b.prototype) return false;
const keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) return false;
return keys.every(k => equals(a[k], b[k]));
};
#!/bin/bash
# Color Escape sequence to use in bash scripts
RCol='\e[0m' # Text Reset
# Regular Bold Underline High Intensity BoldHigh Intens Background High Intensity Backgrounds
Bla='\e[0;30m'; BBla='\e[1;30m'; UBla='\e[4;30m'; IBla='\e[0;90m'; BIBla='\e[1;90m'; On_Bla='\e[40m'; On_IBla='\e[0;100m';
Red='\e[0;31m'; BRed='\e[1;31m'; URed='\e[4;31m'; IRed='\e[0;91m'; BIRed='\e[1;91m'; On_Red='\e[41m'; On_IRed='\e[0;101m';
Gre='\e[0;32m'; BGre='\e[1;32m'; UGre='\e[4;32m'; IGre='\e[0;92m'; BIGre='\e[1;92m'; On_Gre='\e[42m'; On_IGre='\e[0;102m';
Yel='\e[0;33m'; BYel='\e[1;33m'; UYel='\e[4;33m'; IYel='\e[0;93m'; BIYel='\e[1;93m'; On_Yel='\e[43m'; On_IYel='\e[0;103m';
Blu='\e[0;34m'; BBlu='\e[1;34m'; UBlu='\e[4;34m'; IBlu='\e[0;94m'; BIBlu='\e[1;94m'; On_Blu='\e[44m'; On_IBlu='\e[0;104m';
@nestoralonso
nestoralonso / unused-branches.sh
Last active September 3, 2020 17:36
Unused Branches
for k in $(git branch -r --merged| sed /\*/d | egrep -v "(^\*|master|dev)"); do
if [ -n "$(git log -1 --before='2 weeks ago' -s $k)" ]; then
echo $k
fi
done
@nestoralonso
nestoralonso / createElement.js
Created September 28, 2020 16:35
Creates a dom object node passing it the attributes
function createElem(name, attrs) {
const elem = document.createElement(name);
return Object.assign(elem, attrs);
}
// Example usage
const newDiv = createElem('div', { className: 'lol', style: 'width: 100px'})
@nestoralonso
nestoralonso / config.yml
Created January 19, 2021 15:37
Config file to make dependabot less spammy? not yet tested .dependabot/config.yml
version: 1
update_configs:
- package_manager: "javascript"
directory: "/"
update_schedule: "weekly"
default_labels:
- "dependencies"
automerged_updates:
- match:
update_type: "in_range"
@nestoralonso
nestoralonso / traverse.js
Created January 27, 2021 19:34
Traverse the values of an object
function traverse(obj, callback) {
const stack = [];
stack.push(obj);
while (stack.length) {
const current = stack[0];
for (const key of Object.keys(current)) {
if (current[key] != null && typeof current[key] === 'object') {
stack.push(current[key]);
@nestoralonso
nestoralonso / range.js
Created March 9, 2021 18:57
Generates an array containing the interval [lower, max) a la python
// Generates an array containing the interval [lower, max) a la python
const range = (lower, max) => Array.from({ length: max - lower }, (x, i) => lower + i);
@nestoralonso
nestoralonso / list-durations
Created May 6, 2021 20:50
List Durations of Video Files, requires java >=11 and ffprobe
#!/usr/bin/env java --source=11
import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.Duration;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Collection;
class ListDurations {
@nestoralonso
nestoralonso / dateSufix.js
Created August 11, 2021 20:59
formats curr date as str (useful for naming new files)
const formatDateAsString = date => {
const fmt = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 2,
useGrouping: false,
});
const dateStr = [date.getFullYear(), date.getMonth() + 1, date.getDate()]
.map(fmt.format)
.join('');
const time = [date.getHours(), date.getMinutes()]
.map(fmt.format)