@@ -13,12 +13,16 @@ const IPv6MAX = (BigInt(2) ** BigInt(128)) - BigInt(1);
13
13
*/
14
14
15
15
export default class IP {
16
+ integer : bigint ;
17
+ short : string ;
18
+ version : number ;
19
+ address : string ;
16
20
/**
17
21
* @constructor
18
22
*/
19
- constructor ( address ) {
20
- this . integer = 0 ;
21
- this . short = 0 ;
23
+ constructor ( address : string ) {
24
+ this . integer = 0n ;
25
+ this . short = '' ;
22
26
this . version = this . _checkVersion ( address ) ;
23
27
this . address = this . _checkAddress ( address , this . version ) ;
24
28
}
@@ -34,7 +38,7 @@ export default class IP {
34
38
if ( this . version === 4 ) {
35
39
const splittedAddr = this . address . split ( '.' ) . reverse ( ) ;
36
40
bigInt = splittedAddr . reduce ( ( bigInt , octet , index ) => {
37
- return ( octet * 256 ** index + bigInt
41
+ return ( Number ( octet ) * 256 ** index + bigInt
38
42
) ;
39
43
} , 0 ) ;
40
44
}
@@ -51,7 +55,7 @@ export default class IP {
51
55
* @param {bigint } bigInt
52
56
* @return {string } -> "184.170.96.196"
53
57
*/
54
- toDottedNotation ( bigInt ) {
58
+ toDottedNotation ( bigInt : bigint ) {
55
59
if ( this . version === 4 ) {
56
60
return (
57
61
[ ( bigInt >> BigInt ( 24 ) & BigInt ( 255 ) ) , ( bigInt >> BigInt ( 16 ) & BigInt ( 255 ) ) ,
@@ -78,15 +82,14 @@ export default class IP {
78
82
* @return {string } -> 01111111000000000000000000000001
79
83
*/
80
84
toBinary ( ) {
81
- if ( this . integer === 0 ) {
85
+ if ( this . integer === 0n ) {
82
86
this . toInteger ( ) ;
83
87
}
84
88
let binary = this . integer . toString ( 2 ) ;
85
- const v = this . version ;
86
- const marks = { 4 : 32 , 6 : 128 } ;
89
+ const markLen = this . version === 4 ? 32 : 128 ;
87
90
88
- if ( binary . length < marks [ v ] ) {
89
- while ( binary . length < marks [ v ] ) {
91
+ if ( binary . length < markLen ) {
92
+ while ( binary . length < markLen ) {
90
93
binary = `0${ binary } ` ;
91
94
}
92
95
}
@@ -98,7 +101,7 @@ export default class IP {
98
101
* @return {string } -> 7f000001
99
102
*/
100
103
toHEX ( ) {
101
- if ( this . integer === 0 ) {
104
+ if ( this . integer === 0n ) {
102
105
this . toInteger ( ) ;
103
106
}
104
107
return this . integer . toString ( 16 ) ;
@@ -109,7 +112,7 @@ export default class IP {
109
112
* IP('127.1.0.0').toCompressed
110
113
* @return {string } -> "127.1"
111
114
*/
112
- toCompressed ( addr , ver ) {
115
+ toCompressed ( addr : string , ver : number ) {
113
116
if ( ver === 4 ) {
114
117
const splittedAddr = addr . split ( '.' ) ;
115
118
const sRange = [ [ 1 , 3 ] , [ 2 , 2 ] , [ 3 , 1 ] , [ 0 , 0 ] ] ;
@@ -132,11 +135,11 @@ export default class IP {
132
135
// 'N/A' - _longestZerosGroup fn return in case if there is NO
133
136
// '0000' blocks in address
134
137
if ( startOfLongest !== 'N/A' || longestLength !== 'N/A' ) {
135
- splitted . splice ( startOfLongest , longestLength , '' ) ;
138
+ splitted . splice ( Number ( startOfLongest ) , Number ( longestLength ) , '' ) ;
136
139
if ( startOfLongest === 0 ) {
137
140
splitted . unshift ( '' ) ;
138
141
}
139
- if ( startOfLongest + longestLength === 8 ) {
142
+ if ( Number ( startOfLongest ) + Number ( longestLength ) === 8 ) {
140
143
splitted . push ( '' ) ;
141
144
}
142
145
}
@@ -172,7 +175,7 @@ export default class IP {
172
175
* @param {string } addr
173
176
* @return {number } -> 4 or 6
174
177
*/
175
- _checkVersion ( addr ) {
178
+ _checkVersion ( addr : string ) {
176
179
// matches all possible chars in both versions of IP
177
180
const reGen = / ^ [ 0 - 9 a - f . : ] + $ / i;
178
181
if ( reGen . test ( addr ) ) {
@@ -184,14 +187,14 @@ export default class IP {
184
187
const reNum = / ^ [ 0 - 9 ] + $ / ;
185
188
186
189
if ( reNum . test ( addr ) ) {
187
- addr = BigInt ( addr ) ;
188
- if ( addr > IPv6MAX || addr <= 0 ) {
190
+ const parsedAddr = BigInt ( addr ) ;
191
+ if ( parsedAddr > IPv6MAX || parsedAddr <= 0 ) {
189
192
throw new Error ( 'Tips: IP address cant be bigger than 2 to the 128-th power or negative number' ) ;
190
193
}
191
- else if ( addr <= IPv4MAX ) {
194
+ else if ( parsedAddr <= IPv4MAX ) {
192
195
return 4 ;
193
196
}
194
- else if ( addr > IPv4MAX ) {
197
+ else if ( parsedAddr > IPv4MAX ) {
195
198
return 6 ;
196
199
}
197
200
}
@@ -210,18 +213,14 @@ export default class IP {
210
213
* @private
211
214
* @return {string } as a valid address
212
215
*/
213
- _checkAddress ( addr , v ) {
216
+ _checkAddress ( addr : string , v : number ) {
214
217
const reNum = / ^ [ 0 - 9 ] + $ / ;
215
218
if ( reNum . test ( addr ) ) {
216
219
this . integer = BigInt ( addr ) ;
217
220
return this . toDottedNotation ( this . integer ) ;
218
221
}
219
222
220
- const marks = {
221
- 4 : [ '.' , this . _isIPv4 , 4 ] ,
222
- 6 : [ ':' , this . _isIPv6 , 8 ] ,
223
- } ;
224
- const splittedAddr = addr . split ( marks [ v ] [ 0 ] ) ;
223
+ const splittedAddr = addr . split ( v === 4 ? '.' : ':' ) ;
225
224
226
225
if ( v === 6 && splittedAddr . length < 8 ) {
227
226
const dbColon = ( addr . match ( / : : / g) || [ ] ) . length ;
@@ -230,8 +229,8 @@ export default class IP {
230
229
}
231
230
}
232
231
233
- if ( marks [ v ] [ 1 ] . call ( this , splittedAddr ) ) { // TODO: make ifs more readable
234
- if ( splittedAddr . length === marks [ v ] [ 2 ] && this . short === 0 ) {
232
+ if ( ( v === 4 ? this . _isIPv4 : this . _isIPv6 ) . call ( this , splittedAddr ) ) { // TODO: make ifs more readable
233
+ if ( splittedAddr . length === ( v === 4 ? 4 : 8 ) && this . short === '' ) {
235
234
return addr ;
236
235
}
237
236
else {
@@ -248,16 +247,16 @@ export default class IP {
248
247
* @private
249
248
* @return {boolean } whether splitted address is valid IPv6 or not
250
249
*/
251
- _isIPv6 ( splittedAddr ) {
250
+ _isIPv6 ( splittedAddr : string [ ] ) {
252
251
if ( splittedAddr . length <= 8 ) {
253
252
let checked = false ;
254
253
const [ isShort , cleanedAddr ] = this . _isShort ( splittedAddr ) ;
255
254
256
255
const regex = / ^ [ 0 - 9 a - f ] { 1 , 4 } $ / i;
257
- const isValid = function ( hextet ) {
256
+ const isValid = function ( hextet : string ) {
258
257
return regex . test ( hextet ) ;
259
258
} ;
260
- checked = cleanedAddr . every ( isValid ) ;
259
+ checked = ( cleanedAddr as string [ ] ) . every ( isValid ) ;
261
260
262
261
if ( checked && isShort ) {
263
262
this . short = splittedAddr . join ( ':' ) ;
@@ -274,13 +273,13 @@ export default class IP {
274
273
* @private
275
274
* @return {boolean } whether splitted address is valid IPv4 or not
276
275
*/
277
- _isIPv4 ( splittedAddr ) {
276
+ _isIPv4 ( splittedAddr : string [ ] ) {
278
277
if ( splittedAddr . length <= 4 ) {
279
278
if ( splittedAddr . length < 4 ) {
280
279
this . short = splittedAddr . join ( '.' ) ;
281
280
}
282
- const isValid = function ( octet ) {
283
- return ( ! ! ( ( octet <= 255 && octet >= 0 ) ) ) ;
281
+ const isValid = function ( octet : string ) {
282
+ return ( ! ! ( ( Number ( octet ) <= 255 && Number ( octet ) >= 0 ) ) ) ;
284
283
} ;
285
284
return splittedAddr . every ( isValid ) ;
286
285
}
@@ -295,7 +294,7 @@ export default class IP {
295
294
* @param {array } splittedAddr
296
295
* @return {array } with both results boolean and cleaned array
297
296
*/
298
- _isShort ( splittedAddr ) {
297
+ _isShort ( splittedAddr : string [ ] ) {
299
298
let isShort = false ;
300
299
const cleanedAddr = [ ...splittedAddr ] ;
301
300
for ( let i = 0 ; i < cleanedAddr . length ; i ++ ) {
@@ -320,7 +319,7 @@ export default class IP {
320
319
* @param {array } splittedAddr
321
320
* @return {string } -> "0000:0000:0000:0000:0000:0000:0000:0001"
322
321
*/
323
- _toRepresentation ( splittedAddr ) {
322
+ _toRepresentation ( splittedAddr : string [ ] ) {
324
323
if ( this . version === 4 ) {
325
324
for ( let i = 0 ; i <= 4 ; i ++ ) {
326
325
if ( splittedAddr [ i ] === '' ) {
@@ -383,7 +382,7 @@ export default class IP {
383
382
* @param {array } zeros
384
383
* @return {array } -> [0, 7]
385
384
*/
386
- function _longestZerosGroup ( splittedAddr ) {
385
+ function _longestZerosGroup ( splittedAddr : string [ ] ) {
387
386
let curr = 0 ;
388
387
let currLongest = 0 ;
389
388
let startOfLongest = 0 ;
0 commit comments