All files / js helpers.js

100% Statements 19/19
91.66% Branches 11/12
100% Functions 4/4
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                  3x     4x   4x       4x         4x   4x   4x   1x 1x     4x       1x       1x 1x 1x           11x 3x   8x       7x   1x          
/**
 * -----------------------------
 * HELPERS
 *
 * These are basic utilities to
 * parse data, add/remove/toggle
 * classes etc.
 * -----------------------------
 */
const _helpers = {
    // Convert seconds into minutes-and-seconds (MMSS) format
    parseTime: seconds => {
        let hours = Math.floor(seconds / 3600);
 
        let mins = Math.floor((seconds % 3600) / 60)
            .toFixed(0)
            .toString();
 
        let secs = Math.floor((seconds % 3600) % 60)
            .toFixed(0)
            .toString();
 
        // Left-pad seconds string if needed
        secs = secs >= 10 ? secs : `0${secs}`;
 
        let parsedTime = `${mins}:${secs}`;
 
        if (hours > 0) {
            // Left-pad minutes string if needed
            mins = mins >= 10 ? mins : `0${mins}`;
            parsedTime = `${hours}:${mins}:${secs}`;
        }
 
        return parsedTime;
    },
 
    // Get File Type
    getFileType: string => string.substr((~-string.lastIndexOf(".") >>> 0) + 2),
 
    // Get File Name
    getFileName: string => {
        let fullFileName = string.replace(/^.*[\\\/]/, "");
        let withNoExtension = fullFileName.split(".")[0];
        return withNoExtension;
    },
 
    // Find parent index (recursively climbs through parents until it finds a
    // valid index or runs out of elements)
    findParentIndex: startingElement => {
        if (typeof startingElement.dataset.picobelIndex !== "undefined") {
            return startingElement.dataset.picobelIndex;
        }
        if (
            typeof startingElement.parentNode !== "undefined" &&
            typeof startingElement.parentNode.dataset !== "undefined"
        ) {
            return _helpers.findParentIndex(startingElement.parentNode);
        }
        return false;
    }
};
 
export default _helpers;