Create a gist now

Instantly share code, notes, and snippets.

js destructuring macro
// 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
@JamesMGreene

Note: Requires use of sweet.js.

@JamesMGreene

Note: Requires use of sweet.js.

@kristianmandrup

Can we replace varr with var by now as mentioned in your article?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment