deploy1 7 년 전
부모
커밋
e1f2475e51
2개의 변경된 파일119개의 추가작업 그리고 0개의 파일을 삭제
  1. 104 0
      index.js
  2. 15 0
      package.json

+ 104 - 0
index.js

@@ -0,0 +1,104 @@
+    function getpath(obj, path) {
+        path = path || "";
+        var arr = path.split(".");
+        var t = obj;
+        var done = false;
+        if (arr.length) {
+            while (arr.length && !done) {
+                var check = arr.shift();
+                if (check !== "") {
+                    t = t[check];
+                }
+                if (typeof t !== "object") {
+                    done = true;
+                }
+            }
+        }
+        return t;
+    }
+
+    function setpath(obj, path, value) {
+        if (typeof obj !== "object" || !obj) {
+            throw new Error("obj is not Object");
+        }
+        if (typeof path !== "string" || path === "") {
+            throw new Error("path must be string with length > 0");
+        }
+        var arr = path.split(".");
+        var done = false;
+        var t = obj;
+        if (arr.length > 1) {
+            while (arr.length && t && !done) {
+                var check = arr.shift();
+                if (typeof t[check] === "object" && arr.length > 0) {
+                    t = t[check];
+                } else {
+                    done = true;
+                    arr.unshift(check);
+                }
+            }
+            var xt = t;
+            while (arr.length) {
+                var tt = arr.shift();
+                if (arr.length) { //go deeper
+                    xt = xt[tt] = {};
+                } else {
+                    //last
+                    xt[tt] = value;
+                }
+            }
+        } else {
+            if (arr.length === 1 && arr[0] !== "") {
+                t[arr[0]] = value;
+            }
+        }
+    }
+
+    function sort_by_1_col(a, b) {
+        return a[0] > b[0] ? -1 : a[0] < b[0] ? 1 : 0;
+    }
+
+    function sort_by_1_col_alpha(a, b) {
+        var x = a[0].toLowerCase(),
+            y = b[0].toLowerCase();
+        return x < y ? -1 : x > y ? 1 : 0;
+    }
+
+    function query(obj, str) {
+
+
+        console.log(obj);
+
+        console.log(str);
+
+
+
+    }
+
+
+    function flatten(obj, pre) {
+        var pre = pre || "";
+        var r = [];
+        for (var i in obj) {
+            if (typeof(obj[i]) === "object") {
+                r = r.concat(flatten(obj[i], (pre.length > 0 ? pre + "." : "") + i));
+            } else {
+                r.push([(pre.length > 0 ? pre + "." : "") + i, "" + obj[i]]);
+
+            }
+
+        }
+        r.sort(sort_by_1_col_alpha)
+        return r;
+
+
+    }
+
+
+
+    module.exports = {
+        get: getpath,
+        set: setpath,
+        flatten: flatten,
+        query: query
+    }

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+  "name": "opath",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "ssh://git@git.tum.dk/tum.dk/opath.git"
+  },
+  "author": "",
+  "license": "ISC"
+}