Passed
Push — master ( fe4dd1...b04473 )
by Rafael S.
01:32
created

type.js ➔ ???   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
cc 2
c 9
b 0
f 0
nc 2
nop 1
dl 0
loc 63
rs 9.4347

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