Sha256: 3532058e8fff5f323f8f1cee6a14af2151ae9552757a2b81e861ce5e700fc0f5

Contents?: true

Size: 1.09 KB

Versions: 68

Compression:

Stored size: 1.09 KB

Contents

var LENGTH = 7;
var CONT_BITS = 1 << LENGTH;
var DATA_BITS = CONT_BITS - 1;

var encodeOne = function (val) {
  var buf = [];
  var left = val;

  while (left) {
    var bits = left & DATA_BITS | CONT_BITS; // set continuation everywhere
    left = left >>> LENGTH;
    buf.push(bits);
  }
  buf[0] = buf[0] & DATA_BITS; // cancel the last continuation
  return buf.reverse();
};

var decodeOne = function (buf) {
  var val = 0;

  for (var i = 0; i < buf.length; i++) {
    val = val << LENGTH | buf[i] & DATA_BITS;
  }
  return val >>> 0; // convert to unsigned 32-bit
};

module.exports = {
  encode: function encode(data) {
    var buf = [];

    for (var i = 0; i < data.length; i++) {
      buf = buf.concat(encodeOne(data[i]));
    }
    return buf;
  },
  decode: function decode(data) {
    var start = 0;
    var vals = [];

    for (var i = 0; i < data.length; i++) {
      if (~data[i] & CONT_BITS) {
        vals.push(decodeOne(data.slice(start, i + 1)));
        start = i + 1;
      }
    }
    if (start < data.length) {
      throw new Error('Incomplete sequence');
    }
    return vals;
  }
};

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.158 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.157 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.156 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.155 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.154 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.153 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.152 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.151 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.150 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.149 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.148 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.147 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.146 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.145 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.144 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.143 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.142 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.141 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.140 tracks/javascript/exercises/variable-length-quantity/example.js