mercredi 29 août 2018

How to design a elegant IP check API

In my project, there are a lot of requirements about IP check. Because we write many functions for network devices. In some case, we don't need loopback IP and broadcast IP, and in some other case, it's valid.

So I design some basic APIs:

function isLoopback(ip){
}

function isMulticast(ip){
}

function isBroadcast(ip, mask){
}

//etc

Now, Developers have to call like this:

isIpv4(ip) && !isLoopback(ip) && !isBroadcast(ip, mask) && !isMulticast(ip)

Is it possible to design a more elegant API for potential developers?

Constant = {
  BROADCAST : 0x0001,
  MULTICAST : 0x0002,
  LOOPBACK  : 0x0004,
  ANY     : 0x0008,//0.0.0.0
};

/**
checkIp('10.180.0.1', 16, Constant.BROADCAST|Constant.MULTICAST|Constant.LOOPBACK|Constant.ANY)
*/
function checkIp(ip, mask, excludes);

I use Extjs to do this, which is a JavaScript framework. Any good ideas?

Aucun commentaire:

Enregistrer un commentaire