Last active
January 4, 2022 01:25
-
-
Save bgoonz/b5004a248360a64e6ab8264483ce5089 to your computer and use it in GitHub Desktop.
random-utils.js
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
var prod_api = "/api"; | |
function getUrlParameter(name) { | |
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); | |
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); | |
var results = regex.exec(location.search); | |
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); | |
}; | |
function tset(k, v) { | |
localStorage.setItem(k, v); | |
} | |
function getLoadingHTML() { | |
var html = '<div class="ui segment"> <div class="ui active inverted dimmer"><div class="ui text loader">Loading</div> </div> <p></p> <br/><br/><br/><br/></div><br/><br/>'; | |
return html; | |
} | |
function tget(k) { | |
return localStorage.getItem(k); | |
} | |
function abortRequests() { | |
if (window.currentHTTPRequest) { | |
window.currentHTTPRequest.abort(); | |
} | |
} | |
function makeRequest(method, url, data) { | |
var id = localStorage.getItem('user_id'); | |
var token = localStorage.getItem('access_token'); | |
return new Promise(function(resolve, reject) { | |
var xhr = new XMLHttpRequest(); | |
window.currentHTTPRequest = xhr; | |
xhr.open(method, url); | |
if (typeof id !== 'undefined') { | |
xhr.setRequestHeader("x-auth-id", id); | |
} | |
if (typeof token !== 'undefined') { | |
xhr.setRequestHeader("x-auth-token", token); | |
} | |
xhr.onload = function() { | |
if (this.status >= 200 && this.status < 300) { | |
resolve(xhr.response); | |
} else { | |
reject({ | |
status: this.status, | |
statusText: xhr.statusText | |
}); | |
} | |
}; | |
xhr.onerror = function() { | |
reject({ | |
status: this.status, | |
statusText: xhr.statusText | |
}); | |
}; | |
if (method == "POST" && data) { | |
xhr.send(data); | |
} else { | |
xhr.send(); | |
} | |
}); | |
} | |
function dateToNiceDayStringDay(myDate) { | |
var month = new Array(); | |
month[0] = "Jan"; | |
month[1] = "Feb"; | |
month[2] = "Mar"; | |
month[3] = "Apr"; | |
month[4] = "May"; | |
month[5] = "Jun"; | |
month[6] = "Jul"; | |
month[7] = "Aug"; | |
month[8] = "Sep"; | |
month[9] = "Oct"; | |
month[10] = "Nov"; | |
month[11] = "Dec"; | |
https: //stackoverflow.com/questions/24998624/day-name-from-date-in-js/24998705 | |
var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat']; | |
return days[myDate.getDay()] + ", " + month[myDate.getMonth()] + " " + myDate.getDate() + " " + myDate.getFullYear(); | |
} | |
function dateToNiceDayStringTimeOnly(myDate) { | |
var hours = myDate.getHours(); | |
var minutes = myDate.getMinutes(); | |
var ampm = hours >= 12 ? 'pm' : 'am'; | |
hours = hours % 12; | |
hours = hours ? hours : 12; | |
minutes = minutes < 10 ? '0' + minutes : minutes; | |
var strTime = hours + ':' + minutes + ampm; | |
return strTime; | |
} | |
function dateToNiceDayStringWithTime(myDate) { | |
var month = new Array(); | |
month[0] = "Jan"; | |
month[1] = "Feb"; | |
month[2] = "Mar"; | |
month[3] = "Apr"; | |
month[4] = "May"; | |
month[5] = "Jun"; | |
month[6] = "Jul"; | |
month[7] = "Aug"; | |
month[8] = "Sep"; | |
month[9] = "Oct"; | |
month[10] = "Nov"; | |
month[11] = "Dec"; | |
var hours = myDate.getHours(); | |
var minutes = myDate.getMinutes(); | |
var ampm = hours >= 12 ? 'pm' : 'am'; | |
hours = hours % 12; | |
hours = hours ? hours : 12; | |
minutes = minutes < 10 ? '0' + minutes : minutes; | |
var strTime = hours + ':' + minutes + ampm; | |
//return myDate.getDate()+" "+month[myDate.getMonth()]+" "+myDate.getFullYear()+" "+strTime; | |
return month[myDate.getMonth()] + " " + myDate.getDate() + " " + myDate.getFullYear() + " " + strTime; | |
} | |
function dateToNiceDayString(myDate) { | |
var month = new Array(); | |
month[0] = "Jan"; | |
month[1] = "Feb"; | |
month[2] = "Mar"; | |
month[3] = "Apr"; | |
month[4] = "May"; | |
month[5] = "Jun"; | |
month[6] = "Jul"; | |
month[7] = "Aug"; | |
month[8] = "Sep"; | |
month[9] = "Oct"; | |
month[10] = "Nov"; | |
month[11] = "Dec"; | |
var hours = myDate.getHours(); | |
var minutes = myDate.getMinutes(); | |
var ampm = hours >= 12 ? 'pm' : 'am'; | |
hours = hours % 12; | |
hours = hours ? hours : 12; | |
minutes = minutes < 10 ? '0' + minutes : minutes; | |
var strTime = hours + ':' + minutes + ampm; | |
//return myDate.getDate()+" "+month[myDate.getMonth()]+" "+myDate.getFullYear()+" "+strTime; | |
return month[myDate.getMonth()] + " " + myDate.getDate() + " " + myDate.getFullYear(); | |
} | |
function getParameterByName(name, url) { | |
if (!url) url = window.location.href; | |
name = name.replace(/[\[\]]/g, '\\$&'); | |
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), | |
results = regex.exec(url); | |
if (!results) return null; | |
if (!results[2]) return ''; | |
return decodeURIComponent(results[2].replace(/\+/g, ' ')); | |
} | |
function toUSD(v) { | |
return "$" + parseFloat(v).toFixed(2); | |
} |
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 setupPopups() { | |
var classname = document.getElementsByClassName("tays_popup2"); | |
for (let i = 0; i < classname.length; i++) { | |
classname[i].addEventListener('mousedown', function(event) { | |
//conole.log(event); | |
//console.log(classname[i]); | |
if (event.target === classname[i] || event.target.classList.contains('tays_popup2_close_button')) { | |
this.style.display = "none"; | |
} | |
}, false); | |
} | |
} | |
setupPopups(); |
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
/** | |
* Alphanumeric characters. | |
* @const | |
*/ | |
const ALPHANUM | |
= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
/** | |
* Hexadecimal digit characters. | |
* @const | |
*/ | |
const HEX_DIGITS = '0123456789abcdef'; | |
/** | |
* Generate a string with random alphanumeric characters with a specific length. | |
* | |
* @param {number} length - The length of the string to return. | |
* @returns {string} A string of random alphanumeric characters with the | |
* specified length. | |
*/ | |
export function randomAlphanumString(length) { | |
return _randomString(length, ALPHANUM); | |
} | |
/** | |
* Get random element of array or string. | |
* | |
* @param {Array|string} arr - Source. | |
* @returns {Array|string} Array element or string character. | |
*/ | |
export function randomElement(arr) { | |
return arr[randomInt(0, arr.length - 1)]; | |
} | |
/** | |
* Returns a random hex digit. | |
* | |
* @returns {Array|string} | |
*/ | |
export function randomHexDigit() { | |
return randomElement(HEX_DIGITS); | |
} | |
/** | |
* Generates a string of random hexadecimal digits with a specific length. | |
* | |
* @param {number} length - The length of the string to return. | |
* @returns {string} A string of random hexadecimal digits with the specified | |
* length. | |
*/ | |
export function randomHexString(length) { | |
return _randomString(length, HEX_DIGITS); | |
} | |
/** | |
* Generates random int within the range [min, max]. | |
* | |
* @param {number} min - The minimum value for the generated number. | |
* @param {number} max - The maximum value for the generated number. | |
* @returns {number} Random int number. | |
*/ | |
export function randomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
/** | |
* Generates a string of random characters with a specific length. | |
* | |
* @param {number} length - The length of the string to return. | |
* @param {string} characters - The characters from which the returned string is | |
* to be constructed. | |
* @private | |
* @returns {string} A string of random characters with the specified length. | |
*/ | |
function _randomString(length, characters) { | |
let result = ''; | |
for (let i = 0; i < length; ++i) { | |
result += randomElement(characters); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment