@@ -7,6 +7,7 @@ import { createScopedContext } from './scope'
7
7
const forAliasRE = / ( [ \s \S ] * ?) \s + (?: i n | o f ) \s + ( [ \s \S ] * ) /
8
8
const forIteratorRE = / , ( [ ^ , \} \] ] * ) (?: , ( [ ^ , \} \] ] * ) ) ? $ /
9
9
const stripParensRE = / ^ \( | \) $ / g
10
+ const destructureRE = / ^ [ { [ ] \s * ( (?: [ \w _ $ ] + \s * , ? \s * ) + ) [ \] } ] $ /
10
11
11
12
interface ChildScope {
12
13
ctx : Context
@@ -31,6 +32,8 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
31
32
32
33
const sourceExp = inMatch [ 2 ] . trim ( )
33
34
let valueExp = inMatch [ 1 ] . trim ( ) . replace ( stripParensRE , '' ) . trim ( )
35
+ let destructureBindings : string [ ] | undefined
36
+ let isArrayDestructure = false
34
37
let indexExp : string | undefined
35
38
let objIndexExp : string | undefined
36
39
@@ -44,15 +47,20 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
44
47
if ( keyAttr === 'key' ) keyExp = JSON . stringify ( keyExp )
45
48
}
46
49
47
- const iteratorMatch = valueExp . match ( forIteratorRE )
48
- if ( iteratorMatch ) {
50
+ let match
51
+ if ( ( match = valueExp . match ( forIteratorRE ) ) ) {
49
52
valueExp = valueExp . replace ( forIteratorRE , '' ) . trim ( )
50
- indexExp = iteratorMatch [ 1 ] . trim ( )
51
- if ( iteratorMatch [ 2 ] ) {
52
- objIndexExp = iteratorMatch [ 2 ] . trim ( )
53
+ indexExp = match [ 1 ] . trim ( )
54
+ if ( match [ 2 ] ) {
55
+ objIndexExp = match [ 2 ] . trim ( )
53
56
}
54
57
}
55
58
59
+ if ( ( match = valueExp . match ( destructureRE ) ) ) {
60
+ destructureBindings = match [ 1 ] . split ( ',' ) . map ( ( s ) => s . trim ( ) )
61
+ isArrayDestructure = valueExp [ 0 ] === '['
62
+ }
63
+
56
64
let mounted = false
57
65
let blocks : Block [ ]
58
66
let scopes : ChildScope [ ]
@@ -69,8 +77,14 @@ export const _for = (el: Element, exp: string, ctx: Context) => {
69
77
index : number ,
70
78
objKey ?: string
71
79
) : ChildScope => {
72
- // TODO destructure
73
- const data = { [ valueExp ] : value }
80
+ const data : any = { }
81
+ if ( destructureBindings ) {
82
+ destructureBindings . forEach (
83
+ ( b , i ) => ( data [ b ] = value [ isArrayDestructure ? i : b ] )
84
+ )
85
+ } else {
86
+ data [ valueExp ] = value
87
+ }
74
88
if ( objKey ) {
75
89
indexExp && ( data [ indexExp ] = objKey )
76
90
objIndexExp && ( data [ objIndexExp ] = index )
0 commit comments