123456789101112131415161718192021222324252627 |
- type FlagHandler = (flag: number) => (flags: number) => number | boolean
- const applyFlagHandler = (fn: FlagHandler) => (flags: number) => (initial = 0) => fn(flags)(initial)
- const $isset = (flag: number) => (flags = 0): boolean => (flags & flag) === flag
- const $set = (flag: number) => (flags = 0): number => flags | flag
- const $unset = (flag: number) => (flags = 0): number => flags ^ flag
- const $toggle = (flag: number) => (flags = 0): number => $isset(flag)(flags) ? $unset(flag)(flags) : $set(flag)(flags)
- const combine = (...options: number[]) => options.reduce((flags, flag) => $set(flag)(flags), 0)
- const createMultiFlagHandler = (fn: FlagHandler) => (...options: number[]) => (flags = 0) => applyFlagHandler(fn)(combine(...options))(flags)
- const set = createMultiFlagHandler($set)
- const unset = createMultiFlagHandler($unset)
- const toggle = createMultiFlagHandler($toggle)
- const isset = createMultiFlagHandler($isset)
- const settings = set(1, 4, 8)()
- console.log(isset(4)(settings))
- export {
- combine,
- set,
- unset,
- toggle,
- isset
- }
|