regex - How to use javascript to calculate the next revision number -
i have revision number attribute in application , string. need pass in current value , calculate next valid 1 , return that.
here valid progression:
.a .b .c 0 0.a 0.b 1 1.a etc
forget whole numbers, controlled elsewhere. deals ones periods. restrictions are:
- the first component number (or nothing)
- then period
- then letter, excluding , o (since resemble 1 , 0) , once reach z should go aa, ab, ac, ..., zz
so
if pass in .a should return .b if pass in 1.h should pass 1.j if pass in 1.z should pass 1.aa
any appreciated.
here's have - don't know how "increment" letter portion:
function calcnextrev(currentrev) { var revparts = currentrev.split("."); var majorrev = revparts[0]; var currentminorrev = revparts[1]; ??? return majorrev + "." + newminorrev; }
try this:
var alfab = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'y', 'z']; var currentrev = '0.az'; var result;
function calcnextrev(currentrev) { var newminorrev; var revparts = currentrev.split("."); var majorrev = revparts[0]; var currentminorrev = revparts[1]; //case string 1 letter long if (currentminorrev.length == 1) { (i = 0; < alfab.length; i++) { if (currentminorrev == alfab[i]) { if (i == alfab.length - 1) { newminorrev = alfab[0] + alfab[0]; } else { var ii = + 1; newminorrev = alfab[ii]; } } } } //case string more 1 letter long if (currentminorrev.length > 1) { var currentminorrev2 = currentminorrev.split(""); var l = currentminorrev2.length - 1; (o = 0; o < alfab.length; o++) { if (currentminorrev2[l] == alfab[o] && o == alfab.length - 1) { var currentalias = currentminorrev2; currentalias[l] = alfab[0]; currentalias.push(alfab[0]); newminorrev = currentalias.join(''); } if (currentminorrev2[l] == alfab[o] && o != alfab.length - 1) { var xo = o + 1; var currentalias = currentminorrev2; currentalias[l] = alfab[xo]; newminorrev = currentalias.join(''); o++; } } }; result = majorrev + "." + newminorrev; return result; } alert(calcnextrev(currentrev));
Comments
Post a Comment