Commits

kai zhu  committed f66f2f5

auto-convert object to string in stringFormat. cleanup externalDataUri.

  • Participants
  • Parent commits 4ba23c9

Comments (0)

Files changed (3)

File libBootstrap.js

 
     stringFormat: function (str, obj) {
       //// shared client / server template engine
-      var arr, fnd, fnd2, flag, ii, jj, result, rgx, rgx2, val;
+      var arr, fndBraket, fndKey, flag, ii, jj, result, rgxBraket, rgxKey, val;
       if (!(obj && (/\{\{/).test(str))) { return str; }
       ii = 0;
       result = '';
-      rgx = (/\{\{(\w[^}]+)([ \w]*)\}\}/g);
+      rgxBraket = (/\{\{(\w[^}]+)([ \w]*)\}\}/g);
+      //// search for {{template}} to replace
       while (true) {
-        fnd = rgx.exec(str);
-        //// stop searching for strings to replace
-        if (!fnd) { break; }
-        result += str.slice(ii, fnd.index);
+        fndBraket = rgxBraket.exec(str);
+        if (!fndBraket) { break; }
+        result += str.slice(ii, fndBraket.index);
         //// get attribute from obj
-        rgx2 = (/["']([^"']+)["']|(\w+)/g);
+        rgxKey = (/["'][^"']+|\w+/g);
         val = obj;
         while (true) {
-          fnd2 = rgx2.exec(fnd[1]);
-          ii = fnd.index + fnd[0].length;
-          if (!(fnd2 && val)) { break; }
-          val = val[fnd2[1] || fnd2[2]];
+          fndKey = rgxKey.exec(fndBraket[1]);
+          ii = fndBraket.index + fndBraket[0].length;
+          if (!(fndKey && val)) { break; }
+          val = val[fndKey[0].replace((/^\W/), '')];
         }
         if (val === undefined) {
-          result += fnd[0];
+          result += fndBraket[0];
           continue;
         }
         //// process flags
-        if (fnd[2]) {
-          arr = fnd[2].trim().split(' ');
+        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;
-            case 'toString':
-              val = val.toString();
-              break;
-            case 'trim':
-              val = val.replace(/\n/g, ' ').trim();
-              break;
             }
           }
         }

File libStable.js2

       if (EXPORTS.EXTERNAL_ROLLUP) {
         EXPORTS.externalRollup();
       }
-
     },
 
     externalDataUri: function (filename) {
-      return 'data:' + EXPORTS.requireMime.lookup(filename) + ';base64,' + EXPORTS.requireFs.readFileSync(filename, 'base64');
+      return '\n"data:' + EXPORTS.requireMime.lookup(filename) + ';base64,' + EXPORTS.requireFs.readFileSync(filename, 'base64') + '"\n';
     },
 
     externalGet: function (arr) {
       var data = EXPORTS.requireFs.readFileSync(EXPORTS.FS_EXTERNAL + '/' + filename, 'utf8') + '\n';
       switch (filename) {
       case 'bootstrap.css':
-        data = data.replace(/\.\.\/img\/([^"]+)/g, function (_, filename) {
+        data = data.replace(/"\.\.\/img\/(.+)"/g, function (_, filename) {
           return local.externalDataUri(EXPORTS.FS_EXTERNAL + '/bootstrap.' + filename);
         });
         break;
       case 'datatables.css':
-        data = data.replace(/\.\.\/images\/([^']+)/g, function (_, filename) {
+        data = data.replace(/'\.\.\/images\/(.+)'/g, function (_, filename) {
           return local.externalDataUri(EXPORTS.FS_EXTERNAL + '/datatables.' + filename);
         });
         break;
       case 'select2.css':
-        data = data.replace(/(\w+\.png)/g, function (_, filename) {
+        data = data.replace(/'(\w+\.png)'/g, function (_, filename) {
           return local.externalDataUri(EXPORTS.FS_EXTERNAL + '/' + filename);
         });
         break;
       [
         'imgAjaxLoader.gif'
       ].forEach(function (filename) {
-        css += '.' + (/\w+/).exec(filename) + ' { background: url("' + local.externalDataUri(EXPORTS.FS_EXTERNAL + '/' + filename) + '") no-repeat center center;';
+        css += '.' + (/\w+/).exec(filename) + ' { background: url(' + local.externalDataUri(EXPORTS.FS_EXTERNAL + '/' + filename) + ') no-repeat center center;';
       });
       EXPORTS.requireFs.writeFileSync(EXPORTS.FS_EXTERNAL + '/externalRollup.css', css);
       EXPORTS.requireFs.writeFileSync(EXPORTS.FS_EXTERNAL + '/externalRollup.js', js);

File libUnstable.js2

 /*
 CHANGELOG
-modify stringFormat to use standard javascript attribute access
+auto-convert object to string in stringFormat. cleanup externalDataUri.
 
 TODO
+auto-generate online documentation
 add optional istanbul coverage in require
 add client side interactive tests
 rewrite db
     init: function () {
       var arr, ii, key;
       for (key in local) {
-        if (local.hasOwnProperty(key) && key !== 'init') {
+        if (local.hasOwnProperty(key) && !(/^_|^init$/).test(key)) {
           EXPORTS[key] = local[key];
         }
       }