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
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>
@uhyo
uhyo / content.js
Created July 4, 2018 13:19
Firefox EventTarget bug reproduction
console.log('Test 1: raw EventTarget');
const et = new EventTarget();
// add handler of 'foo' event
et.addEventListener('foo', (e)=> {
console.log('foo event emitted:', e.detail);
});
// dispatch 'foo' event
et.dispatchEvent(new CustomEvent('foo', { detail: 123 }));
console.log('Test 2: subclass of EventTarget');
@uhyo
uhyo / perm.js
Created December 8, 2017 08:24
Permutation using generator
function* perm(arr, prefix=[]){
if (arr.length <= 1){
yield [...prefix, ...arr];
return;
}
for (const [i, x] of arr.entries()) {
const arr2 = [...arr.slice(0, i), ...arr.slice(i+1)];
yield* perm(arr2, [...prefix, x]);
}
}
@uhyo
uhyo / count.js
Last active January 10, 2019 04:53
Sotsuron counter (for node.js)
#! /usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const child_process = require('child_process');
// parse args
@uhyo
uhyo / count.sh
Last active January 19, 2017 17:16
Sotsuron word counter
#!/bin/bash
CHAP=
ALLFLAG=0
while getopts ac: OPT
do
case $OPT in
a) ALLFLAG=1;;
c) CHAP=$OPTARG;;
@uhyo
uhyo / g.js
Last active November 9, 2016 04:46
Googleの"大統領選"の検索結果の州をクリックすると色が変えられるやつ
(()=>{
const svg = document.querySelector('svg');
const svg2 = cl(svg);
const gs = svg2.querySelectorAll('g');
for (let i=0; i < gs.length; i++){
const g = gs[i];
const cl = g.getAttribute('class');
if (cl){
const c = g.getAttribute('class').split(' ');
if (c.some(x=>/__states$/.test(x))){
@uhyo
uhyo / tcp.js
Created July 16, 2016 15:51
tcp
const PORT = 8080;
const net = require('net');
const srv = net.createServer(sock=>{
console.log('New connection');
sock.on('data', buf=>{
console.log('---------- data start ----------');