11import { Exception } from '@handlebars/parser' ;
2- import { createFrame , isArray , isFunction } from '../utils' ;
2+ import { createFrame , isArray , isFunction , isMap , isSet } from '../utils' ;
33
44export default function ( instance ) {
55 instance . registerHelper ( 'each' , function ( context , options ) {
@@ -21,7 +21,7 @@ export default function (instance) {
2121 data = createFrame ( options . data ) ;
2222 }
2323
24- function execIteration ( field , index , last ) {
24+ function execIteration ( field , value , index , last ) {
2525 if ( data ) {
2626 data . key = field ;
2727 data . index = index ;
@@ -31,7 +31,7 @@ export default function (instance) {
3131
3232 ret =
3333 ret +
34- fn ( context [ field ] , {
34+ fn ( value , {
3535 data : data ,
3636 blockParams : [ context [ field ] , field ] ,
3737 } ) ;
@@ -41,9 +41,19 @@ export default function (instance) {
4141 if ( isArray ( context ) ) {
4242 for ( let j = context . length ; i < j ; i ++ ) {
4343 if ( i in context ) {
44- execIteration ( i , i , i === context . length - 1 ) ;
44+ execIteration ( i , context [ i ] , i , i === context . length - 1 ) ;
4545 }
4646 }
47+ } else if ( isMap ( context ) ) {
48+ const j = context . size ;
49+ for ( const [ key , value ] of context ) {
50+ execIteration ( key , value , i ++ , i === j ) ;
51+ }
52+ } else if ( isSet ( context ) ) {
53+ const j = context . size ;
54+ for ( const value of context ) {
55+ execIteration ( i , value , i ++ , i === j ) ;
56+ }
4757 } else if ( typeof Symbol === 'function' && context [ Symbol . iterator ] ) {
4858 const newContext = [ ] ;
4959 const iterator = context [ Symbol . iterator ] ( ) ;
@@ -52,7 +62,7 @@ export default function (instance) {
5262 }
5363 context = newContext ;
5464 for ( let j = context . length ; i < j ; i ++ ) {
55- execIteration ( i , i , i === context . length - 1 ) ;
65+ execIteration ( i , context [ i ] , i , i === context . length - 1 ) ;
5666 }
5767 } else {
5868 let priorKey ;
@@ -62,13 +72,13 @@ export default function (instance) {
6272 // the last iteration without have to scan the object twice and create
6373 // an intermediate keys array.
6474 if ( priorKey !== undefined ) {
65- execIteration ( priorKey , i - 1 ) ;
75+ execIteration ( priorKey , context [ priorKey ] , i - 1 ) ;
6676 }
6777 priorKey = key ;
6878 i ++ ;
6979 } ) ;
7080 if ( priorKey !== undefined ) {
71- execIteration ( priorKey , i - 1 , true ) ;
81+ execIteration ( priorKey , context [ priorKey ] , i - 1 , true ) ;
7282 }
7383 }
7484 }
0 commit comments