Skip to content

Instantly share code, notes, and snippets.

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

ikasoba ikasoba

貴様ッ!見ているなッ!
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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]))
function isBase64(value){
if (value == "")return false;
for (let i = 0; i<value.length; ){
const x = value.slice(i,i+=4);
if (i==value.length-1 && !x.match(/^[a-zA-Z0-9]+={0,4}$/))return false;
if (i<value.length-1 && !x.match(/^[a-zA-Z0-9]+$/))return false;
if (x.length != 4)return false;
}
return true
}
@ikasoba
ikasoba / baseN.js
Created June 29, 2022 10:04
任意進数のエンコード、デコード
class BaseN {
table = undefined
/** @type {bigint} */
n = undefined
constructor(chars){
this.table = new Map([...Object.entries(chars).map(([k,v])=>[v,BigInt(k)]),...Object.entries(chars).map(([k,v])=>[BigInt(k),v])])
this.n = BigInt(chars.length)
}
/** @param {arrayBuffer} buf */
enc(buf){
@ikasoba
ikasoba / hulu_pictureinpicture.raw
Last active July 9, 2022 04:56
Huluでピクチャインピクチャ
javascript:(()%3D%3E%7Bconst%20p%3Ddocument.querySelector(%22video%5Bid*%3Dvjs_video%5D%22)%3Bp.disablePictureInPicture%3Dfalse%3Bp.requestPictureInPicture()%7D)()