Skip to content

Instantly share code, notes, and snippets.

@alcedoatthis
Forked from honzabrecka/flags.ts
Last active May 10, 2017 10:06
Show Gist options
  • Save alcedoatthis/e9d6bbd1f2fb37825fd182af3240f41b to your computer and use it in GitHub Desktop.
Save alcedoatthis/e9d6bbd1f2fb37825fd182af3240f41b to your computer and use it in GitHub Desktop.
enum Flags {
A = 1 << 0,
B = 1 << 1,
C = 1 << 2,
}
// number means uint
const allowed = (flags: Flags[], flag: Flags): boolean =>
(flags.reduce((p, c) => p | c) & flag) === flag
const allowedFlags = [Flags.B | Flags.C]
console.log(allowed(allowedFlags, Flags.A))// -> false
console.log(allowed(allowedFlags, Flags.B))// -> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment