Skip to content

Commit 4929710

Browse files
authored
Merge pull request #130 from mrstebo/feature/internet
Feature/internet
2 parents 3be9e8c + 7af08d3 commit 4929710

File tree

10 files changed

+520
-6
lines changed

10 files changed

+520
-6
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

Gulpfile.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ gulp.task('test', ['istanbul'], () => {
4040
colors: true,
4141
harmony: true
4242
}))
43-
.on('error', function(err) {
44-
gutil.log(gutil.colors.red(err));
45-
this.emit('end');
46-
})
47-
.pipe(istanbul.writeReports())
4843
.pipe(istanbul.enforceThresholds({ thresholds: { global: 90 } }));
44+
// .pipe(istanbul.writeReports())
4945
});
5046

5147
gulp.task('coveralls', ['istanbul', 'test'], () => {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Contents
4242
- [Faker.Hacker](doc/hacker.md)
4343
- [Faker.HarryPotter](doc/harry_potter.md)
4444
- [Faker.Hipster](doc/hipster.md)
45+
- [Faker.Internet](doc/internet.md)
4546
- [Faker.LordOfTheRings](doc/lord_of_the_rings.md)
4647
- [Faker.Lorem](doc/lorem.md)
4748
- [Faker.LoremFlickr](doc/lorem_flickr.md)

data/internet.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"freeEmails": [
3+
"gmail.com",
4+
"yahoo.com",
5+
"hotmail.com"
6+
],
7+
"domainSuffixes": [
8+
"com",
9+
"biz",
10+
"info",
11+
"name",
12+
"net",
13+
"org",
14+
"io",
15+
"co"
16+
]
17+
}

doc/internet.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Faker.Internet
2+
3+
```js
4+
// Optional argument name=nil
5+
Faker.Internet.email() //=> "eliza@mann.net"
6+
7+
Faker.Internet.email('Nancy') //=> "nancy@terry.biz"
8+
9+
// Optional argument name=nil
10+
Faker.Internet.freeEmail() //=> "freddy@gmail.com"
11+
12+
Faker.Internet.freeEmail('Nancy') //=> "nancy@yahoo.com"
13+
14+
// Optional argument name=nil
15+
Faker.Internet.safeEmail() //=> "christelle@example.org"
16+
17+
Faker.Internet.safeEmail('Nancy') //=> "nancy@example.net"
18+
19+
// Optional arguments specifier=nil, separators=%w(. _)
20+
Faker.Internet.userName() //=> "alexie"
21+
22+
Faker.Internet.userName('Nancy') //=> "nancy"
23+
24+
Faker.Internet.userName('Nancy Johnson', %w(. _ -)) //=> "johnson-nancy"
25+
26+
// Optional arguments: min_length=5, max_length=8
27+
Faker.Internet.userName(5..8)
28+
29+
// Optional arguments: min_length=8, max_length=16
30+
Faker.Internet.password() //=> "vg5msvy1uerg7"
31+
32+
Faker.Internet.password(8) //=> "yfgjik0hgzdqs0"
33+
34+
Faker.Internet.password(10, 20) //=> "eoc9shwd1hwq4vbgfw"
35+
36+
Faker.Internet.password(10, 20, true) //=> "3k5qS15aNmG"
37+
38+
Faker.Internet.password(10, 20, true, true) //=> "*%NkOnJsH4"
39+
40+
Faker.Internet.domainName() //=> "effertz.info"
41+
42+
Faker.Internet.fixUmlauts('äöüß') //=> "aeoeuess"
43+
44+
Faker.Internet.domainWord() //=> "haleyziemann"
45+
46+
Faker.Internet.domainSuffix() //=> "info"
47+
48+
Faker.Internet.ipV4Address() //=> "24.29.18.175"
49+
50+
// Private IP range according to RFC 1918 and 127.0.0.0/8 and 169.254.0.0/16.
51+
Faker.Internet.privateIPV4Address() //=> "10.0.0.1"
52+
53+
// Guaranteed not to be in the ip range from the private_ip_v4_address method.
54+
Faker.Internet.publicIPV4Address() //=> "24.29.18.175"
55+
56+
Faker.Internet.ipV4CIDR() //=> "24.29.18.175/21"
57+
58+
Faker.Internet.ipV6Address() //=> "ac5f:d696:3807:1d72:2eb5:4e81:7d2b:e1df"
59+
60+
Faker.Internet.ipV6CIDR() //=> "ac5f:d696:3807:1d72:2eb5:4e81:7d2b:e1df/78"
61+
62+
// Optional argument prefix=''
63+
Faker.Internet.macAddress() //=> "e6:0d:00:11:ed:4f"
64+
Faker.Internet.macAddress('55:44:33') //=> "55:44:33:02:1d:9b"
65+
66+
// Optional arguments: host=domain_name, path="/#{user_name}"
67+
Faker.Internet.url() //=> "http://thiel.com/chauncey_simonis"
68+
Faker.Internet.url('example.com') //=> "http://example.com/clotilde.swift"
69+
Faker.Internet.url('example.com', '/foobar.html') //=> "http://example.com/foobar.html"
70+
71+
// Optional arguments: words=nil, glue=nil
72+
Faker.Internet.slug() //=> "pariatur_laudantium"
73+
Faker.Internet.slug('foo bar') //=> "foo.bar"
74+
Faker.Internet.slug('foo bar', '-') //=> "foo-bar"
75+
```

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"version:patch": "npm version patch",
88
"prepublish": "./node_modules/.bin/gulp build",
9-
"test": "./node_modules/.bin/gulp test coveralls"
9+
"test": "./node_modules/.bin/gulp test coveralls",
10+
"watch": "./node_modules/.bin/gulp watch"
1011
},
1112
"repository": {
1213
"type": "git",

src/faker/internet.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import { itemFromCollection, randomNumber } from '../utils/random';
2+
import shuffle from '../utils/shuffle';
3+
4+
const data = require('../../data/internet.json');
5+
const nameData = require('../../data/name.json');
6+
const loremData = require('../../data/lorem.json');
7+
8+
// 0-9, a-z
9+
const CHARACTERS = [...Array(10).keys()].concat([...Array(26).keys()].map(i => String.fromCharCode(97+i)));
10+
const SYMBOLS = ['!', '@', '#', '$', '%', '^', '&', '*'];
11+
const PRIVATE_NET_REGEX = [
12+
/^10\./, // 10.0.0.0 – 10.255.255.255
13+
/^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, // 100.64.0.0 – 100.127.255.255
14+
/^127\./, // 127.0.0.0 – 127.255.255.255
15+
/^169\.254\./, // 169.254.0.0 – 169.254.255.255
16+
/^172\.(1[6-9]|2\d|3[0-1])\./, // 172.16.0.0 – 172.31.255.255
17+
/^192\.0\.0\./, // 192.0.0.0 – 192.0.0.255
18+
/^192\.168\./, // 192.168.0.0 – 192.168.255.255
19+
/^198\.(1[8-9])\./ // 198.18.0.0 – 198.19.255.255
20+
];
21+
const RESERVED_NETS_REGEX = [
22+
/^0\./, // 0.0.0.0 – 0.255.255.255
23+
/^192\.0\.2\./, // 192.0.2.0 – 192.0.2.255
24+
/^192\.88\.99\./, // 192.88.99.0 – 192.88.99.255
25+
/^198\.51\.100\./, // 198.51.100.0 – 198.51.100.255
26+
/^203\.0\.113\./, // 203.0.113.0 – 203.0.113.255
27+
/^(22[4-9]|23\d)\./, // 224.0.0.0 – 239.255.255.255
28+
/^(24\d|25[0-5])\./ // 240.0.0.0 – 255.255.255.254 and 255.255.255.255
29+
];
30+
31+
export function email(name=null) {
32+
return [
33+
userName(name),
34+
domainName()
35+
].join('@');
36+
}
37+
38+
export function freeEmail(name=null) {
39+
return [
40+
userName(name),
41+
itemFromCollection(data['freeEmails'])
42+
].join('@');
43+
}
44+
45+
export function safeEmail(name=null) {
46+
return [
47+
userName(name),
48+
`example.${itemFromCollection(['org', 'com', 'net'])}`
49+
].join('@');
50+
}
51+
52+
export function userName(specifier=null, separators=null) {
53+
const userNameSeparator = itemFromCollection(separators || ['.', '_']);
54+
if (typeof specifier === 'string') {
55+
return shuffle(specifier.match(/\w+/g).map(x => x)).join(userNameSeparator);
56+
}
57+
const firstName = itemFromCollection(nameData['firstNames']).toLowerCase();
58+
const lastName = itemFromCollection(nameData['lastNames']).toLowerCase();
59+
return itemFromCollection([
60+
firstName,
61+
[firstName, lastName].join(userNameSeparator)
62+
]);
63+
}
64+
65+
export function password(minLength=8, maxLength=16, mixCase=true, specialChars=false) {
66+
const diffLength = maxLength - minLength;
67+
const extraCharacters = randomNumber(0, diffLength + 1);
68+
const chars = specialChars ? [...CHARACTERS, ...SYMBOLS] : CHARACTERS;
69+
70+
return [...Array(minLength + extraCharacters).keys()].reduce((result, val, index) => {
71+
const c = itemFromCollection(chars).toString();
72+
return result + (mixCase && index % 2 == 0 ? c.toUpperCase() : c);
73+
}, '');
74+
}
75+
76+
export function domainName() {
77+
return [
78+
domainWord(),
79+
domainSuffix()
80+
].join('.');
81+
}
82+
83+
export function fixUmlauts(value='') {
84+
return value
85+
.replace(/ä/g, 'ae')
86+
.replace(/ö/g, 'oe')
87+
.replace(/ü/g, 'ue');
88+
}
89+
90+
export function domainWord() {
91+
return itemFromCollection(nameData['lastNames']);
92+
}
93+
94+
export function domainSuffix() {
95+
return itemFromCollection(data['domainSuffixes'])
96+
}
97+
98+
export function ipV4Address() {
99+
return [
100+
randomNumber(2, 254),
101+
randomNumber(2, 254),
102+
randomNumber(2, 254),
103+
randomNumber(2, 254)
104+
].join('.');
105+
}
106+
107+
export function privateIPV4Address() {
108+
let addr;
109+
do { addr = ipV4Address(); } while (!privateNetChecker(addr));
110+
return addr;
111+
}
112+
113+
export function publicIPV4Address() {
114+
let addr;
115+
do { addr = ipV4Address(); } while (reservedNetChecker(addr));
116+
return addr;
117+
}
118+
119+
export function ipV4CIDR() {
120+
return `${ipV4Address()}/${randomNumber(1, 32)}`;
121+
}
122+
123+
export function ipV6Address() {
124+
return [...Array(8).keys()].map(_ => randomNumber(4096, 65536).toString(16)).join(':');
125+
}
126+
127+
export function ipV6CIDR() {
128+
return `${ipV6Address()}/${randomNumber(1, 128)}`;
129+
}
130+
131+
export function macAddress(prefix='') {
132+
const prefixDigits = prefix.split(':').filter(x => x).map(x => parseInt(x, 16));
133+
const addressDigits = [...Array(6 - prefixDigits.length).keys()].map(x => randomNumber(0, 255));
134+
return [...prefixDigits, ...addressDigits].map(x => x.toString(16)).join(':');
135+
}
136+
137+
export function url(host=null, path=null, scheme='http') {
138+
host = host || domainName();
139+
path = path || `/${userName()}`;
140+
return `${scheme}://${host}${path}`;
141+
}
142+
143+
export function slug(words='', glue='') {
144+
return (words || [...Array(2).keys()].map(_ => itemFromCollection(loremData['words'])).join(' '))
145+
.replace(/\s+/g, glue || itemFromCollection(['-', '_', '.']))
146+
.toLowerCase();
147+
}
148+
149+
function privateNetChecker(addr) {
150+
return PRIVATE_NET_REGEX.some(x => addr.match(x));
151+
}
152+
153+
function reservedNetChecker(addr) {
154+
return [...PRIVATE_NET_REGEX, ...RESERVED_NETS_REGEX].some(x => addr.match(x));
155+
}

src/utils/shuffle.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default collection => {
2+
let i, j, k;
3+
const result = [...collection];
4+
for (i = collection.length; i > 0; i--) {
5+
j = Math.floor(Math.random() * i);
6+
k = result[i - 1];
7+
result[i - 1] = result[j];
8+
result[j] = k;
9+
}
10+
return result;
11+
}

0 commit comments

Comments
 (0)