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.180 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.179 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.178 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.177 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.176 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.175 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.174 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.173 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.172 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.171 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.170 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.169 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.167 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.166 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.165 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.164 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.163 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.162 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.161 tracks/javascript/exercises/variable-length-quantity/example.js
trackler-2.2.1.160 tracks/javascript/exercises/variable-length-quantity/example.js