flags.js 677 B

1234567891011
  1. const $isset = (flag, flags = 0) => (flags & flag) === flag;
  2. const $set = (flag, flags = 0) => flags | flag;
  3. const $unset = (flag, flags = 0) => flags ^ flag;
  4. const $toggle = (flag, flags = 0) => $isset(flag, flags) ? $unset(flag, flags) : $set(flag, flags);
  5. const combine = (...options) => options.reduce((flags, flag) => $set(flag, flags), 0);
  6. const createMultiFlagHandler = (fn) => (...options) => (flags = 0) => fn(combine(...options), flags);
  7. const set = createMultiFlagHandler($set);
  8. const unset = createMultiFlagHandler($unset);
  9. const toggle = createMultiFlagHandler($toggle);
  10. const isset = createMultiFlagHandler($isset);
  11. export { combine, set, unset, toggle, isset };