# All files / src/utils leb128.js

100% Statements 21/21
100% Branches 8/8
100% Functions 2/2
100% Lines 21/21

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    24x
    864x
    864x
    1047x
    1047x
    1047x

    1047x
    864x
    864x

    183x

    864x

    24x
    5780x
    5780x
    5828x
    5828x
    5828x
    5780x
    5780x

    48x

    5780x

     // @flow
    export const encodeSigned = (value: number) => {
      const encoding = [];
      while (true) {
        const byte = value & 127;
        value = value >> 7;
        const signbit = byte & 0x40;

        if ((value === 0 && !signbit) || (value === -1 && signbit)) {
          encoding.push(byte);
          break;
        } else {
          encoding.push(byte | 0x80);
        }
      }
      return encoding;
    };

    export const encodeUnsigned = (value: number) => {
      const encoding = [];
      while (true) {
        const i = value & 127;
        value = value >>> 7;
        if (value === 0) {
          encoding.push(i);
          break;
        }

        encoding.push(i | 0x80);
      }

      return encoding;
    };