Non Standard Library

Many programming languages have standard libraries. Chunks of shared code that provide a level playing field for all developers. Unbalance that field with the Non Standard Library - A collection of code snippets i've created, collected and curated over the years.

bprint - [#bash] - Prints text with the current time
Code:
bprint() {
	echo $(date +"[%H:%M:%S] ### $1 ###")
}
Usage:
#!/bin/bash
bprint "Hello World"
Output:
[16:45:30] ### Hello World ###
Random Strings - [#bash] - Generate random strings using bash
Code:
rand_alphanum() {
	echo $(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $1 | head -n 1)
}
Usage:
#!/bin/bash
s=$(rand_alphanum 32)
echo $s
Output:
Et7daigh0oop3ki8pha0mei1EcieThaz
time_for_human - [#javascript] - Returns a human readable amount of time for a given amount of seconds
Code:
function time_to_human(seconds)
{
    // Define the time periods we care about
    time_periods = [
        ["week",   1 * 60 * 60 * 24 * 7],
        ["day",    1 * 60 * 60 * 24],
        ["hour",   1 * 60 * 60],
        ["minute", 1 * 60],
        ["second", 1],
    ];

    human_string = [];
    final_string = "";

    // Loop through each time period
    time_periods.forEach(function($period) {
        // Check if time period is available from seconds remaining
        if (seconds >= $period[1]) {
            // Get the amount of time period
            units = Math.floor(seconds / period[1]);
            // Apply pluralisation, if necessary (note the backticks)
            word = (units == 1 ? period[0] : `${period[0]}s`);
            // Add the units and word to the working array
            human_string.push(`${units} ${word}`);
            // Update the seconds remaining
            seconds = seconds % period[1];
        }
    });

    // Parse the working array
    if (count(human_string) > 1) {
        // Add commas and " and "
        final_string = human_string.slice(0, -1);
        final_string = implode(", ", final_string);
        final_string = `${final_string} and `;
    }
    // Add the last element (seconds usually)
    final_string = `${human_string[-1]}`;

    return final_string;
}
Usage:
var human_time = time_to_human(50693);
console.log(human_time);
Output:
14 hours, 4 minutes and 53 seconds
plural - [#javascript] - Utility function for returning a plural
Code:
function plural(amount, singular_word, plural_word = false)
{
    var word = "";
    // Check if the amount is 1
    if (amount == 1) {
        // It is, return the singular
        word = singular_word;
    } else {
        // Amount is not 1, return the plural
        if (plural_word) {
            // More complicated than addind a single s, return the plural
            word = plural_word;
        } else {
            // The common case, return the singular with an added s
            word = singular_word + "s";
        }
    }
    return word;
}
Usage:
//[Example 1]
clowder = plural(2, "cat");
console.log(clowder);

//[Example 2]
vine = plural(2, 'tomato', 'tomatoes');
console.log(vine);
Output:
[Output 1]
cats

[Output 2]
tomatoes

© 2020 MrMena Enterprises - Report a problem with this page