ip-utilties 1.0.1 → 1.0.3

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.1",
3
+ "version": "1.0.3",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/ip.mjs CHANGED
@@ -9,7 +9,6 @@ const ipv4 = {
9
9
  segments: 4,
10
10
  segmentLength: 8,
11
11
  segmentMask: 0xffn,
12
- mask: 0xffffffffn,
13
12
  base: 10
14
13
  };
15
14
 
@@ -30,7 +29,6 @@ const ipv6 = {
30
29
  segments: 8,
31
30
  segmentLength: 16,
32
31
  segmentMask: 0xffffn,
33
- mask: 0xffffffffffffffffffffffffffffffffn,
34
32
  base: 16
35
33
  };
36
34
 
@@ -183,7 +181,7 @@ function _is(definition, address) {
183
181
  }
184
182
 
185
183
  export function asBigInt(address) {
186
- return _asBigInt(isIPv4(address) ? ipv4 : ipv6, address);
184
+ return _asBigInt(_definition(address), address);
187
185
  }
188
186
 
189
187
  function _asBigInt(definition, address) {
@@ -191,16 +189,10 @@ function _asBigInt(definition, address) {
191
189
  return address;
192
190
  }
193
191
 
194
- const ea = _encode(definition, address);
195
-
196
- let result = 0n;
197
-
198
- for (let i = 0; i < ea.length; i++) {
199
- result = result << BigInt(definition.segmentLength);
200
- result += BigInt(ea[i]);
201
- }
202
-
203
- return result;
192
+ return _encode(definition, address).reduce(
193
+ (a, c) => (a << BigInt(definition.segmentLength)) + BigInt(c),
194
+ 0n
195
+ );
204
196
  }
205
197
 
206
198
  function _encodeBigInt(definition, address) {
@@ -215,24 +207,25 @@ function _encodeBigInt(definition, address) {
215
207
  }
216
208
 
217
209
  export function prefixIP(address, length) {
218
- const definition = isIPv4(address) ? ipv4 : ipv6;
210
+ const definition = _definition(address);
219
211
  return _decode(definition, _prefix(definition, address, length));
220
212
  }
221
213
 
222
214
  function _prefix(definition, address, length) {
223
215
  return (
224
216
  _asBigInt(definition, address) &
225
- (definition.mask << BigInt(definition.bitLength - length))
217
+ (-1n << BigInt(definition.bitLength - length))
226
218
  );
227
219
  }
228
220
 
229
221
  export function rangeIP(address, prefix, lowerAdd = 0, upperReduce = 0) {
230
- const definition = isIPv4(address) ? ipv4 : ipv6;
231
-
222
+ const definition = _definition(address);
232
223
  const from = _prefix(definition, address, prefix);
233
224
  const to = from | ((1n << BigInt(definition.bitLength - prefix)) - 1n);
234
-
235
- return [_encode(definition, from + BigInt(lowerAdd)), _encode(definition, to - BigInt(upperReduce))];
225
+ return [
226
+ _encode(definition, from + BigInt(lowerAdd)),
227
+ _encode(definition, to - BigInt(upperReduce))
228
+ ];
236
229
  }
237
230
 
238
231
  export function normalizeCIDR(address) {
package/types/ip.d.mts CHANGED
@@ -8,7 +8,7 @@ export function decodeIPv4(address: any, length: any): string;
8
8
  export function decodeIP(address: any, length: any): string;
9
9
  export function isIPv4(address: any): boolean;
10
10
  export function isIPv6(address: any): boolean;
11
- export function asBigInt(address: any): bigint;
11
+ export function asBigInt(address: any): any;
12
12
  export function prefixIP(address: any, length: any): string;
13
13
  export function rangeIP(address: any, prefix: any, lowerAdd?: number, upperReduce?: number): any[];
14
14
  export function normalizeCIDR(address: any): {