All files index.js

100% Statements 21/21
100% Branches 7/7
100% Functions 3/3
100% Lines 21/21
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    1x 1016x 1016x 898x   118x 118x 118x 203x     118x     1x 7x 4x 7x 7x   7x 1009x   1009x     1009x 1009x   1009x 1009x    
'use strict';
 
const padStart = (string, targetLength, padString) => {
    const stringLength = string.length;
    if (stringLength == targetLength)
        return string;
 
    const stringDiffLength = targetLength - stringLength;
    let newString = '';
    for (let i = 1; i <= stringDiffLength; i++) {
        newString += padString;
    }
 
    return newString + string;
};
 
module.exports = (length) => {
    if (length == undefined || length < 1)
        length = 15;
    const maxValue = parseInt(padStart('', length, '9'));
    let repeat = 0,
        last;
    return () => {
        let nonce = Math.floor(Math.random() * maxValue);
        /* istanbul ignore if */
        if (nonce == last) {
            repeat++;
        } else {
            repeat = 0;
            last = nonce;
        }
        nonce += repeat;
        return padStart(`${nonce}`, length, '0');
    };
};