ip-utilties 1.0.4 → 1.0.6

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
@@ -12,6 +12,14 @@
12
12
 
13
13
  ip v4/v6 utility functions
14
14
 
15
+ ## 3 different representations
16
+
17
+ ```javascript
18
+ const ip4AsString = "10.0.0.1";
19
+ const ip4AsArray = new Uint8Array([10,0,0,1]);
20
+ const ip4or6AsBigInt = 10n << 14n | 1n;
21
+ ```
22
+
15
23
  # API
16
24
 
17
25
  # install
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/ip.mjs CHANGED
@@ -5,7 +5,6 @@ const ipv4 = {
5
5
  },
6
6
  separator: ".",
7
7
  bitLength: 32,
8
- byteLength: 4,
9
8
  segments: 4,
10
9
  segmentLength: 8,
11
10
  segmentMask: 0xffn,
@@ -25,28 +24,12 @@ const ipv6 = {
25
24
  separator: ":",
26
25
  compressor: "::",
27
26
  bitLength: 128,
28
- byteLength: 8,
29
27
  segments: 8,
30
28
  segmentLength: 16,
31
29
  segmentMask: 0xffffn,
32
30
  base: 16
33
31
  };
34
32
 
35
- export function IPV4(...args) {
36
- return _create(ipv4, ...args);
37
- }
38
-
39
- export function IPV6(...args) {
40
- return _create(ipv6, ...args);
41
- }
42
-
43
- function _create(definition, ...args) {
44
- if (args.length === 1) {
45
- return _encode(definition, args[0]);
46
- }
47
- return new definition.factory(args);
48
- }
49
-
50
33
  export function encodeIP(address) {
51
34
  const d = _definition(address);
52
35
  return d && _encode(d, address);
@@ -80,7 +63,7 @@ function _encode(definition, address) {
80
63
  case "object":
81
64
  if (
82
65
  address instanceof definition.factory &&
83
- address.length === definition.byteLength
66
+ address.length === definition.segments
84
67
  ) {
85
68
  return address;
86
69
  }
@@ -169,7 +152,7 @@ function _is(definition, address) {
169
152
  case "object":
170
153
  return (
171
154
  address instanceof definition.factory &&
172
- address.length === definition.byteLength
155
+ address.length === definition.segments
173
156
  );
174
157
  }
175
158
 
@@ -271,15 +254,16 @@ export function normalizeIP(address) {
271
254
 
272
255
  export function reverseArpa(address) {
273
256
  if (isIPv6(address)) {
274
- const ea = encodeIPv6(address);
275
- const result = [];
276
- for (let i = 0; i < ea.length; i++) {
277
- const v = ea[i];
278
- for (let i = 0; i < 4; i++) {
279
- result.push(((v >> (12 - 4 * i)) & 0x000f).toString(16));
280
- }
281
- }
282
- return result.reverse().join(".") + ".ip6.arpa";
257
+ return (
258
+ encodeIPv6(address)
259
+ .reduce(
260
+ (a, segment) => (a += segment.toString(16).padStart(4, "0")),
261
+ ""
262
+ )
263
+ .split("")
264
+ .reverse()
265
+ .join(".") + ".ip6.arpa"
266
+ );
283
267
  }
284
268
 
285
269
  return address.split(".").reverse().join(".") + ".in-addr.arpa";
package/types/ip.d.mts CHANGED
@@ -1,5 +1,3 @@
1
- export function IPV4(...args: any[]): any;
2
- export function IPV6(...args: any[]): any;
3
1
  export function encodeIP(address: any): any;
4
2
  export function encodeIPv6(address: any): any;
5
3
  export function encodeIPv4(address: any): any;