flags.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. exports.__esModule = true;
  3. var applyFlagHandler = function (fn) { return function (flags) { return function (initial) {
  4. if (initial === void 0) { initial = 0; }
  5. return fn(flags)(initial);
  6. }; }; };
  7. var $isset = function (flag) { return function (flags) {
  8. if (flags === void 0) { flags = 0; }
  9. return (flags & flag) === flag;
  10. }; };
  11. var $set = function (flag) { return function (flags) {
  12. if (flags === void 0) { flags = 0; }
  13. return flags | flag;
  14. }; };
  15. var $unset = function (flag) { return function (flags) {
  16. if (flags === void 0) { flags = 0; }
  17. return flags ^ flag;
  18. }; };
  19. var $toggle = function (flag) { return function (flags) {
  20. if (flags === void 0) { flags = 0; }
  21. return $isset(flag)(flags) ? $unset(flag)(flags) : $set(flag)(flags);
  22. }; };
  23. var combine = function () {
  24. var options = [];
  25. for (var _i = 0; _i < arguments.length; _i++) {
  26. options[_i] = arguments[_i];
  27. }
  28. return options.reduce(function (flags, flag) { return $set(flag)(flags); }, 0);
  29. };
  30. exports.combine = combine;
  31. var createMultiFlagHandler = function (fn) { return function () {
  32. var options = [];
  33. for (var _i = 0; _i < arguments.length; _i++) {
  34. options[_i] = arguments[_i];
  35. }
  36. return function (flags) {
  37. if (flags === void 0) { flags = 0; }
  38. return applyFlagHandler(fn)(combine.apply(void 0, options))(flags);
  39. };
  40. }; };
  41. var set = createMultiFlagHandler($set);
  42. exports.set = set;
  43. var unset = createMultiFlagHandler($unset);
  44. exports.unset = unset;
  45. var toggle = createMultiFlagHandler($toggle);
  46. exports.toggle = toggle;
  47. var isset = createMultiFlagHandler($isset);
  48. exports.isset = isset;
  49. var settings = set(1, 4, 8)();
  50. console.log(isset(4)(settings));