Spot the bug - #97: Theme Color Parser

Color helper has one tiny bug.

function normalizeHex(hex) {
  const value = hex.startsWith('#') ? hex : '#' + hex;
  return value.slice(0, 6);
}

console.log(normalizeHex('#12abef'));

Reply with what is broken and how you would fix it.

Is it the range used by slice?

Close, but the range isn’t the bug by itself. slice(0, 6) counts from index 0, and the # is sitting right there at index 0. So you’re keeping the # plus only 5 hex chars, not 6.

1 Like