Skip to content

Instantly share code, notes, and snippets.

@josfaber
Last active November 20, 2021 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josfaber/ee7b41846e8a6d1e3437e3e992886437 to your computer and use it in GitHub Desktop.
Save josfaber/ee7b41846e8a6d1e3437e3e992886437 to your computer and use it in GitHub Desktop.
string utils
/**
* 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