Skip to content

Commit

Permalink
Merge pull request #130 from mrstebo/feature/internet
Browse files Browse the repository at this point in the history
Feature/internet
  • Loading branch information
mrstebo authored Oct 15, 2017
2 parents 3be9e8c + 7af08d3 commit 4929710
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
6 changes: 1 addition & 5 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ gulp.task('test', ['istanbul'], () => {
colors: true,
harmony: true
}))
.on('error', function(err) {
gutil.log(gutil.colors.red(err));
this.emit('end');
})
.pipe(istanbul.writeReports())
.pipe(istanbul.enforceThresholds({ thresholds: { global: 90 } }));
// .pipe(istanbul.writeReports())
});

gulp.task('coveralls', ['istanbul', 'test'], () => {
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Contents
- [Faker.Hacker](doc/hacker.md)
- [Faker.HarryPotter](doc/harry_potter.md)
- [Faker.Hipster](doc/hipster.md)
- [Faker.Internet](doc/internet.md)
- [Faker.LordOfTheRings](doc/lord_of_the_rings.md)
- [Faker.Lorem](doc/lorem.md)
- [Faker.LoremFlickr](doc/lorem_flickr.md)
Expand Down
17 changes: 17 additions & 0 deletions data/internet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"freeEmails": [
"gmail.com",
"yahoo.com",
"hotmail.com"
],
"domainSuffixes": [
"com",
"biz",
"info",
"name",
"net",
"org",
"io",
"co"
]
}
75 changes: 75 additions & 0 deletions doc/internet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Faker.Internet

```js
// Optional argument name=nil
Faker.Internet.email() //=> "[email protected]"

Faker.Internet.email('Nancy') //=> "[email protected]"

// Optional argument name=nil
Faker.Internet.freeEmail() //=> "[email protected]"

Faker.Internet.freeEmail('Nancy') //=> "[email protected]"

// Optional argument name=nil
Faker.Internet.safeEmail() //=> "[email protected]"

Faker.Internet.safeEmail('Nancy') //=> "[email protected]"

// Optional arguments specifier=nil, separators=%w(. _)
Faker.Internet.userName() //=> "alexie"

Faker.Internet.userName('Nancy') //=> "nancy"

Faker.Internet.userName('Nancy Johnson', %w(. _ -)) //=> "johnson-nancy"

// Optional arguments: min_length=5, max_length=8
Faker.Internet.userName(5..8)

// Optional arguments: min_length=8, max_length=16
Faker.Internet.password() //=> "vg5msvy1uerg7"

Faker.Internet.password(8) //=> "yfgjik0hgzdqs0"

Faker.Internet.password(10, 20) //=> "eoc9shwd1hwq4vbgfw"

Faker.Internet.password(10, 20, true) //=> "3k5qS15aNmG"

Faker.Internet.password(10, 20, true, true) //=> "*%NkOnJsH4"

Faker.Internet.domainName() //=> "effertz.info"

Faker.Internet.fixUmlauts('äöüß') //=> "aeoeuess"

Faker.Internet.domainWord() //=> "haleyziemann"

Faker.Internet.domainSuffix() //=> "info"

Faker.Internet.ipV4Address() //=> "24.29.18.175"

// Private IP range according to RFC 1918 and 127.0.0.0/8 and 169.254.0.0/16.
Faker.Internet.privateIPV4Address() //=> "10.0.0.1"

// Guaranteed not to be in the ip range from the private_ip_v4_address method.
Faker.Internet.publicIPV4Address() //=> "24.29.18.175"

Faker.Internet.ipV4CIDR() //=> "24.29.18.175/21"

Faker.Internet.ipV6Address() //=> "ac5f:d696:3807:1d72:2eb5:4e81:7d2b:e1df"

Faker.Internet.ipV6CIDR() //=> "ac5f:d696:3807:1d72:2eb5:4e81:7d2b:e1df/78"

// Optional argument prefix=''
Faker.Internet.macAddress() //=> "e6:0d:00:11:ed:4f"
Faker.Internet.macAddress('55:44:33') //=> "55:44:33:02:1d:9b"

// Optional arguments: host=domain_name, path="/#{user_name}"
Faker.Internet.url() //=> "http://thiel.com/chauncey_simonis"
Faker.Internet.url('example.com') //=> "http://example.com/clotilde.swift"
Faker.Internet.url('example.com', '/foobar.html') //=> "http://example.com/foobar.html"

// Optional arguments: words=nil, glue=nil
Faker.Internet.slug() //=> "pariatur_laudantium"
Faker.Internet.slug('foo bar') //=> "foo.bar"
Faker.Internet.slug('foo bar', '-') //=> "foo-bar"
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"version:patch": "npm version patch",
"prepublish": "./node_modules/.bin/gulp build",
"test": "./node_modules/.bin/gulp test coveralls"
"test": "./node_modules/.bin/gulp test coveralls",
"watch": "./node_modules/.bin/gulp watch"
},
"repository": {
"type": "git",
Expand Down
155 changes: 155 additions & 0 deletions src/faker/internet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { itemFromCollection, randomNumber } from '../utils/random';
import shuffle from '../utils/shuffle';

const data = require('../../data/internet.json');
const nameData = require('../../data/name.json');
const loremData = require('../../data/lorem.json');

// 0-9, a-z
const CHARACTERS = [...Array(10).keys()].concat([...Array(26).keys()].map(i => String.fromCharCode(97+i)));
const SYMBOLS = ['!', '@', '#', '$', '%', '^', '&', '*'];
const PRIVATE_NET_REGEX = [
/^10\./, // 10.0.0.0 – 10.255.255.255
/^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, // 100.64.0.0 – 100.127.255.255
/^127\./, // 127.0.0.0 – 127.255.255.255
/^169\.254\./, // 169.254.0.0 – 169.254.255.255
/^172\.(1[6-9]|2\d|3[0-1])\./, // 172.16.0.0 – 172.31.255.255
/^192\.0\.0\./, // 192.0.0.0 – 192.0.0.255
/^192\.168\./, // 192.168.0.0 – 192.168.255.255
/^198\.(1[8-9])\./ // 198.18.0.0 – 198.19.255.255
];
const RESERVED_NETS_REGEX = [
/^0\./, // 0.0.0.0 – 0.255.255.255
/^192\.0\.2\./, // 192.0.2.0 – 192.0.2.255
/^192\.88\.99\./, // 192.88.99.0 – 192.88.99.255
/^198\.51\.100\./, // 198.51.100.0 – 198.51.100.255
/^203\.0\.113\./, // 203.0.113.0 – 203.0.113.255
/^(22[4-9]|23\d)\./, // 224.0.0.0 – 239.255.255.255
/^(24\d|25[0-5])\./ // 240.0.0.0 – 255.255.255.254 and 255.255.255.255
];

export function email(name=null) {
return [
userName(name),
domainName()
].join('@');
}

export function freeEmail(name=null) {
return [
userName(name),
itemFromCollection(data['freeEmails'])
].join('@');
}

export function safeEmail(name=null) {
return [
userName(name),
`example.${itemFromCollection(['org', 'com', 'net'])}`
].join('@');
}

export function userName(specifier=null, separators=null) {
const userNameSeparator = itemFromCollection(separators || ['.', '_']);
if (typeof specifier === 'string') {
return shuffle(specifier.match(/\w+/g).map(x => x)).join(userNameSeparator);
}
const firstName = itemFromCollection(nameData['firstNames']).toLowerCase();
const lastName = itemFromCollection(nameData['lastNames']).toLowerCase();
return itemFromCollection([
firstName,
[firstName, lastName].join(userNameSeparator)
]);
}

export function password(minLength=8, maxLength=16, mixCase=true, specialChars=false) {
const diffLength = maxLength - minLength;
const extraCharacters = randomNumber(0, diffLength + 1);
const chars = specialChars ? [...CHARACTERS, ...SYMBOLS] : CHARACTERS;

return [...Array(minLength + extraCharacters).keys()].reduce((result, val, index) => {
const c = itemFromCollection(chars).toString();
return result + (mixCase && index % 2 == 0 ? c.toUpperCase() : c);
}, '');
}

export function domainName() {
return [
domainWord(),
domainSuffix()
].join('.');
}

export function fixUmlauts(value='') {
return value
.replace(/ä/g, 'ae')
.replace(/ö/g, 'oe')
.replace(/ü/g, 'ue');
}

export function domainWord() {
return itemFromCollection(nameData['lastNames']);
}

export function domainSuffix() {
return itemFromCollection(data['domainSuffixes'])
}

export function ipV4Address() {
return [
randomNumber(2, 254),
randomNumber(2, 254),
randomNumber(2, 254),
randomNumber(2, 254)
].join('.');
}

export function privateIPV4Address() {
let addr;
do { addr = ipV4Address(); } while (!privateNetChecker(addr));
return addr;
}

export function publicIPV4Address() {
let addr;
do { addr = ipV4Address(); } while (reservedNetChecker(addr));
return addr;
}

export function ipV4CIDR() {
return `${ipV4Address()}/${randomNumber(1, 32)}`;
}

export function ipV6Address() {
return [...Array(8).keys()].map(_ => randomNumber(4096, 65536).toString(16)).join(':');
}

export function ipV6CIDR() {
return `${ipV6Address()}/${randomNumber(1, 128)}`;
}

export function macAddress(prefix='') {
const prefixDigits = prefix.split(':').filter(x => x).map(x => parseInt(x, 16));
const addressDigits = [...Array(6 - prefixDigits.length).keys()].map(x => randomNumber(0, 255));
return [...prefixDigits, ...addressDigits].map(x => x.toString(16)).join(':');
}

export function url(host=null, path=null, scheme='http') {
host = host || domainName();
path = path || `/${userName()}`;
return `${scheme}://${host}${path}`;
}

export function slug(words='', glue='') {
return (words || [...Array(2).keys()].map(_ => itemFromCollection(loremData['words'])).join(' '))
.replace(/\s+/g, glue || itemFromCollection(['-', '_', '.']))
.toLowerCase();
}

function privateNetChecker(addr) {
return PRIVATE_NET_REGEX.some(x => addr.match(x));
}

function reservedNetChecker(addr) {
return [...PRIVATE_NET_REGEX, ...RESERVED_NETS_REGEX].some(x => addr.match(x));
}
11 changes: 11 additions & 0 deletions src/utils/shuffle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default collection => {
let i, j, k;
const result = [...collection];
for (i = collection.length; i > 0; i--) {
j = Math.floor(Math.random() * i);
k = result[i - 1];
result[i - 1] = result[j];
result[j] = k;
}
return result;
}
Loading

0 comments on commit 4929710

Please sign in to comment.