Skip to content

Instantly share code, notes, and snippets.

@caub
Last active November 28, 2017 23:45
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 caub/0e58be6f384e681a3413d65d9d641106 to your computer and use it in GitHub Desktop.
Save caub/0e58be6f384e681a3413d65d9d641106 to your computer and use it in GitHub Desktop.
Things I'd love to see in ES....
  • Shortening const to something like ref, val, cst because

    • it's the most frequently used keyword
    • conflicts with console.log auto-completion
  • null should be Object.freeze(Object.create(null)) or a special Symbol.nullable

  • ([] =~= []) // true shallow compare

  • ({ a: {} } =*= { a: {} }) // true deep compare

  • Making function arguments flexible and coherent with arrays

// (,i)=>i // would be like (_, i)=>i, (IGNOREME, i)=>i, or (...[,i])=>i
// someFunction(a,,b) // someFunction(a, undefined, b), someFunction(...[a,,b])
// actually, you just make a function for those cases, e.g.: 
const range = (length, cb=x=>x) => Array.from({length}, (_,i)=>cb(i))
  • Making someArray[-i] be someArray[someArray.length-i] for any strictly positive integer i.
    However it could break old applications. I've used M[-1], M[-2] for matrices for examples, for more easily using adjacent rows/cells (without if conditions), but it's hacky and avoidable
class A extends Array{ get [-1]() {return this[this.length-1] } }  // or
Object.defineProperty(Array.prototype, -1, { get() {return this[this.length - 1] } }); // sigh
  • Extend .slice to be variadic, and concats all pairs of arguments, it makes things like removing one item easy: a.slice(0, i, i+1) (without mutation)
const {slice} = Array.prototype;
Array.prototype.slice = function(...a){ const args=Array.from({length:Math.ceil(a.length/2)}, (_,i)=>[a[2*i],a[2*i+1]]); return args.reduce((b,x)=>b.concat(slice.call(this, ...x)), [])};
  • Extend .join to be variadic:
Array.prototype.join = function(...arr){ return (this[0]||'')+this.slice(1).reduce((a,s,i)=>a+(arr[i]||',')+s, '') }; [1,2,3].join('__', '@')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment