DEV Community

ksawery297
ksawery297

Posted on • Edited on

title

Top comments (6)

Collapse
 
benherbst profile image
Ben Herbst • Edited

Very good lib, gonna use in every project!

Collapse
 
ksawery297 profile image
ksawery297

Thanks! Let me know how it went πŸ˜€

Collapse
 
lionelrowe profile image
lionel-rowe

You can get a generalized form of your hard-coded alphabet exports by having a function to generate ranges of characters based on codepoints:

function charRange(startChar, endChar) {
    const start = startChar.codePointAt(0)
    const end = endChar.codePointAt(0)

    return [...new Array(end - start + 1)]
        .map((_, i) => String.fromCodePoint(i + start))
}

const lowerAlphas = charRange('a', 'z') // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
const upperAlphas = charRange('A', 'Z') // ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
const digits = charRange('0', '9') // ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

const printableAsciis = charRange('\x20', '\x7e') // [' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~']
Enter fullscreen mode Exit fullscreen mode

You can also get all ASCII special characters (i.e. non-alphanumerics) by diffing printableAsciis with the alphanumeric versions:

const exclude = new Set([...lowerAlphas, ...upperAlphas, ...digits])
const specials = printableAsciis.filter((char) => !exclude.has(char)) // [' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~']
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ksawery297 profile image
ksawery297

That's a good point, thanks! If you want, you can create a pull request to replace it with your solution.

Collapse
 
efpage profile image
Eckehard

Extended string functions are very welcome. Are there any performance tests comparing different approaches?

Collapse
 
ksawery297 profile image
ksawery297

Hi! No, not yet. Turbo strings is still under development so performance might not be the best. After it will be more stable, I will probably add some benchmarks in the readme.