简体   繁体   English

如何使用JavaScript计算下一个修订号

[英]How to use javascript to calculate the next revision number

I have a revision number attribute in my application and it is simply a string. 我的应用程序中有一个修订号属性,它只是一个字符串。 i need to pass in the current value and calculate the next valid one and return that. 我需要传递当前值并计算下一个有效值并将其返回。

Here is the valid progression: 这是有效的进度:

.A
.B
.C
0
0.A
0.B
1
1.A
etc

Forget the whole numbers, that is controlled elsewhere. 忘记整数,该数字在其他位置控制。 This deals with only the ones with periods. 这仅处理带有句点的句点。 The restrictions are: 限制是:

  • The first component is always a number (or nothing) 第一个部分始终是数字(或什么都不是)
  • Then a period 然后一段
  • Then a letter, excluding I and O (since they resemble 1 and 0) and once you reach Z it should go to AA, AB, AC, ..., ZZ 然后是一个字母,不包括I和O(因为它们类似于1和0),一旦到达Z,它应该转到AA,AB,AC,...,ZZ

So 所以

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

Any help would be appreciated. 任何帮助,将不胜感激。

Here's what I have - I just don't know how to "increment" the letter portion: 这就是我所拥有的-我只是不知道如何“增加”字母部分:

function calcNextRev(currentRev)
{
var revParts = currentRev.split(".");
var majorRev = revParts[0];
var currentMinorRev = revParts[1];

???

return majorRev + "." + newMinorRev;
}

Try this: 尝试这个:

(demo here) (演示在这里)

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 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 currentRev ='0.AZ'; var result; var结果;

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));

Essentially what you are doing is counting , albeit in base 24, and using as digits ABCDEFGHJKLMNPQRSTUVWXYZ instead of the "normal" 0123456789ABCDEFGHIJKLMNO . 本质上,您正在执行的操作是计数 (尽管以24为基数),并使用数字ABCDEFGHJKLMNPQRSTUVWXYZ代替“正常” 0123456789ABCDEFGHIJKLMNO So we'll use JavaScript's little-known ability to handle non-base-10 numbers, in the form of parseInt(value,base) and value.toString(base) . 因此,我们将使用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;
}

This also gives you three-letter and more version numbers "for free"; 这还为您提供了三个字母和更多的“免费”版本号; "ZZ" will go to "AAA". “ ZZ”将转到“ AAA”。 In addition, it allows you to jump easily any number of versions ahead or back. 此外,它还允许您轻松地前后跳转任意数量的版本。

To handle the leading numeric version number: 要处理前导数字版本号:

full_version.replace(/[A-Z][A-Z]?/, function (letter_portion) {
    return increment (letter_portion);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM