All files / util abToHex.js

100% Statements 7/7
100% Branches 2/2
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                              2x 1x   1x 1x 2x   1x     1x  
/**
 * ArrayBuffer转16进制字符串
 * @method util/abToHex
 * @param {ArrayBuffer} buffer 需要转换的 ArrayBuffer
 * @returns {String} 16进制字符串
 * @example
 * var $abToHex = require('@spore-ui/kit/packages/util/abToHex');
 * var ab = new ArrayBuffer(2);
 * var dv = new DataView(ab);
 * dv.setUint8(0, 171);
 * dv.setUint8(1, 205);
 * $abToHex(ab); // => 'abcd'
 */
 
function abToHex(buffer) {
  if (Object.prototype.toString.call(buffer) !== '[object ArrayBuffer]') {
    return '';
  }
  var u8arr = new Uint8Array(buffer);
  var fn = function (bit) {
    return ('00' + bit.toString(16)).slice(-2);
  };
  return Array.prototype.map.call(u8arr, fn).join('');
}
 
module.exports = abToHex;