Passed
Push — master ( 2cddad...eee927 )
by Rafael S.
01:38
created

index.js ➔ getSingleType   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 9.4285
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
    let theType = getSingleType(type, base);
42
    value = theType.char ? value[0] : value;
43
    return toBytes.toBytes(value, theType.bits, theType);
44
}
45
46
/**
47
 * Turn a byte buffer into a readable value.
48
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes.
49
 * @param {Object} type One of the available types.
50
 * @param {number} base The base of the input. Optional. Default is 10.
51
 * @return {number|string}
52
 */
53
function unpack(buffer, type, base=10) {
54
    let theType = getSingleType(type, base);
55
    return fromBytes.fromBytes(buffer, theType.bits, theType);
56
}
57
58
/**
59
 * Turn a array of numbers into a byte buffer.
60
 * @param {!Array<number>} values The values.
61
 * @param {Object} type One of the available types.
62
 * @param {number} base The base of the input. Optional. Default is 10.
63
 * @return {!Array<number>|!Array<string>}
64
 */
65
function packArray(values, type, base=10) {
66
    let theType = getArrayType(type, base);
67
    return toBytes.toBytes(values, theType.bits, theType);
68
}
69
70
/**
71
 * Turn a byte array into a sequence of readable values.
72
 * @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte array.
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 unpackArray(buffer, type, base=10) {
78
    let theType = getArrayType(type, base);
79
    return fromBytes.fromBytes(buffer, theType.bits, theType);
80
}
81
82
/**
83
 * Make the type a single value type on the specified base.
84
 * @param {Object} type One of the available types.
85
 * @param {number} base The base of the input.
86
 * @return {Object}
87
 */
88
function getSingleType(type, base) {
89
    let theType = Object.assign({}, type);
90
    theType.base = base;
91
    theType.single = true;
92
    return theType;
93
}
94
95
/**
96
 * Make the type a array with the specified base.
97
 * @param {Object} type One of the available types.
98
 * @param {number} base The base of the input.
99
 * @return {Object}
100
 */
101
function getArrayType(type, base) {
102
    let theType = Object.assign({}, type);
103
    theType.base = base;
104
    theType.single = false;
105
    return theType;
106
}
107
108
// interface
109
module.exports.pack = pack;
110
module.exports.unpack = unpack;
111
module.exports.packArray = packArray;
112
module.exports.unpackArray = unpackArray;
113
114
// types
115
module.exports.chr = {"bits": 8, "char": true, "single": true};
116
module.exports.bool = {"bits": 1, "single": true};
117
module.exports.int2 = {"bits": 2, "signed": true, "single": true};
118
module.exports.uInt2 = {"bits": 2, "single": true};
119
module.exports.int4 = {"bits": 4, "signed": true, "single": true};
120
module.exports.uInt4 = {"bits": 4, "single": true};
121
module.exports.int8 = {"bits": 8, "signed": true, "single": true};
122
module.exports.uInt8 = {"bits": 8, "single": true};
123
module.exports.int16  = {"bits": 16, "signed": true, "single": true};
124
module.exports.uInt16 = {"bits": 16, "single": true};
125
module.exports.float16 = {"bits": 16, "float": true, "single": true};
126
module.exports.int24 = {"bits": 24, "signed": true, "single": true};
127
module.exports.uInt24 = {"bits": 24, "single": true};
128
module.exports.int32 = {"bits": 32, "signed": true, "single": true};
129
module.exports.uInt32 = {"bits": 32, "single": true};
130
module.exports.float32 = {"bits": 32, "float": true, "single": true};
131
module.exports.int40 = {"bits": 40, "signed": true, "single": true};
132
module.exports.uInt40 = {"bits": 40, "single": true};
133
module.exports.int48 = {"bits": 48, "signed": true, "single": true};
134
module.exports.uInt48 = {"bits": 48, "single": true};
135
module.exports.float64 = {"bits": 64, "float": true, "single": true};
136
137
module.exports.findString = findString;
138
module.exports.toBytes = toBytes.toBytes;
139
module.exports.fromBytes = fromBytes.fromBytes;
140
module.exports.packBooleans = bitPacker.packBooleans;
141
module.exports.unpackBooleans = bitPacker.unpackBooleans;
142
module.exports.packCrumbs = bitPacker.packCrumbs;
143
module.exports.unpackCrumbs = bitPacker.unpackCrumbs;
144
module.exports.packNibbles = bitPacker.packNibbles;
145
module.exports.unpackNibbles = bitPacker.unpackNibbles;
146
module.exports.BitDepthOffsets = bitDepth.BitDepthOffsets;
147
module.exports.BitDepthMaxValues = bitDepth.BitDepthMaxValues;
148