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

src/helpers.js   A

Complexity

Total Complexity 27
Complexity/F 2.45

Size

Lines of Code 151
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 0
c 5
b 0
f 0
nc 1
dl 0
loc 151
rs 10
noi 0
wmc 27
mnd 3
bc 24
fnc 11
bpm 2.1818
cpm 2.4545

10 Functions

Rating   Name   Duplication   Size   Complexity  
A helpers.js ➔ padding 0 3 1
A helpers.js ➔ bytePadding 0 9 3
A helpers.js ➔ paddingNibble 0 6 3
A helpers.js ➔ paddingCrumb 0 5 4
A helpers.js ➔ lPadZeros 0 6 2
A helpers.js ➔ makeBigEndian 0 5 2
A helpers.js ➔ bytesToBase 0 11 3
A helpers.js ➔ outputToBase 0 11 4
A helpers.js ➔ turnToArray 0 6 3
A helpers.js ➔ getType 0 6 1
1
/**
2
 * helpers: Functions to work with bytes and byte arrays.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
const endianness = require("endianness");
8
let Type = require("../src/type");
9
10
/**
11
 * Padding for binary strings.
12
 * @param {!Array<string>} bytes The bytes as binary strings.
13
 * @param {number} base The base.
14
 * @param {number} index The byte to pad.
15
 */
16
function padding(bytes, base, index) {
17
    bytes[index] = bytePadding(bytes[index], base);
18
}
19
20
/**
21
 * Padding with 0s for byte strings.
22
 * @param {string} theByte The byte as a binary or hex string.
23
 * @param {number} base The base.
24
 * @returns {string} The padded byte.
25
 */
26
function bytePadding(theByte, base) {
27
    let offset = theByte.length + 1;
28
    if (base == 2) {
29
        offset = 8;
30
    } else if (base == 16) {
31
        offset = 2;
32
    }
33
    return lPadZeros(theByte, offset);
34
}
35
36
/**
37
 * Fix the size of nibbles.
38
 * @param {!Array<string>} nibbles The nibble as a binary or hex string.
39
 * @param {number} base The base.
40
 * @param {number} index The nibble offset.
41
 */
42
function paddingNibble(nibbles, base, index) {
43
    if (base == 2 && nibbles[index].length < 4) {
44
        nibbles[index] = 
45
            new Array((5 - nibbles[index].length)).join("0")  + nibbles[index];
46
    }
47
}   
48
49
/**
50
 * Fix the size of crumbs.
51
 * @param {!Array<string>} crumbs The nibble as a binary or hex string.
52
 * @param {number} base The base.
53
 * @param {number} index The nibble offset.
54
 */
55
function paddingCrumb(crumbs, base, index) {
56
    if ((base == 2 || base == 16) && crumbs[index].length < 2) {
57
        crumbs[index] = '0' + crumbs[index];
58
    }
59
}   
60
61
/**
62
 * Pad a string with zeros to the left.
63
 * TODO: This should support both arrays and strings.
64
 * @param {string} value The string (representing a binary or hex value).
65
 * @param {number} numZeros the max number of zeros.
66
 *      For 1 binary byte string it should be 8.
67
 */
68
function lPadZeros(value, numZeros) {
69
    while (value.length < numZeros) {
70
        value = '0' + value;
71
    }
72
    return value;
73
}
74
75
/**
76
 * Swap the endianness to big endian.
77
 * @param {!Array<number>} bytes The values.
78
 * @param {Object} type The type.
79
 */
80
function makeBigEndian(bytes, type) {
81
    if (type.be) {
82
        endianness(bytes, type.offset);
83
    }
84
}
85
86
/**
87
 * Turn bytes to base 2, 10 or 16.
88
 * @param {!Array<string>|!Array<number>} bytes The bytes.
89
 * @param {number} base The base.
90
 * @param {Function} padFunction The function to use for padding.
91
 */
92
function bytesToBase(bytes, base, padFunction=padding) {
93
    if (base != 10) {
94
        let i = 0;
95
        let len = bytes.length;
96
        while (i < len) {
97
            bytes[i] = bytes[i].toString(base);
98
            padFunction(bytes, base, i);
99
            i++;
100
        }
101
    }
102
}
103
104
/**
105
 * Turn the output to the correct base.
106
 * @param {!Array<number>} bytes The bytes.
107
 * @param {number} bitDepth The bit depth of the data.
108
 * @param {number} base The desired base for the output data.
109
 */
110
function outputToBase(bytes, bitDepth, base) {
111
    if (bitDepth == 4) {
112
        bytesToBase(bytes, base, paddingNibble);
113
    } else if (bitDepth == 2) {
114
        bytesToBase(bytes, base, paddingCrumb);
115
    } else if(bitDepth == 1) {
116
        bytesToBase(bytes, base, function(){});
117
    }else {
118
        bytesToBase(bytes, base);
119
    }
120
}
121
122
/**
123
 * Get the full type spec for the reading/writing.
124
 * @param {Object} atype One of the available types.
125
 * @param {number} base The base of the input.
126
 * @param {boolean} single True if its a single value, false for array.
127
 * @return {Object}
128
 */
129
function getType(atype, base, single) {
130
    let theType = Object.assign(new Type({}), atype);
131
    theType.base = base;
132
    theType.single = single;
133
    return theType;
134
}
135
136
/**
137
 * Make a single value an array in case it is not.
138
 * If the value is a string it stays a string.
139
 * @param {!Array<number>|number|string} values The value or values.
140
 * @return {!Array<number>|string}
141
 */
142
function turnToArray(values) {
143
    if (!Array.isArray(values) && typeof values != "string") {
144
        values = [values];
145
    }
146
    return values;
147
}
148
149
module.exports.makeBigEndian = makeBigEndian;
150
module.exports.outputToBase = outputToBase;
151
module.exports.padding = padding;
152
module.exports.paddingNibble = paddingNibble;
153
module.exports.paddingCrumb = paddingCrumb;
154
module.exports.bytePadding = bytePadding;
155
module.exports.lPadZeros = lPadZeros;
156
module.exports.getType = getType;
157
module.exports.turnToArray = turnToArray;
158