mybase 1.1.6 → 1.1.7

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": "mybase",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -0,0 +1 @@
1
+ export declare function int2ip(ipInt: number): string;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.int2ip = void 0;
7
+ const net_1 = __importDefault(require("net"));
8
+ //we only support ipv4
9
+ function int2ip(ipInt) {
10
+ if (typeof ipInt !== 'number' || ipInt < 0)
11
+ throw new Error(`require number as input`);
12
+ const ip4 = ((ipInt >>> 24) + '.' + (ipInt >> 16 & 255) + '.' + (ipInt >> 8 & 255) + '.' + (ipInt & 255));
13
+ if (net_1.default.isIP(ip4) !== 4)
14
+ throw new Error(`Invalid IP: ${ip4}`);
15
+ return ip4;
16
+ }
17
+ exports.int2ip = int2ip;
@@ -0,0 +1,33 @@
1
+ const { int2ip } =require('./int2ip')
2
+
3
+ describe('int2ip', () => {
4
+ it('should convert integer to valid IPv4 address', () => {
5
+ const ipInt = 3232235521; // equivalent to 192.168.0.1
6
+ const result = int2ip(ipInt);
7
+ expect(result).toBe('192.168.0.1');
8
+ });
9
+
10
+ it('should throw an error for non-numeric input', () => {
11
+ const ipInt = '3232235521'; // string input instead of number
12
+ expect(() => int2ip(ipInt)).toThrow('require number as input');
13
+ expect(() => int2ip([])).toThrow('require number as input');
14
+ expect(() => int2ip(null)).toThrow('require number as input');
15
+ expect(() => int2ip({})).toThrow('require number as input');
16
+ });
17
+
18
+ it('overflowing is allowed', () => {
19
+ const ipInt = 4294967297;
20
+ let ip = int2ip(ipInt)
21
+ expect(ip).toBe('0.0.0.1')
22
+ });
23
+
24
+ it('should support 0', () => {
25
+ const ipInt = 0;
26
+ expect(int2ip(ipInt)).toBe('0.0.0.0')
27
+ });
28
+
29
+ it('negative numbers should throw', () => {
30
+ expect(() => int2ip(-1)).toThrow('require number as input');
31
+ })
32
+
33
+ });
@@ -0,0 +1,9 @@
1
+ import net from 'net'
2
+
3
+ //we only support ipv4
4
+ export function int2ip (ipInt: number) : string {
5
+ if (typeof ipInt !== 'number' || ipInt<0) throw new Error(`require number as input`)
6
+ const ip4 = ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) )
7
+ if (net.isIP(ip4) !== 4) throw new Error(`Invalid IP: ${ip4}`)
8
+ return ip4
9
+ }
@@ -0,0 +1 @@
1
+ export declare function ip2int(ip: string): number;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ip2int = void 0;
7
+ const net_1 = __importDefault(require("net"));
8
+ // function int2ip (ipInt) {
9
+ // return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
10
+ // }
11
+ // only supports ipv4
12
+ function ip2int(ip) {
13
+ if (net_1.default.isIP(ip) !== 4)
14
+ throw new Error(`Invalid IP: ${ip}`);
15
+ return ip.split('.').reduce(function (ipInt, octet) { return (ipInt << 8) + parseInt(octet, 10); }, 0) >>> 0;
16
+ }
17
+ exports.ip2int = ip2int;
@@ -0,0 +1,30 @@
1
+ const { ip2int } = require('./ip2int');
2
+ const { randomIP } = require('./randomIP');
3
+
4
+ describe('ip2int', () => {
5
+ it('should convert IP address to integer', () => {
6
+ const ipAddress = '192.168.0.1';
7
+ const result = ip2int(ipAddress);
8
+ expect(result).toBe(3232235521);
9
+ })
10
+
11
+ it('invalid inputs', () => {
12
+ expect(() => ip2int('')).toThrow()
13
+ expect(() => ip2int('192.168.0')).toThrow()
14
+ expect(() => ip2int(null)).toThrow()
15
+ expect(() => ip2int(undefined)).toThrow()
16
+ })
17
+
18
+ it('should not support ipv6', () => {
19
+ const ipAddress = '::';
20
+ expect(() => ip2int(ipAddress)).toThrow()
21
+ })
22
+
23
+ it('test 100 random ips', () => {
24
+ for (let i = 0; i < 100; i++) {
25
+ const ipAddress = randomIP();
26
+ const result = ip2int(ipAddress);
27
+ expect(result).toBe(ip2int(ipAddress));
28
+ }
29
+ })
30
+ });
@@ -0,0 +1,10 @@
1
+ import net from 'net'
2
+ // function int2ip (ipInt) {
3
+ // return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
4
+ // }
5
+
6
+ // only supports ipv4
7
+ export function ip2int(ip:string) {
8
+ if (net.isIP(ip) !== 4) throw new Error(`Invalid IP: ${ip}`)
9
+ return ip.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0;
10
+ }
@@ -5,3 +5,6 @@ export function randomIP() : string {
5
5
  }
6
6
  return int2ip(Math.random()*Math.pow(2,32))
7
7
  }
8
+
9
+
10
+
package/ts/index.d.ts CHANGED
@@ -11,3 +11,5 @@ export * from "./funcs/vaultFill";
11
11
  export * from "./funcs/asJSON";
12
12
  export * from "./funcs/promiseTimeout";
13
13
  export * from "./funcs/randomIP";
14
+ export * from "./funcs/ip2int";
15
+ export * from "./funcs/int2ip";
package/ts/index.js CHANGED
@@ -27,3 +27,5 @@ __exportStar(require("./funcs/vaultFill"), exports);
27
27
  __exportStar(require("./funcs/asJSON"), exports);
28
28
  __exportStar(require("./funcs/promiseTimeout"), exports);
29
29
  __exportStar(require("./funcs/randomIP"), exports);
30
+ __exportStar(require("./funcs/ip2int"), exports);
31
+ __exportStar(require("./funcs/int2ip"), exports);
package/ts/index.ts CHANGED
@@ -11,3 +11,5 @@ export * from "./funcs/vaultFill"
11
11
  export * from "./funcs/asJSON"
12
12
  export * from "./funcs/promiseTimeout"
13
13
  export * from "./funcs/randomIP"
14
+ export * from "./funcs/ip2int"
15
+ export * from "./funcs/int2ip"