generate-ip 2.1.0 → 2.1.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 +24 -47
- package/dist/cli.min.js +5 -0
- package/dist/generate-ip.min.js +1 -11
- package/docs/LICENSE.md +1 -1
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
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.1.
|
|
6
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/generate-ip-2.1.2"><img height=31 src="https://img.shields.io/badge/Latest_Build-2.1.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
|
-
<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%2Fsrc%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>
|
|
8
|
+
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:generate-ip/src/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%2Fsrc%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
|
|
|
10
10
|
<br>
|
|
11
11
|
|
|
@@ -39,56 +39,42 @@ $ npm install generate-ip
|
|
|
39
39
|
|
|
40
40
|
### <img height=18 src="https://i.imgur.com/JIeAdsr.png"> Node.js
|
|
41
41
|
|
|
42
|
-
####
|
|
42
|
+
#### ES module*:
|
|
43
43
|
|
|
44
44
|
```js
|
|
45
45
|
import { ipv4, ipv6 } from 'generate-ip';
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
####
|
|
48
|
+
#### CommonJS:
|
|
49
49
|
|
|
50
50
|
```js
|
|
51
51
|
const { ipv4, ipv6 } = require('generate-ip');
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
###### _*Node.js version 14 or higher required_
|
|
55
|
+
|
|
54
56
|
### <picture><source media="(prefers-color-scheme: dark)" srcset="https://i.imgur.com/JSEb19A.png"><img width=16 src="https://i.imgur.com/5VPxf9y.png"></picture> Web
|
|
55
57
|
|
|
56
|
-
#### HTML script tag:
|
|
58
|
+
#### <> HTML script tag:
|
|
57
59
|
|
|
58
60
|
```html
|
|
59
|
-
<script src="https://cdn.jsdelivr.net/npm/generate-ip@2.1.
|
|
61
|
+
<script src="https://cdn.jsdelivr.net/npm/generate-ip@2.1.2/dist/generate-ip.min.js"></script>
|
|
60
62
|
```
|
|
61
63
|
|
|
62
64
|
#### ES6:
|
|
63
65
|
|
|
64
66
|
```js
|
|
65
67
|
(async () => {
|
|
66
|
-
await import('https://cdn.jsdelivr.net/npm/generate-ip@2.1.
|
|
68
|
+
await import('https://cdn.jsdelivr.net/npm/generate-ip@2.1.2/dist/generate-ip.min.js');
|
|
67
69
|
// Your code here...
|
|
68
70
|
})();
|
|
69
71
|
```
|
|
70
72
|
|
|
71
|
-
#### ES5:
|
|
72
|
-
|
|
73
|
-
```js
|
|
74
|
-
var xhr = new XMLHttpRequest();
|
|
75
|
-
xhr.open('GET', 'https://cdn.jsdelivr.net/npm/generate-ip@2.1.0/dist/generate-ip.min.js');
|
|
76
|
-
xhr.onload = function() {
|
|
77
|
-
if (xhr.status === 200) {
|
|
78
|
-
var generateIP = document.createElement('script');
|
|
79
|
-
generateIP.textContent = xhr.responseText;
|
|
80
|
-
document.head.append(generateIP);
|
|
81
|
-
// Your code here...
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
xhr.send();
|
|
85
|
-
```
|
|
86
|
-
|
|
87
73
|
### <img height=17 src="https://raw.githubusercontent.com/KudoAI/chatgpt.js/main/starters/media/images/icons/tampermonkey-icon28.png"><img height=17.5 src="https://raw.githubusercontent.com/KudoAI/chatgpt.js/main/starters/media/images/icons/violentmonkey-icon100.png"> Greasemonkey
|
|
88
74
|
|
|
89
75
|
```js
|
|
90
76
|
...
|
|
91
|
-
// @require https://cdn.jsdelivr.net/npm/generate-ip@2.1.
|
|
77
|
+
// @require https://cdn.jsdelivr.net/npm/generate-ip@2.1.2/dist/generate-ip.min.js
|
|
92
78
|
// ==/UserScript==
|
|
93
79
|
|
|
94
80
|
// Your code here...
|
|
@@ -96,13 +82,17 @@ xhr.send();
|
|
|
96
82
|
|
|
97
83
|
<br>
|
|
98
84
|
|
|
85
|
+
📝 **Note:** To always import the latest version (not recommended in production!) remove the `@2.1.2` version tag from the jsDelivr URL: `https://cdn.jsdelivr.net/npm/generate-ip/dist/generate-ip.min.js`
|
|
86
|
+
|
|
87
|
+
<br>
|
|
88
|
+
|
|
99
89
|
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
100
90
|
|
|
101
91
|
## 📋 API usage
|
|
102
92
|
|
|
103
93
|
### `ipv4` methods
|
|
104
94
|
|
|
105
|
-
Use the `ipv4` methods to [**generate**](#ipv4generateoptions) and [**validate**](#ipv4validateaddress-options) IPv4 addresses.
|
|
95
|
+
💡 Use the `ipv4` methods to [**generate**](#ipv4generateoptions) and [**validate**](#ipv4validateaddress-options) IPv4 addresses.
|
|
106
96
|
|
|
107
97
|
#
|
|
108
98
|
|
|
@@ -112,15 +102,7 @@ Generates **one** IPv4 address if `qty` option is not given, returning a string:
|
|
|
112
102
|
|
|
113
103
|
```js
|
|
114
104
|
const ip = ipv4.generate();
|
|
115
|
-
console.log(ip);
|
|
116
|
-
|
|
117
|
-
/* sample output:
|
|
118
|
-
|
|
119
|
-
ipv4.generate() » Generating IPv4 address...
|
|
120
|
-
ipv4.generate() » IPv4 address generated!
|
|
121
|
-
ipv4.generate() » Check returned string.
|
|
122
|
-
'36.42.224.208'
|
|
123
|
-
*/
|
|
105
|
+
console.log(ip); // sample output: '36.42.224.208'
|
|
124
106
|
```
|
|
125
107
|
|
|
126
108
|
...or **multiple** IPv4 addresses if `qty` option is given, returning an array of strings:
|
|
@@ -173,7 +155,7 @@ Name | Type | Description | Default Value
|
|
|
173
155
|
|
|
174
156
|
### `ipv6` methods
|
|
175
157
|
|
|
176
|
-
Use the `ipv6` methods to [**generate**](#ipv6generateoptions), [**format**](#ipv6formatipv6address-options), and [**validate**](#ipv6validateaddress-options) IPv6 addresses.
|
|
158
|
+
💡 Use the `ipv6` methods to [**generate**](#ipv6generateoptions), [**format**](#ipv6formatipv6address-options), and [**validate**](#ipv6validateaddress-options) IPv6 addresses.
|
|
177
159
|
|
|
178
160
|
#
|
|
179
161
|
|
|
@@ -183,15 +165,7 @@ Generates **one** IPv6 address if `qty` option is not given, returning a string:
|
|
|
183
165
|
|
|
184
166
|
```js
|
|
185
167
|
const ip = ipv6.generate();
|
|
186
|
-
console.log(ip);
|
|
187
|
-
|
|
188
|
-
/* sample output:
|
|
189
|
-
|
|
190
|
-
ipv6.generate() » Generating IPv6 address...
|
|
191
|
-
ipv6.generate() » IPv6 address generated!
|
|
192
|
-
ipv6.generate() » Check returned string.
|
|
193
|
-
'1379:6748:810c:5e16:b6c9:ae2:939f:8f2a'
|
|
194
|
-
*/
|
|
168
|
+
console.log(ip); // sample output: '1379:6748:810c:5e16:b6c9:ae2:939f:8f2a'
|
|
195
169
|
```
|
|
196
170
|
|
|
197
171
|
...or **multiple** IPv6 addresses if `qty` option is given, returning an array of strings:
|
|
@@ -242,7 +216,7 @@ console.log(formattedAddress);
|
|
|
242
216
|
ipv6.format() » Expanding '::' into zero series...
|
|
243
217
|
ipv6.format() » Adding leading zeros...
|
|
244
218
|
ipv6.format() » IP formatted successfully!
|
|
245
|
-
ipv6.format() »
|
|
219
|
+
ipv6.format() » 000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96
|
|
246
220
|
'000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96'
|
|
247
221
|
*/
|
|
248
222
|
```
|
|
@@ -298,6 +272,9 @@ Generated IP is logged to the console + copied to the clipboard.
|
|
|
298
272
|
### Command line options
|
|
299
273
|
|
|
300
274
|
```
|
|
275
|
+
Parameter options:
|
|
276
|
+
-q, --quiet Suppress all logging except errors.
|
|
277
|
+
|
|
301
278
|
Info commands:
|
|
302
279
|
-h, --help Display help screen.
|
|
303
280
|
-v, --version Show version number.
|
|
@@ -309,7 +286,7 @@ Info commands:
|
|
|
309
286
|
|
|
310
287
|
## 🏛️ MIT License
|
|
311
288
|
|
|
312
|
-
**Copyright © 2024 [Adam Lui](https://github.com/adamlui)**
|
|
289
|
+
**Copyright © 2024 [Adam Lui](https://github.com/adamlui) & contributors**
|
|
313
290
|
|
|
314
291
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
315
292
|
|
|
@@ -321,6 +298,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|
|
321
298
|
|
|
322
299
|
<img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
|
|
323
300
|
|
|
324
|
-
<a href="https://github.com/adamlui/js-utils">**
|
|
301
|
+
<a href="https://github.com/adamlui/js-utils">**JavaScript utilities**</a> /
|
|
325
302
|
<a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
|
|
326
303
|
<a href="#-generate-ip">Back to top ↑</a>
|
package/dist/cli.min.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const e=require(__dirname.match(/src/)?"./generate-ip":"./generate-ip.min")["ipv4"],o=require("child_process")["execSync"],r="[0m",n="[1;91m",t="[1;33m",i={},a={flags:{quietMode:/^--?q(?:uiet)?(?:-?mode)?$/},infoCmds:{help:/^--?h(?:elp)?$/,version:/^--?ve?r?s?i?o?n?$/}};if(process.argv.forEach(o=>{var e,s;o.startsWith("-")&&(e=Object.keys(a.flags).find(e=>a.flags[e].test(o)),s=Object.keys(a.infoCmds).find(e=>a.infoCmds[e].test(o)),e?i[e]=!0:s||(console.error(`
|
|
3
|
+
${n}ERROR: Arg [${o}] not recognized.`+r),console.info(`
|
|
4
|
+
${t}Valid arguments are below.`+r),c(["flags","infoCmds"]),process.exit(1)))}),process.argv.some(e=>a.infoCmds.help.test(e)))c();else if(process.argv.some(e=>a.infoCmds.version.test(e)))console.info("v"+require("./package.json").version);else{const l=e.generate({verbose:!i.quietMode});i.quietMode||console.info("\nCopying to clipboard..."),s(l)}function c(e=["cmdFormat","formatOptions","infoCmds"]){const o={cmdFormat:[`
|
|
5
|
+
${t}generate-ip [options|commands]`+r],flags:["\nBoolean options:"," -q, --quiet Suppress all logging except errors."],infoCmds:["\nInfo commands:"," -h, --help Display help screen."," -v, --version Show version number."]};e.forEach(e=>{o[e]?.forEach(e=>{{const r=process.stdout.columns||80,n=[],o=e.match(/\S+|\s+/g);let s="";o.forEach(e=>{var o=r-(0===n.length?0:29);s.length+e.length>o&&(n.push(0===n.length?s:s.trimStart()),s=""),s+=e}),n.push(0===n.length?s:s.trimStart()),n.forEach((e,o)=>console.info(0===o?e:" ".repeat(29)+e))}})})}function s(e){e=e.replace(/\s+$/,"").replace(/"/g,'""'),"darwin"===process.platform?o(`printf "${e}" | pbcopy`):"linux"===process.platform?o(`printf "${e}" | xclip -selection clipboard`):"win32"===process.platform&&o(`Set-Clipboard -Value "${e}"`,{shell:"powershell"})}
|
package/dist/generate-ip.min.js
CHANGED
|
@@ -1,11 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const i=require("crypto")["randomInt"],n=require("child_process")["execSync"],s={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[t])return console.error(`ipv4.generate() » ERROR: [${t}] 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(i(0,256));var n=r.join(".");return e.verbose&&console.info("ipv4.generate() » IPv4 address generated!"+(require.main!==module?"\nipv4.generate() » Check returned string.":"")),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[n])return console.error(`ipv4.validate() » ERROR: [${n}] option can only be set to \`true\` or \`false\`.`)}o.verbose&&console.info("ipv4.validate() » Validating IPv4 address...");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+=i(0,16).toString(16);r.push(o)}var n=this.format(r.join(":"),{...e,verbose:!1});return e.verbose&&console.info("ipv6.generate() » IPv6 address generated!"+(require.main!==module?"\nipv6.generate() » Check returned string.":"")),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.");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\`.`)}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() » IP formatted successfully!\nipv6.format() » Check returned string."):console.info("ipv6.format() » IP already formatted to specs.")),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 IPv6 address...");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",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 g of Object.keys(f[d]))o||process.argv.forEach(e=>{e.startsWith("-")&&(r=f[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),t(["infoCmds"]),process.exit(1)),process.argv.some(e=>f.infoCmds.help.test(e)))t();else if(process.argv.some(e=>f.infoCmds.version.test(e)))console.info("v"+require("./package.json").version);else{const u=s.generate({verbose:!1});a(u),console.log(v+u+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"})}}
|
|
1
|
+
let l;try{l=require("crypto").randomInt}catch(e){const n=window.crypto||window.msCrypto;l=(e,o)=>{var r=n?.getRandomValues(new Uint32Array(1))[0]/4294967295||Math.random();return Math.floor(r*(o-e))+e}}const s={generate:function(o={}){var e={verbose:!0,qty:1};for(const i in o={...e,...o}){if(!Object.prototype.hasOwnProperty.call(e,i))return console.error(`ipv4.generate() » ERROR: \`${i}\` is an invalid option.`),void console.info(`ipv4.generate() » Valid options: [ ${Object.keys(e).join(", ")} ]`);if(["verbose"].includes(i)&&"boolean"!=typeof o[i])return console.error(`ipv4.generate() » ERROR: [${i}] option can only be \`true\` or \`false\`.`);if(["qty"].includes(i)&&(isNaN(o[i])||o[i]<1))return console.error(`ipv4.generate() » ERROR: [${i}] option can only be an integer > 0.`)}o.verbose&&console.info(`ipv4.generate() » Generating IPv4 address${1<o.qty?"es":""}...`);var r=[];if(1<o.qty)for(let e=0;e<o.qty;e++)r.push(this.generate({...o,qty:1,verbose:!1}));else{var n=[];for(let e=0;e<4;e++)n.push(l(0,256));r.push(n.join("."))}var t=1<o.qty?r:r[0];return o.verbose&&(console.info(`ipv4.generate() » IPv4 address${1<o.qty?"es":""} generated!`),console.info(1===o.qty?"ipv4.generate() » "+t:"ipv4.generate() » Check returned array.")),t},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 in o){if(!Object.prototype.hasOwnProperty.call(r,n))return console.error(`ipv4.validate() » ERROR: \`${n}\` is an invalid option.`),void console.info(`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 IPv4 address...");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(o={}){var e={verbose:!0,qty:1,leadingZeros:!1,doubleColon:!0};for(const t in o={...e,...o}){if(!Object.prototype.hasOwnProperty.call(e,t))return console.error(`ipv6.generate() » ERROR: \`${t}\` is an invalid option.`),void console.info(`ipv6.generate() » Valid options: [ ${Object.keys(e).join(", ")} ]`);if(["verbose","leadingZeros","doubleColon"].includes(t)&&"boolean"!=typeof o[t])return console.error(`ipv6.generate() » ERROR: [${t}] option can only be \`true\` or \`false\`.`);if(["qty"].includes(t)&&(isNaN(o[t])||o[t]<1))return console.error(`ipv6.generate() » ERROR: [${t}] option can only be an integer > 0.`)}o.verbose&&console.info(`ipv6.generate() » Generating IPv6 address${1<o.qty?"es":""}...`);var r=[];if(1<o.qty)for(let e=0;e<o.qty;e++)r.push(this.generate({...o,qty:1,verbose:!1}));else{const i=[],{qty:s,...a}=o;for(let e=0;e<8;e++){let o="";for(let e=0;e<4;e++)o+=l(0,16).toString(16);i.push(o)}r.push(this.format(i.join(":"),{...a,verbose:!1}))}var n=1<o.qty?r:r[0];return o.verbose&&(console.info(`ipv6.generate() » IPv6 address${1<o.qty?"es":""} generated!`),console.info(1===o.qty?"ipv6.generate() » "+n:"ipv6.generate() » Check returned array.")),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.");if(!this.validate(e,{verbose:!1}))return console.error(`ipv6.format() » ERROR: ${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.`),void console.info(`ipv6.format() » Valid options: [ ${Object.keys(t).join(", ")} ]`);if("boolean"!=typeof o[a])return console.error(`ipv6.format() » ERROR: [${a}] option can only be set to \`true\` or \`false\`.`)}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() » IP formatted successfully!"):console.info("ipv6.format() » IP already formatted to specs."),console.info("ipv6.format() » "+i)),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.`),void console.info(`ipv6.validate() » Valid options: [ ${Object.keys(r).join(", ")} ]`);if("boolean"!=typeof o[i])return console.error(`ipv6.validate() » ERROR: [${i}] option can only be set to \`true\` or \`false\`.`)}o.verbose&&console.info("ipv6.validate() » Validating IPv6 address...");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}};try{module.exports={ipv4:s,ipv6:e}}catch(e){}try{window.ipv4=s,window.ipv6=e}catch(e){}
|
package/docs/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 🏛️ MIT License
|
|
2
2
|
|
|
3
|
-
**Copyright © 2024 [Adam Lui](https://github.com/adamlui)**
|
|
3
|
+
**Copyright © 2024 [Adam Lui](https://github.com/adamlui) & contributors**
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "generate-ip",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.2",
|
|
4
4
|
"description": "Randomly generate IPv4 and IPv6 addresses.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
7
7
|
"email": "adam@kudoai.com",
|
|
8
8
|
"url": "https://github.com/adamlui"
|
|
9
9
|
},
|
|
10
|
-
"homepage": "https://
|
|
10
|
+
"homepage": "https://js-utils.com/generate-ip",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"main": "dist/generate-ip.min.js",
|
|
13
13
|
"files": [
|
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
"generateip": "dist/cli.min.js",
|
|
19
19
|
"generate-ip": "dist/cli.min.js"
|
|
20
20
|
},
|
|
21
|
+
"directories": {
|
|
22
|
+
"lib": "./src",
|
|
23
|
+
"doc": "./docs"
|
|
24
|
+
},
|
|
21
25
|
"scripts": {
|
|
22
26
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
23
27
|
"build": "minify-js src dist",
|
|
@@ -52,6 +56,6 @@
|
|
|
52
56
|
"url": "https://github.com/sponsors/adamlui"
|
|
53
57
|
},
|
|
54
58
|
"devDependencies": {
|
|
55
|
-
"@adamlui/minify.js": "^1.4.
|
|
59
|
+
"@adamlui/minify.js": "^1.4.8"
|
|
56
60
|
}
|
|
57
61
|
}
|