Mustache.js Test

Testing various methods of mustache.js libarary

by nickadeemus2002

JavaScript

/***
* my tests
*/  
//object
//var myObj = {};
//console.log(myObj.toString());

/***
* library code below
*/

(function(root){

    console.log('root:');
console.log(root);
    
//properties
var whiteRe = /\s*/;                   //check for whitespace
var spaceRe = /\s+/;                   //check for space
var nonSpaceRe = /\S/;                 //check for non-whitespace
var eqRe = /\s*=/;                     
var curlyRe = /\s*\}/;                 //check for culry
var tagRe = /#|\^|\/|>|\{|&|=|!/;      //check for special characters


//use JS RegExp Obj to define internal regex test method
var RegExp_test = RegExp.prototype.test;
function testRegExp(re, string) {
    return RegExp_test.call(re, string);
}

//test for whitespace
function isWhitespace(string) {
  return !testRegExp(nonSpaceRe, string);
}

//test for array using native .isArray or use JS Obj.toString to test
var Object_toString = Object.prototype.toString;
var isArray = Array.isArray || function (obj) {
  return Object_toString.call(obj) === '[object Array]';
};

//make HTML safe with escape characters
var entityMap = {
    "&": "&",
    "<": "&lt;",
    ">": "&gt;",
    '"': '&quot;',
    "'": '&#39;',
    "/": '&#x2F;'
  };

function escapeHtml(string) {
    return String(string).replace(/[&<>"'\/]/g, function (s) {
      return entityMap[s];
    });
}

    //test escapeHtml
    //console.log(escapeHtml('<h1>Hello ! & >< Worl/d</h1>'));

//---------------
//Scanner Obj
//---------------

//constructor
function Scanner(string) {
  this.string = string;
  this.tail = string;
  this.pos = 0;
}

//Returns `true` if the tail is empty (end of string).
Scanner.prototype.eos = function () {
  return this.tail === "";
};

    //~ eos test
    //var scanned = new Scanner('this is my cool test string');
    //console.log(scanned.eos());


// Tries to match the given regular expression at the current position.
// Returns the matched text if it can match, the empty string...