Passed
Push — master ( 0d02ce...fc63cf )
by Rafael S.
01:35
created

index.js   A

Complexity

Total Complexity 13
Complexity/F 1.86

Size

Lines of Code 185
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 2 Features 0
Metric Value
cc 0
nc 1
dl 0
loc 185
rs 10
c 11
b 2
f 0
wmc 13
mnd 2
bc 11
fnc 7
bpm 1.5713
cpm 1.8571
noi 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A ➔ unpackStruct 0 14 3
A ➔ pack 0 5 2
A ➔ packArray 0 4 1
A ➔ unpack 0 4 1
A ➔ packStruct 0 7 2
A ➔ findString 0 12 3
A ➔ unpackArray 0 4 1
1
/*!
2
 * byte-data
3
 * Readable data to and from byte buffers.
4
 * Copyright (c) 2017 Rafael da Silva Rocha.
5
 * https://github.com/rochars/byte-data
6
 *
7
 */
8
9
let toBytes = require("./src/to-bytes");
10
let fromBytes = require("./src/from-bytes");
11
let bitPacker = require("./src/bit-packer");
12
let bitDepth = require("./src/bit-depth");
13
let helpers = require("./src/helpers");
14
15
/**
16
 * Turn a number or string into a byte buffer.
17
 * @param {number|string} value The value.
18
 * @param {Object} type One of the available types.
19
 * @param {number} base The base of the output. Optional. Default is 10.
20
 * @return {!Array<number>|!Array<string>}
21
 */
22
function pack(value, type, base=10) {
23
    let theType = helpers.getType(type, base, true);
24
    value = theType.char ? value[0] : value;
25
    return toBytes.toBytes(helpers.turnToArray(value), theType.bits, theType);
26
}
27
28
/**
29
 * Turn a byte buffer into a readable value.
30
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes.
31
 * @param {Object} type One of the available types.
32
 * @param {number} base The base of the input. Optional. Default is 10.
33
 * @return {number|string}
34
 */
35
function unpack(buffer, type, base=10) {
36
    let theType = helpers.getType(type, base, true);
37
    return fromBytes.fromBytes(buffer, theType.bits, theType);
38
}
39
40
/**
41
 * Turn a array of numbers into a byte buffer.
42
 * @param {!Array<number>|string} values The values.
43
 * @param {Object} type One of the available types.
44
 * @param {number} base The base of the output. Optional. Default is 10.
45
 * @return {!Array<number>|!Array<string>}
46
 */
47
function packArray(values, type, base=10) {
48
    let theType = helpers.getType(type, base, false);
49
    return toBytes.toBytes(values, theType.bits, theType);
50
}
51
52
/**
53
 * Turn a byte array into a sequence of readable values.
54
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte array.
55
 * @param {Object} type One of the available types.
56
 * @param {number} base The base of the input. Optional. Default is 10.
57
 * @return {!Array<number>|string}
58
 */
59
function unpackArray(buffer, type, base=10) {
60
    let theType = helpers.getType(type, base, false);
61
    return fromBytes.fromBytes(buffer, theType.bits, theType);
62
}
63
64
/**
65
 * Find and return the start index of some string.
66
 * Return -1 if the string is not found.
67
 * @param {!Array<number>|Uint8Array} bytes Array of bytes.
68
 * @param {string} text Some string to look for.
69
 * @return {number} The start index of the first occurrence, -1 if not found
70
 */
71
function findString(bytes, text) {
72
    let found = "";
73
    for (let i = 0; i < bytes.length; i++) {
74
        found = fromBytes.fromBytes(
75
            bytes.slice(i, i + text.length),
76
            8, {"bits": 8, "char": true, "single": false});
77
        if (found == text) {
78
            return i;
79
        }
80
    }
81
    return -1;
82
}
83
84
/**
85
 * Turn a struct into a byte buffer.
86
 * A struct is an array of values of not necessarily the same type.
87
 * @param {Array} struct The struct values.
88
 * @param {Array} def The struct type definition.
89
 * @param {number} base The base of the output. Optional. Default is 10.
90
 * @return {!Array<number>|!Array<string>}
91
 */
92
function packStruct(struct, def, base=10) {
93
    let bytes = [];
94
    for (let i = 0; i < struct.length; i++) {
95
        bytes = bytes.concat(pack(struct[i], def[i], base));
96
    }
97
    return bytes;
98
}
99
100
/**
101
 * Turn a byte buffer into a structure.
102
 * A struct is an array of values of not necessarily the same type.
103
 * @param {Array} buffer The byte buffer.
104
 * @param {Array} def The struct type definition.
105
 * @param {number} base The base of the input. Optional. Default is 10.
106
 * @return {!Array<number>|!Array<string>}
107
 */
