mightyplow пре 5 година
родитељ
комит
e21747a247
1 измењених фајлова са 22 додато и 0 уклоњено
  1. 22 0
      src/flags.ts

+ 22 - 0
src/flags.ts

@@ -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
+}