aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeyla Jähnig <[email protected]>2022-10-14 23:54:31 +0200
committerGitHub <[email protected]>2022-10-14 21:54:31 +0000
commita90f2fe65c705a5593215b0a35945b77c1d575f1 (patch)
treebec933c9817ff19c05654a9376333c3c74a39316
parent9c1437d6034ef5537c079746761c4c71347f768b (diff)
downloadfaker-a90f2fe65c705a5593215b0a35945b77c1d575f1.tar.xz
faker-a90f2fe65c705a5593215b0a35945b77c1d575f1.zip
feat(internet)!: ip now returns ipv4 and ipv6 (#1059)
-rw-r--r--src/modules/internet/index.ts6
-rw-r--r--test/__snapshots__/internet.spec.ts.snap6
-rw-r--r--test/internet.spec.ts12
3 files changed, 17 insertions, 7 deletions
diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts
index 2da5b59f..3c4eedee 100644
--- a/src/modules/internet/index.ts
+++ b/src/modules/internet/index.ts
@@ -282,16 +282,16 @@ export class InternetModule {
}
/**
- * Generates a random IPv4 address.
+ * Generates a random IPv4 or IPv6 address.
*
* @example
* faker.internet.ip() // '245.108.222.0'
+ * faker.internet.ip() // '4e5:f9c5:4337:abfd:9caf:1135:41ad:d8d3'
*
* @since 2.0.1
*/
ip(): string {
- // TODO @Shinigami92 2022-03-21: We may want to return a IPv4 or IPv6 address here in a later major release
- return this.ipv4();
+ return this.faker.datatype.boolean() ? this.ipv4() : this.ipv6();
}
/**
diff --git a/test/__snapshots__/internet.spec.ts.snap b/test/__snapshots__/internet.spec.ts.snap
index 697bc613..d2c43911 100644
--- a/test/__snapshots__/internet.spec.ts.snap
+++ b/test/__snapshots__/internet.spec.ts.snap
@@ -34,7 +34,7 @@ exports[`internet > 42 > httpStatusCode > noArgs 1`] = `207`;
exports[`internet > 42 > httpStatusCode > with options 1`] = `410`;
-exports[`internet > 42 > ip 1`] = `"95.203.243.46"`;
+exports[`internet > 42 > ip 1`] = `"cf2b:c992:7210:7d59:2ba0:0fbd:f302:f294"`;
exports[`internet > 42 > ipv4 1`] = `"95.203.243.46"`;
@@ -94,7 +94,7 @@ exports[`internet > 1211 > httpStatusCode > noArgs 1`] = `505`;
exports[`internet > 1211 > httpStatusCode > with options 1`] = `429`;
-exports[`internet > 1211 > ip 1`] = `"237.117.228.199"`;
+exports[`internet > 1211 > ip 1`] = `"117.228.199.57"`;
exports[`internet > 1211 > ipv4 1`] = `"237.117.228.199"`;
@@ -154,7 +154,7 @@ exports[`internet > 1337 > httpStatusCode > noArgs 1`] = `205`;
exports[`internet > 1337 > httpStatusCode > with options 1`] = `407`;
-exports[`internet > 1337 > ip 1`] = `"67.143.40.54"`;
+exports[`internet > 1337 > ip 1`] = `"8234:8705:3894:5f4b:41c6:1a52:bf27:dccc"`;
exports[`internet > 1337 > ipv4 1`] = `"67.143.40.54"`;
diff --git a/test/internet.spec.ts b/test/internet.spec.ts
index 1dbccc23..cbbf2745 100644
--- a/test/internet.spec.ts
+++ b/test/internet.spec.ts
@@ -368,11 +368,21 @@ describe('internet', () => {
});
describe('ip()', () => {
- it('should return a random IPv4 address with four parts', () => {
+ it('should return a random IPv4 or IPv6 address', () => {
const ip = faker.internet.ip();
expect(ip).toBeTruthy();
expect(ip).toBeTypeOf('string');
+ expect(ip).toSatisfy(validator.isIP);
+ });
+ });
+
+ describe('ipv4()', () => {
+ it('should return a random IPv4 with four parts', () => {
+ const ip = faker.internet.ipv4();
+
+ expect(ip).toBeTruthy();
+ expect(ip).toBeTypeOf('string');
expect(ip).toSatisfy((value: string) => validator.isIP(value, 4));
const parts = ip.split('.');