generate-ip 2.0.1 → 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 +23 -22
- package/dist/generate-ip.min.js +8 -8
- package/package.json +1 -1
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
|
|
|
@@ -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` | Boolean | Show logging in console/terminal.
|
|
73
|
+
Name | Type | Description | Default Value
|
|
74
|
+
------------|---------|-----------------------------------|---------------
|
|
75
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
76
76
|
|
|
77
77
|
#
|
|
78
78
|
|
|
@@ -94,9 +94,9 @@ true
|
|
|
94
94
|
|
|
95
95
|
Available options (passed as object properties):
|
|
96
96
|
|
|
97
|
-
Name | Type | Description
|
|
98
|
-
|
|
99
|
-
`verbose` | Boolean | Show logging in console/terminal.
|
|
97
|
+
Name | Type | Description | Default Value
|
|
98
|
+
------------|---------|-----------------------------------|---------------
|
|
99
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
100
100
|
|
|
101
101
|
#
|
|
102
102
|
|
|
@@ -117,11 +117,11 @@ console.log(ip); // sample output: 1379:6748:810c:5e16:b6c9:ae2:939f:8f2a
|
|
|
117
117
|
|
|
118
118
|
Available options (passed as object properties):
|
|
119
119
|
|
|
120
|
-
Name | Type | Description
|
|
121
|
-
|
|
122
|
-
`verbose` | Boolean | Show logging in console/terminal.
|
|
123
|
-
`leadingZeros` | Boolean | Include leading zeros in hex pieces.
|
|
124
|
-
`doubleColon` | Boolean | Replace series of zeros w/ `::`
|
|
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`
|
|
125
125
|
|
|
126
126
|
#
|
|
127
127
|
|
|
@@ -131,26 +131,27 @@ Name | Type | Description
|
|
|
131
131
|
Formats an IPv6 address according to `options` passed, returning a string:
|
|
132
132
|
|
|
133
133
|
```js
|
|
134
|
-
const address = '0d::ffff:192.1.56.10/96'
|
|
134
|
+
const address = '0d::ffff:192.1.56.10/96',
|
|
135
135
|
formattedAddress = ipv6.format(address, { leadingZeros: true, doubleColon: false });
|
|
136
136
|
|
|
137
|
+
console.log(formattedAddress);
|
|
138
|
+
|
|
137
139
|
/* outputs:
|
|
138
140
|
|
|
139
141
|
ipv6.format() » Formatting 0d::ffff:192.1.56.10/96...
|
|
140
142
|
ipv6.format() » Expanding '::' into zero series...
|
|
141
143
|
ipv6.format() » Adding leading zeros...
|
|
142
|
-
|
|
143
144
|
000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96
|
|
144
145
|
*/
|
|
145
146
|
```
|
|
146
147
|
|
|
147
148
|
Available options:
|
|
148
149
|
|
|
149
|
-
Name | Type | Description
|
|
150
|
-
|
|
151
|
-
`verbose` | Boolean | Show logging in console/terminal.
|
|
152
|
-
`leadingZeros` | Boolean | Include leading zeros in hex pieces.
|
|
153
|
-
`doubleColon` | Boolean | Replace series of zeros w/ `::`
|
|
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`
|
|
154
155
|
|
|
155
156
|
#
|
|
156
157
|
|
|
@@ -172,9 +173,9 @@ true
|
|
|
172
173
|
|
|
173
174
|
Available options (passed as object properties):
|
|
174
175
|
|
|
175
|
-
Name | Type | Description
|
|
176
|
-
|
|
177
|
-
`verbose` | Boolean | Show logging in console/terminal.
|
|
176
|
+
Name | Type | Description | Default Value
|
|
177
|
+
------------|---------|-----------------------------------|---------------
|
|
178
|
+
`verbose` | Boolean | Show logging in console/terminal. | `true`
|
|
178
179
|
|
|
179
180
|
<br>
|
|
180
181
|
|
package/dist/generate-ip.min.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const
|
|
3
|
-
ipv4.generate() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e
|
|
4
|
-
ipv4.validate() » Valid options: [ ${Object.keys(r).join(", ")} ]`);if("boolean"!=typeof o
|
|
5
|
-
ipv6.generate() » Valid options: [ ${Object.keys(o).join(", ")} ]`);if("boolean"!=typeof e[
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
ipv6.validate() » Valid options: [ ${Object.keys(r).join(", ")} ]`);if("boolean"!=typeof o
|
|
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
9
|
${p}ERROR: Arg [${r}] not recognized.`+l),console.info(`
|
|
10
|
-
${c}Valid arguments are below.`+l),
|
|
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
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"})}}
|