Skip to content

Instantly share code, notes, and snippets.

@royashbrook
Created May 16, 2020 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royashbrook/11fcdd2d4a5150d0e1531e6717384b8e to your computer and use it in GitHub Desktop.
Save royashbrook/11fcdd2d4a5150d0e1531e6717384b8e to your computer and use it in GitHub Desktop.
Random Javascript Things
//some utility functions.
const tagwrapper = (tag,txt,att='') => `<${tag}${att.length===0?'':' ' + att}>${txt}</${tag}>`;
const htmlhelpers = ['h4','table','thead','tbody','tr','td','th','div']; //etc
for(const s of htmlhelpers){eval(`var ${s} = (txt,att='')=>tagwrapper('${s}',txt,att);`)}
const groupByArr = (x, key) => x.reduce((rv, x)=> { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, []);
const groupByObj = (x, key) => x.reduce((rv, x)=> { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, {});
const setElementHTML = (id,html) => {
const ctx = $(id)
ctx.empty();
ctx.html(html)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment