Skip to content

Instantly share code, notes, and snippets.

type UnionToIntersection<U> = (
U extends any ? (k: U) => void : never
) extends (k: infer I) => void
? I
: never
type SubpropertyMerge<T> = (
T extends object ? (
T extends string | number | ((...args: any) => any) | symbol | boolean ? T
: { [K in keyof T]: SubpropertyMerge<T[K]> }
const eg = smoosh([
{ foo: {a: 1}},
{ foo: {b: 2}}
]);
eg.foo.a
eg.foo.b
// take an array of objects, do your best to smoosh them together
function smoosh(objs) {
@SephReed
SephReed / ColorTools.ts
Last active October 4, 2019 18:02
Incomplete snippet of code used for HSLum
export function rangeLimit(num: number, min: number, max: number) {
return Math.max(min, Math.min(num, max));
}
export function rangeWrap(num: number, circumference: number) {
return ((num % 1) + 1) % 1;
}
const ratioR = 4;
const ratioG = 4;
const ratioB = 3;
@SephReed
SephReed / gist:eeb62da1f3fabcce1e37b9364779563f
Created September 15, 2019 19:00
How to make Typescript Hydration Typings / Function
// Typings
export type ReportRequest<OUTLINE> = {[key in keyof OUTLINE]?: true};
export type Reporter<OUTLINE> =
(request: ReportRequest<OUTLINE>) => Required<Pick<OUTLINE, keyof typeof request>>;
// Example model
interface IModel {
Here's a vanilly implementation. Just use object keys to check that all of the props have matching values.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function findPropsMatch(list, target) {
const props = Object.keys(target);
return list.find((checkMe) =>
!props.find((prop) => checkMe[prop] !== target[prop])
return div("user-div", {
// shortcut for adding/removing classnames based off state
ddxClass: () => state.userSelected ? "--selected" : "",
// shortcut for general purpose SDx manipulation
ddx: (userDiv) => userDiv.tabIndex = state.userTabIndex
})
const someDiv = div("parent-element", [
div("some-child-element"),
// passing a function creates a replacer
() => div("example-rerender", state.userName), // <--- Replacer
]);
const replacer = new RerenderReplacer(() => {
const uiElement = document.createElement("div");
uiElement.innerHTML = state.userName;
return uiElement;
});
document.body.appendChild(replacer.static());
interface ILayered extends Array<number | ILayered> { }
function flatten(flattenMe: ILayered) {
const flat: number[] = [];
const permeate = (layered: ILayered) => {
layered.forEach((item) => {
if(Array.isArray(item)) {
permeate(item);
} else {
flat.push(item);
}