mybase 1.1.28 → 1.1.29

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.28",
3
+ "version": "1.1.29",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -1,13 +1,7 @@
1
1
  "use strict";
2
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
- };
7
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
8
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
9
4
  };
10
- var _IPAddress_instances, _IPAddress_padIp6, _IPAddress_padIp4;
11
5
  Object.defineProperty(exports, "__esModule", { value: true });
12
6
  exports.IPAddress = void 0;
13
7
  const __1 = require("./../");
@@ -16,7 +10,6 @@ const ip6 = require('ip6'); // "url": "https://github.com/elgs/ip6"
16
10
  // need version "ip6": "0.2.7",!!
17
11
  class IPAddress {
18
12
  constructor(ip) {
19
- _IPAddress_instances.add(this);
20
13
  if (ip === '-')
21
14
  ip = '0.0.0.0';
22
15
  try {
@@ -109,8 +102,8 @@ class IPAddress {
109
102
  }
110
103
  randomPad() {
111
104
  if (this.kind() === 4)
112
- return __classPrivateFieldGet(this, _IPAddress_instances, "m", _IPAddress_padIp4).call(this);
113
- return __classPrivateFieldGet(this, _IPAddress_instances, "m", _IPAddress_padIp6).call(this);
105
+ return this.padIp4();
106
+ return this.padIp6();
114
107
  }
115
108
  normalized() {
116
109
  if (this.kind() === 4)
@@ -122,6 +115,24 @@ class IPAddress {
122
115
  return this._ip.toString();
123
116
  return ip6.abbreviate(this._ip.toString());
124
117
  }
118
+ padIp6() {
119
+ const hextets = this.normalized().split(':'); // we need to have the full form
120
+ const paddedHextets = hextets.map(hextet => {
121
+ const cleanHextet = Number(`0x${hextet}`).toString(16);
122
+ const padLength = Math.floor(Math.random() * (4 - cleanHextet.length));
123
+ return cleanHextet.padStart(padLength + cleanHextet.length, '0');
124
+ });
125
+ return paddedHextets.join(':');
126
+ }
127
+ padIp4() {
128
+ const octets = this._ip.toString().split('.');
129
+ const paddedOctets = octets.map(octet => {
130
+ const cleanOctet = String(Number(octet));
131
+ const padLength = Math.floor(Math.random() * (3 - cleanOctet.length));
132
+ return cleanOctet.padStart(padLength + cleanOctet.length, '0');
133
+ });
134
+ return paddedOctets.join('.');
135
+ }
125
136
  asciiChecksum() {
126
137
  // support ipv4 and ipv6
127
138
  // FNV-1a hash parameters
@@ -147,21 +158,4 @@ class IPAddress {
147
158
  }
148
159
  }
149
160
  exports.IPAddress = IPAddress;
150
- _IPAddress_instances = new WeakSet(), _IPAddress_padIp6 = function _IPAddress_padIp6() {
151
- const hextets = this.normalized().split(':'); // we need to have the full form
152
- const paddedHextets = hextets.map(hextet => {
153
- const cleanHextet = Number(`0x${hextet}`).toString(16);
154
- const padLength = Math.floor(Math.random() * (4 - cleanHextet.length));
155
- return cleanHextet.padStart(padLength + cleanHextet.length, '0');
156
- });
157
- return paddedHextets.join(':');
158
- }, _IPAddress_padIp4 = function _IPAddress_padIp4() {
159
- const octets = this._ip.toString().split('.');
160
- const paddedOctets = octets.map(octet => {
161
- const cleanOctet = String(Number(octet));
162
- const padLength = Math.floor(Math.random() * (3 - cleanOctet.length));
163
- return cleanOctet.padStart(padLength + cleanOctet.length, '0');
164
- });
165
- return paddedOctets.join('.');
166
- };
167
161
  //# sourceMappingURL=IPAddress.js.map
@@ -109,8 +109,8 @@ export class IPAddress {
109
109
  }
110
110
 
111
111
  public randomPad(): string {
112
- if (this.kind() === 4) return this.#padIp4()
113
- return this.#padIp6()
112
+ if (this.kind() === 4) return this.padIp4()
113
+ return this.padIp6()
114
114
  }
115
115
 
116
116
  public normalized(): string {
@@ -123,7 +123,7 @@ export class IPAddress {
123
123
  return ip6.abbreviate(this._ip.toString())
124
124
  }
125
125
 
126
- #padIp6(): string {
126
+ private padIp6(): string {
127
127
  const hextets = this.normalized().split(':') // we need to have the full form
128
128
  const paddedHextets = hextets.map(hextet => {
129
129
  const cleanHextet = Number(`0x${hextet}`).toString(16)
@@ -134,7 +134,7 @@ export class IPAddress {
134
134
  return paddedHextets.join(':')
135
135
  }
136
136
 
137
- #padIp4(): string {
137
+ private padIp4(): string {
138
138
  const octets = this._ip.toString().split('.')
139
139
  const paddedOctets = octets.map(octet => {
140
140
  const cleanOctet = String(Number(octet))
@@ -1,165 +0,0 @@
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
- const ip6addr_1 = __importDefault(require("ip6addr"));
7
- const IPAddress_1 = require("./IPAddress");
8
- const __1 = require("../");
9
- describe('IPAddress', () => {
10
- it('should create an instance of IPAddress', () => {
11
- const ipAddress = new IPAddress_1.IPAddress('192.168.0.1');
12
- expect(ipAddress).toBeInstanceOf(IPAddress_1.IPAddress);
13
- });
14
- it('should convert IPAddress to string', () => {
15
- const ipAddress = new IPAddress_1.IPAddress('192.168.0.1');
16
- const result = ipAddress.toString();
17
- expect(result).toBe('192.168.0.1');
18
- });
19
- it('should create an instance of IPAddress from string', () => {
20
- const ipAddress = IPAddress_1.IPAddress.fromString('192.168.0.1');
21
- expect(ipAddress).toBeInstanceOf(IPAddress_1.IPAddress);
22
- });
23
- it('should throw an error when creating an instance of IPAddress from invalid string', () => {
24
- expect(() => IPAddress_1.IPAddress.fromString('192.168.0')).toThrow();
25
- expect(() => IPAddress_1.IPAddress.fromString('0.0.0')).toThrow();
26
- expect(() => IPAddress_1.IPAddress.fromString('')).toThrow();
27
- //@ts-ignore
28
- expect(() => IPAddress_1.IPAddress.fromString(null)).toThrow();
29
- //@ts-ignore
30
- expect(() => IPAddress_1.IPAddress.fromString(undefined)).toThrow();
31
- });
32
- it('should serialize ipv4 to string', () => {
33
- expect(IPAddress_1.IPAddress.fromString('192.168.0.1').serialize()).toBe('192168000001');
34
- expect(IPAddress_1.IPAddress.fromString('0.0.0.0').serialize()).toBe('000000000000');
35
- expect(IPAddress_1.IPAddress.fromString('0.0.0.1').serialize()).toBe('000000000001');
36
- expect(IPAddress_1.IPAddress.fromString('0.00.000.01').serialize()).toBe('000000000001');
37
- expect(IPAddress_1.IPAddress.fromString('0.10.0.0').serialize()).toBe('000010000000');
38
- expect(IPAddress_1.IPAddress.fromString('255.255.255.255').serialize()).toBe('255255255255');
39
- });
40
- it('should serialize ipv6 to string', () => {
41
- expect(IPAddress_1.IPAddress.fromString('::').serialize()).toBe('00000000000000000000000000000000');
42
- expect(IPAddress_1.IPAddress.fromString('::1').serialize()).toBe('00000000000000000000000000000001');
43
- expect(IPAddress_1.IPAddress.fromString('::01').serialize()).toBe('00000000000000000000000000000001');
44
- expect(IPAddress_1.IPAddress.fromString('::001').serialize()).toBe('00000000000000000000000000000001');
45
- expect(IPAddress_1.IPAddress.fromString('::0001').serialize()).toBe('00000000000000000000000000000001');
46
- expect(IPAddress_1.IPAddress.fromString('::1:1').serialize()).toBe('00000000000000000000000000010001');
47
- expect(IPAddress_1.IPAddress.fromString('::2:1').serialize()).toBe('00000000000000000000000000020001');
48
- });
49
- // testing unserialize
50
- it('should unserialize ipv4 from string', () => {
51
- expect(IPAddress_1.IPAddress.unserialize('192168000001').toString()).toBe('192.168.0.1');
52
- expect(IPAddress_1.IPAddress.unserialize('192168000002').toString()).toBe('192.168.0.2');
53
- expect(IPAddress_1.IPAddress.unserialize('000000000000').toString()).toBe('0.0.0.0');
54
- });
55
- it('should throw invalid serialization string', () => {
56
- expect(() => IPAddress_1.IPAddress.unserialize('92168000001')).toThrow(Error);
57
- expect(() => IPAddress_1.IPAddress.unserialize('168000001')).toThrow(Error);
58
- expect(() => IPAddress_1.IPAddress.unserialize('292168000002')).toThrow(Error);
59
- });
60
- it('randomly generated ipv4 serialization', () => {
61
- for (let i = 0; i < 100; i++) {
62
- const ip = (0, __1.randomIP)();
63
- expect(IPAddress_1.IPAddress.unserialize(IPAddress_1.IPAddress.fromString(ip).serialize()).toString()).toBe(ip);
64
- }
65
- });
66
- it('randomly generated ipv6 serialization and unserialization', () => {
67
- for (let i = 0; i < 100; i++) {
68
- const ip = (0, __1.randomIP6)();
69
- let ip2 = IPAddress_1.IPAddress.unserialize(IPAddress_1.IPAddress.fromString(ip).serialize()).toString();
70
- expect(ip6addr_1.default.compare(ip, ip2)).toEqual(0);
71
- }
72
- });
73
- it('it should detect the kind of ip4', () => {
74
- for (let i = 0; i < 100; i++)
75
- expect(IPAddress_1.IPAddress.randomIP4().kind()).toBe(4);
76
- });
77
- it('should return shortened address by default toString', () => {
78
- expect(new IPAddress_1.IPAddress('001.02.3.004').toString()).toBe('1.2.3.4');
79
- });
80
- it('randomPad return valid ipv4 results', () => {
81
- let compared = 0;
82
- while (compared < 100) {
83
- const ip1 = IPAddress_1.IPAddress.randomIP4();
84
- const ip2 = ip1.randomPad();
85
- if (ip1.toString() !== ip2) { // find ips they are padded from the original
86
- compared++;
87
- }
88
- expect(IPAddress_1.IPAddress.validate(ip2)).toBe(true);
89
- }
90
- });
91
- it('compare ipv4 should detect equality even if they are padded', () => {
92
- let compared = 0;
93
- while (compared < 100) {
94
- const ip1 = IPAddress_1.IPAddress.randomIP4();
95
- const ip2 = ip1.randomPad();
96
- if (ip1.toString() !== ip2) { // find ips they are padded from the original
97
- compared++;
98
- }
99
- expect(ip1.equals(IPAddress_1.IPAddress.fromString(ip2))).toBe(true);
100
- }
101
- });
102
- });
103
- describe('IPAddress IPv6', () => {
104
- it('should work with any form of valid ipv6, prefixed or not', () => {
105
- expect(new IPAddress_1.IPAddress('2605:2700:0000:0003:a800:00ff:fe10:f0d3')).toBeInstanceOf(IPAddress_1.IPAddress);
106
- expect(new IPAddress_1.IPAddress('2605:2700:0000:003:a800:00ff:fe10:f0d3')).toBeInstanceOf(IPAddress_1.IPAddress);
107
- expect(new IPAddress_1.IPAddress('2605:2700:000:003:a800:00ff:fe10:f0d3')).toBeInstanceOf(IPAddress_1.IPAddress);
108
- expect(new IPAddress_1.IPAddress('2605:2700:0:003:a800:00ff:fe10:f0d3')).toBeInstanceOf(IPAddress_1.IPAddress);
109
- expect(new IPAddress_1.IPAddress('2605:2700:0:003:a800:0ff:fe10:f0d3')).toBeInstanceOf(IPAddress_1.IPAddress);
110
- });
111
- it('should return shortened address by default toString', () => {
112
- expect(new IPAddress_1.IPAddress('2605:2700:0000:0003:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
113
- expect(new IPAddress_1.IPAddress('2605:2700:0000:003:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
114
- expect(new IPAddress_1.IPAddress('2605:2700:0000:03:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
115
- expect(new IPAddress_1.IPAddress('2605:2700:0000:03:a800:ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
116
- });
117
- it('ip6 addresses should be lowercased', () => {
118
- expect(new IPAddress_1.IPAddress('2605:2700:0000:0003:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
119
- expect(new IPAddress_1.IPAddress('2605:2700:0000:003:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
120
- expect(new IPAddress_1.IPAddress('2605:2700:0000:03:a800:00ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
121
- expect(new IPAddress_1.IPAddress('2605:2700:0000:03:a800:ff:fe10:f0d3').toString()).toBe('2605:2700::3:a800:ff:fe10:f0d3');
122
- });
123
- it('it should detect the kind of ip6', () => {
124
- expect(new IPAddress_1.IPAddress('2605:2700:0000:0003:a800:00ff:fe10:f0d3').kind()).toBe(6);
125
- for (let i = 0; i < 100; i++)
126
- expect(IPAddress_1.IPAddress.randomIP6().kind()).toBe(6);
127
- });
128
- it('randomPad return valid ipv6 results be compareable with equals', () => {
129
- let compared = 0;
130
- while (compared < 500) {
131
- const ip1 = IPAddress_1.IPAddress.randomIP6();
132
- const ip2 = ip1.randomPad();
133
- expect(ip1.kind()).toBe(6);
134
- if (ip1.toString() !== ip2) { // find ips they are padded from the original
135
- compared++;
136
- }
137
- expect(IPAddress_1.IPAddress.validate(ip2)).toBe(true);
138
- expect(ip1.equals(IPAddress_1.IPAddress.fromString(ip2))).toBe(true);
139
- }
140
- });
141
- });
142
- describe('IPAddress asciiChecksum', () => {
143
- it('should return the correct checksum for ipv4', () => {
144
- expect(new IPAddress_1.IPAddress('1.2.3.4').asciiChecksum()).toBe('e');
145
- expect(new IPAddress_1.IPAddress('1.2.3.6').asciiChecksum()).toBe('i');
146
- });
147
- it('should return the correct checksum for ipv6', () => {
148
- expect(new IPAddress_1.IPAddress('::').asciiChecksum()).toBe('a');
149
- expect(new IPAddress_1.IPAddress('::1').asciiChecksum()).toBe('a');
150
- expect(new IPAddress_1.IPAddress('::f').asciiChecksum()).toBe('s');
151
- });
152
- it('it should always return a single character ipv4/v6', () => {
153
- for (let i = 0; i < 10000; i++) {
154
- expect(new IPAddress_1.IPAddress((0, __1.randomIP)()).asciiChecksum().length).toBe(1);
155
- expect(new IPAddress_1.IPAddress((0, __1.randomIP6)()).asciiChecksum().length).toBe(1);
156
- }
157
- });
158
- it('it should return a-z0-9 ascii only', () => {
159
- for (let i = 0; i < 10000; i++) {
160
- expect('abcdefghijklmnopqrstuvwxyz0123456789').toContain(new IPAddress_1.IPAddress((0, __1.randomIP)()).asciiChecksum());
161
- expect('abcdefghijklmnopqrstuvwxyz0123456789').toContain(new IPAddress_1.IPAddress((0, __1.randomIP6)()).asciiChecksum());
162
- }
163
- });
164
- });
165
- //# sourceMappingURL=IPAddress.jest.js.map