Passed
Push — master ( ad5e1b...55188c )
by Rafael S.
01:26
created

index.js ➔ unpack   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
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
14
/**
15
 * Find and return the start index of some string.
16
 * Return -1 if the string is not found.
17
 * @param {!Array<number>|Uint8Array} bytes Array of bytes.
18
 * @param {string} chunk Some string to look for.
19
 * @return {number} The start index of the first occurrence, -1 if not found
20
 */
21
function findString(bytes, chunk) {
22
    let found = "";
23
    for (let i = 0; i < bytes.length; i++) {
24
        found = fromBytes.fromBytes(bytes.slice(i, i + chunk.length),
25
            8, {"char": true});
26
        if (found == chunk) {
27
            return i;
28
        }
29
    }
30
    return -1;
31
}
32
33
/**
34
 * Turn a number or string into a byte buffer.
35
 * @param {number|string} value The value.
36
 * @param {Object} type One of the available types.
37
 * @param {number} base The base of the input. Optional. Default is 10.
38
 * @return {!Array<number>|!Array<string>}
39
 */
40
function pack(value, type, base=10) {
41
    type.base = base;
42
    return toBytes.toBytes(value, type.bitDepth, type);
43
}
44
45
/**
46
 * Turn a byte buffer into a readable value.
47
 * @param {!Array<number>|Uint8Array} buffer An array of bytes.
48
 * @param {Object} type One of the available types.
49
 * @param {number} base The base of the input. Optional. Default is 10.
50
 * @return {number|string}
51
 */
52
function unpack(buffer, type, base=10) {
53
    type.base = base;
54
    return fromBytes.fromBytes(buffer, type.bitDepth, type);
55
}
56
57
/**
58
 * Turn a sequence of numbers into a byte buffer.
59
 * @param {!Array<number>} values The value.
60
 * @param {Object} type One of the available types.
61
 * @param {number} base The base of the input. Optional. Default is 10.
62
 * @return {!Array<number>|!Array<string>}
63
 */
64
function packSequence(values, type, base=10) {
65
    type.base = base;
66
    type.single = false;
67
    return toBytes.toBytes(values, type.bitDepth, type);
68
}
69
70
/**
71
 * Turn a byte buffer into a sequence of readable values.
72
 * @param {!Array<number>|Uint8Array} buffer An array of bytes.
73
 * @param {Object} type One of the available types.
74
 * @param {number} base The base of the input. Optional. Default is 10.
75
 * @return {!Array<number>|string}
76
 */
77
function unpackSequence(buffer, type, base=10) {
78
    type.base = base;
79
    type.single = false;
80
    return fromBytes.fromBytes(buffer, type.bitDepth, type);
81
}
82
83
// interface
84
module.exports.pack = pack;
85
module.exports.unpack = unpack;
86
module.exports.packSequence = packSequence;
87
module.exports.unpackSequence = unpackSequence;
88
89
// types
90
module.exports.int8 = {"bitDepth": 8, "signed": true, "single": true};
91
module.exports.uInt8 = {"bitDepth": 8, "single": true};
92
93
module.exports.int16  = {"bitDepth": 16, "signed": true, "single": true};
94
module.exports.uInt16 = {"bitDepth": 16, "single": true};
95
module.exports.float16 = {"bitDepth": 16, "float": true, "single": true};
96
97
module.exports.int24 = {"bitDepth": 24, "signed": true, "single": true};
98
module.exports.uInt24 = {"bitDepth": 24, "single": true};
99
100
module.exports.int32 = {"bitDepth": 32, "signed": true, "single": true};
101
module.exports.uInt32 = {"bitDepth": 32, "single": true};
102
module.exports.float32 = {"bitDepth": 32, "float": true, "single": true};
103
104
module.exports.int40 = {"bitDepth": 40, "signed": true, "single": true};
105
module.exports.uInt40 = {"bitDepth": 40, "single": true};
106
107
module.exports.int48 = {"bitDepth": 48, "signed": true, "single": true};
108
module.exports.uInt48 = {"bitDepth": 48, "single": true};
109
110
module.exports.float64 = {"bitDepth": 64, "float": true, "single": true};
111
112
113
// Legacy types
114
module.exports.floatLE = {"float": true, "single": true};
115
module.exports.intLE = {"signed": true, "single": true};
116
module.exports.uIntLE = {"single": true};
117
module.exports.floatBE = {"float": true, "single": true, "be": true};
118
module.exports.intBE = {"signed": true, "single": true, "be": true};
119
module.exports.uIntBE = {"single": true, "be": true};
120
module.exports.char = {"char": true, "single": true};
121
122
module.exports.floatArrayLE = {"float": true};
123
module.exports.intArrayLE = {"signed": true};
124
module.exports.uIntArrayLE = {"base": 10};
125
module.exports.floatArrayBE = {"float": true, "be": true};
126
module.exports.intArrayBE = {"signed": true, "be": true};
127
module.exports.uIntArrayBE = {"be": true};
128
module.exports.str = {"char": true};
129
130
// Legacy interface
131
module.exports.findString = findString;
132
module.exports.toBytes = toBytes.toBytes;
133
module.exports.fromBytes = fromBytes.fromBytes;
134
module.exports.packBooleans = bitPacker.packBooleans;
135
module.exports.unpackBooleans = bitPacker.unpackBooleans;
136
module.exports.packCrumbs = bitPacker.packCrumbs;
137
module.exports.unpackCrumbs = bitPacker.unpackCrumbs;
138
module.exports.packNibbles = bitPacker.packNibbles;
139
module.exports.unpackNibbles = bitPacker.unpackNibbles;
140
module.exports.BitDepthOffsets = bitDepth.BitDepthOffsets;
141
module.exports.BitDepthMaxValues = bitDepth.BitDepthMaxValues;
142