generate-ip 2.0.0 → 2.0.1
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 +45 -14
- 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.1"><img height=31 src="https://img.shields.io/badge/Latest_Build-2.0.1-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
|
|
|
@@ -72,7 +72,7 @@ Available options (passed as object properties):
|
|
|
72
72
|
|
|
73
73
|
Name | Type | Description | Default Value
|
|
74
74
|
------------|---------|--------------------------------------------------------------------------------|---------------
|
|
75
|
-
`verbose` |
|
|
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
97
|
Name | Type | Description | Default Value
|
|
91
98
|
------------|---------|--------------------------------------------------------------------------------|---------------
|
|
92
|
-
`verbose` |
|
|
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
|
|
|
@@ -112,14 +119,14 @@ Available options (passed as object properties):
|
|
|
112
119
|
|
|
113
120
|
Name | Type | Description | Default Value
|
|
114
121
|
---------------|---------|--------------------------------------------------------------------------------|---------------
|
|
115
|
-
`verbose` |
|
|
116
|
-
`leadingZeros` |
|
|
117
|
-
`doubleColon` |
|
|
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
|
|
|
@@ -141,9 +148,33 @@ Available options:
|
|
|
141
148
|
|
|
142
149
|
Name | Type | Description | Default Value
|
|
143
150
|
---------------|---------|--------------------------------------------------------------------------------|---------------
|
|
144
|
-
`verbose` |
|
|
145
|
-
`leadingZeros` |
|
|
146
|
-
`doubleColon` |
|
|
151
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
152
|
+
`leadingZeros` | Boolean | Include leading zeros in hex pieces. | `false`
|
|
153
|
+
`doubleColon` | Boolean | Replace series of zeros w/ `::` | `true`
|
|
154
|
+
|
|
155
|
+
#
|
|
156
|
+
|
|
157
|
+
#### `ipv6.validate(address[, options])`
|
|
158
|
+
|
|
159
|
+
Checks if given `address` is a valid IPv6 address:
|
|
160
|
+
|
|
161
|
+
```js
|
|
162
|
+
const ipIsValid = ipv6.validate('0:0:0:0:0:ffff:192.1.56.10/96');
|
|
163
|
+
console.log(ipIsValid);
|
|
164
|
+
|
|
165
|
+
/* outputs:
|
|
166
|
+
|
|
167
|
+
ipv6.validate() » Validating 0:0:0:0:0:ffff:192.1.56.10/96...
|
|
168
|
+
ipv6.validate() » IP is valid IPv6 address!
|
|
169
|
+
true
|
|
170
|
+
*/
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Available options (passed as object properties):
|
|
174
|
+
|
|
175
|
+
Name | Type | Description | Default Value
|
|
176
|
+
------------|---------|--------------------------------------------------------------------------------|---------------
|
|
177
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
147
178
|
|
|
148
179
|
<br>
|
|
149
180
|
|
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 s=require("crypto")["randomInt"],n=require("child_process")["execSync"],i={generate:function(e={}){var o={verbose:!0};e={...o,...e};for(const t of Object.keys(e))if(!Object.prototype.hasOwnProperty.call(o,t))return console.error(`ipv4.generate() » ERROR: \`${t}\` is an invalid option.`+`
|
|
3
|
+
ipv4.generate() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e.verbose)return console.error("ipv4.generate() » ERROR: [verbose] option can only be `true` or `false`.");e.verbose&&console.info("ipv4.generate() » Generating IPv4 address...");var r=[];for(let e=0;e<4;e++)r.push(s(0,256));var n=r.join(".");return e.verbose&&console.log("\n"+n),n},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.verbose)return console.error("ipv4.validate() » ERROR: [verbose] option can only be `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 t in e={...o,...e}){if(!Object.prototype.hasOwnProperty.call(o,t))return console.error(`ipv6.generate() » ERROR: \`${t}\` is an invalid option.`+`
|
|
5
|
+
ipv6.generate() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e[t])return console.error(`ipv6.generate() » ERROR: [${t}] 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+=s(0,16).toString(16);r.push(o)}var n=this.format(r.join(":"),{...e,verbose:!1});return e.verbose&&console.log("\n"+n),n},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.");for(const a in o){if(!Object.prototype.hasOwnProperty.call(t,a))return console.error(`ipv6.format() » ERROR: \`${a}\` is an invalid option.`+`
|
|
6
|
+
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\`.`)}if(!this.validate(e,{verbose:!1}))return console.error(`ipv6.format() » ERROR:
|
|
7
|
+
- ${e} is not a valid IPv6 address.`);o.verbose&&console.info(`ipv6.format() » Formatting ${e}...`);let s=e;if(s=o.doubleColon?(o.verbose&&console.info("ipv6.format() » Replacing zero series w/ '::'..."),s.replace(/:(?:0+:)+/,"::")):(o.verbose&&console.info("ipv6.format() » Expanding '::' into zero series..."),n=s.split(":").filter(Boolean).length,r=o.leadingZeros?"0000":"0",n=Array(8-n).fill(r).join(":"),s.replace("::",`:${n}:`)),o.leadingZeros){o.verbose&&console.info("ipv6.format() » Adding leading zeros...");var i=s.split(":");for(let e=0;e<i.length;e++)for(;i[e].length<4;)i[e]="0"+i[e];s=i.join(":")}else o.verbose&&console.info("ipv6.format() » Stripping leading zeros..."),s=e.replace(/(^|(?<=:))0+(?!:)/g,"$1");return o.verbose&&console.info(s!==e?s:"ipv6.format() » Address cannot be formatted!\n"),s},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 s of Object.keys(o))if(!Object.prototype.hasOwnProperty.call(r,s))return console.error(`ipv6.validate() » ERROR: \`${s}\` is an invalid option.`+`
|
|
8
|
+
ipv6.validate() » Valid options: [ ${Object.keys(r).join(", ")} ]`);if("boolean"!=typeof o.verbose)return console.error("ipv6.validate() » ERROR: [verbose] 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&&i.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:i,ipv6:e};else{const l="[0m",p="[1;91m",c="[1;33m",v="[1;97m",f={infoCmds:{help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/}};let o=!1,r;for(const d of Object.keys(f))for(const b of Object.keys(f[d]))o||process.argv.forEach(e=>{e.startsWith("-")&&(r=f[d][b].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),t(["infoCmds"]),process.exit(1)),process.argv.some(e=>/^--?h(?:elp)?$/.test(e)))t();else if(process.argv.some(e=>/^--?ve?r?s?i?o?n?$/.test(e)))console.info("v"+require("./package.json").version);else{const g=i.generate({verbose:!1});a(g),console.log(v+g+l)}function t(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.1",
|
|
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"
|