Passed
Push — master ( 56251b...c677ca )
by Rafael S.
02:02
created

number-buffer.js ➔ getFPParser_   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2017-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 Sserialize and deserialize integers and floats.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
import {validateFloatType, validateIntType} from './validation.js';
31
import {IEEE754Buffer} from 'ieee754-buffer';
32
import TwosComplementBuffer from 'twos-complement-buffer';
33
import UintBuffer from 'uint-buffer';
34
35
/**
36
 * A class to pack and unpack integers and floating-point numbers.
37
 * Signed integers are two's complement.
38
 * Floating-point numbers are IEEE 754 standard.
39
 */
40
export default class NumberBuffer {
41
  
42
  /**
43
   * Read one number from a byte buffer.
44
   * @param {number} bits The number of bits of the number.
45
   * @param {boolean} fp Tue for floating-point numbers.
46
   * @param {boolean} signed True for signed numbers.
47
   * @throws {Error} If the type definition is not valid.
48
   */
49
  constructor(bits, fp, signed) {
50
    /** @type {TwosComplementBuffer|UintBuffer|IEEE754Buffer} */
51
    this.parser = null;
52
    if (fp) {
53
      validateFloatType(bits);
54
      this.parser = this.getFPParser_(bits);
55
    } else {
56
      validateIntType(bits);
57
      this.parser = signed ?
58
        new TwosComplementBuffer(bits) : new UintBuffer(bits);
59
      this.parser.bytes = this.parser.bytes === 8 ? 4 : this.parser.bytes;
60
    }
61
  }
62
63
  /**
64
   * Read one number from a byte buffer.
65
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
66
   * @param {number=} index The index to read.
67
   * @return {number} The number.
68
   * @throws {Error} On overflow.
69
   */
70
  unpack(buffer, index=0) {
71
    return this.parser.unpack(buffer, index);
72
  }
73
74
  /**
75
   * Write one number to a byte buffer.
76
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
77
   * @param {number} num The number.
78
   * @param {number=} index The index being written in the byte buffer.
79
   * @return {number} The next index to write on the byte buffer.
80
   * @throws {Error} If num is NaN.
81
   * @throws {Error} On overflow.
82
   */
83
  pack(buffer, num, index=0) {
84
    return this.parser.pack(buffer, num, index);
85
  }
86
87
  /**
88
   * Return a instance of IEEE754Buffer.
89
   * @param {number} bits The number of bits.
90
   * @return {IEEE754Buffer}
91
   * @private
92
   */
93
  getFPParser_(bits) {
94
    if (bits === 16) {
95
      return new IEEE754Buffer(5, 11);
96
    } else if(bits === 32) {
97
      return new IEEE754Buffer(8, 23);
98
    } else {
99
      return new IEEE754Buffer(11, 52);
100
    }
101
  }
102
}
103