Passed
Push — master ( 8ff0a1...a2cde2 )
by Rafael S.
01:21
created

src/from-bytes.js   A

Complexity

Total Complexity 19
Complexity/F 2.38

Size

Lines of Code 116
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
cc 0
nc 1
dl 0
loc 116
rs 10
c 8
b 0
f 0
wmc 19
mnd 2
bc 15
fnc 8
bpm 1.875
cpm 2.375
noi 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A from-bytes.js ➔ getSingleValue 0 8 2
A from-bytes.js ➔ getBitReader 0 4 2
A from-bytes.js ➔ fromBytes 0 15 2
A from-bytes.js ➔ signFunction 0 4 3
A from-bytes.js ➔ readBytes 0 19 3
A from-bytes.js ➔ bytesToInt 0 10 3
A from-bytes.js ➔ getReaderName 0 4 3
1
/*
2
 * from-bytes: convert bytes to numbers and strings.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
const reader = require("../src/read-bytes.js");
8
const bitDepths = require("../src/bit-depth.js");
9
const helpers = require("../src/helpers.js");
10
11
/**
12
 * Turn a byte buffer into what the bytes represent.
13
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes.
14
 * @param {Object} type One of the available types.
15
 * @return {!Array<number>|number|string}
16
 */
17
function fromBytes(buffer, type) {
18
    let bitDepth = type.bits;
19
    helpers.fixFloat16Endianness(buffer, type);
20
    helpers.makeBigEndian(buffer, type.be, bitDepth);
21
    bytesToInt(buffer, type.base);
22
    let values = readBytes(
23
            buffer,
24
            type,
25
            getBitReader(type)
26
        );
27
    if (type.single) {
28
        values = getSingleValue(values, type);
29
    }
30
    return values;
31
}
32
33
/**
34
 * Return a function to read binary data.
35
 * @param {Object} type One of the available types.
36
 * @return {Function}
37
 */
38
function getBitReader(type) {
39
    return type.char ?
40
        reader.readChar : reader[getReaderName(type.bits, type.float)];
41
}
42
43
/**
44
 * Return the first value from the result value array.
45
 * @param {!Array<number>|string} values The values.
46
 * @param {Object} type One of the available types.
47
 * @return {number|string}
48
 */
49
function getSingleValue(values, type) {
50
    if (type.char) {
51
        values = values.slice(0, type.bits / 8);
52
    } else {
53
        values = values[0];
54
    }
55
    return values;
56
}
57
58
/**
59
 * Turn a array of bytes into an array of what the bytes should represent.
60
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
61
 * @param {Object} type The type.
62
 * @param {Function} bitReader The function to read the bytes.
63
 * @return {!Array<number>|string}
64
 */
65
function readBytes(bytes, type, bitReader) {
66
    let values = [];
67
    let i = 0;
68
    let len = bytes.length - (type.offset -1);
69
    let theSignFunction = signFunction(type);
70
    while (i < len) {
71
        values.push(
72
            theSignFunction(
73
                    bitReader(bytes, i, type),
74
                    bitDepths.BitDepthMaxValues[type.bits]
75
                )
76
            );
77
        i += type.offset;
78
    }
79
    if (type.char) {
80
        values = values.join("");
81
    }
82
    return values;
83
}
84
85
/**
86
 * Return the function to apply sign or not to a type.
87
 * @param {Object} type The type.
88
 * @return {Function}
89
 */
90
function signFunction(type) {
91
    return type.signed && !type.float ?
92
        helpers.signed : function(x){return x;};
93
}
94
95
/**
96
 * Build a bit reading function name based on the arguments.
97
 * @param {number} bits The bit depth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64.
98
 * @param {boolean} float True if the values are IEEE floating point numbers.
99
 * @return {string}
100
 */
101
function getReaderName(bits, float) {
102
    return 'read' + (bits < 8 ? 8 : bits) +
103
        'Bit' + (float ? "Float" : "");
104
}
105
106
/**
107
 * Turn bytes to base 10.
108
 * @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings.
109
 * @param {number} base The base.
110
 */
111
function bytesToInt(bytes, base) {
112
    if (base != 10) {
113
        let i = 0;
114
        let len = bytes.length;
115
        while(i < len) {
116
            bytes[i] = parseInt(bytes[i], base);
117
            i++;
118
        }
119
    }
120
}
121
122
module.exports.fromBytes = fromBytes;
123