Skip to content

Instantly share code, notes, and snippets.

@nikonov91-dev
Last active April 5, 2022 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikonov91-dev/62f4d1fdb1ac0ef2b7f56c079169a3f8 to your computer and use it in GitHub Desktop.
Save nikonov91-dev/62f4d1fdb1ac0ef2b7f56c079169a3f8 to your computer and use it in GitHub Desktop.
function deviceNamesSystem(dn) {
dn.push('kuojoa{-truncated-}');
dn.push('fmgnwjjsfayewzpxslt1');
dn.push('mixer');
dn.push('mixer');
const updatedDN = [];
let newName = '';
let countMax = [];
const incrementCount = (c = '') => +c + 1;
const findMatch = ee => ee.match(/^(.+?)(\d*)$/);
dn.forEach( e => {
let foundMatch = findMatch(e);
if (!foundMatch[2] && !updatedDN.find( ee => findMatch(ee)[1] === foundMatch[1] )) {
newName = foundMatch[1];
} else {
countMax = Math.max(...updatedDN
.filter(ee => findMatch(ee)[1] === foundMatch[1])
.map( ee => findMatch(ee)[2]));
newName = foundMatch[1] + incrementCount(countMax)
}
updatedDN.push(newName);
})
return updatedDN
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment