What is a method that can be used to increment letters?

前端 未结 14 2740
我在风中等你
我在风中等你 2020-11-27 04:19

Does anyone know of a Javascript library (e.g. underscore, jQuery, MooTools, etc.) that offers a method of incrementing a letter?

I would like to be able to do somet

14条回答
  •  情深已故
    2020-11-27 04:34

    Based on @Nathan wall answer increment and decrement

    // Albhabet auto increment and decrement
    class StringIdGenerator {
        constructor(chars = '') {
          this._chars = chars;
        }
    
      next() {
        var u = this._chars.toUpperCase();
        if (this._same(u,'Z')){
            var txt = '';
            var i = u.length;
            while (i--) {
                txt += 'A';
            }
            this._chars = txt+'A';
            return (txt+'A');
        } else {
          var p = "";
          var q = "";
          if(u.length > 1){
              p = u.substring(0, u.length - 1);
              q = String.fromCharCode(p.slice(-1).charCodeAt(0));
          }
          var l = u.slice(-1).charCodeAt(0);
          var z = this._nextLetter(l);
          if(z==='A'){
            this._chars = p.slice(0,-1) + this._nextLetter(q.slice(-1).charCodeAt(0)) + z;
              return p.slice(0,-1) + this._nextLetter(q.slice(-1).charCodeAt(0)) + z;
          } else {
            this._chars = p+z;
              return p + z;
          }
        }
      }
    
      prev() {
        var u = this._chars.toUpperCase();
        console.log("u "+u)
        var l = u.slice(-1).charCodeAt(0);
        var z = this._nextLetter(l);
        var rl = u.slice(1)
        var y = (rl == "A") ? "Z" :this._prevLetter(rl.charCodeAt(0))
          var txt = '';
          var i = u.length;
          var j = this._chars
          var change = false
          while (i--) {
            if(change){
              if (u[u.length-1] == "A"){
                txt += this._prevLetter(u[i].charCodeAt(0))
              }else{
                txt += u[i]
              }
              
            }else{
              if (u[u.length-1] == "A"){
                txt += this._prevLetter(u[i].charCodeAt(0))
                change = true
              }else{
                change = true
                txt += this._prevLetter(u[i].charCodeAt(0))
              }
            }
          }
          if(u == "A" && txt == "Z"){
            this._chars = ''
          }else{
            this._chars = this._reverseString(txt);
          }
          console.log(this._chars)
          return (j);
      }
      _reverseString(str) {
          return str.split("").reverse().join("");
      }
      _nextLetter(l){
          if(l<90){
              return String.fromCharCode(l + 1);
          }
          else{
              return 'A';
          }
      }
    
      _prevLetter(l){
        if(l<=90){
          if(l == 65) l = 91
            return String.fromCharCode(l-1);
        }
        else{
            return 'A';
        }
      }
      _same(str,char){
          var i = str.length;
          while (i--) {
              if (str[i]!==char){
                  return false;
              }
          }
          return true;
      }
        
    }
    

    Usage

    const ids = new StringIdGenerator();
    
    ids.next(); 
    ids.prev();
    

提交回复
热议问题