Skip to content

Commit f944377

Browse files
committed
Add destruct cases
1 parent f493227 commit f944377

4 files changed

Lines changed: 334 additions & 327 deletions

File tree

feature/destruct.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ export const destructure = (pattern, value, ctx) => {
1414
const [op, ...raw] = pattern;
1515
const items = flatten(raw);
1616
if (op === '{}') {
17+
const used = [];
1718
for (const item of items) {
19+
// Rest: {...rest}
20+
if (Array.isArray(item) && item[0] === '...') {
21+
const rest = {};
22+
for (const k in value) if (!used.includes(k)) rest[k] = value[k];
23+
ctx[item[1]] = rest;
24+
break;
25+
}
1826
let key, binding, def;
1927
// Shorthand: {x} → item is 'x'
2028
// With default: {x = 1} → ['=', 'x', default]
2129
// Rename: {x: y} → [':', 'x', 'y']
2230
if (typeof item === 'string') { key = binding = item }
2331
else if (item[0] === '=') { typeof item[1] === 'string' ? (key = binding = item[1]) : ([, key, binding] = item[1]); def = item[2] }
2432
else { [, key, binding] = item }
33+
used.push(key);
2534
let val = value[key];
2635
if (val === undefined && def) val = compile(def)(ctx);
2736
destructure(binding, val, ctx);

test/feature/jessie.js

Lines changed: 0 additions & 300 deletions
This file was deleted.

0 commit comments

Comments
 (0)