Passed
Push — master ( ed6a26...4f5e17 )
by Rafael S.
01:43
created

to-bytes.js ➔ writeBytes   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
c 2
b 0
f 0
nc 6
nop 4
dl 0
loc 17
rs 9.2
1
/*
2
 * to-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 intBits = require("int-bits");
0 ignored issues
show
Unused Code introduced by
The constant intBits seems to be never used. Consider removing it.
Loading history...
8
const pad = require("../src/byte-padding.js");
9
const endianness = require("endianness");
10
const writer = require("../src/write-bytes.js");
11
const bitDepths = require("../src/bit-depth.js");
12
13
/**
14
 * Turn numbers and strings to bytes.
15
 * @param {!Array<number>|string} numbers float64 numbers.
16
 * @param {number} bitDepth The desired bitDepth for the data.
17
 * @param {Object} params The params. defaults to:
18
 *   - "float", defaults to false, true for floats.
19
 *       float is available for 16, 32 and 64 bit depths.
20
 *   - "base", defaults to 10, can be 2, 10 or 16
21
 *   - "char", defaults to false, true for strings
22
 *   - "be", defaults to false, true for big endian
23
 * @return {!Array<number>} the bytes.
24
 */
25
function toBytes(numbers, bitDepth, params={}) {
26
    let base = 10;
27
    if ("base" in params) {
28
        base = params.base;
29
    }
30
    let isBigEndian = params.be;
31
    let isChar = params.char;
32
    let isFloat = params.float;
33
    let bytes = writeBytes(numbers, isChar, isFloat, bitDepth);
34
    makeBigEndian(bytes, isBigEndian, bitDepth);
35
    outputToBase(bytes, bitDepth, base);
36
    return bytes;
37
}
38
39
/**
40
 * Turn the output to the correct base.
41
 * @param {!Array<number>} bytes The bytes.
42
 * @param {number} bitDepth The bitDepth of the data.
43
 * @param {number} base The base.
44
 */
45
function outputToBase(bytes, bitDepth, base) {
46
    if (bitDepth == 4) {
47
        bytesToBase(bytes, base, pad.paddingNibble);
48
    } else if (bitDepth == 2) {
49
        bytesToBase(bytes, base, pad.paddingCrumb);
50
    } else if(bitDepth == 1) {
51
        bytesToBase(bytes, base, function(){});
52
    }else {
53
        bytesToBase(bytes, base);
54
    }
55
}
56
57
/**
58
 * Write values as bytes.
59
 * @param {!Array<number>|string} numbers The values.
60
 * @param {boolean} isChar True if it is a string.
61
 * @param {boolean} isFloat True if it is a IEEE floating point number.
62
 * @param {number} bitDepth The bitDepth of the data.
63
 * @return {!Array<number>} the bytes.
64
 */
65
function writeBytes(numbers, isChar, isFloat, bitDepth) {
66
    let bitWriter;
67
    if (isChar) {
68
        bitWriter = writer.writeString;
69
    } else {
70
        bitWriter = writer['write' + bitDepth + 'Bit' + (isFloat ? "Float" : "")];
71
    }
72
    let i = 0;
73
    let j = 0;
74
    let len = numbers.length;
75
    let bytes = [];
76
    while (i < len) {            
77
        j = bitWriter(bytes, numbers, i, j);
78
        i++;
79
    }
80
    return bytes;
81
}
82
83
/**
84
 * Write values as bytes.
85
 * @param {!Array<number>} bytes The values.
86
 * @param {boolean} isBigEndian True if the bytes should be big endian.
87
 * @param {number} bitDepth The bitDepth of the data.
88
 */
89
function makeBigEndian(bytes, isBigEndian, bitDepth) {
90
    if (isBigEndian) {
91
        endianness.endianness(bytes, bitDepths.bitDepthOffsets[bitDepth]);
92
    }
93
}
94
95
/**
96
 * Turn bytes to base.
97
 * @param {!Array<string>|!Array<number>} bytes The bytes.
98
 * @param {number} base The base.
99
 * @param {Function} padFunction The function to use for padding.
100
 */
101
function bytesToBase(bytes, base, padFunction=pad.padding) {
102
    if (base != 10) {
103
        let i = 0;
104
        let len = bytes.length;
105
        while (i < len) {
106
            bytes[i] = bytes[i].toString(base);
107
            padFunction(bytes, base, i);
108
            i++;
109
        }
110
    }
111
}
112
113
module.exports.toBytes = toBytes;
114