JavaScripTools Manual


Using the WrapperParser

The WrapperParser wraps another parser, allowing post-processing the result for it's format method, and pre-processing the value for the parse method. Like the CustomParser, the WrapperParser also uses a formatFunction and a parseFunction. The formatFunction will be invoked after the wrapped parser's format method, and will receive as arguments the formatted value and the original value. The parseFunction will be invoked before the parser's parse method and will receive the value.


//A parser that writes / reads dates inside an html tag, making the color red when the month is january

//Function to add the tag
function addTag(dateAsString, date) {
    var style;
    if (date != null && date.getMonth() == 0) {
        style = " style='color:red'"; 
    return "<div" + style + ">" + dateAsString + "</div>";
//Function to remove the tag
function removeTag(value) {
    if (value == null) {
        return null;
    return value.replace(/<\/?[^>]+>/gi, '');
var dateParser = new DateParser("yyyy-MM-dd");
var parser = new WrapperParser(dateParser, addTag, removeTag);
parser.format(new Date(2000, 0, 10)) -> "<div style='color:red'>2000-01-10</div>"
parser.format(new Date(2000, 1, 10)) -> "<div>2000-01-10</div>"
parser.parse("<div>2000-01-10</div>") -> "date with day=10, month=0, year=2000"

Using the CustomParser
Table of Contents Next:
Other parsers