Skip to content

Instantly share code, notes, and snippets.

View ikasoba's full-sized avatar
貴様ッ!見ているなッ!

ikasoba ikasoba

貴様ッ!見ているなッ!
View GitHub Profile
@ikasoba
ikasoba / sql.js
Last active June 23, 2022 09:54
tagged template literal to bind
// license: MIT
/**
* usage: sql`insert into hoge values (2,${`(select json_agg(users) from users)`})` //out: ["insert into hoge values (2,$1)",["(select json_agg(users) from users)"]]
* @template T
* @type {(r:TemplateStringsArray,...v:T[])=>[string, T[]]}
*/
export const sql = (r,...rawValues) => {
const valuearr = [...new Set(rawValues)]
const values = new Map(valuearr.map((v,i) => [v,i+1]))
@ikasoba
ikasoba / h.ts
Last active May 23, 2022 13:25
htmlを出力するための関数
export interface HElement {
tag: string, attrs: Record<string,string>, children: (HElement|string)[], toHTML(): string, toDOM(): Element
}
export type HComponent = ((attrs:Record<string,string>,children:HElement["children"],componentUtil:HComponentUtil)=>HElement)
export interface HComponentUtil_nonPrivate {
__nativeElement__?:Element
__event__: {
didMount:null|(()=>void)
@ikasoba
ikasoba / base58.js
Last active May 22, 2022 06:11
base58 encoder and decoder for js
const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
const base58Table = {...Object.fromEntries(Object.entries(base58Chars).map(([k,v])=>[v,BigInt(k)])),...base58Chars}
/** @param {arrayBuffer} buf */
function base58enc(buf){
const u8 = new Uint8Array(buf)
let int = 0n
u8.forEach( (x,i) => int += BigInt(x * 256 ** (u8.length-1-i)) )
let res = ""
while (1){
@ikasoba
ikasoba / sortJSON.js
Created May 14, 2022 09:14
jsonのソート
const sortJSON = o => Object.fromEntries(Object.entries(o).sort((a,b)=>a[0].charCodeAt(0) - b[0].charCodeAt(0)))
@ikasoba
ikasoba / html.js
Last active April 14, 2022 11:52
jsでhtmlを組み立てるやつ
/**
* @example
* html("div",{class:"hogehoge"},[
* html("p",{},"i am salmon!!"),
* "<script>alert('raw html is not embeddable');</script>"
* ]);
*/
const html = (name,attrs,...children) => ((children.length==1) && void(children[0] instanceof Array) && (children=children[0])) || {name:name,attrs:attrs,children:children,toString(){return `<${this.name} ${Object.entries(this.attrs).map(([k,v])=>(void(v=v.replace(/"|'|<|>/g,([c])=>"&x"+(c.charCodeAt(0).toString(16))+";")) || `${k}=${v?.match("'") ? '"'+v+'"' : v.match(/"|[\s]/) ? "'"+v+"'" : v}`)).join(" ")}>${children.map(x=>typeof x === "string" ? x.replace(/"|'|<|>/g,([c])=>"&#x"+(c.charCodeAt(0).toString(16))+";") : x.toString()).join("")}</${name}>`}}
@ikasoba
ikasoba / genChrs.js
Last active April 5, 2022 06:38
generate characters
const genChrs = (f,t)=>f<t ? String.fromCharCode(f++) + genChrs(f,t) : String.fromCharCode(f++)
@ikasoba
ikasoba / $enum.js
Created March 7, 2022 13:04
add enum to javascript
/**
* @example
* console.log($enum("A","B","C"),$enum(10)("A","B","C")) // {A:0,B:1,C:2} {A:10,B:11,C:12}
*/
export function $enum(...start){
const mkobj=(...keys)=>Object.fromEntries(keys.map((x,i)=>[x,typeof start[0] === "number" ? start[0]+i : i]))
return start.find(x=>typeof x !== "number") ? mkobj(...start) : mkobj
}
export default $enum;
@ikasoba
ikasoba / fizzBuzz1l.js
Last active March 6, 2022 08:04
fizz buzzをワンライナーで書いた
Array.from({length:500},(_,n)=>((i=n+1)=>(i%3==0) + (i%15==0) + (i%5==0)*2)()).map((x,i)=>x&1 ? "Fizz" : x&2 ? "Buzz" : x&4 ? "Fizz Buzz" : i+1).forEach((m)=>console.log(m))
@ikasoba
ikasoba / type_calculator.ts
Last active March 5, 2022 12:36
typescriptの型で計算機
export type Down<A> = A extends [null, ...null[]]
? ((..._: A) => null) extends (_:null, ...a:infer T) => null
? T
: never
: never
export type Up<A extends null[],B extends null|null[] = null> = [...A,...(B extends null ? [B] : B)]
export type Num<N extends number,A extends null[] = []> = {
0:A,
@ikasoba
ikasoba / range.ts
Created March 5, 2022 09:31
typescriptで0~(n-1)までの整数値のみ型
type Append<T extends any[],U extends any> = [...T,U]
type range<N extends number,A extends any[]> = {
0:range<N,Append<A,A["length"]>>,
1:A
}[A["length"] extends N ? 1 : 0]
const nums:range<4,[]>=[0,1,2,3]
console.log(nums)