保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
'use strict';
 
// detect either spaces or tabs but not both to properly handle tabs
// for indentation and spaces for alignment
const INDENT_RE = /^(?:( )+|\t+)/;
 
function getMostUsed(indents) {
    let result = 0;
    let maxUsed = 0;
    let maxWeight = 0;
 
    for (const entry of indents) {
        // TODO: use destructuring when targeting Node.js 6
        const key = entry[0];
        const val = entry[1];
 
        const u = val[0];
        const w = val[1];
 
        if (u > maxUsed || (u === maxUsed && w > maxWeight)) {
            maxUsed = u;
            maxWeight = w;
            result = Number(key);
        }
    }
 
    return result;
}
 
module.exports = str => {
    if (typeof str !== 'string') {
        throw new TypeError('Expected a string');
    }
 
    // used to see if tabs or spaces are the most used
    let tabs = 0;
    let spaces = 0;
 
    // remember the size of previous line's indentation
    let prev = 0;
 
    // remember how many indents/unindents as occurred for a given size
    // and how much lines follow a given indentation
    //
    // indents = {
    //    3: [1, 0],
    //    4: [1, 5],
    //    5: [1, 0],
    //   12: [1, 0],
    // }
    const indents = new Map();
 
    // pointer to the array of last used indent
    let current;
 
    // whether the last action was an indent (opposed to an unindent)
    let isIndent;
 
    for (const line of str.split(/\n/g)) {
        if (!line) {
            // ignore empty lines
            continue;
        }
 
        let indent;
        const matches = line.match(INDENT_RE);
 
        if (matches) {
            indent = matches[0].length;
 
            if (matches[1]) {
                spaces++;
            } else {
                tabs++;
            }
        } else {
            indent = 0;
        }
 
        const diff = indent - prev;
        prev = indent;
 
        if (diff) {
            // an indent or unindent has been detected
 
            isIndent = diff > 0;
 
            current = indents.get(isIndent ? diff : -diff);
 
            if (current) {
                current[0]++;
            } else {
                current = [1, 0];
                indents.set(diff, current);
            }
        } else if (current) {
            // if the last action was an indent, increment the weight
            current[1] += Number(isIndent);
        }
    }
 
    const amount = getMostUsed(indents);
 
    let type;
    let indent;
    if (!amount) {
        type = null;
        indent = '';
    } else if (spaces >= tabs) {
        type = 'space';
        indent = ' '.repeat(amount);
    } else {
        type = 'tab';
        indent = '\t'.repeat(amount);
    }
 
    return {
        amount,
        type,
        indent
    };
};