|
@@ -0,0 +1,22 @@
|
|
|
+const $isset = (flag: number, sequence = 0) => (sequence & flag) === flag
|
|
|
+const $set = (flag: number, sequence = 0) => sequence | flag
|
|
|
+const $unset = (flag: number, sequence = 0) => sequence & ~flag
|
|
|
+const $toggle = (flag: number, sequence = 0) => sequence ^ flag
|
|
|
+
|
|
|
+const combine = (flags: number[]) => flags.reduce((flags, flag) => flags + flag, 0)
|
|
|
+
|
|
|
+const createMultiFlagHandler = <T>(fn: (flag: number, flags: number) => T) =>
|
|
|
+ (flag: number | number[], sequence = 0): T =>
|
|
|
+ fn(Array.isArray(flag) ? combine(flag) : flag, sequence)
|
|
|
+
|
|
|
+const set = createMultiFlagHandler($set)
|
|
|
+const unset = createMultiFlagHandler($unset)
|
|
|
+const toggle = createMultiFlagHandler($toggle)
|
|
|
+const isset = createMultiFlagHandler($isset)
|
|
|
+
|
|
|
+export {
|
|
|
+ isset,
|
|
|
+ set,
|
|
|
+ unset,
|
|
|
+ toggle
|
|
|
+}
|