Prechádzať zdrojové kódy

add first verison of flag helpers

mightyplow 5 rokov pred
commit
c0c10b92c9
5 zmenil súbory, kde vykonal 107 pridanie a 0 odobranie
  1. 2 0
      .gitignore
  2. 50 0
      flags.js
  3. 27 0
      flags.ts
  4. 13 0
      package-lock.json
  5. 15 0
      package.json

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+node_modules/
+.flags.js

+ 50 - 0
flags.js

@@ -0,0 +1,50 @@
+"use strict";
+exports.__esModule = true;
+var applyFlagHandler = function (fn) { return function (flags) { return function (initial) {
+    if (initial === void 0) { initial = 0; }
+    return fn(flags)(initial);
+}; }; };
+var $isset = function (flag) { return function (flags) {
+    if (flags === void 0) { flags = 0; }
+    return (flags & flag) === flag;
+}; };
+var $set = function (flag) { return function (flags) {
+    if (flags === void 0) { flags = 0; }
+    return flags | flag;
+}; };
+var $unset = function (flag) { return function (flags) {
+    if (flags === void 0) { flags = 0; }
+    return flags ^ flag;
+}; };
+var $toggle = function (flag) { return function (flags) {
+    if (flags === void 0) { flags = 0; }
+    return $isset(flag)(flags) ? $unset(flag)(flags) : $set(flag)(flags);
+}; };
+var combine = function () {
+    var options = [];
+    for (var _i = 0; _i < arguments.length; _i++) {
+        options[_i] = arguments[_i];
+    }
+    return options.reduce(function (flags, flag) { return $set(flag)(flags); }, 0);
+};
+exports.combine = combine;
+var createMultiFlagHandler = function (fn) { return function () {
+    var options = [];
+    for (var _i = 0; _i < arguments.length; _i++) {
+        options[_i] = arguments[_i];
+    }
+    return function (flags) {
+        if (flags === void 0) { flags = 0; }
+        return applyFlagHandler(fn)(combine.apply(void 0, options))(flags);
+    };
+}; };
+var set = createMultiFlagHandler($set);
+exports.set = set;
+var unset = createMultiFlagHandler($unset);
+exports.unset = unset;
+var toggle = createMultiFlagHandler($toggle);
+exports.toggle = toggle;
+var isset = createMultiFlagHandler($isset);
+exports.isset = isset;
+var settings = set(1, 4, 8)();
+console.log(isset(4)(settings));

+ 27 - 0
flags.ts

@@ -0,0 +1,27 @@
+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
+}

+ 13 - 0
package-lock.json

@@ -0,0 +1,13 @@
+{
+  "name": "flags",
+  "version": "1.0.0",
+  "lockfileVersion": 1,
+  "requires": true,
+  "dependencies": {
+    "typescript": {
+      "version": "3.6.4",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz",
+      "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg=="
+    }
+  }
+}

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+  "name": "flags",
+  "version": "1.0.0",
+  "description": "",
+  "main": "flags.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "keywords": [],
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "typescript": "^3.6.4"
+  }
+}