generate-ip 2.0.0 → 2.0.2
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 +56 -24
- package/dist/generate-ip.min.js +10 -5
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
### Randomly generate IPv4 and IPv6 addresses.
|
|
4
4
|
|
|
5
5
|
<a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
6
|
-
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-ip-2.0.
|
|
6
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-ip-2.0.2"><img height=31 src="https://img.shields.io/badge/Latest_Build-2.0.2-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/generate-ip?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/generate-ip?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
|
|
8
8
|
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-ip/generate-ip.js"><img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Agenerate-ip%2Fgenerate-ip.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
|
|
9
9
|
|
|
@@ -37,7 +37,7 @@ $ npm install generate-ip
|
|
|
37
37
|
|
|
38
38
|
## 🔌 API usage
|
|
39
39
|
|
|
40
|
-
**generate-ip**
|
|
40
|
+
**generate-ip** provides the [**`ipv4`**](#ipv4-methods) and [**`ipv6`**](#ipv6-methods) APIs that can be imported into your app as ECMAScript modules or CommonJS modules.
|
|
41
41
|
|
|
42
42
|
#### ESM:
|
|
43
43
|
|
|
@@ -55,7 +55,7 @@ const { ipv4, ipv6 } = require('generate-ip');
|
|
|
55
55
|
|
|
56
56
|
### `ipv4` methods
|
|
57
57
|
|
|
58
|
-
Use the `ipv4` methods to generate and validate IPv4 addresses.
|
|
58
|
+
Use the `ipv4` methods to [**generate**](#ipv4generateoptions) and [**validate**](#ipv4validateaddress-options) IPv4 addresses.
|
|
59
59
|
|
|
60
60
|
#
|
|
61
61
|
|
|
@@ -70,9 +70,9 @@ console.log(ip); // sample output: 36.42.224.208
|
|
|
70
70
|
|
|
71
71
|
Available options (passed as object properties):
|
|
72
72
|
|
|
73
|
-
Name | Type | Description
|
|
74
|
-
|
|
75
|
-
`verbose` |
|
|
73
|
+
Name | Type | Description | Default Value
|
|
74
|
+
------------|---------|-----------------------------------|---------------
|
|
75
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
76
76
|
|
|
77
77
|
#
|
|
78
78
|
|
|
@@ -82,20 +82,27 @@ Checks if given `address` is a valid IPv4 address:
|
|
|
82
82
|
|
|
83
83
|
```js
|
|
84
84
|
const ipIsValid = ipv4.validate('36.42.224.208');
|
|
85
|
-
console.log(ipIsValid);
|
|
85
|
+
console.log(ipIsValid);
|
|
86
|
+
|
|
87
|
+
/* outputs:
|
|
88
|
+
|
|
89
|
+
ipv4.validate() » Validating 36.42.224.208...
|
|
90
|
+
ipv4.validate() » IP is valid IPv4 address!
|
|
91
|
+
true
|
|
92
|
+
*/
|
|
86
93
|
```
|
|
87
94
|
|
|
88
95
|
Available options (passed as object properties):
|
|
89
96
|
|
|
90
|
-
Name | Type | Description
|
|
91
|
-
|
|
92
|
-
`verbose` |
|
|
97
|
+
Name | Type | Description | Default Value
|
|
98
|
+
------------|---------|-----------------------------------|---------------
|
|
99
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
93
100
|
|
|
94
101
|
#
|
|
95
102
|
|
|
96
103
|
### `ipv6` methods
|
|
97
104
|
|
|
98
|
-
Use the `ipv6` methods to generate, format, and validate IPv6 addresses.
|
|
105
|
+
Use the `ipv6` methods to [**generate**](#ipv6generateoptions), [**format**](#ipv6formatipv6address-options), and [**validate**](#ipv6validateaddress-options) IPv6 addresses.
|
|
99
106
|
|
|
100
107
|
#
|
|
101
108
|
|
|
@@ -110,40 +117,65 @@ console.log(ip); // sample output: 1379:6748:810c:5e16:b6c9:ae2:939f:8f2a
|
|
|
110
117
|
|
|
111
118
|
Available options (passed as object properties):
|
|
112
119
|
|
|
113
|
-
Name | Type | Description
|
|
114
|
-
|
|
115
|
-
`verbose` |
|
|
116
|
-
`leadingZeros` |
|
|
117
|
-
`doubleColon` |
|
|
120
|
+
Name | Type | Description | Default Value
|
|
121
|
+
---------------|---------|--------------------------------------|---------------
|
|
122
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
123
|
+
`leadingZeros` | Boolean | Include leading zeros in hex pieces. | `false`
|
|
124
|
+
`doubleColon` | Boolean | Replace series of zeros w/ `::` | `true`
|
|
118
125
|
|
|
119
126
|
#
|
|
120
127
|
|
|
121
128
|
|
|
122
|
-
#### `ipv6.format(
|
|
129
|
+
#### `ipv6.format(address[, options])`
|
|
123
130
|
|
|
124
131
|
Formats an IPv6 address according to `options` passed, returning a string:
|
|
125
132
|
|
|
126
133
|
```js
|
|
127
|
-
const address = '0d::ffff:192.1.56.10/96'
|
|
134
|
+
const address = '0d::ffff:192.1.56.10/96',
|
|
128
135
|
formattedAddress = ipv6.format(address, { leadingZeros: true, doubleColon: false });
|
|
129
136
|
|
|
137
|
+
console.log(formattedAddress);
|
|
138
|
+
|
|
130
139
|
/* outputs:
|
|
131
140
|
|
|
132
141
|
ipv6.format() » Formatting 0d::ffff:192.1.56.10/96...
|
|
133
142
|
ipv6.format() » Expanding '::' into zero series...
|
|
134
143
|
ipv6.format() » Adding leading zeros...
|
|
135
|
-
|
|
136
144
|
000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96
|
|
137
145
|
*/
|
|
138
146
|
```
|
|
139
147
|
|
|
140
148
|
Available options:
|
|
141
149
|
|
|
142
|
-
Name | Type | Description
|
|
143
|
-
|
|
144
|
-
`verbose` |
|
|
145
|
-
`leadingZeros` |
|
|
146
|
-
`doubleColon` |
|
|
150
|
+
Name | Type | Description | Default Value
|
|
151
|
+
---------------|---------|--------------------------------------|---------------
|
|
152
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
153
|
+
`leadingZeros` | Boolean | Include leading zeros in hex pieces. | `false`
|
|
154
|
+
`doubleColon` | Boolean | Replace series of zeros w/ `::` | `true`
|
|
155
|
+
|
|
156
|
+
#
|
|
157
|
+
|
|
158
|
+
#### `ipv6.validate(address[, options])`
|
|
159
|
+
|
|
160
|
+
Checks if given `address` is a valid IPv6 address:
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
const ipIsValid = ipv6.validate('0:0:0:0:0:ffff:192.1.56.10/96');
|
|
164
|
+
console.log(ipIsValid);
|
|
165
|
+
|
|
166
|
+
/* outputs:
|
|
167
|
+
|
|
168
|
+
ipv6.validate() » Validating 0:0:0:0:0:ffff:192.1.56.10/96...
|
|
169
|
+
ipv6.validate() » IP is valid IPv6 address!
|
|
170
|
+
true
|
|
171
|
+
*/
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Available options (passed as object properties):
|
|
175
|
+
|
|
176
|
+
Name | Type | Description | Default Value
|
|
177
|
+
------------|---------|-----------------------------------|---------------
|
|
178
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
147
179
|
|
|
148
180
|
<br>
|
|
149
181
|
|
package/dist/generate-ip.min.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
${
|
|
5
|
-
|
|
6
|
-
${
|
|
2
|
+
const t=require("crypto")["randomInt"],n=require("child_process")["execSync"],s={generate:function(e={}){var o={verbose:!0};e={...o,...e};for(const n of Object.keys(e)){if(!Object.prototype.hasOwnProperty.call(o,n))return console.error(`ipv4.generate() » ERROR: \`${n}\` is an invalid option.
|
|
3
|
+
`+`ipv4.generate() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e[n])return console.error(`ipv4.generate() » ERROR: \`${n}\` option can only be set to \`true\` or \`false\`.`)}e.verbose&&console.info("ipv4.generate() » Generating IPv4 address...");var r=[];for(let e=0;e<4;e++)r.push(t(0,256));return r.join(".")},validate:function(e,o={}){var r={verbose:!0};if(o={...r,...o},"string"!=typeof e)return console.error("ipv4.validate() » ERROR: 1st arg <address> must be a string.");for(const n of Object.keys(o)){if(!Object.prototype.hasOwnProperty.call(r,n))return console.error(`ipv4.validate() » ERROR: \`${n}\` is an invalid option.
|
|
4
|
+
`+`ipv4.validate() » Valid options: [ ${Object.keys(r).join(", ")} ]`);if("boolean"!=typeof o[n])return console.error(`ipv4.validate() » ERROR: \`${n}\` option can only be set to \`true\` or \`false\`.`)}o.verbose&&console.info(`ipv4.validate() » Validating ${e}...`);e=e.split("."),e=!(4!==e.length||e.some(e=>!/^\d+$/.test(e)||parseInt(e,10)<0||255<parseInt(e,10)));return o.verbose&&console.info(`ipv4.validate() » IP is ${e?"":"in"}valid IPv4 address!`),e}},e={generate:function(e={}){var o={verbose:!0,leadingZeros:!1,doubleColon:!0};for(const n in e={...o,...e}){if(!Object.prototype.hasOwnProperty.call(o,n))return console.error(`ipv6.generate() » ERROR: \`${n}\` is an invalid option.
|
|
5
|
+
`+`ipv6.generate() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e[n])return console.error(`ipv6.generate() » ERROR: [${n}] option can only be \`true\` or \`false\`.`)}e.verbose&&console.info("ipv6.generate() » Generating IPv6 address...");var r=[];for(let e=0;e<8;e++){let o="";for(let e=0;e<4;e++)o+=t(0,16).toString(16);r.push(o)}return this.format(r.join(":"),{...e,verbose:!1})},format:function(e,o={}){var r,n,t={verbose:!0,leadingZeros:!1,doubleColon:!0};if(o={...t,...o},"string"!=typeof e)return console.error("ipv6.format() » ERROR: 1st arg <address> must be a string.");if(!this.validate(e,{verbose:!1}))return console.error(`ipv6.format() » ERROR:
|
|
6
|
+
- ${e} is not a valid IPv6 address.`);for(const a in o){if(!Object.prototype.hasOwnProperty.call(t,a))return console.error(`ipv6.format() » ERROR: \`${a}\` is an invalid option.
|
|
7
|
+
`+`ipv6.format() » Valid options: [ ${Object.keys(t).join(", ")} ]`);if("boolean"!=typeof o[a])return console.error(`ipv6.format() » ERROR: [${a}] option can only be \`true\` or \`false\`.`)}o.verbose&&console.info(`ipv6.format() » Formatting ${e}...`);let i=e;if(i=o.doubleColon?(o.verbose&&console.info("ipv6.format() » Replacing zero series w/ '::'..."),i.replace(/:(?:0+:)+/,"::")):(o.verbose&&console.info("ipv6.format() » Expanding '::' into zero series..."),n=i.split(":").filter(Boolean).length,r=o.leadingZeros?"0000":"0",n=Array(8-n).fill(r).join(":"),i.replace("::",`:${n}:`)),o.leadingZeros){o.verbose&&console.info("ipv6.format() » Adding leading zeros...");var s=i.split(":");for(let e=0;e<s.length;e++)for(;s[e].length<4;)s[e]="0"+s[e];i=s.join(":")}else o.verbose&&console.info("ipv6.format() » Stripping leading zeros..."),i=e.replace(/(^|(?<=:))0+(?!:)/g,"$1");return o.verbose&&i!==e&&console.info("ipv6.format() » Address cannot be formatted!"),i},validate:function(e,o={}){var r={verbose:!0};if(o={...r,...o},"string"!=typeof e)return console.error("ipv6.validate() » ERROR: 1st arg <address> must be a string.");for(const i in o){if(!Object.prototype.hasOwnProperty.call(r,i))return console.error(`ipv6.validate() » ERROR: \`${i}\` is an invalid option.
|
|
8
|
+
`+`ipv6.validate() » Valid options: [ ${Object.keys(r).join(", ")} ]`);if("boolean"!=typeof o[i])return console.error(`ipv6.validate() » ERROR: [${i}] option can only be \`true\` or \`false\`.`)}o.verbose&&console.info(`ipv6.validate() » Validating ${e}...`);const n=e.split(/::?/),t=n[n.length-1];e=!(e.includes("::")&&2<e.split("::").length||/:{3,}/g.test(e)||n.length<2||8<n.length||n.some(e=>!(/^[\dA-Fa-f]{1,4}$/.test(e)||e===t&&s.validate(t.replace(/\/(?:0|(?:[1-2]?\d)|32|96)$/,""),{verbose:!1}))));return o.verbose&&console.info(`ipv6.validate() » IP is ${e?"":"in"}valid IPv6 address!`),e}};if(require.main!==module)module.exports={ipv4:s,ipv6:e};else{const l="[0m",p="[1;91m",c="[1;33m",f="[1;97m",v={infoCmds:{help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/}};let o=!1,r;for(const d of Object.keys(v))for(const g of Object.keys(v[d]))o||process.argv.forEach(e=>{e.startsWith("-")&&(r=v[d][g].test(e)?void(o=!0):e)});if(r&&(console.error(`
|
|
9
|
+
${p}ERROR: Arg [${r}] not recognized.`+l),console.info(`
|
|
10
|
+
${c}Valid arguments are below.`+l),i(["infoCmds"]),process.exit(1)),process.argv.some(e=>v.infoCmds.help.test(e)))i();else if(process.argv.some(e=>v.infoCmds.version.test(e)))console.info("v"+require("./package.json").version);else{const b=s.generate({verbose:!1});a(b),console.log(f+b+l)}function i(e=["cmdFormat","formatOptions","infoCmds"]){const o={cmdFormat:[`
|
|
11
|
+
${c}generate-ip [commands]`+l],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};e.forEach(e=>{o[e]?.forEach(e=>{{const n=process.stdout.columns||80,t=[],o=e.match(/\S+|\s+/g);let r="";o.forEach(e=>{var o=n-(0===t.length?0:29);r.length+e.length>o&&(t.push(0===t.length?r:r.trimStart()),r=""),r+=e}),t.push(0===t.length?r:r.trimStart()),t.forEach((e,o)=>console.info(0===o?e:" ".repeat(29)+e))}})})}function a(e){e=e.replace(/\s+$/,"").replace(/"/g,'""'),"darwin"===process.platform?n(`printf "${e}" | pbcopy`):"linux"===process.platform?n(`printf "${e}" | xclip -selection clipboard`):"win32"===process.platform&&n(`Set-Clipboard -Value "${e}"`,{shell:"powershell"})}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "generate-ip",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Randomly generate IPv4 and IPv6 addresses.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
@@ -10,7 +10,10 @@
|
|
|
10
10
|
"homepage": "https://github.com/adamlui/js-utils",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"main": "dist/generate-ip.min.js",
|
|
13
|
-
"files": [
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"docs"
|
|
16
|
+
],
|
|
14
17
|
"bin": {
|
|
15
18
|
"generateip": "dist/generate-ip.min.js",
|
|
16
19
|
"generate-ip": "dist/generate-ip.min.js"
|