# 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
    28x
    1123x
    1123x
    1269x
    1269x
    1269x

    1269x
    1123x
    1123x

    146x

    1123x

    28x
    13455x
    13455x
    13525x
    13525x
    13525x
    13455x
    13455x

    70x

    13455x

     // @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;
    };