Skip to content

Instantly share code, notes, and snippets.

@StoneCypher
Last active August 25, 2018 05:05
Show Gist options
  • Save StoneCypher/ec12d02f1a2998f14263 to your computer and use it in GitHub Desktop.
Save StoneCypher/ec12d02f1a2998f14263 to your computer and use it in GitHub Desktop.
comment time; make generic the name of the range label
function timeString(userMilliseconds) {
var // the actual data ranges. now trivial to extend
ranges = [
{ range: 86400, singular: 'day', plural: 'days' },
{ range: 3600, singular: 'hour', plural: 'hours' },
{ range: 60, singular: 'minute', plural: 'minutes' },
{ range: 1, singular: 'second', plural: 'seconds' }
],
// epsilon is anything smaller than the lowest handled granularity
// so in this case, anything smaller than one second
epsilon = ranges[ranges.length - 1].range, // a measure of the smallest time range this function handles
milliseconds = Math.abs(userMilliseconds), // why does this handle negative times, instead of throwing?
totalSeconds = Math.floor(milliseconds / 1000), //
sb = '';
// if the second arg Clause is true, extend the string buffer with the text in the first arg
function extend_sb_with_if(Str, Clause) {
if (Clause) { sb += Str; }
}
// add the number to the string buffer, then the appropriate string label for if it's plural/singular/zero
function add_num_w_plural_label(Num, Singular, Plural) {
sb += Num.toString()
+ ' '
+ ((Num === 1)?
Singular.toString()
: Plural.toString()
);
}
// maybe add the string "and " to the string buffer, if we're out of content otherwise and the sb isn't already empty
function maybe_and() {
extend_sb_with_if('and ', (totalSeconds == 0 && sb.length > 0));
}
// maybe add a space for content to follow, if the content we have otherwise isn't exhausted or below the size of notice
function maybe_space(userEpsilon) {
if (totalSeconds < userEpsilon) { return; }
extend_sb_with_if(' ', (totalSeconds > 0));
}
// knock the remaining content down by a certain distance. mostly a label for clarity.
function decTotalBy(X) {
totalSeconds -= X;
}
// get the number of whole steps from size of the step and the size of the range, calculate and decrement the
// size of the set of whole steps, return the step count
function GetStepThenDec(StepSize) {
var StepResidue = Math.floor(totalSeconds / StepSize);
decTotalBy(StepResidue * StepSize);
return StepResidue;
}
// for one single range, if there is enough content to bother, handle the range decrement and get its result count,
// possibly add the "and" text, add the range count as a number to the buffer with the appropriate label, maybe
// add a space, bail
function handle_range(RangeData, isFirst, useEpsilon) {
if (totalSeconds >= RangeData.range) {
var rangecount = GetStepThenDec(RangeData.range);
if (!(isFirst)) { maybe_and(); }
add_num_w_plural_label(rangecount, RangeData.singular, RangeData.plural);
maybe_space(useEpsilon);
}
}
// and now do the actual work, by looping all the ranges
for (i in ranges) {
handle_range(ranges[i], i === 0, epsilon)
}
// should probably throw if there's something left in totalSeconds that isn't smaller than epsilon
// that would indicate a calculation error
/* todo */
/* if (totalSeconds >= epsilon) { table_flip; } */
// output of work buffer is code green, all systems blast off go go go
return sb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment