deploy1 7 år sedan
förälder
incheckning
cbfe3e7fbf
3 ändrade filer med 298 tillägg och 0 borttagningar
  1. 97 0
      index.js
  2. 15 0
      package.json
  3. 186 0
      test/tools2.test.js

+ 97 - 0
index.js

@@ -0,0 +1,97 @@
+module.exports = function(){
+
+
+ var async = require("async");
+ var actions = {};
+ var filters = {};
+
+ function add_action(actionname, actionfunction, priority) {
+     var prio = priority || 10;
+     actions[actionname] = actions[actionname] || [];
+     actions[actionname].push([prio, actionfunction]);
+     actions[actionname].sort(function(a, b) {
+         return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0;
+     })
+ }
+ var actions_applied = {};
+function do_action(actionname) {
+     actions_applied[actionname] = (actions_applied[actionname] || 0) + 1;
+     var args = Array.prototype.slice.apply(arguments, [1]);
+     if (actions[actionname]) {
+         return actions[actionname].map(function(arr) {
+             var pri = arr[0];
+             var func = arr[1];
+             return func.apply({}, args);
+         })
+     }
+ }
+ function add_filters (filtername, filterfunction, priority, filterinstancenamed) {
+     filters[filtername] = filters[filtername] || [];
+     if (typeof(priority) === "string" && typeof(filterinstancenamed) === "undefined") {
+         filterinstancenamed = priority;
+         priority = 10;
+     }
+     if (filterinstancenamed) {
+         var cindex = filters[filtername].length;
+         filters[filtername].map(function(filter, index) {
+             if (filter[2] === filterinstancenamed) {
+                 cindex = index;
+             }
+         })
+         filters[filtername][cindex] = [priority || 0, filterfunction, filterinstancenamed];
+     } else {
+         var cename = "";
+         filters[filtername].push([priority || 0, filterfunction, cename]);
+     }
+     filters[filtername].sort(function(a, b) {
+         return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0;
+     })
+ }
+ var filters_applied = {};
+ function apply_filters(filtername, val) {
+     var restargs = Array.prototype.slice.call(arguments, 2);
+     filters_applied[filtername] = (filters_applied[filtername] || 0) + 1;
+     // var oval = $lib._.clone(val);
+     //$lib.log("apply_filters", filtername, "M:" + (filters[filtername] || []).length);
+     if (filters[filtername]) {
+         var index = 0,
+             mindex = filters[filtername].length;
+         while (index < mindex) {
+             val = filters[filtername][index][1].apply(filters[filtername][index][1], [val].concat(restargs));
+             index++;
+         }
+     }
+     return val;
+ }
+function apply_filters_map(filtername) {
+     var restargs = Array.prototype.slice.call(arguments, 1);
+     var cb = restargs.pop();
+     var arr = [];
+     if (typeof(filtername) === "object") {
+         filtername.map(function(name) {
+             arr = arr.concat(apply_filters(name, []));
+         })
+     } else {
+         arr = apply_filters(filtername, []);
+     }
+     console.log("AAAAAAAAAAAAAAAAAAAAAAAAA",filtername);
+     console.log("AAAAAAAAAAAAAAAAAAAAAAAAA",arr);
+     console.log("AAAAAAAAAAAAAAAAAAAAAAAAA",restargs);
+     
+     async.mapSeries(arr, function(func, next) {
+         func.apply(func, restargs.concat(next));
+     }, cb)
+ }
+
+
+ return {
+ 	add_action: add_action,
+ 	do_action: do_action,
+ 	add_filters: add_filters,
+ 	apply_filters: apply_filters,
+ 	apply_filters_map: apply_filters_map
+
+ };
+
+}
+

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+    "name": "tools2",
+    "version": "1.0.0",
+    "description": "",
+    "main": "index.js",
+    "scripts": {
+        "test": "mocha"
+    },
+    "repository": {
+        "type": "git",
+        "url": "ssh://git@git.tum.dk/tum.dk/tools2.js.git"
+    },
+    "author": "",
+    "license": "ISC"
+}

+ 186 - 0
test/tools2.test.js

