Passed
Branch v15.x (8faa65)
by Rafael S.
02:13
created

lib/int-buffer.js   A

Complexity

Total Complexity 5
Complexity/F 1.67

Size

Lines of Code 42
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
eloc 10
nc 1
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 5
mnd 1
bc 3
fnc 3
bpm 1
cpm 1.6666
noi 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A int-buffer.js ➔ pack 0 3 1
A int-buffer.js ➔ unpack 0 3 1
A int-buffer.js ➔ constructor 0 5 2
1
/*
2
 * Copyright (c) 2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview Pack and unpack two's complement ints and unsigned ints.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
import TwosComplementBuffer from 'twos-complement-buffer';
31
import UintBuffer from 'uint-buffer';
32
33
/**
34
 * A class to write and read two's complement ints and unsigned ints
35
 * to and from byte buffers.
36
 */
37
export default class IntBuffer {
38
  
39
  /**
40
   * @param {number} bits The number of bits used by the integer.
41
   */
42
  constructor(theType) {
43
    /** @type {TwosComplementBuffer|UintBuffer} */
44
    this.parser = theType.signed ?
45
      new TwosComplementBuffer(theType.bits) : new UintBuffer(theType.bits);
46
  }
47
48
  /**
49
   * Write one unsigned integer to a byte buffer.
50
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
51
   * @param {number} num The number.
52
   * @param {number=} index The index being written in the byte buffer.
53
   * @return {number} The next index to write on the byte buffer.
54
   * @throws {Error} If num is NaN.
55
   * @throws {Error} On overflow.
56
   */
57
  pack(buffer, num, index=0) {
58
    return this.parser.pack(buffer, num, index);
59
  }
60
61
  /**
62
   * Read one unsigned integer from a byte buffer.
63
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
64
   * @param {number=} index The index to read.
65
   * @return {number} The number.
66
   * @throws {Error} On overflow.
67
   */
68
  unpack(buffer, index=0) {
69
    return this.parser.unpack(buffer, index);
70
  }
71
}
72