All files index.js

100% Statements 19/19
100% Branches 10/10
100% Functions 1/1
100% Lines 19/19
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 461x 6x   6x 1x 5x 1x       5x   19x   9x   9x     9x   35x   35x 6x 6x       9x 3x 3x                 2x          
module.exports = (input) => {
  let text = input;
  // type check
  if (typeof text === 'undefined') {
    return text;
  } if (typeof text !== 'string') {
    text = text.toString();
  }
 
  // will not check strings which is longer than the half of the original string
  for (let i = 1; i <= text.length / 2; i += 1) {
    // check strings which can divide original strings without reminder
    if (text.length % i === 0) {
      // repeated: potential repeated string
      const repeated = text.substring(0, i);
      // we will set this flag false if it is not repeated on every step
      let isRepeated = true;
 
      // start scanning the original string
      for (let j = 0; j < text.length - repeated.length + 1; j += repeated.length) {
        // part of the original string to be compared with potential repeated
        const compare = text.substring(j, j + repeated.length);
 
        if (compare !== repeated) {
          isRepeated = false;
          break;
        }
      }
 
      if (isRepeated) {
        const repeatCount = text.length / repeated.length;
        return {
          repeated,
          count: repeatCount,
        };
      }
    }
  }
 
  // if no repeats found, then return the input itself
  return {
    repeated: text,
    count: 1,
  };
};