aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket/src/main/webapp/vnfmarket/node_modules/istanbul/lib/util/tree-summarizer.js
blob: df350f50eb83af0683f34f5ebc55e47ad7d3c076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
 */

var path = require('path'),
    SEP = path.sep || '/',
    utils = require('../object-utils');

function commonArrayPrefix(first, second) {
    var len = first.length < second.length ? first.length : second.length,
        i,
        ret = [];
    for (i = 0; i < len; i += 1) {
        if (first[i] === second[i]) {
            ret.push(first[i]);
        } else {
            break;
        }
    }
    return ret;
}

function findCommonArrayPrefix(args) {
    if (args.length === 0) {
        return [];
    }

    var separated = args.map(function (arg) { return arg.split(SEP); }),
        ret = separated.pop();

    if (separated.length === 0) {
        return ret.slice(0, ret.length - 1);
    } else {
        return separated.reduce(commonArrayPrefix, ret);
    }
}

function Node(fullName, kind, metrics) {
    this.name = fullName;
    this.fullName = fullName;
    this.kind = kind;
    this.metrics = metrics || null;
    this.parent = null;
    this.children = [];
}

Node.prototype = {
    displayShortName: function () {
        return this.relativeName;
    },
    fullPath: function () {
        return this.fullName;
    },
    addChild: function (child) {
        this.children.push(child);
        child.parent = this;
    },
    toJSON: function () {
        return {
            name: this.name,
            relativeName: this.relativeName,
            fullName: this.fullName,
            kind: this.kind,
            metrics: this.metrics,
            parent: this.parent === null ? null : this.parent.name,
            children: this.children.map(function (node) { return node.toJSON(); })
        };
    }
};

function TreeSummary(summaryMap, commonPrefix) {
    this.prefix = commonPrefix;
    this.convertToTree(summaryMap, commonPrefix);
}

TreeSummary.prototype = {
    getNode: function (shortName) {
        return this.map[shortName];
    },
    convertToTree: function (summaryMap, arrayPrefix) {
        var nodes = [],
            rootPath = arrayPrefix.join(SEP) + SEP,
            root = new Node(rootPath, 'dir'),
            tmp,
            tmpChildren,
            seen = {},
            filesUnderRoot = false;

        seen[rootPath] = root;
        Object.keys(summaryMap).forEach(function (key) {
            var metrics = summaryMap[key],
                node,
                parentPath,
                parent;
            node = new Node(key, 'file', metrics);
            seen[key] = node;
            nodes.push(node);
            parentPath = path.dirname(key) + SEP;
            if (parentPath === SEP + SEP || parentPath === '.' + SEP) {
                parentPath = SEP + '__root__' + SEP;
            }
            parent = seen[parentPath];
            if (!parent) {
                parent = new Node(parentPath, 'dir');
                root.addChild(parent);
                seen[parentPath] = parent;
            }
            parent.addChild(node);
            if (parent === root) { filesUnderRoot = true; }
        });

        if (filesUnderRoot && arrayPrefix.length > 0) {
            arrayPrefix.pop(); //start at one level above
            tmp = root;
            tmpChildren = tmp.children;
            tmp.children = [];
            root = new Node(arrayPrefix.join(SEP) + SEP, 'dir');
            root.addChild(tmp);
            tmpChildren.forEach(function (child) {
                if (child.kind === 'dir') {
                    root.addChild(child);
                } else {
                    tmp.addChild(child);
                }
            });
        }
        this.fixupNodes(root, arrayPrefix.join(SEP) + SEP);
        this.calculateMetrics(root);
        this.root = root;
        this.map = {};
        this.indexAndSortTree(root, this.map);
    },

    fixupNodes: function (node, prefix, parent) {
        var that = this;
        if (node.name.indexOf(prefix) === 0) {
            node.name = node.name.substring(prefix.length);
        }
        if (node.name.charAt(0) === SEP) {
            node.name = node.name.substring(1);
        }
        if (parent) {
            if (parent.name !== '__root__' + SEP) {
                node.relativeName = node.name.substring(parent.name.length);
            } else {
                node.relativeName = node.name;
            }
        } else {
            node.relativeName = node.name.substring(prefix.length);
        }
        node.children.forEach(function (child) {
            that.fixupNodes(child, prefix, node);
        });
    },
    calculateMetrics: function (entry) {
        var that = this,
            fileChildren;
        if (entry.kind !== 'dir') {return; }
        entry.children.forEach(function (child) {
            that.calculateMetrics(child);
        });
        entry.metrics = utils.mergeSummaryObjects.apply(
            null,
            entry.children.map(function (child) { return child.metrics; })
        );
        // calclulate "java-style" package metrics where there is no hierarchy
        // across packages
        fileChildren = entry.children.filter(function (n) { return n.kind !== 'dir'; });
        if (fileChildren.length > 0) {
            entry.packageMetrics = utils.mergeSummaryObjects.apply(
                null,
                fileChildren.map(function (child) { return child.metrics; })
            );
        } else {
            entry.packageMetrics = null;
        }
    },
    indexAndSortTree: function (node, map) {
        var that = this;
        map[node.name] = node;
        node.children.sort(function (a, b) {
            a = a.relativeName;
            b = b.relativeName;
            return a < b ? -1 : a > b ? 1 : 0;
        });
        node.children.forEach(function (child) {
            that.indexAndSortTree(child, map);
        });
    },
    toJSON: function () {
        return {
            prefix: this.prefix,
            root: this.root.toJSON()
        };
    }
};

function TreeSummarizer() {
    this.summaryMap = {};
}

TreeSummarizer.prototype = {
    addFileCoverageSummary: function (filePath, metrics) {
        this.summaryMap[filePath] = metrics;
    },
    getTreeSummary: function () {
        var commonArrayPrefix = findCommonArrayPrefix(Object.keys(this.summaryMap));
        return new TreeSummary(this.summaryMap, commonArrayPrefix);
    }
};

module.exports = TreeSummarizer;