Skip to content

Instantly share code, notes, and snippets.

View uhyo's full-sized avatar
🈚
Current status: totemo yowai programmer

uhyo uhyo

🈚
Current status: totemo yowai programmer
View GitHub Profile
@uhyo
uhyo / useBufferflyEffect.ts
Created January 12, 2022 00:35
useButterflyEffect()
const effects: Map<number, () => void> = new Map();
let nextEffectId = 1;
/**
* Similar to useEffect, but an effect from another randomly chosen instance of useButterflyEffect is called
* when given deps change.
*/
function useButterflyEffect(effect: () => void, deps: readonly unknown[]) {
useEffect(() => {
const effectId = nextEffectId++;
@uhyo
uhyo / g.js
Created November 4, 2020 01:44
Googleの大統領選の結果の州をクリックすると色を変えられるやつ2020
(()=>{
const svg = document.querySelector('svg.vlmbi.H21Mnd');
const svg2 = cl(svg);
const paths = svg2.querySelectorAll('path.xH0v4d');
const cs = [
'rgb(218, 220, 224)',
'rgb(0, 155, 216)',
'rgb(166, 220, 241)',
'rgb(241, 176, 180)',
'rgb(219, 29, 40)',
function offsetRange(arr) {
return (
arr.map(v => [v, v+1])
.join(",")
.replace(/(,\d+)\1/g, "-")
.replace(/(-+,)\d+/g, (_, s) => "-" + (s.length))
.replace(/(,\d+),\d+/g, (_, s) => `${s}-1`)
.split(",")
.map((v) => {
const [offset, range] = v.split("-");
@uhyo
uhyo / gist:f97499872fb0685bd5a4b11fa0f55aef
Last active May 26, 2020 12:23
Sync way of obtaining promise result
Promise.prototype.then = (()=> {
const _then = Promise.prototype.then;
return function(...args) {
_then.call(this, (res) => {
promiseResultMap.set(this, res);
})
this.then = _then;
return _then.apply(this, args);
}
})();
@uhyo
uhyo / use.js
Created April 9, 2020 10:57
JavaScript Top
const use\uffa0std = {
collections: {
get HashMap() {
globalThis.HashMap = class HashMap {};
return undefined;
}
}
}
@uhyo
uhyo / fizzbuzz.js
Last active October 16, 2019 09:14
fizzbuzz.js
let fizzbuzz = false;
function* range() {
for (let i=1; i<=100; i++) yield i;
}
async function fizz() {
for (const i of range()) {
if (i % 3 === 0) {
fizzbuzz=true;
process.stdout.write("Fizz");
}
const numBegArr = ["-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
const numBegSet = new Set(numBegArr);
const isNumericBeginning = char => numBegSet.has(char);
const isNumericBeginning2 = char => numBegArr.includes(char);
const isNumericBeginning3 = char =>
char === "-" ||
char === "0" ||
char === "1" ||
char === "2" ||
@uhyo
uhyo / arr.ts
Created June 4, 2019 13:46
Array with least number of elements
type AtLeast<N extends number, T> = AtLeastRec<N, T, T[], []>;
type AtLeastRec<Num, Elm, T extends unknown[], C extends unknown[]> = {
0: T;
1: ((arg: Elm, ...rest: T) => void) extends ((...args: infer T2) => void)
? ((arg: unknown, ...rest: C) => void) extends ((...args: infer T3) => void)
? AtLeastRec<Num, Elm, T2, T3>
: never
: never;
}[C extends { length: Num } ? 0 : 1];
type Digged<T, Keys extends string[]> = DigFunI<T, (...args: Keys) => void>[0];
interface DigFunI<T, F> extends Array<F extends (()=>void) ? T :
F extends ((key: infer K, ...rest: infer Rest) => void) ?
K extends keyof T ? (DigFunI<T[K],((...args: Rest)=>void)> extends Array<infer E> ? E : unknown)
: unknown : unknown>{
}
function dig<T, Keys extends string[]>(obj: T, ...keys: Keys): Digged<T, Keys> {
let result: any= obj;
// Builderオブジェクトの型
type Builder<Props, Result> = ({} extends Props
? {
build: () => Result;
}
: {}) &
{ [P in keyof Props]-?: SetFunction<Props, P, Result> };
type SetFunction<Props, K extends keyof Props, Result> = (
value: Exclude<Props[K], undefined>