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 867x 867x 1080x 1080x 1080x   1080x 867x 867x   213x     867x     24x 6298x 6298x 6345x 6345x 6345x 6298x 6298x     47x     6298x    
// @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;
};