Last active
January 8, 2019 23:11
-
-
Save chrdek/ab22354a00cc6086dc6c2a5f5308d173 to your computer and use it in GitHub Desktop.
Generate random urls with predefined query information.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name":"rand-url-create", | |
"version":"0.0.1", | |
"description":"Random URL Generator URI encoded param", | |
"license":"Creative Commons Zero v1.0 Universal", | |
"author":"C. Dek. chrdek - github" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Function for generating urls (with encoded url qstrings). | |
//gist: installs only avail. via NPM > v3 | |
module.exports = function createRandURL(obj, url=true) { | |
var siteName=""; | |
if (obj===null || typeof obj =='undefined') return `${siteName}`; | |
var protopart = [..."http://,https://".split(',')][(~~(Math.random()+"").substr(-4)%2)]; | |
var prefix = [...["www.",""]][[...Number((Math.random()+"").substr(2,6)).toString(2)].pop()]; | |
var qstrpart = "?"+Object.entries(obj).map(k=>typeof k[1]===typeof[]?k[1].map(e=>`${k[0]}=${encodeURIComponent(e)}`):`${k[0]}=${encodeURIComponent(k[1])}`).join("&").replace(/(\,)+/g,"&"); | |
var tldar = ["com", "co.uk", "pl", "cn", "cz", "io", "ru", "net", "nl", "es", "eu", "ro", "ac.uk", "org", "edu", "gov", "br"]; | |
var tldselectedpart = tldar[Number((Math.random()+"").substr(2,15).match(/[0-9]{2}/g).filter(e=>e<=tldar.length).pop())]; | |
var routepart = `/${(~new(Date)).toString(36).substr(-3)}/${(~new(Date)).toString(36).substr(-5)}`; | |
//Only alphanumeric characters (in lowercase) | |
var rndStringSet1 = [..."abdecfrtghyunji76kolpmwq9012zsx3485"].map(c=> Math.random().toString(36).substr(2)); | |
//Alphanumeric - with dashes in between characters.. | |
var rndStringSet2 = `${(~new(Date)).toString(36)}-${(new Date%9e29).toString(36)}`; | |
//Use characters from set 1, or set 2 for website name creation.. | |
switch (url){ | |
case true: siteName = rndStringSet1.join``.substring(0,25); | |
break; | |
case false: siteName = rndStringSet2; | |
break; | |
default: | |
break; | |
} | |
return `${protopart}${prefix}${siteName}.${(tldselectedpart)?tldselectedpart:tldar[tldar.length-1]}${routepart}${qstrpart}`; | |
} | |
//SAMPLE USE CASES: | |
// | |
// Example 1 (No querystring parameters, string set 1) | |
// createRandURL({},true) | |
// Output 1 "https://www.og5cgmtoaief2qylvepnbphdb.br/bxr/dobxr?" | |
// | |
// Example 2 (Including querystring parameters with URI encoded chars, string set 2) | |
// createRandURL({a:[3,5,2],b:'dd',c:'ds&@#'},false) | |
// Output 2 "https://www.4dlgbh-jq6qyasi.ro/gbh/dlgbh?a=3&a=5&a=2&b=dd&c=ds%26%40%23" | |
// | |
// Example 3 (Default usage, string set 1, no params.) | |
// createRandURL({}) | |
// Output 3 "http://2iorvr6hbccvn002sffig4ei6.co.uk/v7w/dfv7w?" | |
// | |
// Example 4 (calling createRandURL with no arguments/null returns "") | |
// createRandURL(); createRandURL(null) | |
// Output 4 "" | |
// | |
// Other cases: | |
// createRandURL({a:'p'}) | |
// "http://www.8en8cabl1tw0b3b8h07ucn6n9.co.uk/det/cxdet?a=p" | |
// createRandURL({a:'p'},false) | |
// "https://4cxa4h-jq6rmgzi.cz/a4h/cxa4h?a=p" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment