{"size": 1647, "ext": "js", "max_stars_repo_path": "src/console-app-lib/console-app/input-sanitizer.js", "max_stars_repo_name": "RafaelKuhn/color-conversion", "max_stars_repo_stars_event_min_datetime": "2021-11-10T18:48:24.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-10T18:48:24.000Z", "max_issues_repo_path": "src/console-app-lib/console-app/input-sanitizer.js", "max_issues_repo_name": "RafaelKuhn/color-conversion", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/console-app-lib/console-app/input-sanitizer.js", "max_forks_repo_name": "RafaelKuhn/color-conversion", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.8695652174, "max_line_length": 133, "alphanum_fraction": 0.6836672738}
// TODO: perhaps transform this into a factory, with 'sanitize' being the main function and 'asRgb1' for example being a sub function

function sanitizeRgb1(rawInput) {
  const rgbSanitized = sanitizeAnyRgb(rawInput);
  
  return { type: 'RGB1', ...rgbSanitized };
}

function sanitizeRgb255(rawInput) {
  const rgbSanitized = sanitizeAnyRgb(rawInput);
  
  return { type: 'RGB255', ...rgbSanitized };
}

function sanitizeHex(rawInput) {
  const trimmedInput = rawInput.trim();
  let hexValue;
  if (trimmedInput.startsWith('#')) {
    hexValue = trimmedInput.substr(1, 6);
  } else {
    hexValue = trimmedInput.substr(0, 6);
  }
  return { type: 'HEX', hexValue }
}

function sanitizeHsv(rawInput) {
  const trimmedInput = getSplitArrayOfNumbers(rawInput);

  const h = trimmedInput[0];
  const s = trimmedInput[1];
  const v = trimmedInput[2];

  return {  type: 'HSV', h, s, v };
}

function sanitizeAnyRgb(rgbRawInput) {
  const trimmedInput = rgbRawInput.trim();
  let splitInput = getSplitArrayOfNumbers(trimmedInput);

  const r = splitInput[0];
  const g = splitInput[1];
  const b = splitInput[2];

  return {r, g, b};
}

function getSplitArrayOfNumbers(rgbRawInput) {
  let splitInput;
  if (rgbRawInput.includes(', ')) {
    splitInput = rgbRawInput.split(', ');
  } else if (rgbRawInput.includes(',')) {
    splitInput = rgbRawInput.split(',');
  } else if (rgbRawInput.includes(' ')) {
    splitInput = rgbRawInput.split(' ');
  }

  splitInput = splitInput.map( (stringElement) => { 
    return Number(stringElement);
  });

  return splitInput;
}

module.exports = {
  sanitizeRgb1,
  sanitizeRgb255,
  sanitizeHex,
  sanitizeHsv
}