|
| 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 | +} |
0 commit comments