ip-utilties 1.0.6 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "1.0.6",
3
+ "version": "1.1.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/ip.mjs CHANGED
@@ -31,7 +31,7 @@ const ipv6 = {
31
31
  };
32
32
 
33
33
  export function encodeIP(address) {
34
- const d = _definition(address);
34
+ const d = _family(address);
35
35
  return d && _encode(d, address);
36
36
  }
37
37
 
@@ -43,43 +43,43 @@ export function encodeIPv4(address) {
43
43
  return _encode(ipv4, address);
44
44
  }
45
45
 
46
- function _encode(definition, address) {
46
+ function _encode(family, address) {
47
47
  switch (typeof address) {
48
48
  case "string":
49
- const res = new definition.factory(definition.segments);
49
+ const res = new family.factory(family.segments);
50
50
 
51
51
  let i = 0;
52
- for (const segment of definition
52
+ for (const segment of family
53
53
  .normalize(address)
54
- .split(definition.separator)) {
55
- res[i++] = parseInt(segment, definition.base);
54
+ .split(family.separator)) {
55
+ res[i++] = parseInt(segment, family.base);
56
56
  }
57
57
 
58
58
  return res;
59
59
 
60
60
  case "bigint":
61
- return _encodeBigInt(definition, address);
61
+ return _encodeBigInt(family, address);
62
62
 
63
63
  case "object":
64
64
  if (
65
- address instanceof definition.factory &&
66
- address.length === definition.segments
65
+ address instanceof family.factory &&
66
+ address.length === family.segments
67
67
  ) {
68
68
  return address;
69
69
  }
70
70
  }
71
71
  }
72
72
 
73
- function _decode(definition, address, length) {
73
+ function _decode(family, address, length) {
74
74
  switch (typeof address) {
75
75
  case "string":
76
76
  if (length === undefined) {
77
77
  return address;
78
78
  }
79
- address = _encode(definition, address);
79
+ address = _encode(family, address);
80
80
  break;
81
81
  case "bigint":
82
- address = _encodeBigInt(definition, address);
82
+ address = _encodeBigInt(family, address);
83
83
  }
84
84
 
85
85
  let result = "";
@@ -88,7 +88,7 @@ function _decode(definition, address, length) {
88
88
  let last = address?.length;
89
89
 
90
90
  if (length !== undefined) {
91
- length /= definition.segmentLength;
91
+ length /= family.segmentLength;
92
92
 
93
93
  if (length < last) {
94
94
  last = length;
@@ -98,22 +98,22 @@ function _decode(definition, address, length) {
98
98
  for (; j < last; j++) {
99
99
  word = address[j];
100
100
 
101
- if (word !== 0 || !definition.compressor || compressed > 0) {
101
+ if (word !== 0 || !family.compressor || compressed > 0) {
102
102
  break;
103
103
  }
104
104
  }
105
105
 
106
106
  if (j > i + 1) {
107
107
  compressed++;
108
- result += definition.compressor;
108
+ result += family.compressor;
109
109
  } else {
110
110
  if (result.length > 0) {
111
- result += definition.separator;
111
+ result += family.separator;
112
112
  }
113
113
  }
114
114
 
115
115
  if (j < last) {
116
- result += word.toString(definition.base);
116
+ result += word.toString(family.base);
117
117
  }
118
118
  }
119
119
 
@@ -140,19 +140,19 @@ export function isIPv6(address) {
140
140
  return _is(ipv6, address);
141
141
  }
142
142
 
143
- function _definition(address) {
143
+ function _family(address) {
144
144
  return [ipv4, ipv6].find(d => _is(d, address));
145
145
  }
146
146
 
147
- function _is(definition, address) {
147
+ function _is(family, address) {
148
148
  switch (typeof address) {
149
149
  case "string":
150
- return address.indexOf(definition.separator) >= 0;
150
+ return address.indexOf(family.separator) >= 0;
151
151
 
152
152
  case "object":
153
153
  return (
154
- address instanceof definition.factory &&
155
- address.length === definition.segments
154
+ address instanceof family.factory &&
155
+ address.length === family.segments
156
156
  );
157
157
  }
158
158
 
@@ -160,50 +160,50 @@ function _is(definition, address) {
160
160
  }
161
161
 
162
162
  export function asBigInt(address) {
163
- return _asBigInt(_definition(address), address);
163
+ return _asBigInt(_family(address), address);
164
164
  }
165
165
 
166
- function _asBigInt(definition, address) {
166
+ function _asBigInt(family, address) {
167
167
  if (typeof address === "bigint") {
168
168
  return address;
169
169
  }
170
170
 
171
- return _encode(definition, address).reduce(
172
- (a, c) => (a << BigInt(definition.segmentLength)) + BigInt(c),
171
+ return _encode(family, address).reduce(
172
+ (a, c) => (a << BigInt(family.segmentLength)) + BigInt(c),
173
173
  0n
174
174
  );
175
175
  }
176
176
 
177
- function _encodeBigInt(definition, address) {
177
+ function _encodeBigInt(family, address) {
178
178
  const segments = [];
179
179
 
180
- for (let i = 0; i < definition.segments; i++) {
181
- segments.push(Number(address & definition.segmentMask));
182
- address >>= BigInt(definition.segmentLength);
180
+ for (let i = 0; i < family.segments; i++) {
181
+ segments.push(Number(address & family.segmentMask));
182
+ address >>= BigInt(family.segmentLength);
183
183
  }
184
184
 
185
- return new definition.factory(segments.reverse());
185
+ return new family.factory(segments.reverse());
186
186
  }
187
187
 
188
188
  export function prefixIP(address, length) {
189
- const definition = _definition(address);
190
- return _decode(definition, _prefix(definition, address, length));
189
+ const family = _family(address);
190
+ return _decode(family, _prefix(family, address, length));
191
191
  }
192
192
 
193
- function _prefix(definition, address, length) {
193
+ function _prefix(family, address, length) {
194
194
  return (
195
- _asBigInt(definition, address) &
196
- (-1n << BigInt(definition.bitLength - length))
195
+ _asBigInt(family, address) &
196
+ (-1n << BigInt(family.bitLength - length))
197
197
  );
198
198
  }
199
199
 
200
200
  export function rangeIP(address, prefix, lowerAdd = 0, upperReduce = 0) {
201
- const definition = _definition(address);
202
- const from = _prefix(definition, address, prefix);
203
- const to = from | ((1n << BigInt(definition.bitLength - prefix)) - 1n);
201
+ const family = _family(address);
202
+ const from = _prefix(family, address, prefix);
203
+ const to = from | ((1n << BigInt(family.bitLength - prefix)) - 1n);
204
204
  return [
205
- _encode(definition, from + BigInt(lowerAdd)),
206
- _encode(definition, to - BigInt(upperReduce))
205
+ _encode(family, from + BigInt(lowerAdd)),
206
+ _encode(family, to - BigInt(upperReduce))
207
207
  ];
208
208
  }
209
209
 
@@ -218,22 +218,22 @@ export function normalizeCIDR(address) {
218
218
  } else {
219
219
  prefixLength = parseInt(prefixLength);
220
220
 
221
- const definition = isIPv6(prefix) ? ipv6 : ipv4;
221
+ const family = isIPv6(prefix) ? ipv6 : ipv4;
222
222
  let n;
223
223
 
224
224
  if (prefixLength) {
225
- n = _prefix(definition, prefix, prefixLength);
225
+ n = _prefix(family, prefix, prefixLength);
226
226
  } else {
227
- n = _encode(definition, prefix);
227
+ n = _encode(family, prefix);
228
228
 
229
229
  if (isLocalhost(n)) {
230
- prefixLength = definition === ipv6 ? 128 : 8;
230
+ prefixLength = family === ipv6 ? 128 : 8;
231
231
  } else {
232
232
  return {};
233
233
  }
234
234
  }
235
- prefix = _decode(definition, n, prefixLength);
236
- longPrefix = _decode(definition, n);
235
+ prefix = _decode(family, n, prefixLength);
236
+ longPrefix = _decode(family, n);
237
237
  }
238
238
 
239
239
  return {
@@ -244,8 +244,8 @@ export function normalizeCIDR(address) {
244
244
  };
245
245
  }
246
246
 
247
- export function formatCIDR(address, subnet) {
248
- return subnet ? `${address}/${subnet.prefixLength}` : address;
247
+ export function formatCIDR(address, prefixLength) {
248
+ return prefixLength ? `${decodeIP(address)}/${prefixLength}` : address;
249
249
  }
250
250
 
251
251
  export function normalizeIP(address) {
package/types/ip.d.mts CHANGED
@@ -20,7 +20,7 @@ export function normalizeCIDR(address: any): {
20
20
  prefixLength: any;
21
21
  cidr: string;
22
22
  };
23
- export function formatCIDR(address: any, subnet: any): any;
23
+ export function formatCIDR(address: any, prefixLength: any): any;
24
24
  export function normalizeIP(address: any): string;
25
25
  export function reverseArpa(address: any): string;
26
26
  export function isLocalhost(address: any): boolean;