ip-utilties 1.0.1 → 1.0.2

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.2",
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
 
@@ -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) {
@@ -222,7 +214,7 @@ export function prefixIP(address, length) {
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
 
@@ -232,7 +224,10 @@ export function rangeIP(address, prefix, lowerAdd = 0, upperReduce = 0) {
232
224
  const from = _prefix(definition, address, prefix);
233
225
  const to = from | ((1n << BigInt(definition.bitLength - prefix)) - 1n);
234
226
 
235
- return [_encode(definition, from + BigInt(lowerAdd)), _encode(definition, to - BigInt(upperReduce))];
227
+ return [
228
+ _encode(definition, from + BigInt(lowerAdd)),
229
+ _encode(definition, to - BigInt(upperReduce))
230
+ ];
236
231
  }
237
232
 
238
233
  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): {