Passed
Push — master ( 56b7f2...fe4dd1 )
by Rafael S.
01:22
created

type.js ➔ ???   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 2
c 6
b 0
f 0
nc 2
nop 1
dl 0
loc 80
rs 8.8387

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * type: The Type class.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
/** @private */
8
const bitParser = require("../src/bit-parser.js");
9
10
/**
11
 * A class to represent byte-data types.
12
 */
13
class Type {
14
15
    constructor(options) {
16
        
17
        /**
18
         * The max number of bits used by data of this type.
19
         * @type {number}
20
         */
21
        this.bits = options["bits"];
22
        
23
        /**
24
         * If this type represent floating-point values or not.
25
         * @type {boolean}
26
         */
27
        this.char = options["char"];
28
        
29
        /**
30
         * If this type it is signed or not.
31
         * @type {boolean}
32
         */
33
        this.float = options["float"];
34
35
        /**
36
         * If this type is big-endian or not.
37
         * @type {boolean}
38
         */
39
        this.be = options["be"];
40
41
        /**
42
         * If this type it is signed or not.
43
         * @type {boolean}
44
         */
45
        this.signed = this.float ? true : options["signed"];
46
47
        /**
48
         * If this type represent a single value or
49
         * an array.
50
         * @type {boolean}
51
         */
52
        this.single = true;
53
54
        /**
55
         * The function to read values of this type
56
         * from byte buffers.
57
         * @type {Function}
58
         */
59
        this.reader = null;
60
61
        /**
62
         * The function to write values of this type
63
         * to byte buffers.
64
         * @type {Function}
65
         */
66
        this.writer = null;
67
68
        /**
69
         * The number of bytes used by data of this type.
70
         * @type {number}
71
         */
72
        this.offset = 0;
73
74
        /**
75
         * The base used as a default representation for
76
         * data of this type.
77
         * @type {number}
78
         */
79
        this.base = 10;
80
81
        /**
82
         * Min value for this type.
83
         * @type {number}
84
         */
85
        this.min = -Infinity;
86
87
        /**
88
         * Max value for this type.
89
         * @type {number}
90
         */
91
        this.max = Infinity;
92
        
93
        this.build_();
94
    }
95
96
    /**
97
     * Sign a number according to the type.
98
     * @param {number} num The number.
99
     */
100
    sign(num) {
101
        if (num > this.max) {
102
            num -= (this.max * 2) + 2;
103
        }
104
        return num;
105
    }
106
107
    /**
108
     * Limit the value according to the bit depth in case of
109
     * overflow or underflow.
110
     * @param {!Array<number>|number|string} value The data.
111
     */
112
    overflow(value) {
113
        if (value > this.max) {
114
            value = this.max;
115
        } else if (value < this.min) {
116
            value = this.min;
117
        }
118
        return value;
119
    }
120
121
    /**
122
     * Build the type.
123
     * @private
124
     */
125
    build_() {
126
        if (this.bits < 8) {
127
            this.offset = 1;
128
        } else {
129
            this.offset = this.bits / 8;
130
        }
131
        this.setReader_();
132
        this.setWriter_();
133
        if (!this.float) {
134
            this.setMinMax_();
135
        }
136
    }
137
138
    /**
139
     * Set the function to read data of this type.
140
     * @private
141
     */
142
    setReader_() {
143
        this.reader = this.char ?
144
            bitParser.BitReader["readChar"] : bitParser.BitReader[
145
                'read' + (this.bits < 8 ? 8 : this.bits) +
146
                'Bit' + (this.float ? "Float" : "")];
147
    }
148
149
    /**
150
     * Set the function to write data of this type.
151
     * @private
152
     */
153
    setWriter_() {
154
        if (this.char) {
155
            this.writer = bitParser.BitWriter["writeString"];
156
        } else {
157
            this.writer = bitParser.BitWriter[
158
                'write' + this.bits + 'Bit' + (this.float ? "Float" : "")];
159
        }
160
    }
161
162
    /**
163
     * Set the minimum and maximum values for the type.
164
     * @private
165
     */
166
    setMinMax_() {
167
        let max = Math.pow(2, this.bits);
168
        if (this.signed) {
169
            this.max = (max / 2) - 1;
170
            this.min = (max / 2) * -1;
171
        } else {
172
            this.max = max - 1;
173
            this.min = 0;
174
        }
175
    }
176
}
177
178
module.exports = Type;
179