[JS] Format string with some dashes

by kyllle

HTML

<input type="text" class="js-input">

CSS

input {
  width: 100px;
  height: 32px;
}

JavaScript

console.clear()

const input = document.querySelector('.js-input')

function formatWithDashes(event) {  
	let inputValue = event.target.value.split('-').join('');
	
	let formattedInputValue = inputValue.split('').reduce((accum, current, index) => {
		if (index === 3 || index === 5) {
    	accum = [...accum, '-']
    }
    
    return [...accum, current].join('')
  }, '')	
  
  return event.target.value = formattedInputValue
}

input.addEventListener('keyup', formatWithDashes)