(a,b,c=_=>(a.find(p=>b=p.path.exec(location.hash))?.render(b),c))=>onhashchange=c()
Shortened with inspiration from @Posandu
const route = (a,b,c=_=>(a.find(p=>b=p.path.exec(location.hash))?.render(b),c))=>onhashchange=c();
const app = document.querySelector('#app');
route([
{path: /^(#|)$/g, render: () => location.hash = 'home'},
{path: /^#home$/, render: () => app.innerHTML = 'home'},
{path: /^#about$/, render: () => app.innerHTML = 'some about stuff'},
{path: /.*/g, render: () => app.innerHTML = '404'}
])
a=>($={})=>a.replace(/#{([\s\S]+?)}/,(_,c)=>eval(c))
const template = a=>$=>a.replace(/#{([\s\S]+?)}/,(_,c)=>eval(c));
document.body.innerHTML = template(`<h1>Hello, #{($ || 'world').toUpperCase()}!`)('Fred');
a=>$=>a.replace(/#{([\s\S]+?)}/g,(_,c)=>new Option(eval(c)).innerHTML).replace(/#html{([\s\S]+?)}/,(_,c)=>eval(c))
const template = a=>$=>a.replace(/#{([\s\S]+?)}/g,(_,c)=>new Option(eval(c)).innerHTML).replace(/#html{([\s\S]+?)}/,(_,c)=>eval(c));
document.body.innerHTML = template(`Hello, #{$.xss}! #html{$.html}`)({
xss: `<script>alert(1)</script>`,
html: `<b>bold</b>`
});
((a={},b=0)=>({pub:(c,...d)=>Object.entries(a).map(([k,v])=>k.split`-`[0]==c?(v(...d)==!1&&delete a[k],k):0).filter(a=>a),sub:(d,e)=>(a[k=d+--b]=e,k),unsub:k=>delete a[k]}))();
const pubsub = ((a={},b=0)=>({pub:(c,...d)=>Object.entries(a).map(([k,v])=>k.split`-`[0]==c?(v(...d)==!1&&delete a[k],k):0).filter(a=>a),sub:(d,e)=>(a[k=d+--b]=e,k),unsub:k=>delete a[k]}))();
pubsub.sub('event', () => {
console.log('event fired!');
})
pubsub.sub('event', () => {
console.log('event fired! this runs only once');
return false; // stop listening
})
pubsub.pub('event'); // logs twice
pubsub.pub('event'); // logs once (second listener doesnt fire)
const eventID = pubsub.sub('otherthing', () => console.log('other thing'))
pubsub.pub('otherthing'); // logs
pubsub.unsub(eventID)
pubsub.pub('otherthing'); // nothing
pubsub.sub('data', (...data) => console.log(data));
pubsub.pub('data', 1, 'more', ['stuff']) // pass as much as you like
// logs [1, 'more', ['stuff']]
pubsub.sub('test', console.log);
pubsub.sub('test', console.log);
pubsub.sub('test', console.log);
const read = pubsub.pub('test', 'hi')
read //=> ['test-5', 'test-6', 'test-7'] the ids of the listeners that were fired
W=0;X=_=>A=~~((m=Math.random)(B=~~(m()*30))*30);X(Z=_=>[...Array(30).keys()]);process.stdin.setRawMode(D=S=[{x:3,y:3}]).on("data",k=>D={w:0,d:1,s:2,a:3}[k]??process.exit());setInterval(_=>(S.pop((C=console).clear({x,y}=S[0])),[_=>y--,_=>x++,_=>y++,_=>x--][D]?.(),S.find(a=>a.x==x&&a.y==y)||x>29||x<0||y>29||y<0?(C.log(W),process.exit()):0,x==A&&y==B?(S.push({x:A,y:B}),Z().map(_=>S.find((c)=>c.x==A&&c.y==B)?X():0),W++):0,S=[{x,y},...S],_=Z().map(_=>Z().fill("\x1b[40m \x1b[0m")),S.map(({x,y})=>_[y][x]="\x1b[42m "),_[B][A]="\x1b[41m ",_.map(r=>C.log(r.join``))),99)