All files / helpers/strings utils.js

100% Statements 45/45
90% Branches 27/30
100% Functions 2/2
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 781x 12x 12x 11x 11x 11x 1x     1x 43x 43x 7x 7x   43x 43x 43x 43x 43x   43x 43x 43x 43x 223x 151x 12x 12x         12x 8x   8x 1x       12x       12x 12x   139x 139x   139x         139x 3x 3x       3x 3x         208x       208x     43x    
export const incrementStringNumber = (str) => {
  try {
    let num = parseInt(str.replace (/[^\d.]/g, ''));
    var reg = new RegExp(num);
    num = num + 1;
    return str.replace(reg, num);
  } catch (e) { return str }
};
 
export const generateRangeString = (strFrom, strTo, clip = true, swapBySize = false) => {
  let inverse = parseInt(strFrom.replace(/[^\d.]/g, '')) > parseInt(strTo.replace(/[^\d.]/g, ''));
  if (swapBySize && inverse) {
    [strFrom, strTo] = [strTo, strFrom];
    inverse = false;
  }
  const rangeArray = [strFrom];
  const strFromPart = strFrom.replace(/[^\d.]/g, '');
  const strToPart = strTo.replace(/[^\d.]/g, '');
  const numberFrom = parseInt(strFromPart);
  const numberTo = parseInt(strToPart);
 
  let valueToInc = numberFrom;
  let currentStrFrom = strFrom;
  let currentStrFromPart = strFromPart;
  for (let index = valueToInc; inverse ? index > numberTo : index < numberTo; inverse ? index-- : index++) {
    if (clip) {
      if (inverse) {
        const auxCurrentStrFromPart = currentStrFromPart;
        currentStrFromPart = currentStrFromPart.replace(
          index,
          inverse ? (index - 1) : (index + 1)
        );
 
        if (strToPart.charAt(0) === '0') {
          if (currentStrFromPart.charAt(0) !== '0') currentStrFromPart = `0${currentStrFromPart}`;
 
          if (currentStrFromPart.length < strToPart.length) {
            currentStrFromPart = `${'0'.repeat(strToPart.length - currentStrFromPart.length)}${currentStrFromPart}`;
          }
        }
 
        currentStrFrom = currentStrFrom.replace(
          auxCurrentStrFromPart,
          currentStrFromPart
        );
        rangeArray.push(currentStrFrom);
        continue;
      } else {
        const auxCurrentStrFromPart = currentStrFromPart;
        const lengthCurrentStrFromPart = auxCurrentStrFromPart.length;
 
        currentStrFromPart = currentStrFromPart.replace(
          index,
          inverse ? (index - 1) : (index + 1)
        );
 
        if ((currentStrFromPart.length > lengthCurrentStrFromPart) && currentStrFromPart.charAt(0) === '0') {
          currentStrFromPart = currentStrFromPart.substring(currentStrFromPart.length - lengthCurrentStrFromPart);
          currentStrFrom = currentStrFrom.replace(
            auxCurrentStrFromPart,
            currentStrFromPart
          );
          rangeArray.push(currentStrFrom);
          continue;
        }
      }
    }
 
    currentStrFrom = currentStrFrom.replace(
      index,
      inverse ? (index - 1) : (index + 1)
    );
    rangeArray.push(currentStrFrom);
  }
 
  return rangeArray;
}