我的应用程序中有一个修订号属性,它只是一个字符串。我需要传入当前值并计算下一个有效值并返回该值。
以下是有效的进展:
.A
.B
.C
0
0.A
0.B
1
1.A
etc
忘记整个数字,这是在其他地方控制的。这仅涉及具有期间的那些。 限制是:
所以
If I pass in .A it should return .B
If I pass in 1.H it should pass back 1.J
If I pass in 1.Z it should pass back 1.AA
任何帮助都将不胜感激。
这就是我所拥有的 - 我只是不知道如何“增加”字母部分:
function calcNextRev(currentRev)
{
var revParts = currentRev.split(".");
var majorRev = revParts[0];
var currentMinorRev = revParts[1];
???
return majorRev + "." + newMinorRev;
}
答案 0 :(得分:2)
试试这个:
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 is 1 letter long
if (currentMinorRev.length == 1) {
for (i = 0; i < alfab.length; i++) {
if (currentMinorRev == alfab[i]) {
if (i == alfab.length - 1) {
newMinorRev = alfab[0] + alfab[0];
} else {
var ii = i + 1;
newMinorRev = alfab[ii];
}
}
}
}
//Case string is more than one letter long
if (currentMinorRev.length > 1) {
var currentMinorRev2 = currentMinorRev.split("");
var l = currentMinorRev2.length - 1;
for (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));
答案 1 :(得分:2)
基本上你正在做的是计算,虽然在基数为24,并且使用数字ABCDEFGHJKLMNPQRSTUVWXYZ
而不是“普通”0123456789ABCDEFGHIJKLMNO
。因此,我们将使用JavaScript鲜为人知的能力来处理非基数为10的数字,形式为parseInt(value,base)
和value.toString(base)
。
var letters = "ABCDEFGHJKLMNPQRSTUVWXYZ".split(""),
base24_to_letters_map = {},
letters_to_base24_map = {};
// Build maps from base 24 digits to desired range of letters and vice versa
for (var i=0; i<24; i++) {
base24_to_letters_map[i.toString(24)] = letters[i];
letters_to_base24_map[letters[i]] = i.toString(24).toUpperCase();
}
// Convert each digit in "val" based on "map"
function convert (val, map) {
return val.replace(/[0-9A-Z]/g, function(chr) { return map[chr]; });
}
function increment (version) {
var base24, number;
base24 = convert (version, letters_to_base24_map); // convert "BJ" to "1A"
number = parseInt (base24, 24); // convert "1A" to 34
number++; // increment
base24 = number.toString (24); // convert 35 to "1B"
version = convert (base24, base24_to_letters_map); // convert 1B to BK
return version;
}
这也为您免费提供三个字母和更多版本号码; “ZZ”将进入“AAA”。此外,它允许您轻松地向前或向后跳转任意数量的版本。
处理前导数字版本号:
full_version.replace(/[A-Z][A-Z]?/, function (letter_portion) {
return increment (letter_portion);
});