Passed
Push — master ( 1b2938...d40a74 )
by Rafael S.
01:33
created

index.js   A

Complexity

Total Complexity 4
Complexity/F 2

Size

Lines of Code 31
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
nc 1
dl 0
loc 31
rs 10
c 2
b 0
f 0
wmc 4
mnd 1
bc 4
fnc 2
bpm 2
cpm 2
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A ➔ swap 0 13 2
1
/*!
2
 * endianness
3
 * Swap endianness in byte arrays.
4
 * Copyright (c) 2017 Rafael da Silva Rocha.
5
 * https://github.com/rochars/endianness
6
 *
7
 */
8
9
/**
10
 * Swap the endianness of units of information in a byte array.
11
 * The original array is modified in-place.
12
 * @param {!Array<number>|!Array<string>|Uint8Array} bytes The bytes.
13
 * @param {number} offset The number of bytes of each unit of information.
14
 */
15
function endianness(bytes, offset) {
16
    let len = bytes.length;
17
    let i = 0;
18
    while (i < len) {
19
        swap(bytes, offset, i);
20
        i += offset;
21
    }
22
}
23
24
/**
25
 * Swap the endianness of a unit of information in a byte array.
26
 * The original array is modified in-place.
27
 * @param {!Array<number>|!Array<string>|Uint8Array} bytes The bytes.
28
 * @param {number} offset The number of bytes of the unit of information.
29
 * @param {number} index The start index of the unit of information.
30
 */
31
function swap(bytes, offset, index) {
32
    let x = 0;
33
    let y = offset - 1;
34
    let limit = parseInt(offset / 2, 10);
35
    let swap;
0 ignored issues
show
Unused Code introduced by
The variable swap seems to be never used. Consider removing it.
Loading history...
36
    while(x < limit) {
37
        swap = bytes[index + x];
0 ignored issues
show
Comprehensibility introduced by
It seems like you are trying to overwrite a function name here. swap is already defined in line 31 as a function. While this will work, it can be very confusing.
Loading history...
38
        bytes[index + x] = bytes[index + y];
39
        bytes[index + y] = swap;
40
        x++;
41
        y--;
42
    }
43
}
44
45
module.exports = endianness;
46