Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / handlebars / lib / handlebars / helpers / each.js
1 import {appendContextPath, blockParams, createFrame, isArray, isFunction} from '../utils';
2 import Exception from '../exception';
3
4 export default function(instance) {
5   instance.registerHelper('each', function(context, options) {
6     if (!options) {
7       throw new Exception('Must pass iterator to #each');
8     }
9
10     let fn = options.fn,
11         inverse = options.inverse,
12         i = 0,
13         ret = '',
14         data,
15         contextPath;
16
17     if (options.data && options.ids) {
18       contextPath = appendContextPath(options.data.contextPath, options.ids[0]) + '.';
19     }
20
21     if (isFunction(context)) { context = context.call(this); }
22
23     if (options.data) {
24       data = createFrame(options.data);
25     }
26
27     function execIteration(field, index, last) {
28       if (data) {
29         data.key = field;
30         data.index = index;
31         data.first = index === 0;
32         data.last = !!last;
33
34         if (contextPath) {
35           data.contextPath = contextPath + field;
36         }
37       }
38
39       ret = ret + fn(context[field], {
40         data: data,
41         blockParams: blockParams([context[field], field], [contextPath + field, null])
42       });
43     }
44
45     if (context && typeof context === 'object') {
46       if (isArray(context)) {
47         for (let j = context.length; i < j; i++) {
48           if (i in context) {
49             execIteration(i, i, i === context.length - 1);
50           }
51         }
52       } else {
53         let priorKey;
54
55         for (let key in context) {
56           if (context.hasOwnProperty(key)) {
57             // We're running the iterations one step out of sync so we can detect
58             // the last iteration without have to scan the object twice and create
59             // an itermediate keys array.
60             if (priorKey !== undefined) {
61               execIteration(priorKey, i - 1);
62             }
63             priorKey = key;
64             i++;
65           }
66         }
67         if (priorKey !== undefined) {
68           execIteration(priorKey, i - 1, true);
69         }
70       }
71     }
72
73     if (i === 0) {
74       ret = inverse(this);
75     }
76
77     return ret;
78   });
79 }