All files / utils quarterUtilities.js

100% Statements 44/44
100% Branches 14/14
100% Functions 7/7
100% Lines 43/43

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9921x             21x             21x             21x 11467x     21x 1x     21x 4x       21x 22280x 2x   22278x 22278x 2x   22276x 22276x 4x   22272x     21x 1052x 1x   1051x 1x   1050x 1050x 2x   1048x     21x 11140x 11136x 11136x 11136x 8338x   2798x       21x 522x 522x 522x 522x 11132x 11132x         522x                          
const quarters = [
    "WINTER",
    "SPRING",
    "SUMMER",
    "FALL"
];
 
const shortQuarters = [
    "W",
    "S",
    "M",
    "F"
];
 
const qtrNumToQuarter = {
    '1': 'WINTER',
    '2': 'SPRING',
    '3': 'SUMMER',
    '4': 'FALL'
};
 
const yyyyqToQyy = (yyyyq) => {
    return `${shortQuarters[parseInt(yyyyq.charAt(4)) - 1]}${yyyyq.substring(2, 4)}`;
}
 
const toFormat = (quarter, year) => {
    return year.toString() + (parseInt(quarter)).toString();
}
 
const fromFormat = (format) => {
    return `${quarters[parseInt(format.charAt(4)) - 1]} ${format.substring(0, 4)}`;
}
 
 
const fromNumericYYYYQ = (yyyyqInt) => {
    if (typeof (yyyyqInt) != 'number') {
        throw new Error("param should be a number");
    }
    const yyyyqStr = yyyyqInt.toString();
    if (yyyyqStr.length !== 5) {
        throw new Error("param should be five digits");
    }
    const qStr = yyyyqStr.substring(4, 5);
    if (!(qStr in qtrNumToQuarter)) {
        throw new Error("param should end in 1,2,3 or 4");
    }
    return yyyyqStr;
}
 
const toNumericYYYYQ = (yyyyqStr) => {
    if (typeof (yyyyqStr) !== 'string') {
        throw new Error("param should be a string");
    }
    if (yyyyqStr.length !== 5) {
        throw new Error("param should be five digits");
    }
    const qStr = yyyyqStr.substring(4, 5);
    if (!(qStr in qtrNumToQuarter)) {
        throw new Error("param should end in 1,2,3 or 4");
    }
    return parseInt(yyyyqStr);
}
 
const nextQuarter = (yyyyqInt) => {
    const _yyyyqStr = fromNumericYYYYQ(yyyyqInt); // just for type/format checking
    const qInt = yyyyqInt % 10;
    const yyyyInt = Math.floor(yyyyqInt / 10);
    if (qInt < 4) {
        return yyyyqInt + 1;
    }
    return (yyyyInt + 1) * 10 + 1;
}
 
 
const quarterRange = (beginYYYYQStr, endYYYYQStr) => {
    let quarterList = [];
    const beginYYYYQInt = toNumericYYYYQ(beginYYYYQStr);
    const endYYYYQInt = toNumericYYYYQ(endYYYYQStr);
    for (let yyyyqInt = beginYYYYQInt; yyyyqInt <= endYYYYQInt; yyyyqInt = nextQuarter(yyyyqInt)) {
        const yyyyqStr = fromNumericYYYYQ(yyyyqInt);
        quarterList.push({
            yyyyq : yyyyqStr,
            qyy: yyyyqToQyy(yyyyqStr)
        });
    }
    return quarterList;
}
 
export {
    fromFormat,
    toFormat,
    yyyyqToQyy,
    fromNumericYYYYQ,
    toNumericYYYYQ,
    nextQuarter,
    quarterRange,
    qtrNumToQuarter
};