flags.ts 798 B

1234567891011121314151617181920212223
  1. const $isset = (flag: number, flags = 0) => (flags & flag) === flag
  2. const $set = (flag: number, flags = 0) => flags | flag
  3. const $unset = (flag: number, flags = 0) => flags ^ flag
  4. const $toggle = (flag: number, flags = 0) => $isset(flag, flags) ? $unset(flag, flags) : $set(flag, flags)
  5. const combine = (...options: number[]) => options.reduce((flags, flag) => $set(flag, flags), 0)
  6. const createMultiFlagHandler = <T>(fn: (flag: number, flags: number) => T) =>
  7. (...options: number[]) =>
  8. (flags = 0): T => fn(combine(...options), flags)
  9. const set = createMultiFlagHandler($set)
  10. const unset = createMultiFlagHandler($unset)
  11. const toggle = createMultiFlagHandler($toggle)
  12. const isset = createMultiFlagHandler($isset)
  13. export {
  14. combine,
  15. set,
  16. unset,
  17. toggle,
  18. isset
  19. }