108
function unpackStruct(buffer, def, base=10) {
109
    let struct = [];
110
    let i = 0;
111
    let j = 0;
112
    while (j < buffer.length) {
113
        let bits = def[i].bits < 8 ? 1 : def[i].bits / 8;
114
        struct = struct.concat(
115
                unpack(buffer.slice(j, j + bits), def[i], base)
116
            );
117
        j += bits;
118
        i++;
119
    }
120
    return struct;
121
}
122
123
// interface
124
module.exports.pack = pack;
125
module.exports.unpack = unpack;
126
module.exports.packArray = packArray;
127
module.exports.unpackArray = unpackArray;
128
module.exports.unpackStruct = unpackStruct;
129
module.exports.packStruct = packStruct;
130
131
// types: LE
132
module.exports.chr = {"bits": 8, "char": true, "single": true};
133
module.exports.fourCC = {"bits": 32, "char": true, "single": true};
134
module.exports.bool = {"bits": 1, "single": true};
135
module.exports.int2 = {"bits": 2, "signed": true, "single": true};
136
module.exports.uInt2 = {"bits": 2, "single": true};
137
module.exports.int4 = {"bits": 4, "signed": true, "single": true};
138
module.exports.uInt4 = {"bits": 4, "single": true};
139
module.exports.int8 = {"bits": 8, "signed": true, "single": true};
140
module.exports.uInt8 = {"bits": 8, "single": true};
141
module.exports.int16  = {"bits": 16, "signed": true, "single": true};
142
module.exports.uInt16 = {"bits": 16, "single": true};
143
module.exports.float16 = {"bits": 16, "float": true, "single": true};
144
module.exports.int24 = {"bits": 24, "signed": true, "single": true};
145
module.exports.uInt24 = {"bits": 24, "single": true};
146
module.exports.int32 = {"bits": 32, "signed": true, "single": true};
147
module.exports.uInt32 = {"bits": 32, "single": true};
148
module.exports.float32 = {"bits": 32, "float": true, "single": true};
149
module.exports.int40 = {"bits": 40, "signed": true, "single": true};
150
module.exports.uInt40 = {"bits": 40, "single": true};
151
module.exports.int48 = {"bits": 48, "signed": true, "single": true};
152
module.exports.uInt48 = {"bits": 48, "single": true};
153
module.exports.float64 = {"bits": 64, "float": true, "single": true};
154
155
// types: BE
156
module.exports.int16BE  = {
157
    "bits": 16, "signed": true, "single": true, "be": true};
158
module.exports.uInt16BE = {
159
    "bits": 16, "single": true, "be": true};
160
module.exports.float16BE = {
161
    "bits": 16, "float": true, "single": true, "be": true};
162
module.exports.int24BE = {
163
    "bits": 24, "signed": true, "single": true, "be": true};
164
module.exports.uInt24BE = {
165
    "bits": 24, "single": true, "be": true};
166
module.exports.int32BE = {
167
    "bits": 32, "signed": true, "single": true, "be": true};
168
module.exports.uInt32BE = {
169
    "bits": 32, "single": true, "be": true};
170
module.exports.float32BE = {
171
    "bits": 32, "float": true, "single": true, "be": true};
172
module.exports.int40BE = {
173
    "bits": 40, "signed": true, "single": true, "be": true};
174
module.exports.uInt40BE = {
175
    "bits": 40, "single": true, "be": true};
176
module.exports.int48BE = {
177
    "bits": 48, "signed": true, "single": true, "be": true};
178
module.exports.uInt48BE = {
179
    "bits": 48, "single": true, "be": true};
180
module.exports.float64BE = {
181
    "bits": 64, "float": true, "single": true, "be": true};
182
183
module.exports.findString = findString;
184
module.exports.toBytes = toBytes.toBytes;
185
module.exports.fromBytes = fromBytes.fromBytes;
186
module.exports.packBooleans = bitPacker.packBooleans;
187
module.exports.unpackBooleans = bitPacker.unpackBooleans;
188
module.exports.packCrumbs = bitPacker.packCrumbs;
189
module.exports.unpackCrumbs = bitPacker.unpackCrumbs;
190
module.exports.packNibbles = bitPacker.packNibbles;
191
module.exports.unpackNibbles = bitPacker.unpackNibbles;
192
module.exports.BitDepthOffsets = bitDepth.BitDepthOffsets;
193
module.exports.BitDepthMaxValues = bitDepth.BitDepthMaxValues;
194