Commits

kai zhu  committed e9c4ee8

rewrite stringFormat. rename dbHandler to handlerDb

  • Participants
  • Parent commits 7512dfd

Comments (0)

Files changed (4)

         case 'port':
           EXPORTS.HTTP_PORT = val;
           break;
+        case 'test':
+          EXPORTS.TEST_ARG = val;
+          break;
         }
       });
       if (EXPORTS.EXIT) { return; }

File libBootstrap.js

       });
     },
 
-    stringFormat: function (str, obj) {
-      //// shared client / server template engine
-      var arr, fndBraket, fndKey, flag, ii = 0, jj, result = '', rgxBraket, rgxKey, val;
-      if (!(obj && (/\{\{/).test(str))) { return str; }
-      rgxBraket = (/\{\{(\w[^}]+)([ \w]*)\}\}/g);
-      //// search for {{template}} to replace
-      while (true) {
-        fndBraket = rgxBraket.exec(str);
-        if (!fndBraket) { break; }
-        result += str.slice(ii, fndBraket.index);
-        //// get attribute from obj
-        rgxKey = (/["'][^"']+|\w+/g);
-        val = obj;
+    stringFormat: function (template, options) {
+      function getValue(keys) {
+        //// get value from options using keys
+        var fndKey, rgxKey = (/["']([^"'])+|(\w+)/g), val = options;
         while (true) {
-          fndKey = rgxKey.exec(fndBraket[1]);
-          ii = fndBraket.index + fndBraket[0].length;
+          fndKey = rgxKey.exec(keys);
           if (!(fndKey && val)) { break; }
-          val = val[fndKey[0].replace((/^\W/), '')];
-        }
-        if (val === undefined) {
-          result += fndBraket[0];
-          continue;
+          val = val[fndKey[0] || fndKey[1]];
         }
-        //// process flags
-        if (fndBraket[2]) {
-          arr = fndBraket[2].trim().split(' ');
-          for (jj = 0; jj < arr.length; jj += 1) {
-            flag = arr[jj];
-            switch (flag) {
-            //// base64 decode
-            case 'base64Encode':
-              val = val.toString('base64');
-              break;
-            //// replace all newlines with whitespaces
-            case 'singleLine':
-              val = val.replace((/\n/g), ' ').trim();
-              break;
-            //// JSON.stringify
-            case 'stringify':
-              val = JSON.stringify(val);
-              break;
-            }
-          }
+        return val;
+      }
+      function format(code) {
+        //// format {{...}}
+        var args, result;
+        args = code.split('(');
+        result = getValue(args.pop());
+        try {
+          return result === undefined ? code : args.length ? getValue(args.pop())(result) : result;
+        } catch (err) {
+          return code;
         }
-        result += val;
       }
-      return (result += str.slice(ii));
+      return template.replace((/\{\{.+?\}\}/g), format);
     }
 
   };

File libStable.js2

       EXPORTS.FS_TMP = EXPORTS.FS_TMP || '/tmp/' + EXPORTS.FS_CWD.replace(/\W/g, '_') + '_' + EXPORTS.HTTP_PORT;
       EXPORTS.FS_CACHE = EXPORTS.FS_CACHE || EXPORTS.FS_TMP + '/cache';
       EXPORTS.FS_DB = EXPORTS.FS_DB || EXPORTS.FS_TMP + '/db';
-      EXPORTS.HTTP_HANDLER['/admin/db'] = EXPORTS.dbHandler;
+      EXPORTS.HTTP_HANDLER['/admin/db'] = EXPORTS.handlerDb;
       //// create directories
       EXPORTS.requireFsExtra.mkdirpSync(EXPORTS.FS_CACHE);
       EXPORTS.requireFsExtra.mkdirpSync(EXPORTS.FS_DB);
       return path;
     },
 
-    dbHandler: function (request, response, next) {
+    fsTempFilename: function (filename) {
+      return EXPORTS.FS_CACHE + '/' + (filename || '').replace(/\W/g, '_') + '.' + new Date().getTime().toString(16) + Math.random().toString(16).slice(1);
+    },
+
+    fsWriteFileAtomic: function (filename, data, callback) {
+      //// atomicly write file by first writing to temporary file, and then renaming to filename
+      var tmp = EXPORTS.fsTempFilename(filename);
+      EXPORTS.requireFs.writeFile(tmp, data, function (err) {
+        if (err) {
+          callback(err);
+          return;
+        }
+        EXPORTS.requireFs.rename(tmp, filename, callback);
+      });
+    },
+
+    handlerDb: function (request, response, next) {
       EXPORTS.streamReadBuffer(request, function (err, data) {
         var action, fnd, path;
         function callback(err, data) {
           next(errHandler);
         }
       });
-    },
-
-    fsTempFilename: function (filename) {
-      return EXPORTS.FS_CACHE + '/' + (filename || '').replace(/\W/g, '_') + '.' + new Date().getTime().toString(16) + Math.random().toString(16).slice(1);
-    },
-
-    fsWriteFileAtomic: function (filename, data, callback) {
-      //// atomicly write file by first writing to temporary file, and then renaming to filename
-      var tmp = EXPORTS.fsTempFilename(filename);
-      EXPORTS.requireFs.writeFile(tmp, data, function (err) {
-        if (err) {
-          callback(err);
-          return;
-        }
-        EXPORTS.requireFs.rename(tmp, filename, callback);
-      });
     }
 
   };

File libUnstable.js2

 /*
 CHANGELOG
-cleanup js instrumenter and rename to coverage. replace manual exports with EXPORTS.exportLocal.
+rewrite stringFormat.  rename dbHandler to handlerDb.
 
 TODO
+add MODULE_ACTION['write']
 auto-generate online documentation
 add optional istanbul coverage in require
-add client side interactive tests
+add client-side interactive tests
 rewrite db
  */
 
     init: function () {
       //// EXPORTS
       EXPORTS.exportLocal(local);
-    }
+      // debug
+      // printd(
+        // EXPORTS.stringFormat2('{{console.log(aa)}}', {aa: 1, console: 34})
+      // );
+    },
 
   };
 
       Object.keys(local).forEach(function (key) {
         if (!window.hasOwnProperty(key)) { window[key] = local[key]; }
       });
-    },
+    }
 
   };