source: node_modules/ip-address/src/common.ts@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 1.3 KB
Line 
1import { Address4 } from './ipv4';
2import { Address6 } from './ipv6';
3
4export interface ReverseFormOptions {
5 omitSuffix?: boolean;
6}
7
8export function isInSubnet(this: Address4 | Address6, address: Address4 | Address6) {
9 if (this.subnetMask < address.subnetMask) {
10 return false;
11 }
12
13 if (this.mask(address.subnetMask) === address.mask()) {
14 return true;
15 }
16
17 return false;
18}
19
20export function isCorrect(defaultBits: number) {
21 return function (this: Address4 | Address6) {
22 if (this.addressMinusSuffix !== this.correctForm()) {
23 return false;
24 }
25
26 if (this.subnetMask === defaultBits && !this.parsedSubnet) {
27 return true;
28 }
29
30 return this.parsedSubnet === String(this.subnetMask);
31 };
32}
33
34export function numberToPaddedHex(number: number) {
35 return number.toString(16).padStart(2, '0');
36}
37
38export function stringToPaddedHex(numberString: string) {
39 return numberToPaddedHex(parseInt(numberString, 10));
40}
41
42/**
43 * @param binaryValue Binary representation of a value (e.g. `10`)
44 * @param position Byte position, where 0 is the least significant bit
45 */
46export function testBit(binaryValue: string, position: number): boolean {
47 const { length } = binaryValue;
48
49 if (position > length) {
50 return false;
51 }
52
53 const positionInString = length - position;
54 return binaryValue.substring(positionInString, positionInString + 1) === '1';
55}
Note: See TracBrowser for help on using the repository browser.