ip-utilties 1.0.3 → 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 +13 -18
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.3",
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
  }
@@ -158,11 +156,7 @@ export function isIPv6(address) {
158
156
  }
159
157
 
160
158
  function _definition(address) {
161
- for (const defintion of [ipv4, ipv6]) {
162
- if (_is(defintion, address)) {
163
- return defintion;
164
- }
165
- }
159
+ return [ipv4, ipv6].find(d => _is(d, address));
166
160
  }
167
161
 
168
162
  function _is(definition, address) {
@@ -173,7 +167,7 @@ function _is(definition, address) {
173
167
  case "object":
174
168
  return (
175
169
  address instanceof definition.factory &&
176
- address.length === definition.byteLength
170
+ address.length === definition.segments
177
171
  );
178
172
  }
179
173
 
@@ -275,15 +269,16 @@ export function normalizeIP(address) {
275
269
 
276
270
  export function reverseArpa(address) {
277
271
  if (isIPv6(address)) {
278
- const ea = encodeIPv6(address);
279
- const result = [];
280
- for (let i = 0; i < ea.length; i++) {
281
- const v = ea[i];
282
- for (let i = 0; i < 4; i++) {
283
- result.push(((v >> (12 - 4 * i)) & 0x000f).toString(16));
284
- }
285
- }
286
- 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
+ );
287
282
  }
288
283
 
289
284
  return address.split(".").reverse().join(".") + ".in-addr.arpa";