lib/types.js
function mapBuilder(map, bufb, codec, width) {
  if (typeof map !== 'object') {
    throw new errors.EncodingError(map, 'Unsure how to encode non-object as map');
  }

  if (Array.isArray(map)) {
    throw new errors.EncodingError(map, 'Unsure how to encode array as map');
  }

  var keys = Object.keys(map);
  if (!width && keys.length === 0) {
    bufb.appendUInt8(0xC1);
    bufb.appendUInt8(1);
    bufb.appendUInt8(0);
    return;
  }

  // Encode all elements into a temp buffer to allow us to front-load appropriate size and count.
  var tempBuilder = new Builder();
  var _len = keys.length;
  for (var _i = 0; _i < _len; ++_i) {
    codec.encode(keys[_i], tempBuilder);
    codec.encode(map[keys[_i]], tempBuilder);
  }
  var tempBuffer = tempBuilder.get();

  // Code, size, length, data
  if (width === 1 || (width !== 4 && tempBuffer.length < 0xFF)) {
    // map8
    if (!width) bufb.appendUInt8(0xC1);
    bufb.appendUInt8(tempBuffer.length + 1);
    bufb.appendUInt8(keys.length * 2);
  } else {
    // map32
    if (!width) bufb.appendUInt8(0xD1);
    bufb.appendUInt32BE(tempBuffer.length + 4);
    bufb.appendUInt32BE(keys.length * 2);
  }

  bufb.appendBuffer(tempBuffer);
}
function mapBuilder(map, bufb, codec, width) {
  if (typeof map !== 'object') {
    throw new errors.EncodingError(map, 'Unsure how to encode non-object as map');
  }

  if (Array.isArray(map)) {
    throw new errors.EncodingError(map, 'Unsure how to encode array as map');
  }

  var keys = Object.keys(map);
  if (!width && keys.length === 0) {
    bufb.appendUInt8(0xC1);
    bufb.appendUInt8(1);
    bufb.appendUInt8(0);
    return;
  }

  // Encode all elements into a temp buffer to allow us to front-load appropriate size and count.
  var tempBuilder = new Builder();
  var _len = keys.length;
  for (var _i = 0; _i < _len; ++_i) {
    codec.encode(keys[_i], tempBuilder);
    codec.encode(map[keys[_i]], tempBuilder);
  }
  var tempBuffer = tempBuilder.get();

  // Code, size, length, data
  if (width === 1 || (width !== 4 && tempBuffer.length < 0xFF)) {
    // map8
    if (!width) bufb.appendUInt8(0xC1);
    bufb.appendUInt8(tempBuffer.length + 1);
    bufb.appendUInt8(keys.length * 2);
  } else {
    // map32
    if (!width) bufb.appendUInt8(0xD1);
    bufb.appendUInt32BE(tempBuffer.length + 4);
    bufb.appendUInt32BE(keys.length * 2);
  }

  bufb.appendBuffer(tempBuffer);
}
function mapBuilder ( map , bufb , codec , width ) { if ( typeof map !== 'object' ) { throw new errors . EncodingError ( map , 'Unsure how to encode non-object as map' ) ; } if ( Array . isArray ( map ) ) { throw new errors . EncodingError ( map , 'Unsure how to encode array as map' ) ; } var keys = Object . keys ( map ) ; if ( ! width && keys . length === 0 ) { bufb . appendUInt8 ( 0xC1 ) ; bufb . appendUInt8 ( 1 ) ; bufb . appendUInt8 ( 0 ) ; return ; } // Encode all elements into a temp buffer to allow us to front-load appropriate size and count. var tempBuilder = new Builder ( ) ; var _len = keys . length ; for ( var _i = 0 ; _i < _len ; ++ _i ) { codec . encode ( keys [ _i ] , tempBuilder ) ; codec . encode ( map [ keys [ _i ] ] , tempBuilder ) ; } var tempBuffer = tempBuilder . get ( ) ; // Code, size, length, data if ( width === 1 || ( width !== 4 && tempBuffer . length < 0xFF ) ) { // map8 if ( ! width ) bufb . appendUInt8 ( 0xC1 ) ; bufb . appendUInt8 ( tempBuffer . length + 1 ) ; bufb . appendUInt8 ( keys . length * 2 ) ; } else { // map32 if ( ! width ) bufb . appendUInt8 ( 0xD1 ) ; bufb . appendUInt32BE ( tempBuffer . length + 4 ) ; bufb . appendUInt32BE ( keys . length * 2 ) ; } bufb . appendBuffer ( tempBuffer ) ; }
A map is encoded as a compound value where the constituent elements form alternating key value pairs.

<pre>
item 0   item 1      item n-1    item n
+-------+-------+----+---------+---------+
| key 1 | val 1 | .. | key n/2 | val n/2 |
+-------+-------+----+---------+---------+
</pre>

Map encodings must contain an even number of items (i.e. an equal number of keys and
values). A map in which there exist two identical key values is invalid. Unless known to
be otherwise, maps must be considered to be ordered - that is the order of the key-value
pairs is semantically important and two maps which are different only in the order in
which their key-value pairs are encoded are not equal.

@param {Object} val          Value to encode.
@param {Builder} bufb        Buffer-builder to encode map into.
@param {Codec} codec         Codec to use for encoding keys and values.
@param {Number} [width]      Should be 1 or 4.  If given, encoder assumes code already written,
and will ensure array is encoded to the given byte-width type. Useful for arrays.
@private
A map is encoded as a compound value where the constituent elements form alternating key value pairs .