Skip to content

Instantly share code, notes, and snippets.

@hallojoe
Created February 24, 2021 15:00
Show Gist options
  • Save hallojoe/b4bea57baaf0ad6230ae2175398818c6 to your computer and use it in GitHub Desktop.
Save hallojoe/b4bea57baaf0ad6230ae2175398818c6 to your computer and use it in GitHub Desktop.
Bitwise flags for classifying characters.
enum CharacterClassifications {
None = 0,
Digit = 1,
Upper = 2,
Lower = 4,
Space = 8,
Control = 16,
SymbolOrPunctuation = Upper | Lower,
AlphaOrDigit = Digit | Upper | Lower | Space
}
const hasFlag = (what: CharacterClassifications, has: CharacterClassifications) => has === (has & what)
console.clear()
console.log("Upper has Upper. Should be true",
hasFlag(CharacterClassifications.Upper, CharacterClassifications.Upper))
console.log("SymbolOrPunctuation has Upper . Should be true",
hasFlag(CharacterClassifications.SymbolOrPunctuation, CharacterClassifications.Upper))
console.log("SymbolOrPunctuation has Lower. Should be true",
hasFlag(CharacterClassifications.SymbolOrPunctuation, CharacterClassifications.Lower))
console.log("AlphaOrDigit has Digit. Should be true",
hasFlag(CharacterClassifications.AlphaOrDigit, CharacterClassifications.Digit))
console.log("AlphaOrDigit has Upper. Should be true",
hasFlag(CharacterClassifications.AlphaOrDigit, CharacterClassifications.Upper))
console.log("AlphaOrDigit has Lower. Should be true",
hasFlag(CharacterClassifications.AlphaOrDigit, CharacterClassifications.Lower))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment