common.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isInSubnet = isInSubnet;
  4. exports.isCorrect = isCorrect;
  5. exports.numberToPaddedHex = numberToPaddedHex;
  6. exports.stringToPaddedHex = stringToPaddedHex;
  7. exports.testBit = testBit;
  8. function isInSubnet(address) {
  9. if (this.subnetMask < address.subnetMask) {
  10. return false;
  11. }
  12. if (this.mask(address.subnetMask) === address.mask()) {
  13. return true;
  14. }
  15. return false;
  16. }
  17. function isCorrect(defaultBits) {
  18. return function () {
  19. if (this.addressMinusSuffix !== this.correctForm()) {
  20. return false;
  21. }
  22. if (this.subnetMask === defaultBits && !this.parsedSubnet) {
  23. return true;
  24. }
  25. return this.parsedSubnet === String(this.subnetMask);
  26. };
  27. }
  28. function numberToPaddedHex(number) {
  29. return number.toString(16).padStart(2, '0');
  30. }
  31. function stringToPaddedHex(numberString) {
  32. return numberToPaddedHex(parseInt(numberString, 10));
  33. }
  34. /**
  35. * @param binaryValue Binary representation of a value (e.g. `10`)
  36. * @param position Byte position, where 0 is the least significant bit
  37. */
  38. function testBit(binaryValue, position) {
  39. const { length } = binaryValue;
  40. if (position > length) {
  41. return false;
  42. }
  43. const positionInString = length - position;
  44. return binaryValue.substring(positionInString, positionInString + 1) === '1';
  45. }
  46. //# sourceMappingURL=common.js.map