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

前端 未结 14 2813
我在风中等你
我在风中等你 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:37

    Make a function with {a: 'b', b: 'c', etc} in a closure:-

    let nextChar = (s => (
        "abcdefghijklmopqrstuvwxyza".split('')
        .reduce((a,b)=> (s[a]=b, b)), // make the lookup
    c=> s[c] // the function returned
    ))({}); // parameter s, starts empty
    

    usage:-

    nextChar('a')
    

    Adding uppercase and digits:-

    let nextCh = (
        (alphabeta, s) => (
            [alphabeta, alphabeta.toUpperCase(), "01234567890"]
                .forEach(chars => chars.split('')
                   .reduce((a,b) => (s[a]=b, b))), 
            c=> s[c] 
        )
    )("abcdefghijklmopqrstuvwxyza", {});
    

    p.s. In some versions of Javascript, you can use [...chars] instead of chars.split('')

提交回复
热议问题