Skip to content

Instantly share code, notes, and snippets.

@linus-amg
Created December 1, 2014 17:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linus-amg/1be45a630ac8e05f5dfc to your computer and use it in GitHub Desktop.
Save linus-amg/1be45a630ac8e05f5dfc to your computer and use it in GitHub Desktop.
handlebars helpers 1 (lleva por ejemplo add, substract, divide, modulus, multiply, floor, ceil, round, remainder, sum)
var Utils = helpersUtils.Utils;
var Library = helpersUtils.Library;
var Dates = helpersUtils.Dates;
var HTML = helpersUtils.Html;
Handlebars.registerHelper('first', function (array, count) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
if (!Utils.isUndefined(count)) {
count = parseFloat(Utils.result(count));
}
if (Utils.isUndefined(count)) {
return array[0];
} else {
return array.slice(0, count);
}
} else {
return Utils.err('{{first}} takes at least one argument (array).');
}
});
Handlebars.registerHelper('withFirst', function (array, count, options) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
if (!Utils.isUndefined(count)) {
count = parseFloat(Utils.result(count));
}
if (Utils.isUndefined(count)) {
options = count;
return options.fn(array[0]);
} else {
array = array.slice(0, count);
var result = '';
for (var item in array) {
result += options.fn(array[item]);
}
return result;
}
} else {
return Utils.err('{{withFirst}} takes at least one argument (array).');
}
});
Handlebars.registerHelper('last', function (array, count) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
if (!Utils.isUndefined(count)) {
count = parseFloat(Utils.result(count));
}
if (Utils.isUndefined(count)) {
return array[array.length - 1];
} else {
return array.slice(-count);
}
} else {
return Utils.err('{{last}} takes at least one argument (array).');
}
});
Handlebars.registerHelper('withLast', function (array, count, options) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
if (!Utils.isUndefined(count)) {
count = parseFloat(Utils.result(count));
}
if (Utils.isUndefined(count)) {
options = count;
return options.fn(array[array.length - 1]);
} else {
array = array.slice(-count);
var result = '';
for (var item in array) {
result += options.fn(array[item]);
}
return result;
}
} else {
return Utils.err('{{withLast}} takes at least one argument (array).');
}
});
Handlebars.registerHelper('after', function (array, count) {
if (!((Utils.isUndefined(array)) && (Utils.isUndefined(count)))) {
array = Utils.result(array);
if (!Utils.isUndefined(count)) {
count = parseFloat(Utils.result(count));
}
return array.slice(count);
} else {
return Utils.err('{{after}} takes two arguments (array, number).');
}
});
Handlebars.registerHelper('withAfter', function (array, count, options) {
if (!((Utils.isUndefined(array)) && (Utils.isUndefined(count)))) {
array = Utils.result(array);
if (!Utils.isUndefined(count)) {
count = parseFloat(Utils.result(count));
}
array = array.slice(count);
var result = '';
for (var item in array) {
result += options.fn(array[item]);
}
return result;
} else {
return Utils.err('{{withAfter}} takes two arguments (array, number).');
}
});
Handlebars.registerHelper('before', function (array, count) {
if (!((Utils.isUndefined(array)) && (Utils.isUndefined(count)))) {
array = Utils.result(array);
if (!Utils.isUndefined(count)) {
count = parseFloat(Utils.result(count));
}
return array.slice(0, -count);
} else {
return Utils.err('{{before}} takes two arguments (array, number).');
}
});
Handlebars.registerHelper('withBefore', function (array, count, options) {
if (!((Utils.isUndefined(array)) && (Utils.isUndefined(count)))) {
array = Utils.result(array);
if (!Utils.isUndefined(count)) {
count = parseFloat(Utils.result(count));
}
array = array.slice(0, -count);
var result = '';
for (var item in array) {
result += options.fn(array[item]);
}
return result;
} else {
return Utils.err('{{withBefore}} takes two arguments (array, number).');
}
});
Handlebars.registerHelper('join', function (array, separator) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
if (!Utils.isUndefined(separator)) {
separator = Utils.result(separator);
}
return array.join(Utils.isUndefined(separator) ? ' ' : separator);
} else {
return Utils.err('{{join}} takes at least one argument (array).');
}
});
Handlebars.registerHelper('sort', function (array, field) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
if (Utils.isUndefined(field)) {
return array.sort();
} else {
field = Utils.result(field);
return array.sort(function (a, b) {
return a[field] > b[field];
});
}
} else {
return Utils.err('{{sort}} takes at least one argument (array).');
}
});
Handlebars.registerHelper('withSort', function (array, field, options) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
var result = '';
if (Utils.isUndefined(field)) {
options = field;
array = array.sort();
for (var i = 0, len = array.length; i < len; i++) {
var item = array[i];
result += options.fn(item);
}
} else {
field = Utils.result(field);
array = array.sort(function (a, b) {
return a[field] > b[field];
});
for (item in array) {
result += options.fn(array[item]);
}
}
return result;
} else {
return Utils.err('{{withSort}} takes at least one argument (array).');
}
});
Handlebars.registerHelper('length', function (array) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
return array.length;
} else {
return Utils.err('{{length}} takes one argument (array).');
}
});
Handlebars.registerHelper('lengthEqual', function (array, length, options) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
if (!Utils.isUndefined(length)) {
length = parseFloat(Utils.result(length));
}
if (array.length === length) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{lengthEqual}} takes two arguments (array, number).');
}
});
Handlebars.registerHelper('empty', function (array, options) {
if (!Utils.isHandlebarsSpecific(array)) {
array = Utils.result(array);
if (!array || array.length <= 0) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{empty}} takes one argument (array).');
}
});
Handlebars.registerHelper('any', function (array, options) {
if (!Utils.isHandlebarsSpecific(array)) {
array = Utils.result(array);
if (array && array.length > 0) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{any}} takes one argument (array).');
}
});
Handlebars.registerHelper('inArray', function (array, value, options) {
if (!((Utils.isUndefined(array)) && (Utils.isUndefined(value)))) {
array = Utils.result(array);
value = Utils.result(value);
if (Utils._indexOf.call(array, value) >= 0) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{inArray}} takes two arguments (array, string|number).');
}
});
Handlebars.registerHelper('eachIndex', function (array, options) {
if (!Utils.isUndefined(array)) {
array = Utils.result(array);
var result = '';
for (var index = i = 0, len = array.length; i < len; index = ++i) {
var value = array[index];
result += options.fn({
item: value,
index: index
});
}
return result;
} else {
return Utils.err('{{eachIndex}} takes one argument (array).');
}
});
Handlebars.registerHelper('eachProperty', function (obj, options) {
if (!Utils.isUndefined(obj)) {
obj = Utils.result(obj);
var result = '';
for (var key in obj) {
var value = obj[key];
result += options.fn({
key: key,
value: value
});
}
return result;
} else {
return Utils.err('{{eachProperty}} takes one argument (object).');
}
});
// Source File: ./src/helpers/comparisons.js
Handlebars.registerHelper('is', function (value, test, options) {
if (!((Utils.isHandlebarsSpecific(value)) && (Utils.isHandlebarsSpecific(test)))) {
value = Utils.result(value);
test = Utils.result(test);
if (value && value === test) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{is}} takes two arguments (string|number, string|number).');
}
});
Handlebars.registerHelper('isnt', function (value, test, options) {
if (!((Utils.isHandlebarsSpecific(value)) && (Utils.isHandlebarsSpecific(test)))) {
value = Utils.result(value);
test = Utils.result(test);
if (!value || value !== test) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{isnt}} takes two arguments (string|number, string|number).');
}
});
Handlebars.registerHelper('gt', function (value, test, options) {
if (!((Utils.isHandlebarsSpecific(value)) && (Utils.isHandlebarsSpecific(test)))) {
value = Utils.result(value);
test = Utils.result(test);
if (value > test) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{gt}} takes two arguments (string|number, string|number).');
}
});
Handlebars.registerHelper('gte', function (value, test, options) {
if (!((Utils.isHandlebarsSpecific(value)) && (Utils.isHandlebarsSpecific(test)))) {
value = Utils.result(value);
test = Utils.result(test);
if (value >= test) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{gte}} takes two arguments (string|number, string|number).');
}
});
Handlebars.registerHelper('lt', function (value, test, options) {
if (!((Utils.isHandlebarsSpecific(value)) && (Utils.isHandlebarsSpecific(test)))) {
value = Utils.result(value);
test = Utils.result(test);
if (value < test) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{lt}} takes two arguments (string|number, string|number).');
}
});
Handlebars.registerHelper('lte', function (value, test, options) {
if (!((Utils.isHandlebarsSpecific(value)) && (Utils.isHandlebarsSpecific(test)))) {
value = Utils.result(value);
test = Utils.result(test);
if (value <= test) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{lte}} takes two arguments (string|number, string|number).');
}
});
Handlebars.registerHelper('or', function (testA, testB, options) {
if (!((Utils.isHandlebarsSpecific(testA)) && (Utils.isHandlebarsSpecific(testB)))) {
testA = Utils.result(testA);
testB = Utils.result(testB);
if (testA || testB) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{or}} takes two arguments (string|number, string|number).');
}
});
Handlebars.registerHelper('and', function (testA, testB, options) {
if (!((Utils.isHandlebarsSpecific(testA)) && (Utils.isHandlebarsSpecific(testB)))) {
testA = Utils.result(testA);
testB = Utils.result(testB);
if (testA && testB) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else {
return Utils.err('{{and}} takes two arguments (string|number, string|number).');
}
});
// Source File: ./src/helpers/dates.js
/**
* Handlebars Handlebars.registerHelper('<http://github.com/assemble/handlebars-Handlebars.registerHelper('
*
* Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors
* Licensed under the MIT License (MIT)
*/
Handlebars.registerHelper('formatDate', function (date, format) {
if (!Utils.isUndefined(date)) {
date = Utils.result(date);
format = Utils.result(format);
date = new Date(date);
return Dates.format(date, format);
} else {
return Utils.err('{{formatDate}} takes two arguments (string|number|date, string).');
}
});
Handlebars.registerHelper('now', function (format) {
if (!Utils.isUndefined(format)) {
format = Utils.result(format);
}
var date = new Date();
if (Utils.isUndefined(format)) {
return date;
} else {
return Dates.format(date, format);
}
});
Handlebars.registerHelper('timeago', function (date) {
if (!Utils.isUndefined(date)) {
date = Utils.result(date);
date = new Date(date);
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return "" + interval + " years ago";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return "" + interval + " months ago";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return "" + interval + " days ago";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return "" + interval + " hours ago";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return "" + interval + " minutes ago";
}
if (Math.floor(seconds) === 0) {
return 'Just now';
} else {
return Math.floor(seconds) + ' seconds ago';
}
} else {
return Utils.err('{{timeago}} takes one argument (string|number|date).');
}
});
// Source File: ./src/helpers/html.js
/**
* Handlebars Helpers <http://github.com/assemble/handlebars-helpers>
*
* Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors
* Licensed under the MIT License (MIT)
*/
Handlebars.registerHelper('ul', function (context, options) {
return ("<ul " + (HTML.parseAttributes(options.hash)) + ">") + context.map(function (item) {
return "<li>" + (options.fn(Utils.result(item))) + "</li>";
}).join('\n') + "</ul>";
});
Handlebars.registerHelper('ol', function (context, options) {
return ("<ol " + (HTML.parseAttributes(options.hash)) + ">") + context.map(function (item) {
return "<li>" + (options.fn(Utils.result(item))) + "</li>";
}).join('\n') + "</ol>";
});
Handlebars.registerHelper('br', function (count, options) {
var br = '<br>';
if (!Utils.isUndefined(count)) {
var i = 0;
count = Utils.result(count);
while (i < (parseFloat(count)) - 1) {
br += '<br>';
i++;
}
}
return Utils.safeString(br);
});
// Source File: ./src/helpers/inflections.js
Handlebars.registerHelper('inflect', function (count, singular, plural, include) {
if (!((Utils.isUndefined(count)) && (Utils.isUndefined(singular)) && (Utils.isUndefined(plural)))) {
count = parseFloat(Utils.result(count));
singular = Utils.result(singular);
plural = Utils.result(plural);
var word = count > 1 || count === 0 ? plural : singular;
if (Utils.isUndefined(include) || include === false) {
return word;
} else {
return "" + count + " " + word;
}
} else {
return Utils.err('{{inflect}} takes at least three arguments (number, string, string).');
}
});
Handlebars.registerHelper('ordinalize', function (value) {
var ref;
if (!Utils.isUndefined(value)) {
value = parseFloat(Utils.result(value));
var normal = Math.abs(Math.round(value));
if (ref = normal % 100, Utils._indexOf.call([11, 12, 13], ref) >= 0) {
return "" + value + "th";
} else {
switch (normal % 10) {
case 1:
return "" + value + "st";
case 2:
return "" + value + "nd";
case 3:
return "" + value + "rd";
default:
return "" + value + "th";
}
}
} else {
return Utils.err('{{ordinalize}} takes one arguments (number).');
}
});
// Source File: ./src/helpers/logging.js
/**
* Handlebars Handlebars.registerHelper('<http://github.com/assemble/handlebars-helpers>
*
* Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors
* Licensed under the MIT License (MIT)
*/
Handlebars.registerHelper('log', function (value) {
if (!Utils.isUndefined(value)) {
value = Utils.result(value);
return console.log(value);
} else {
return Utils.err('{{log}} takes one arguments (string|number|boolean|array|object).');
}
});
Handlebars.registerHelper('debug', function (value) {
if (!Utils.isUndefined(value)) {
value = Utils.result(value);
}
console.log('Context: ', this);
if (!Utils.isUndefined(value)) {
console.log('Value: ', value);
}
return console.log('-----------------------------------------------');
});
/**
* Handlebars Helpers <http://github.com/assemble/handlebars-helpers>
*
* Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors
* Licensed under the MIT License (MIT)
*/
Handlebars.registerHelper('add', function (value, addition) {
if (!((Utils.isUndefined(value)) && (Utils.isUndefined(addition)))) {
value = parseFloat(Utils.result(value));
addition = parseFloat(Utils.result(addition));
return value + addition;
} else {
return Utils.err('{{add}} takes two arguments (number, number).');
}
});
Handlebars.registerHelper('subtract', function (value, substraction) {
if (!((Utils.isUndefined(value)) && (Utils.isUndefined(substraction)))) {
value = parseFloat(Utils.result(value));
substraction = parseFloat(Utils.result(substraction));
return value - substraction;
} else {
return Utils.err('{{subtract}} takes two arguments (number, number).');
}
});
Handlebars.registerHelper('divide', function (value, divisor) {
if (!((Utils.isUndefined(value)) && (Utils.isUndefined(divisor)))) {
value = parseFloat(Utils.result(value));
divisor = parseFloat(Utils.result(divisor));
return value / divisor;
} else {
return Utils.err('{{divide}} takes two arguments (number, number).');
}
});
/**
* {{mod}}
* Returns the modulus of two numbers
* @author: Liam Moat <https://github.com/liammoat>
*/
Handlebars.registerHelper('mod', function (value, divisor) {
if (!((Utils.isUndefined(value)) && (Utils.isUndefined(divisor)))) {
value = parseFloat(Utils.result(value));
divisor = parseFloat(Utils.result(divisor));
return value % divisor;
} else {
return Utils.err('{{mod}} takes two arguments (number, number).');
}
});
Handlebars.registerHelper('multiply', function (value, multiplier) {
if (!((Utils.isUndefined(value)) && (Utils.isUndefined(multiplier)))) {
value = parseFloat(Utils.result(value));
multiplier = parseFloat(Utils.result(multiplier));
return value * multiplier;
} else {
return Utils.err('{{multiply}} takes two arguments (number, number).');
}
});
Handlebars.registerHelper('floor', function (value) {
if (!(Utils.isUndefined(value))) {
value = parseFloat(Utils.result(value));
return Math.floor(value);
} else {
return Utils.err('{{floor}} takes one argument (number).');
}
});
Handlebars.registerHelper('ceil', function (value) {
if (!(Utils.isUndefined(value))) {
value = parseFloat(Utils.result(value));
return Math.ceil(value);
} else {
return Utils.err('{{ceil}} takes one argument (number).');
}
});
Handlebars.registerHelper('round', function (value) {
if (!(Utils.isUndefined(value))) {
value = parseFloat(Utils.result(value));
return Math.round(value);
} else {
return Utils.err('{{round}} takes one argument (number).');
}
});
Handlebars.registerHelper('remainder', function (first, second) {
if (!((Utils.isUndefined(first)) && (Utils.isUndefined(second)))) {
first = parseFloat(Utils.result(first));
second = parseFloat(Utils.result(second));
return first % second;
} else {
return Utils.err('{{remainder}} takes two arguments (number, number).');
}
});
Handlebars.registerHelper('sum', function () {
var sum = 0;
var args = _.flatten(arguments);
for (var i = 0; i < args.length - 1; i++) {
if ("number" === typeof args[i]) {
sum += args[i];
}
}
return Number(sum);
});
// Source File: ./src/helpers/miscellaneous.js
/**
* Handlebars Helpers <http://github.com/assemble/handlebars-helpers>
*
* Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors
* Licensed under the MIT License (MIT)
*/
Handlebars.registerHelper('default', function (value, defaultValue) {
if (!((Utils.isHandlebarsSpecific(value)) && (Utils.isUndefined(defaultValue)))) {
value = Utils.result(value);
defaultValue = Utils.result(defaultValue);
return value || defaultValue;
} else {
return Utils.err('{{default}} takes two arguments (string|number, string|number).');
}
});
if (typeof Ember === 'undefined' || Ember === null) {
Handlebars.registerHelper('partial', function (name, data, template) {
if (!(Utils.isUndefined(name))) {
name = Utils.result(name);
data = Utils.result(data);
var path = Library.Config.partialsPath + name;
if (!Utils.isUndefined(template)) {
template = Utils.result(template);
}
if (Library.Handlebars.partials[name] == null) {
if (!Utils.isUndefined(template)) {
if (Utils.isString(template)) {
template = Library.Handlebars.compile(template);
}
Library.Handlebars.registerPartial(name, template);
} else if ((typeof define !== 'undefined' && define !== null) && (Utils.isFunc(define)) && define.amd) {
if (!Library.Config.precompiledTemplates) {
path = '!text' + path;
}
require([path], function (template) {
if (Utils.isString(template)) {
template = Library.Handlebars.compile(template);
}
return Library.Handlebars.registerPartial(name, template);
});
} else if (typeof require !== 'undefined' && require !== null) {
template = require(path);
if (Utils.isString(template)) {
template = Library.Handlebars.compile(template);
}
Library.Handlebars.registerPartial(name, template);
} else {
Utils.err('{{partial}} no amd or commonjs module support found.');
}
}
return Utils.safeString(Library.Handlebars.partials[name](data));
} else {
return Utils.err('{{partial}} takes at least one argument (string).');
}
});
}
// Source File: ./src/helpers/numbers.js
/**
* Handlebars Helpers <http://github.com/assemble/handlebars-helpers>
*
* Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors
* Licensed under the MIT License (MIT)
*/
Handlebars.registerHelper('toFixed', function (number, digits) {
if (!Utils.isUndefined(number)) {
number = parseFloat(Utils.result(number));
digits = Utils.isUndefined(digits) ? 0 : Utils.result(digits);
return number.toFixed(digits);
} else {
return Utils.err('{{toFixed}} takes at least one argument (number).');
}
});
Handlebars.registerHelper('toPrecision', function (number, precision) {
if (!Utils.isUndefined(number)) {
number = parseFloat(Utils.result(number));
precision = Utils.isUndefined(precision) ? 1 : Utils.result(precision);
return number.toPrecision(precision);
} else {
return Utils.err('{{toPrecision}} takes at least one argument (number).');
}
});
Handlebars.registerHelper('toExponential', function (number, fractions) {
if (!Utils.isUndefined(number)) {
number = parseFloat(Utils.result(number));
fractions = Utils.isUndefined(fractions) ? 0 : Utils.result(fractions);
return number.toExponential(fractions);
} else {
return Utils.err('{{toExponential}} takes at least one argument (number).');
}
});
Handlebars.registerHelper('toInt', function (number) {
if (!Utils.isUndefined(number)) {
number = Utils.result(number);
return parseInt(number, 10);
} else {
return Utils.err('{{toInt}} takes one argument (number).');
}
});
Handlebars.registerHelper('toFloat', function (number) {
if (!Utils.isUndefined(number)) {
number = Utils.result(number);
return parseFloat(number);
} else {
return Utils.err('{{toFloat}} takes one argument (number).');
}
});
Handlebars.registerHelper('digitGrouping', function (number, separator) {
if (!Utils.isUndefined(number)) {
number = parseFloat(Utils.result(number));
separator = Utils.isUndefined(separator) ? ',' : Utils.result(separator);
return number.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + separator);
} else {
return Utils.err('{{digitGrouping}} takes at least one argument (number).');
}
});
// Source File: ./src/helpers/strings.js
/**
* Handlebars Helpers <http://github.com/assemble/handlebars-helpers>
*
* Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors
* Licensed under the MIT License (MIT)
*/
Handlebars.registerHelper('lowercase', function (str) {
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
return str.toLowerCase();
} else {
return Utils.err('{{lowercase}} takes one argument (string).');
}
});
Handlebars.registerHelper('uppercase', function (str) {
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
return str.toUpperCase();
} else {
return Utils.err('{{uppercase}} takes one argument (string).');
}
});
Handlebars.registerHelper('capitalizeFirst', function (str) {
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
return str.charAt(0).toUpperCase() + str.slice(1);
} else {
return Utils.err('{{capitalizeFirst}} takes one argument (string).');
}
});
Handlebars.registerHelper('capitalizeEach', function (str) {
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1);
});
} else {
return Utils.err('{{capitalizeEach}} takes one argument (string).');
}
});
Handlebars.registerHelper('titleize', function (str) {
var word;
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
var title = str.replace(/[ \-_]+/g, ' ');
var words = title.match(/\w+/g);
var capitalize = function (word) {
return word.charAt(0).toUpperCase() + word.slice(1);
};
return ((function () {
var results = [];
for (var i = 0, len = words.length; i < len; i++) {
word = words[i];
results.push(capitalize(word));
}
return results;
})()).join(' ');
} else {
return Utils.err('{{titleize}} takes one argument (string).');
}
});
Handlebars.registerHelper('sentence', function (str) {
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
return str.replace(/((?:\S[^\.\?\!]*)[\.\?\!]*)/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
} else {
return Utils.err('{{sentence}} takes one argument (string).');
}
});
Handlebars.registerHelper('reverse', function (str) {
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
return str.split('').reverse().join('');
} else {
return Utils.err('{{reverse}} takes one argument (string).');
}
});
Handlebars.registerHelper('truncate', function (str, length, omission) {
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
if (Utils.isUndefined(omission)) {
omission = '';
}
if (str.length > length) {
return str.substring(0, length - omission.length) + omission;
} else {
return str;
}
} else {
return Utils.err('{{truncate}} takes one argument (string).');
}
});
Handlebars.registerHelper('center', function (str, spaces) {
if (!((Utils.isUndefined(str)) && (Utils.isUndefined(spaces)))) {
str = Utils.result(str);
spaces = Utils.result(spaces);
var space = '';
var i = 0;
while (i < spaces) {
space += '&nbsp;';
i++;
}
return "" + space + str + space;
} else {
return Utils.err('{{center}} takes two arguments (string, number).');
}
});
Handlebars.registerHelper('newLineToBr', function (str) {
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
return str.replace(/\r?\n|\r/g, '<br>');
} else {
return Utils.err('{{newLineToBr}} takes one argument (string).');
}
});
Handlebars.registerHelper('sanitize', function (str, replaceWith) {
if (!Utils.isUndefined(str)) {
str = Utils.result(str);
if (Utils.isUndefined(replaceWith)) {
replaceWith = '-';
}
return str.replace(/[^a-z0-9]/gi, replaceWith);
} else {
return Utils.err('{{sanitize}} takes one argument (string).');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment