deploy1 7 anos atrás
pai
commit
b0dbc1abfb
3 arquivos alterados com 303 adições e 99 exclusões
  1. 8 99
      index.js
  2. 112 0
      lib/actionsfilters.js
  3. 183 0
      lib/spfprintf.js

+ 8 - 99
index.js

@@ -1,111 +1,20 @@
-function create_tool(){
 
-    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
-
-    };
-
-
-}
 
 
 module.exports =  function extend(reciever) {
-        var tool = create_tool();
+        var tool = require("./lib/actionsfilters")();
+        var spfprintf = require("./lib/spfprintf");
+
         if (typeof reciever === "object" || typeof reciever === "function") {
             for(var p in tool){
                 reciever[p] = tool[p];
             }
+
+            for(var p in spfprintf){
+                reciever[p] = spfprintf[p];
+            }
+
         }
         return reciever;
 }

+ 112 - 0
lib/actionsfilters.js

@@ -0,0 +1,112 @@
+function create_tool(){
+
+    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
+
+    };
+
+
+}
+
+
+module.exports =  function extend(reciever) {
+        var tool = create_tool();
+        if (typeof reciever === "object" || typeof reciever === "function") {
+            for(var p in tool){
+                reciever[p] = tool[p];
+            }
+        }
+        return reciever;
+}

+ 183 - 0
lib/spfprintf.js

@@ -0,0 +1,183 @@
+/*
+ * extsprintf.js: extended POSIX-style sprintf
+ */
+
+var mod_assert = require('assert');
+var mod_util = require('util');
+
+/*
+ * Public interface
+ */
+exports.sprintf = jsSprintf;
+exports.printf = jsPrintf;
+exports.fprintf = jsFprintf;
+
+/*
+ * Stripped down version of s[n]printf(3c).  We make a best effort to throw an
+ * exception when given a format string we don't understand, rather than
+ * ignoring it, so that we won't break existing programs if/when we go implement
+ * the rest of this.
+ *
+ * This implementation currently supports specifying
+ *	- field alignment ('-' flag),
+ * 	- zero-pad ('0' flag)
+ *	- always show numeric sign ('+' flag),
+ *	- field width
+ *	- conversions for strings, decimal integers, and floats (numbers).
+ *	- argument size specifiers.  These are all accepted but ignored, since
+ *	  Javascript has no notion of the physical size of an argument.
+ *
+ * Everything else is currently unsupported, most notably precision, unsigned
+ * numbers, non-decimal numbers, and characters.
+ */
+function jsSprintf(fmt)
+{
+	var regex = [
+	    '([^%]*)',				/* normal text */
+	    '%',				/* start of format */
+	    '([\'\\-+ #0]*?)',			/* flags (optional) */
+	    '([1-9]\\d*)?',			/* width (optional) */
+	    '(\\.([1-9]\\d*))?',		/* precision (optional) */
+	    '[lhjztL]*?',			/* length mods (ignored) */
+	    '([diouxXfFeEgGaAcCsSp%jr])'	/* conversion */
+	].join('');
+
+	var re = new RegExp(regex);
+	var args = Array.prototype.slice.call(arguments, 1);
+	var flags, width, precision, conversion;
+	var left, pad, sign, arg, match;
+	var ret = '';
+	var argn = 1;
+
+	mod_assert.equal('string', typeof (fmt));
+
+	while ((match = re.exec(fmt)) !== null) {
+		ret += match[1];
+		fmt = fmt.substring(match[0].length);
+
+		flags = match[2] || '';
+		width = match[3] || 0;
+		precision = match[4] || '';
+		conversion = match[6];
+		left = false;
+		sign = false;
+		pad = ' ';
+
+		if (conversion == '%') {
+			ret += '%';
+			continue;
+		}
+
+		if (args.length === 0)
+			throw (new Error('too few args to sprintf'));
+
+		arg = args.shift();
+		argn++;
+
+		if (flags.match(/[\' #]/))
+			throw (new Error(
+			    'unsupported flags: ' + flags));
+
+		if (precision.length > 0)
+			throw (new Error(
+			    'non-zero precision not supported'));
+
+		if (flags.match(/-/))
+			left = true;
+
+		if (flags.match(/0/))
+			pad = '0';
+
+		if (flags.match(/\+/))
+			sign = true;
+
+		switch (conversion) {
+		case 's':
+			if (arg === undefined || arg === null)
+				throw (new Error('argument ' + argn +
+				    ': attempted to print undefined or null ' +
+				    'as a string'));
+			ret += doPad(pad, width, left, arg.toString());
+			break;
+
+		case 'd':
+			arg = Math.floor(arg);
+			/*jsl:fallthru*/
+		case 'f':
+			sign = sign && arg > 0 ? '+' : '';
+			ret += sign + doPad(pad, width, left,
+			    arg.toString());
+			break;
+
+		case 'x':
+			ret += doPad(pad, width, left, arg.toString(16));
+			break;
+
+		case 'j': /* non-standard */
+			if (width === 0)
+				width = 10;
+			ret += mod_util.inspect(arg, false, width);
+			break;
+
+		case 'r': /* non-standard */
+			ret += dumpException(arg);
+			break;
+
+		default:
+			throw (new Error('unsupported conversion: ' +
+			    conversion));
+		}
+	}
+
+	ret += fmt;
+	return (ret);
+}
+
+function jsPrintf() {
+	var args = Array.prototype.slice.call(arguments);
+	args.unshift(process.stdout);
+	jsFprintf.apply(null, args);
+}
+
+function jsFprintf(stream) {
+	var args = Array.prototype.slice.call(arguments, 1);
+	return (stream.write(jsSprintf.apply(this, args)));
+}
+
+function doPad(chr, width, left, str)
+{
+	var ret = str;
+
+	while (ret.length < width) {
+		if (left)
+			ret += chr;
+		else
+			ret = chr + ret;
+	}
+
+	return (ret);
+}
+
+/*
+ * This function dumps long stack traces for exceptions having a cause() method.
+ * See node-verror for an example.
+ */
+function dumpException(ex)
+{
+	var ret;
+
+	if (!(ex instanceof Error))
+		throw (new Error(jsSprintf('invalid type for %%r: %j', ex)));
+
+	/* Note that V8 prepends "ex.stack" with ex.toString(). */
+	ret = 'EXCEPTION: ' + ex.constructor.name + ': ' + ex.stack;
+
+	if (ex.cause && typeof (ex.cause) === 'function') {
+		var cex = ex.cause();
+		if (cex) {
+			ret += '\nCaused by: ' + dumpException(cex);
+		}
+	}
+
+	return (ret);
+}