Kaynağa Gözat

add source file

mightyplow 5 yıl önce
ebeveyn
işleme
e21747a247
1 değiştirilmiş dosya ile 22 ekleme ve 0 silme
  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
+}