-
Shortening
const
to something likeref
,val
,cst
because- it's the most frequently used keyword
- conflicts with
console.log
auto-completion
-
null
should beObject.freeze(Object.create(null))
or a specialSymbol.nullable
-
([] =~= []) // true
shallow compare -
({ a: {} } =*= { a: {} }) // true
deep compare
// (,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]
besomeArray[someArray.length-i]
for any strictly positive integeri
.
However it could break old applications. I've usedM[-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('__', '@')