All files / str ipToHex.js

100% Statements 7/7
75% Branches 3/4
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                      2x 8x 8x 8x 4x   8x       1x  
/**
 * 十进制IP地址转十六进制
 * @method str/ipToHex
 * @param {String} ip 十进制数字的IPV4地址
 * @return {String} 16进制数字IPV4地址
 * @example
 * var $ipToHex = require('@spore-ui/kit/packages/str/ipToHex');
 * $ipToHex('255.255.255.255'); //return 'ffffffff'
 */
 
function ipToHex(ip) {
  return ip.replace(/(\d+)\.*/g, function (match, num) {
    num = parseInt(num, 10) || 0;
    num = num.toString(16);
    if (num.length < 2) {
      num = '0' + num;
    }
    return num;
  });
}
 
module.exports = ipToHex;