ip-utilties 1.0.0 → 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/README.md CHANGED
@@ -18,9 +18,9 @@ ip v4/v6 utility functions
18
18
 
19
19
  With [npm](http://npmjs.org) do:
20
20
 
21
- ```shell
21
+ ```shell
22
22
  npm install ip-utilties
23
- ```
23
+ ```
24
24
 
25
25
  # license
26
26
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "1.0.0",
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
 
@@ -62,7 +60,7 @@ export function encodeIPv4(address) {
62
60
  return _encode(ipv4, address);
63
61
  }
64
62
 
65
- export function _encode(definition, address) {
63
+ function _encode(definition, address) {
66
64
  switch (typeof address) {
67
65
  case "string":
68
66
  const res = new definition.factory(definition.segments);
@@ -167,7 +165,7 @@ function _definition(address) {
167
165
  }
168
166
  }
169
167
 
170
- export function _is(definition, address) {
168
+ function _is(definition, address) {
171
169
  switch (typeof address) {
172
170
  case "string":
173
171
  return address.indexOf(definition.separator) >= 0;
@@ -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) {
@@ -219,10 +211,10 @@ export function prefixIP(address, length) {
219
211
  return _decode(definition, _prefix(definition, address, length));
220
212
  }
221
213
 
222
- export function _prefix(definition, address, length) {
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) {
@@ -283,7 +278,7 @@ export function normalizeIP(address) {
283
278
  export function reverseArpa(address) {
284
279
  if (isIPv6(address)) {
285
280
  const ea = encodeIPv6(address);
286
- let result = [];
281
+ const result = [];
287
282
  for (let i = 0; i < ea.length; i++) {
288
283
  const v = ea[i];
289
284
  for (let i = 0; i < 4; i++) {
package/types/ip.d.mts CHANGED
@@ -3,16 +3,13 @@ export function IPV6(...args: any[]): any;
3
3
  export function encodeIP(address: any): any;
4
4
  export function encodeIPv6(address: any): any;
5
5
  export function encodeIPv4(address: any): any;
6
- export function _encode(definition: any, address: any): any;
7
6
  export function decodeIPv6(address: any, length: any): string;
8
7
  export function decodeIPv4(address: any, length: any): string;
9
8
  export function decodeIP(address: any, length: any): string;
10
9
  export function isIPv4(address: any): boolean;
11
10
  export function isIPv6(address: any): boolean;
12
- export function _is(definition: any, address: any): boolean;
13
- export function asBigInt(address: any): bigint;
11
+ export function asBigInt(address: any): any;
14
12
  export function prefixIP(address: any, length: any): string;
15
- export function _prefix(definition: any, address: any, length: any): bigint;
16
13
  export function rangeIP(address: any, prefix: any, lowerAdd?: number, upperReduce?: number): any[];
17
14
  export function normalizeCIDR(address: any): {
18
15
  longPrefix?: undefined;