@@ -0,0 +1,186 @@
+        var tools = require("../tools2");
+
+
+        var expect = require('expect.js');
+
+
+        describe('Tools API', function() {
+            //  before(setup);
+            //  after(cleanup);
+
+
+            describe('add_action do_action', function() {
+
+                it('Can add an `action` function to be called with do_action', function(done) {
+                    var tool = tools();
+                    tool.add_action("test", function() {
+                        expect(true)
+                        done();
+                    }, 10);
+                    tool.do_action("test");
+                });
+
+                it('Can add an `action` function to be called with do_action with extra arguments', function(
+                    done) {
+                    var tool = tools();
+
+                    tool.add_action("test", function(myarg) {
+                        expect(myarg).to.equal("hello");
+                        done();
+                    }, 10);
+                    tool.do_action("test", "hello");
+                });
+
+                it('Can add an `action` function to be called with do_action sorted', function(done) {
+                    var tool = tools();
+                    var iii = 0;
+                    tool.add_action("test", function() {
+                        expect(iii).to.equal(2)
+                        iii++
+                    }, 30);
+                    tool.add_action("test", function() {
+                        expect(iii).to.equal(1)
+                        iii++
+                        done();
+                    }, 20);
+                    tool.add_action("test", function() {
+                        expect(iii).to.equal(3)
+                        iii++
+                    }, 50);
+                    tool.add_action("test", function() {
+                        expect(iii).to.equal(0)
+                        iii++
+                    }, 10);
+                    tool.do_action("test");
+                });
+
+
+                it('Can add an `action` function to be called with do_action with extra arguments sorted',
+                    function(done) {
+                        var tool = tools();
+                        var iii = 0;
+                        tool.add_action("test", function(myarg) {
+                            expect(iii).to.equal(2)
+                            expect(myarg).to.equal("hello");
+                            iii++
+                        }, 30);
+                        tool.add_action("test", function(myarg) {
+                            expect(iii).to.equal(1)
+                            expect(myarg).to.equal("hello");
+                            iii++
+                            done();
+                        }, 20);
+                        tool.add_action("test", function(myarg) {
+                            expect(iii).to.equal(3)
+                            expect(myarg).to.equal("hello");
+                            iii++
+                        }, 50);
+                        tool.add_action("test", function(myarg) {
+                            expect(iii).to.equal(0)
+                            expect(myarg).to.equal("hello");
+                            iii++
+                        }, 10);
+                        tool.do_action("test", "hello");
+                    });
+
+
+
+
+            })
+
+            describe('add_filters apply_filters', function() {
+                it('Can add an `filter` function to be called with apply_filters ARRAY', function(done) {
+                    var tool = tools();
+                    tool.add_filters("test", function(a) {
+                        a.push("test");
+                        return a;
+                    }, 10, "test");
+                    var t = tool.apply_filters("test", []);
+                    expect(t).to.be.an('array');
+                    expect(t.length).to.equal(1);
+                    expect(t[0]).to.equal("test");
+                    done();
+                });
+
+
+                it('Can add an `filter` function to be called with apply_filters ARRAY sorted', function(done) {
+                    var tool = tools();
+                    tool.add_filters("test", function(a) {
+                        a.push("test");
+                        return a;
+                    }, 10);
+                    tool.add_filters("test", function(a) {
+                        a.push("test2");
+                        return a;
+                    }, 5);
+                    var t = tool.apply_filters("test", []);
+                    expect(t).to.be.an('array');
+                    expect(t.length).to.equal(2);
+                    expect(t[0]).to.equal("test2");
+                    done();
+                });
+
+                it('Can add an `filter` function to be called with apply_filters ARRAY sorted 2', function(done) {
+                    var tool = tools();
+                    tool.add_filters("test", function(a) {
+                        if(a.length>0){
+                            a[0]=a[0]+":1";
+                        }
+                        return a;
+                    }, 10);
+                    var t = tool.apply_filters("test", ["1","2","3"]);
+                    expect(t).to.be.an('array');
+                    expect(t.length).to.equal(3);
+                    expect(t[0]).to.equal("1:1");
+                    done();
+                });
+
+
+
+                it('Can add an `filter` function to be called with apply_filters ARRAY sorted', function(done) {
+                    var tool = tools();
+                    tool.add_filters("test", function(a) {
+                        a.push(function(b, cb) {
+                            console.log("LLL", arguments);
+                            cb(null, b())
+                        })
+                        return a;
+                    }, 10);
+                    tool.add_filters("test", function(a) {
+                        a.push(function(b, cb) {
+                            console.log("oooo", arguments);
+                            cb(null, b())
+                        })
+
+                        return a;
+                    }, 5);
+                    tool.add_filters("testaa", function(a) {
+                        a.push(function(b, cb) {
+                            console.log("aaa", arguments);
+                            cb(null, b())
+                        })
+
+                        return a;
+                    }, 5);
+                    
+
+                    var t = tool.apply_filters_map(["test","testaa"],function(ao){
+                    		console.log("OOOOOAAA",ao)
+
+                    }, function(err, result) {
+                        console.log("JJJJJJJJJJJJ", arguments);
+
+                    });
+                    console.log(t);
+
+                    expect(t).to.be.an('array');
+                    expect(t.length).to.equal(2);
+                    expect(t[0]).to.equal("test2");
+                    done();
+                });
+
+
+            })
+
+
+        })