All files / str ipToHex.ts

100% Statements 8/8
50% Branches 1/2
100% Functions 2/2
100% Lines 8/8

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                    1x 2x 8x 8x 8x   4x   8x       1x  
/**
 * 十进制IP地址转十六进制
 * @method ipToHex
 * @param {String} ip 十进制数字的IPV4地址
 * @return {String} 16进制数字IPV4地址
 * @example
 * import { ipToHex } from '@spore-ui/tskit';
 * ipToHex('255.255.255.255'); //return 'ffffffff'
 */
 
export function ipToHex(ip: string): string {
  return ip.replace(/(\d+)\.*/g, (match, digit) => {
    let num = digit;
    num = parseInt(num, 10) || 0;
    num = num.toString(16);
    if (num.length < 2) {
      num = `0${num}`;
    }
    return num;
  });
}
 
export default ipToHex;