Frage

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

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;
}
War es hilfreich?

Lösung

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

Andere Tipps

Essentially what you are doing is counting, albeit in base 24, and using as digits ABCDEFGHJKLMNPQRSTUVWXYZ instead of the "normal" 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).

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". 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);
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top