Я сделал версию, в которой вы передаете позицию, которую хотите увеличить.
Если позиция не существует, она создается, если позиция не пройдена, это 3
const nextVersion = (ver, pos=3) => {
let [main,...rest] = ver.split(".")
rest = Array.isArray(rest) && rest.length > 0 ? rest : Array(pos).fill(0,0,pos);
if (rest.length<pos) rest.push(0); // make sure it is long enough
+rest[pos-1]++; // interestingly enough the + was not needed here
return `${main}.${rest.join(".")}`; // or use rest.unshift(main)
}
console.log(nextVersion("22",3))
console.log(nextVersion("1.0.0.1",3))
console.log(nextVersion("22",1))
console.log(nextVersion("22.1",1))
console.log(nextVersion("22.1",2))
console.log(nextVersion("1.0.0.1",1))
console.log(nextVersion("22")); // no parameters