Last active
May 11, 2018 16:18
-
-
Save westc/caef64d9ac562f0ac0284ba6d14c7834 to your computer and use it in GitHub Desktop.
Creates a stub for a URL based on a string and the current time. (JavaScript, PHP & Python)
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
<?php | |
function get_stub($str_start, $opt_max_length=15, $opt_chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") { | |
$number = intval(microtime(true) * 1e3); | |
$s = ""; | |
$l = strlen($opt_chars); | |
while (number) { | |
$remainder = $number % $l; | |
$number = ($number - $remainder) / $l; | |
$s = substr($opt_chars, $remainder, 1) . $s; | |
} | |
$number = $opt_max_length - strlen($s) - 1; | |
return $number > 0 ? substr($str_start, 0, $number) . "-$s" : null; | |
} |
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
import time | |
def get_stub(str_start, opt_max_length=15, opt_chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"): | |
number = int(round(time.time() * 1e3)) | |
s = "" | |
l = len(opt_chars) | |
while number > 0: | |
remainder = number % l | |
number /= l | |
s = opt_chars[remainder] + s | |
number = opt_max_length - len(s) - 1 | |
return (str_start[0:number] + "-" + s) if number > 0 else None |
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
/** | |
* Gets a stub string based on the given prefix and the time while limiting the | |
* stub's length. | |
* @param {string} prefix | |
* Prefix to start the stub with. This prefix may be cut short to accomodate | |
* for the time part of the string. | |
* @param {number=} opt_maxLength | |
* Defaults to 15. The maximum length of the returned stub. | |
* @param {string=} opt_chars | |
* Defaults to the characters A to Z followed by the characters a to z | |
* followed by the numbers 0 to 9. Characters used to represent the current | |
* time (milliseconds). | |
* @returns {?string} | |
* If the string the represents the time (epoch) is less than maximum length | |
* minus 1 a stub starting at least part of the prefix, followed by a dash | |
* ("-"), followed the epoch representation will be returned. Otherwise null | |
* is returned. | |
*/ | |
function getStub(prefix, opt_maxLength=15, opt_chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') { | |
for (var remainder, number = +new Date, s = '', l = opt_chars.length; number; ) { | |
remainder = number % l; | |
number = (number - remainder) / l; | |
s = opt_chars.charAt(remainder) + s; | |
} | |
number = opt_maxLength - s.length - 1; | |
return number > 0 ? prefix.slice(0, number) + '-' + s : null; | |
} |
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
/** | |
* Gets a stub string based on the given prefix and the time while limiting the | |
* stub's length. | |
* @param {string} prefix | |
* Prefix to start the stub with. This prefix may be cut short to accomodate | |
* for the time part of the string. | |
* @param {number=} opt_maxLength | |
* Defaults to 15. The maximum length of the returned stub. | |
* @param {string=} opt_chars | |
* Defaults to the characters A to Z followed by the characters a to z | |
* followed by the numbers 0 to 9. Characters used to represent the current | |
* time (milliseconds). | |
* @returns {?string} | |
* If the string the represents the time (epoch) is less than maximum length | |
* minus 1 a stub starting at least part of the prefix, followed by a dash | |
* ("-"), followed the epoch representation will be returned. Otherwise null | |
* is returned. | |
*/ | |
function getStub(prefix, opt_maxLength, opt_chars) { | |
opt_maxLength = opt_maxLength || 15; | |
opt_chars = opt_chars || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
for (var remainder, number = +new Date, s = '', l = opt_chars.length; number; ) { | |
remainder = number % l; | |
number = (number - remainder) / l; | |
s = opt_chars.charAt(remainder) + s; | |
} | |
number = opt_maxLength - s.length - 1; | |
return number > 0 ? prefix.slice(0, number) + '-' + s : null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment