GetFormatter and GetPredicate are almost identical, and could probably
be combined. This assuming that any formatter may have params, and if
so, saves them to include later in _DoSubstitute and _DoPredicate.
var _CALL_RE = /^if\s+(@|[A-Za-z0-9_-]+)(?:\s+(.*?))?\s*$/;
function GetFormatter(format_str) {
format_str = format_str.trim();
var formatter = more_formatters(format_str) || DEFAULT_FORMATTERS
[format_str];
if (formatter === undefined) {
var matches = format_str.match(_CALL_RE);
if (matches && (formatter =
DEFAULT_FORMATTERS[matches[1].trim()]))
{
var args = matches[2].trim();
formatter = [formatter,args];
}
};
if (formatter === undefined) {
throw new JsonTemplateException({
name: 'BadFormatter',
message: format_str + ' is not a valid formatter'
});
}
return formatter;
}
function GetPredicate(predicate_str) {
predicate_str = predicate_str.trim();
var predicate = DEFAULT_PREDICATES[predicate_str];
if (predicate === undefined) {
var matches = predicate_str.match();
if (matches && (predicate =
DEFAULT_PREDICATES[matches[1].trim()]))
{
var args = matches[2].trim();
predicate = [predicate,args];
}
};
if (predicate === undefined) {
throw new JsonTemplateException({
name: 'BadPredicate',
message: predicate_str + ' is not a valid predicate'
});
}
return predicate;
}
function _DoSubstitute(statement, context, callback) {
log('Substituting: ' + statement.name);
var value;
if (statement.name == '@') {
value = context.CursorValue();
}
else {
value = context.Lookup(statement.name);
}
// Format values
for (var i = 0; i < statement.formatters.length; i++) {
var f = statement.formatters[i];
if (Ext.isArray(f)) {
value = f[0](value,f[1]);
} else {
value = f(value);
}
}
callback(value);
}
function _DoPredicate(block, context) {
var do_section, predicate_target_value = context.PushSection
(block.predicate_target);
var f = block.predicate;
if (Ext.isArray(f)) {
do_section = f[0](predicate_target_value, f[1]);
}
else {
do_section = f(predicate_target_value);
}
context.Pop();
return do_section;
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JSON
Template" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/json-template?hl=en
-~----------~----~----~----~------~----~------~--~---