| 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 | 1x
 
 
 
1x
 
 
11x
 
 
 
11x
11x
11x
11x
11x
11x
 
 
 
 
 
 
 
 
 
51x
 
 
 
 
 
 
 
 
 
 
 
 
51x
51x
51x
51x
51x
51x
 
51x
 
 
 
 
 
 
 
 
 
11x
 
 
204x
 
204x
204x
204x
 
204x
30x
 
 
204x
30x
15x
 
30x
 
 
204x
 
 
11x
 
11x
11x
11x
 
 
11x
 
 
11x
1x
 
 
 
 
11x
1x
 
 
 
10x
 
10x
 
 
11x
10x
 
 
1x
1x
 
1x
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1x
 
 
8x
 
 
8x
 
 
 
 
1x | 'use strict'
 
var _ = require('underscore')
 
function DFHelper() {}
 
_.extend(DFHelper, {
    noop: function() {},
    parse: function(stdout, options) {
        Iif (!stdout) {
            return void 0
        }
 
        var multiplier = DFHelper.getMultiplierByPrefix(options.prefixMultiplier)
        var isDisplayPrefixMultiplier = !!options.isDisplayPrefixMultiplier
        var displayedPrefixMultiplier = isDisplayPrefixMultiplier ? options.prefixMultiplier : null
        var precision = options.precision
        var format = DFHelper.format
        var response = _(
            stdout
                .trim()
 
                // split into rows
                .split(/\r|\n|\r\n/)
 
                // strip column headers away
                .slice(1)
        ).map(function(row) {
            var columns = row
 
                // one or more whitespaces followed by one or more digits
                // must be interpreted as column delimiter
                .replace(/\s+(\d+)/g, '\t$1')
 
                // one or more whitespaces followed by a slash
                // must be interpreted as the last column delimiter
                .replace(/\s+\//g, '\t/')
 
                // split into columns
                .split(/\t/)
 
            var filesystem = columns[0]
            var size = format(columns[1], multiplier, precision, displayedPrefixMultiplier)
            var used = format(columns[2], multiplier, precision, displayedPrefixMultiplier)
            var available = format(columns[3], multiplier, precision, displayedPrefixMultiplier)
            var capacity = format(columns[4], 0.01)
            var mount = _.rest(columns, 5).join(' ')
 
            return {
                filesystem: filesystem,
                size: size,
                used: used,
                available: available,
                capacity: capacity,
                mount: mount,
            }
        })
 
        return response
    },
    format: function(s, multiplier, precision, displayedPrefixMultiplier) {
        multiplier = multiplier || 1
 
        var tmp = parseInt(s, 10) * multiplier
        var amount = Math.pow(10, precision)
        var hasPrecision = _.isNumber(precision)
 
        if (hasPrecision) {
            tmp = Math.round(tmp * amount) / amount
        }
 
        if (displayedPrefixMultiplier) {
            if (hasPrecision) {
                tmp = tmp.toFixed(precision)
            }
            tmp = tmp + displayedPrefixMultiplier
        }
 
        return tmp
    },
    getMultiplierByPrefix: function(prefix) {
        var prefixMultipliers = DFHelper.getPrefixMultipliers()
 
        var defaultPrefix = 'KiB'
        var defaultMultiplier = 1024
        var multiplier = 1
 
        // prefixes are case insensitive
        prefix = (prefix || defaultPrefix).toLowerCase()
 
        // invalid prefix multipliers are ignored
        if (!prefixMultipliers[prefix]) {
            prefix = defaultPrefix
        }
 
        // df outputs sizes in KiB
        // nothing to do
        if (prefix === defaultPrefix) {
            return multiplier
        }
 
        // convert to bytes then convert to unit of choice
        multiplier = defaultMultiplier / prefixMultipliers[prefix]
 
        return multiplier
    },
    getPrefixMultipliers: function() {
        if (DFHelper._prefixMultipliers) {
            return DFHelper._prefixMultipliers
        }
 
        var pow2 = DFHelper._pow2
        var pow10 = DFHelper._pow10
 
        DFHelper._prefixMultipliers = {
            kib: pow2(10),
            mib: pow2(20),
            gib: pow2(30),
            tib: pow2(40),
            pib: pow2(50),
            eib: pow2(60),
            zib: pow2(70),
            yib: pow2(80),
 
            kb: pow10(3),
            mb: pow10(6),
            gb: pow10(9),
            tb: pow10(12),
            pb: pow10(15),
            eb: pow10(18),
            zb: pow10(21),
            yb: pow10(24),
        }
 
        return DFHelper._prefixMultipliers
    },
    _pow2: function(exponent) {
        return Math.pow(2, exponent)
    },
    _pow10: function(exponent) {
        return Math.pow(10, exponent)
    },
    _prefixMultipliers: null,
})
 
module.exports = DFHelper
  |