Skip to content

Instantly share code, notes, and snippets.

@yojona
yojona / color.ts
Created January 16, 2024 16:17
hex alpha
export const alpha = (color: string, opacity: number) => {
// if it has an alpha, remove it
if (color.length > 7) {
color = color.substring(0, color.length - 2);
}
// coerce values so ti is between 0 and 1.
const newOpacity = Math.round(Math.min(Math.max(opacity, 0), 1) * 255);
let opacityHex = newOpacity.toString(16).toUpperCase();
@yojona
yojona / usePeerData
Created August 29, 2020 21:47
usePeerData - PeerJS for data
import { useState, useEffect, useCallback } from 'react';
import Peer from 'peerjs';
const usePeer = ({ id, host, port, path, onOpen, onConnection, onDisconnection, onClose, onError, onMessage }) => {
const [peer, setPeer] = useState();
const [peerId, setPeerId] = useState();
const [connection, setConnection] = useState();
const [message, setMessage] = useState();
const onPeerOpen = useCallback((current) => {
@yojona
yojona / Recursive Filter
Created March 1, 2020 00:39
JS recursive children filter
// https://stackblitz.com/edit/recursive-filter-prop?file=index.ts
// perf: https://jsbench.me/8cjrlaine7/1
function flatFilter(nestedProp, compareKey, compareId, arr) {
return arr.filter(o => {
const keep = o[compareKey] != compareId;
if (keep && o[nestedProp]) {
o[nestedProp] = flatFilter(nestedProp, compareKey, compareId, o[nestedProp]);
}
return keep;
@yojona
yojona / .bashrc
Created May 15, 2019 14:53
Expose your localhost to internet
# Add this function to your source
expose () {
if [ -n "$1" ]; then
ssh -R $3:80:$1 serveo.net
else
ssh -R 80:$1 serveo.net
fi
}
const toCamelCase = string => {
return string.replace(/^([A-Z])|[\s-_](\w)/g, (match, p1, p2, offset) => {
if (p2) return p2.toUpperCase();
return p1.toLowerCase();
});
}
console.log(toCamelCase("Hola perro"))
@yojona
yojona / countries_codes_and_coordinates.json
Last active December 4, 2020 17:04 — forked from tadast/countries_codes_and_coordinates.csv
Countries with their (ISO 3166-1) Alpha-2 code, Alpha-3 code, UN M49, average latitude and longitude
[
{
"country": "Afghanistan",
"alpha_2_code": "AF",
"alpha_3_code": "AFG",
"numeric_code": 4,
"latitude": 33,
"longitude": 65
},
{