Skip to content

Instantly share code, notes, and snippets.

View lxsmnsyc's full-sized avatar
🤖
I'm not a robot

Alexis H. Munsayac lxsmnsyc

🤖
I'm not a robot
View GitHub Profile
@lxsmnsyc
lxsmnsyc / string-interpolation.ts
Last active September 30, 2020 18:07
string-interpolation.ts
export function interpolate(template: string, ...replacements: string[]): string {
return template.replace(/{([^{}]*)}/g, (match, point) => replacements[point] ?? match);
}
export function keyedInterpolate(template: string, replacements: Record<string, string>): string {
return template.replace(/{([^{}]*)}/g, (match, point) => replacements[point] ?? match);
}
function createNode() {
return {
score: 1,
incoming: [],
outgoing: [],
total: 0,
};
}
function getIncomingEdge(node, toMatch) {
export default function useValidState(validator, initialState) {
const [state, setState] = useState(initialState);
const [error, setError] = useState();
const set = useCallback((action) => {
setState((prev) => {
const value = typeof action === 'function'
? action(prev)
: action;
/**
* @license
* MIT License
*
* Copyright (c) 2020 Alexis Munsayac
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
/**
* @license
* MIT License
*
* Copyright (c) 2020 Alexis Munsayac
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
function shuffle(str) {
let out = "";
while (str.length > 0) {
const index = (str.length * Math.random()) | 0;
out += str[index];
str = str.substr(0, index) + str.substr(index + 1);
}
return out;
}
@lxsmnsyc
lxsmnsyc / styletron.ts
Created March 3, 2020 09:06
Styletron SSR Setup wtih TypeScript
import { Client, Server } from 'styletron-engine-atomic';
import { DebugEngine } from 'styletron-react';
function getHydratedClass(): HTMLStyleElement[] {
const els = document.getElementsByClassName('_styletron_hydrate_');
const newEls = [];
for (let i = 0; i < els.length; i += 1) {
const el: Element = els[i];