Last active
November 20, 2021 16:01
-
-
Save josfaber/ee7b41846e8a6d1e3437e3e992886437 to your computer and use it in GitHub Desktop.
string utils
This file contains 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
/** | |
* return a random, unique-ish id | |
*/ | |
function guid() { | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); | |
return v.toString(16); | |
}); | |
} | |
/** | |
* pads a number and returns the string | |
* @param {int} n - the integer to pad | |
* @param {int} a - total amount of characters in returned string | |
* @param {string} p - the character to use for padding | |
*/ | |
function pad(n, a, p) { | |
if (String(n).length >= a) { | |
return String(n); | |
} | |
var s = ""; | |
for (var i = 0; i < a; i++) { | |
s += p; | |
} | |
return (s + String(n)).slice(-a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment