Skip to content

Instantly share code, notes, and snippets.

@Frdrcpeter007
Created February 20, 2021 13:50
Show Gist options
  • Save Frdrcpeter007/b36ec182986c3e56f29cad15f736c28c to your computer and use it in GitHub Desktop.
Save Frdrcpeter007/b36ec182986c3e56f29cad15f736c28c to your computer and use it in GitHub Desktop.
Cette fonction insère une - entre deux nombre impair dans une chaine, on peut l'utiliser pour faire des slugs
/**
* Cette insère une - entre deux nombre impair dans une chaine
* @param {String} str
* @return {String}
*/
function dashInsert(str) {
let newString = str[0] || '';
for (let i = 1, lastNumberIsOdd = str[0] % 2 === 1; i < str.length; i++) {
const numberIsOdd = str[i] % 2 === 1;
newString += numberIsOdd && lastNumberIsOdd ? '-' + str[i] : str[i];
lastNumberIsOdd = numberIsOdd;
}
return newString;
}
module.exports = dashInsert;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment