public
Last active

js destructuring macro

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 
// Note: there are bugs in hygienic renaming, so this doesn't work all the time yet.
 
macro varr {
case [$var (,) ...] = $expr => {
var i = 0;
var arr = $expr;
$(var $var = arr[i++];) ...
}
 
case {$var (,) ...} = $expr => {
var obj = $expr;
$(var $var = obj.$var;) ...
}
 
case $var:ident = $expr => {
var $var = $expr
}
}
 
varr [x, y, z] = [0, 1, 2];
console.log(x, y, z); // 0 1 2
 
varr {x, y, z} = {x: 5, y: 6, z: 7};
console.log(x, y, z); // 5 6 7
 
varr w = 10;
console.log(w); // 10

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.