ip-utilties 1.0.4 → 1.0.5

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.
Files changed (3) hide show
  1. package/README.md +8 -0
  2. package/package.json +1 -1
  3. package/src/ip.mjs +12 -13
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.5",
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,7 +24,6 @@ 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,
@@ -80,7 +78,7 @@ function _encode(definition, address) {
80
78
  case "object":
81
79
  if (
82
80
  address instanceof definition.factory &&
83
- address.length === definition.byteLength
81
+ address.length === definition.segments
84
82
  ) {
85
83
  return address;
86
84
  }
@@ -169,7 +167,7 @@ function _is(definition, address) {
169
167
  case "object":
170
168
  return (
171
169
  address instanceof definition.factory &&
172
- address.length === definition.byteLength
170
+ address.length === definition.segments
173
171
  );
174
172
  }
175
173
 
@@ -271,15 +269,16 @@ export function normalizeIP(address) {
271
269
 
272
270
  export function reverseArpa(address) {
273
271
  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";
272
+ return (
273
+ encodeIPv6(address)
274
+ .reduce(
275
+ (a, segment) => (a += segment.toString(16).padStart(4, "0")),
276
+ ""
277
+ )
278
+ .split("")
279
+ .reverse()
280
+ .join(".") + ".ip6.arpa"
281
+ );
283
282
  }
284
283
 
285
284
  return address.split(".").reverse().join(".") + ".in-addr.arpa";