common-dustjs-helpers
Version:
A small library of "standard" helper functions for the Dust.js web templating framework.
676 lines (632 loc) • 25.5 kB
JavaScript
// Generated by CoffeeScript 1.12.6
(function() {
var CommonDustjsHelpers, exports,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
CommonDustjsHelpers = (function() {
function CommonDustjsHelpers() {
this._compare = bind(this._compare, this);
this._attribute_comparator = bind(this._attribute_comparator, this);
this._get_substring = bind(this._get_substring, this);
this._inner_if_helper = bind(this._inner_if_helper, this);
this._to_int = bind(this._to_int, this);
this.upcase_helper = bind(this.upcase_helper, this);
this.unless_helper = bind(this.unless_helper, this);
this.switch_helper = bind(this.switch_helper, this);
this.trim_helper = bind(this.trim_helper, this);
this.titlecase_helper = bind(this.titlecase_helper, this);
this.substring_helper = bind(this.substring_helper, this);
this.repeat_helper = bind(this.repeat_helper, this);
this.regexp_helper = bind(this.regexp_helper, this);
this.random_helper = bind(this.random_helper, this);
this.odd_helper = bind(this.odd_helper, this);
this.last_helper = bind(this.last_helper, this);
this.if_helper = bind(this.if_helper, this);
this.first_helper = bind(this.first_helper, this);
this.filter_helper = bind(this.filter_helper, this);
this.even_helper = bind(this.even_helper, this);
this.elements_helper = bind(this.elements_helper, this);
this.downcase_helper = bind(this.downcase_helper, this);
this.deorphan_helper = bind(this.deorphan_helper, this);
this.count_helper = bind(this.count_helper, this);
this.get_filters = bind(this.get_filters, this);
this.get_helpers = bind(this.get_helpers, this);
this.export_filters_to = bind(this.export_filters_to, this);
this.export_helpers_to = bind(this.export_helpers_to, this);
this.export_to = bind(this.export_to, this);
}
CommonDustjsHelpers.dust = null;
CommonDustjsHelpers.prototype.export_to = function(dust) {
this.export_helpers_to(dust);
this.export_filters_to(dust);
return this;
};
CommonDustjsHelpers.prototype.export_helpers_to = function(dust) {
dust.helpers = this.get_helpers(dust.helpers);
CommonDustjsHelpers.dust = dust;
return this;
};
CommonDustjsHelpers.prototype.export_filters_to = function(dust) {
dust.filters = this.get_filters(dust.filters);
CommonDustjsHelpers.dust = dust;
return this;
};
CommonDustjsHelpers.prototype.render_template = function(template_body, context, callback) {
return CommonDustjsHelpers.dust.renderSource(template_body, context, callback);
};
CommonDustjsHelpers.prototype.get_helpers = function(helpers) {
if (helpers == null) {
helpers = {};
}
helpers['count'] = this.count_helper;
helpers['deorphan'] = this.deorphan_helper;
helpers['downcase'] = this.downcase_helper;
helpers['elements'] = this.elements_helper;
helpers['even'] = this.even_helper;
helpers['filter'] = this.filter_helper;
helpers['first'] = this.first_helper;
if (helpers['idx'] == null) {
helpers['idx'] = this.classic_idx;
}
helpers['if'] = this.if_helper;
helpers['index'] = this.index_helper;
helpers['last'] = this.last_helper;
helpers['odd'] = this.odd_helper;
helpers['random'] = this.random_helper;
helpers['regexp'] = this.regexp_helper;
helpers['repeat'] = this.repeat_helper;
if (helpers['sep'] == null) {
helpers['sep'] = this.classic_sep;
}
helpers['substring'] = this.substring_helper;
helpers['switch'] = this.switch_helper;
helpers['titlecase'] = helpers['Titlecase'] = this.titlecase_helper;
helpers['trim'] = this.trim_helper;
helpers['unless'] = this.unless_helper;
helpers['upcase'] = helpers['UPCASE'] = this.upcase_helper;
return helpers;
};
CommonDustjsHelpers.prototype.get_filters = function(filters) {
if (filters == null) {
filters = {};
}
filters['json'] = this.json_filter;
return filters;
};
CommonDustjsHelpers.prototype.json_filter = function(value) {
var json, ref;
if ((ref = typeof value) === 'number' || ref === 'boolean') {
return "" + value;
} else if (typeof value === 'string') {
json = JSON.stringify(value);
json = json.substring(1, json.length - 1);
return json;
} else if (value != null) {
return JSON.stringify(value);
} else {
return value;
}
};
CommonDustjsHelpers.prototype.count_helper = function(chunk, context, bodies, params) {
var ref, value;
value = this._eval_dust_string((ref = params.of) != null ? ref : params["in"], chunk, context);
if (Array.isArray(value)) {
chunk.write(value.length);
} else if (typeof value === "object") {
chunk.write(Object.keys(value).length);
}
return chunk;
};
CommonDustjsHelpers.prototype.deorphan_helper = function(chunk, context, bodies, params) {
return chunk.capture(bodies.block, context, (function(_this) {
return function(data, chunk) {
var match;
data = _this._eval_dust_string(data, chunk, context);
match = data.match(/^((.|\s)+[^\s]{1})\s+([^\s]+\s*)$/);
if ((match != null) && (match[1] != null) && (match[2] != null)) {
data = match[1] + " " + match[3];
}
chunk.write(data);
return chunk.end();
};
})(this));
};
CommonDustjsHelpers.prototype.downcase_helper = function(chunk, context, bodies, params) {
return chunk.capture(bodies.block, context, function(data, chunk) {
chunk.write(data.toLowerCase());
return chunk.end();
});
};
CommonDustjsHelpers.prototype.elements_helper = function(chunk, context, bodies, params) {
var comparator, ctx, dir, index, index_name, j, k, key_name, len, obj, p, pair, pairs, ref, ref1, reverse_sort, sort, v, value_name;
obj = (ref = params.of) != null ? ref : params["in"];
if (typeof obj !== 'object') {
obj = null;
}
index_name = "$idx";
if (((params != null ? params['idx'] : void 0) != null) || ((params != null ? params['index'] : void 0) != null)) {
index_name = this._eval_dust_string((ref1 = params['idx']) != null ? ref1 : params['index'], chunk, context);
}
key_name = "$key";
if ((params != null ? params['key'] : void 0) != null) {
key_name = this._eval_dust_string(params['key'], chunk, context);
}
value_name = "$value";
if ((params != null ? params['value'] : void 0) != null) {
value_name = this._eval_dust_string(params['value'], chunk, context);
}
sort = null;
if ((params != null ? params['sort'] : void 0) != null) {
sort = this._eval_dust_string(params['sort'], chunk, context);
if (/^(t(rue)?$)/i.test(sort)) {
sort = true;
} else if (/^(f(alse)?$)/i.test(sort)) {
sort = null;
}
}
if (sort != null) {
reverse_sort = false;
if ((params != null ? params['dir'] : void 0) != null) {
dir = this._eval_dust_string(params['dir'], chunk, context);
if (/^D((SC)|(ES(C(END(ING)?)?)?))?$/i.test(dir)) {
reverse_sort = true;
}
}
}
pairs = [];
if (obj != null) {
index = 0;
for (k in obj) {
v = obj[k];
pair = {
key: k,
value: v
};
if (typeof sort === 'string') {
if (sort === "") {
pair.sortkey = v;
} else {
pair.sortkey = v != null ? v[sort] : void 0;
}
}
pairs.push(pair);
}
if (sort != null) {
if (sort === true) {
comparator = this._attribute_comparator("key", reverse_sort);
} else if (typeof sort === 'string') {
comparator = this._attribute_comparator("sortkey", reverse_sort);
}
pairs = pairs.sort(comparator);
}
}
if (pairs.length > 0) {
for (j = 0, len = pairs.length; j < len; j++) {
p = pairs[j];
ctx = {};
ctx[index_name] = index;
ctx[key_name] = p.key;
ctx[value_name] = p.value;
context = context.push(ctx);
context.stack.index = index;
context.stack.of = pairs.length;
chunk = bodies.block(chunk, context);
index++;
}
} else {
if (bodies["else"] != null) {
chunk = chunk.render(bodies["else"], context);
}
}
return chunk;
};
CommonDustjsHelpers.prototype.even_helper = function(chunk, context, bodies, params) {
var c, ref;
if ((context != null ? (ref = context.stack) != null ? ref.index : void 0 : void 0) != null) {
c = context.stack.index % 2 === 0;
return this._render_if_else(c, chunk, context, bodies, params);
} else {
return chunk;
}
};
CommonDustjsHelpers.prototype.filter_helper = function(chunk, context, bodies, params) {
var filter_type;
if ((params != null ? params.type : void 0) != null) {
filter_type = this._eval_dust_string(params.type, chunk, context);
}
return chunk.capture(bodies.block, context, function(data, chunk) {
if (filter_type != null) {
data = CommonDustjsHelpers.dust.filters[filter_type](data);
}
chunk.write(data);
return chunk.end();
});
};
CommonDustjsHelpers.prototype.first_helper = function(chunk, context, bodies, params) {
var c, ref;
if ((context != null ? (ref = context.stack) != null ? ref.index : void 0 : void 0) != null) {
c = context.stack.index === 0;
return this._render_if_else(c, chunk, context, bodies, params);
}
return chunk;
};
CommonDustjsHelpers.prototype.classic_idx = function(chunk, context, bodies) {
return bodies.block(chunk, context.push(context.stack.index));
};
CommonDustjsHelpers.prototype.if_helper = function(chunk, context, bodies, params) {
var execute_body;
execute_body = this._inner_if_helper(chunk, context, bodies, params);
return this._render_if_else(execute_body, chunk, context, bodies, params);
};
CommonDustjsHelpers.prototype.index_helper = function(chunk, context, bodies, params) {
var index, ref;
if ((context != null ? (ref = context.stack) != null ? ref.index : void 0 : void 0) != null) {
index = 1 + context.stack.index;
} else {
index = null;
}
if ((bodies != null ? bodies.block : void 0) != null) {
return bodies.block(chunk, context.push(index));
} else {
chunk.write(index);
return chunk;
}
};
CommonDustjsHelpers.prototype.last_helper = function(chunk, context, bodies, params) {
var c, ref;
if ((context != null ? (ref = context.stack) != null ? ref.index : void 0 : void 0) != null) {
c = context.stack.index === (context.stack.of - 1);
return this._render_if_else(c, chunk, context, bodies, params);
}
return chunk;
};
CommonDustjsHelpers.prototype.odd_helper = function(chunk, context, bodies, params) {
var c, ref;
if ((context != null ? (ref = context.stack) != null ? ref.index : void 0 : void 0) != null) {
c = context.stack.index % 2 === 1;
return this._render_if_else(c, chunk, context, bodies, params);
}
return chunk;
};
CommonDustjsHelpers.prototype.random_helper = function(chunk, context, bodies, params) {
var ctxvar, max, min, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, v;
if (params != null) {
if ((params.m != null) || (params.min != null) || (params.from != null)) {
min = this._to_int(this._eval_dust_string((ref = (ref1 = params.m) != null ? ref1 : params.min) != null ? ref : params.from, chunk, context));
}
if ((params.M != null) || (params.max != null) || (params.to != null)) {
max = this._to_int(this._eval_dust_string((ref2 = (ref3 = params.M) != null ? ref3 : params.max) != null ? ref2 : params.to, chunk, context));
}
if ((params.v != null) || (params["var"] != null) || (params.val != null) || (params.set != null) || (params.s != null)) {
ctxvar = this._eval_dust_string((ref4 = (ref5 = (ref6 = (ref7 = params.v) != null ? ref7 : params["var"]) != null ? ref6 : params.val) != null ? ref5 : params.set) != null ? ref4 : params.s, chunk, context);
}
}
if (min == null) {
min = 0;
}
if (max == null) {
max = 1;
}
if (min > max) {
ref8 = [max, min], min = ref8[0], max = ref8[1];
}
if (max - min === 0) {
v = 0;
} else {
v = Math.round(Math.random() * (max - min)) + min;
}
if (ctxvar != null) {
if ((ref9 = context.stack.head) != null) {
ref9[ctxvar] = v;
}
}
if ((bodies != null ? bodies.block : void 0) != null) {
return bodies.block(chunk, context.push(v));
} else {
return chunk;
}
};
CommonDustjsHelpers.prototype.regexp_helper = function(chunk, context, bodies, params) {
var ctx, flags, match, pattern, string;
if ((params != null ? params.string : void 0) != null) {
string = this._eval_dust_string(params.string, chunk, context);
}
if ((params != null ? params.pattern : void 0) != null) {
pattern = this._eval_dust_string(params.pattern, chunk, context);
}
if ((params != null ? params.flags : void 0) != null) {
flags = this._eval_dust_string(params.flags, chunk, context);
}
if ((params != null ? params["var"] : void 0) != null) {
match = this._eval_dust_string(params["var"], chunk, context);
}
if (match == null) {
match = "";
}
match = "$" + match;
if (!((string != null) && (pattern != null))) {
return this._render_if_else(false, chunk, context, bodies, params);
} else {
pattern = new RegExp(pattern, flags);
ctx = {};
ctx[match] = string.match(pattern);
return this._render_if_else(ctx[match] != null, chunk, context.push(ctx), bodies, params);
}
};
CommonDustjsHelpers.prototype.repeat_helper = function(chunk, context, bodies, params) {
var i, j, ref, ref1, ref2, ref3, ref4, times;
times = parseInt(this._eval_dust_string(params.times, chunk, context));
if ((times != null) && !isNaN(times)) {
if ((ref = context.stack.head) != null) {
ref['$len'] = times;
}
for (i = j = 0, ref1 = times; 0 <= ref1 ? j < ref1 : j > ref1; i = 0 <= ref1 ? ++j : --j) {
if ((ref2 = context.stack.head) != null) {
ref2['$idx'] = i;
}
chunk = bodies.block(chunk, context.push(i, i, times));
}
if ((ref3 = context.stack.head) != null) {
ref3['$idx'] = void 0;
}
if ((ref4 = context.stack.head) != null) {
ref4['$len'] = void 0;
}
}
return chunk;
};
CommonDustjsHelpers.prototype.classic_sep = function(chunk, context, bodies) {
if (context.stack.index === (context.stack.of - 1)) {
return chunk;
}
return bodies.block(chunk, context);
};
CommonDustjsHelpers.prototype.substring_helper = function(chunk, context, bodies, params) {
var from_index, ref, ref1, ref2, ref3, str, to_index;
if (params != null) {
if ((params["of"] != null) || (params.string != null) || (params.str != null) || (params.value != null) || (params.val != null)) {
str = this._eval_dust_string((ref = (ref1 = (ref2 = (ref3 = params["of"]) != null ? ref3 : params.string) != null ? ref2 : params.str) != null ? ref1 : params.value) != null ? ref : params.val, chunk, context);
}
if ((params.from != null) && (this._to_int(params.from) != null)) {
from_index = this._to_int(params.from);
}
if ((params.to != null) && (this._to_int(params.to) != null)) {
to_index = this._to_int(params.to);
}
}
if (str != null) {
chunk.write(this._get_substring(str, from_index, to_index));
return chunk;
} else {
return chunk.capture(bodies.block, context, (function(_this) {
return function(data, chunk) {
chunk.write(_this._get_substring(data, from_index, to_index));
return chunk.end();
};
})(this));
}
};
CommonDustjsHelpers.prototype.titlecase_helper = function(chunk, context, bodies, params) {
return chunk.capture(bodies.block, context, function(data, chunk) {
chunk.write(data.replace(/([^\W_]+[^\s-]*) */g, (function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1);
})));
return chunk.end();
});
};
CommonDustjsHelpers.prototype.trim_helper = function(chunk, context, bodies, params) {
return chunk.capture(bodies.block, context, function(data, chunk) {
chunk.write(data.trim());
return chunk.end();
});
};
CommonDustjsHelpers.prototype.switch_helper = function(chunk, context, bodies, params) {
var body, found_match, name, ref, ref1, ref2, ref3, ref4, val;
val = (ref = (ref1 = (ref2 = (ref3 = (ref4 = params.on) != null ? ref4 : params.when) != null ? ref3 : params["for"]) != null ? ref2 : params.value) != null ? ref1 : params.val) != null ? ref : params.v;
if (val != null) {
val = this._eval_dust_string(val, chunk, context);
}
if (val === null || val === (void 0) || val === "") {
if (bodies.block != null) {
chunk = chunk.render(bodies.block, context);
}
} else {
found_match = false;
for (name in bodies) {
body = bodies[name];
if (("" + name) === ("" + val)) {
chunk = chunk.render(body, context);
found_match = true;
break;
}
}
if (!found_match) {
if (bodies["else"] != null) {
chunk = chunk.render(bodies["else"], context);
}
}
}
return chunk;
};
CommonDustjsHelpers.prototype.unless_helper = function(chunk, context, bodies, params) {
var execute_body;
execute_body = this._inner_if_helper(chunk, context, bodies, params);
execute_body = !execute_body;
return this._render_if_else(execute_body, chunk, context, bodies, params);
};
CommonDustjsHelpers.prototype.upcase_helper = function(chunk, context, bodies, params) {
return chunk.capture(bodies.block, context, function(data, chunk) {
chunk.write(data.toUpperCase());
return chunk.end();
});
};
CommonDustjsHelpers.prototype._eval_dust_string = function(str, chunk, context) {
var buf;
if (typeof str === "function") {
if (str.length === 0) {
str = str();
} else {
buf = '';
(chunk.tap(function(data) {
buf += data;
return '';
})).render(str, context).untap();
str = buf;
}
}
return str;
};
CommonDustjsHelpers.prototype._to_int = function(val) {
if (/^-?[0-9]+/.test(val)) {
return parseInt(val);
} else {
return null;
}
};
CommonDustjsHelpers.prototype._render_if_else = function(b, chunk, context, bodies, params) {
if (b === true) {
if (bodies.block != null) {
chunk = chunk.render(bodies.block, context);
}
} else {
if (bodies["else"] != null) {
chunk = chunk.render(bodies["else"], context);
}
}
return chunk;
};
CommonDustjsHelpers.prototype._inner_if_helper = function(chunk, context, bodies, params) {
var above, below, c, countof, execute_body, isntval, isval, j, len, matches, re, ref, value;
execute_body = false;
if (params != null) {
if (params.test != null) {
value = this._eval_dust_string(params.test, chunk, context);
}
ref = ['count', 'count_of', 'count-of', 'countof'];
for (j = 0, len = ref.length; j < len; j++) {
c = ref[j];
if (params[c] != null) {
countof = this._eval_dust_string(params[c], chunk, context);
if ((countof != null ? countof.length : void 0) != null) {
value = countof.length;
}
}
}
if (value == null) {
value = this._eval_dust_string(params.value, chunk, context);
}
if (value != null) {
if (("" + value) === ("" + (parseFloat(value)))) {
value = parseFloat(value);
}
if (params.matches != null) {
matches = this._eval_dust_string(params.matches, chunk, context);
re = new RegExp(matches);
execute_body = re.test(value);
} else if (params['is'] != null) {
isval = this._eval_dust_string(params['is'], chunk, context);
if (typeof value === 'number' && (!isNaN(parseFloat(isval)))) {
isval = parseFloat(isval);
}
execute_body = value === isval;
} else if (params['isnt'] != null) {
isntval = this._eval_dust_string(params['isnt'], chunk, context);
if (typeof value === 'number' && (!isNaN(parseFloat(isntval)))) {
isntval = parseFloat(isntval);
}
execute_body = value !== isntval;
} else if (params.above != null) {
above = this._eval_dust_string(params.above, chunk, context);
if (typeof value === 'number' && (!isNaN(parseFloat(above)))) {
above = parseFloat(above);
}
execute_body = value > above;
} else if (params.below != null) {
below = this._eval_dust_string(params.below, chunk, context);
if (typeof value === 'number' && (!isNaN(parseFloat(below)))) {
below = parseFloat(below);
}
execute_body = value < below;
} else {
if (typeof value === 'boolean') {
execute_body = value;
} else if (typeof value === 'number') {
execute_body = value > 0;
} else if (typeof value === 'string') {
if (/^(T|Y|(ON))/i.test(value)) {
execute_body = true;
} else if (/^-?[0-9]+(\.[0-9]+)?$/.test(value) && parseInt(value) > 0) {
execute_body = true;
} else {
execute_body = false;
}
} else if (Array.isArray(value) && value.length > 0) {
execute_body = true;
} else if ((value != null) && typeof value === "object" && (Object.keys(value).length > 0)) {
execute_body = true;
} else {
execute_body = false;
}
}
}
}
return execute_body;
};
CommonDustjsHelpers.prototype._get_substring = function(str, from_index, to_index) {
var ref, substring;
substring = null;
if ((from_index != null) && from_index < 0) {
from_index = str.length + from_index;
}
if ((to_index != null) && to_index < 0) {
to_index = str.length + to_index;
}
if ((from_index != null) && (to_index != null) && from_index > to_index) {
ref = [to_index, from_index], from_index = ref[0], to_index = ref[1];
}
if ((from_index != null) && (to_index == null)) {
substring = str.substring(from_index);
} else if ((to_index != null) && (from_index == null)) {
substring = str.substring(0, to_index);
} else if ((from_index != null) && (to_index != null)) {
substring = str.substring(from_index, to_index);
} else {
substring = str;
}
return substring;
};
CommonDustjsHelpers.prototype._attribute_comparator = function(attr, reverse) {
if (reverse == null) {
reverse = false;
}
return (function(_this) {
return function(A, B) {
var a, b, ref;
if (reverse) {
ref = [B, A], A = ref[0], B = ref[1];
}
a = A != null ? A[attr] : void 0;
b = B != null ? B[attr] : void 0;
return _this._compare(a, b);
};
})(this);
};
CommonDustjsHelpers.prototype._compare = function(a, b) {
if ((a != null) && (b != null)) {
if (a.localeCompare != null) {
return a.localeCompare(b);
} else {
return (a > b ? 1 : (a < b ? -1 : 0));
}
} else if ((a != null) && (b == null)) {
return 1;
} else if ((b != null) && (a == null)) {
return -1;
} else {
return 0;
}
};
return CommonDustjsHelpers;
})();
exports = exports != null ? exports : this;
exports.CommonDustjsHelpers = CommonDustjsHelpers;
}).call(this);