Last active
August 13, 2023 05:47
-
-
Save SiddharthShyniben/e155fb4ab9d723a4f309514f0f6cf180 to your computer and use it in GitHub Desktop.
Lots of handy code golf. Mostly ported from 140byt.es but modernized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add or remove classes. classify(element, '+class-to-add -class-to-remove other-class-to-add') | |
(a,b)=>b.split` `.map(c=>c[0]=='-'?['remove',c.slice(1)]:['add',c[0]=='+'?c.slice(1):c]).map(([d,e])=>a[d](e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// make rgb colors lighter or darker, ([r, g, b], num) => [r, g, b] | |
(c,n)=>c.map(d=>(d+=n)<0?0:d>255?255:d|0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// html escape. str => str | |
s=>new Option(s).innerHTML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hex=>[r,g,b] | |
a=>{a='0x'+a.slice(1).replace(a.length>4&&/./g,'$&$&');return[a>>16,a>>8&255,a&255]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// random id. (len, radix) => str | |
(a=32,b=16)=>"0".repeat(a).replace(/./g,_=>(0|Math.random()*b).toString(b)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// levenshtein distance between strings | |
(a,b,c,d,e,f,g)=>{for(d=[e=0];a[e];e++)for(c=[f=0];b[++f];)g=d[f]=e?Math.min(d[--f],c[f]-(a[e-1]==b[f]),c[++f]=d[f])+1:f;return g} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// localstorage helper. () => {get, set} | |
(a=this.localStorage||{},b=JSON)=>{get:c=>a[c]&&b.parse(a[c]),set:(c,d)=>a[c]=b.stringify(d)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// famous pubsub. ()=>{pub, sub} | |
(a={},b=0)=>({pub:(d,e)=>Object.keys(a).map(k=>k.split`-`[0]==d&&(a[k](e)!==!1||delete a[k])),sub:(d,e)=>a[d+--b]=e}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (r,g,b)=>hex | |
(a,b,c)=>"#"+((256+a<<8|b)<<8|c).toString(16).slice(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple spa router. ([route, fn, route2, fn2...], poll) => {} | |
(a,b=99,c,e=(d=location.hash)=>{for(b=0,c=d;(d=a[b++])&&!(d=d.exec(c));b++);a[b](d)})=>(e(),setInterval(e,b)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// time since (1m ago, 20s ago, 1y ago). date => str | |
(a,b=[1e3,60,60,24],c=0)=>{for(a=new Date-a;a>2*b[c];)a/=b[c++];return~~a+" "+"msmhd"[c]+"s ago"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bruteforce sudoku solver...? flatArr => never (in place, throws when done) | |
R=(a,i,j,m,g)=>{for(i=80;a[i];i--||A);for(m=10;g=a[i]=--m;g&&R(a))for(j in a)g*=j==i||a[j]^m||i%9^j%9&&i/9^j/9&&i/27^j/27|i%9/3^j%9/3} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ordinal suffix (nth, nst, ...). n => str | |
a=>["th","st","nd","rd"][(a=~~(a<0?-a:a)%100)>10&&a<14||(a%=10)>3?0:a] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple templates. (source, ctx) => (this, ctx) => output | |
(a,b)=>(c,d)=>a.replace(/#{([^}]*)}/g,(a,e)=>Function("x","with(x)return "+e).call(c,d||b||{})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment