Skip to content

Instantly share code, notes, and snippets.

@bricss
Last active January 27, 2017 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bricss/8300559 to your computer and use it in GitHub Desktop.
Save bricss/8300559 to your computer and use it in GitHub Desktop.
(function() {'use strict';
/**
* Handlebars addition helper.
*
* Usage:
* {{addition 10 to="10"}}
* {{addition key to="20"}}
* {{addition key to="-5"}}
*/
Handlebars.registerHelper('addition', function(context, options) {
return context + parseFloat(options.hash.to);
});
/**
* Handlebars capitalize helper.
*
* Usage:
* {{capitalize key}}
*/
Handlebars.registerHelper('capitalize', function(value) {
return value.toLowerCase().replace(/\b./g, function(match) {
return match.toUpperCase();
});
});
/**
* Handlebars lowercase helper.
*
* Usage:
* {{lowercase key}}
*/
Handlebars.registerHelper('lowercase', function(value) {
return value.toLowerCase();
});
/**
* Handlebars uppercase helper.
*
* Usage:
* {{uppercase key}}
*/
Handlebars.registerHelper('uppercase', function(value) {
return value.toUpperCase();
});
/**
* Handlebars truncate helper.
*
* Usage:
* {{truncate key}}
* {{truncate key length="150"}}
* {{truncate key length="150" trailing="---"}}
*/
Handlebars.registerHelper('truncate', function(context, options) {
var ln = options.hash.length || 100;
var trail = options.hash.trailing || '';
if (context.length > ln) {
return context.substring(0, ln) + trail;
}
return context;
});
/**
* Handlebars regexp helpers.
*
* Usage:
* {{#lex key "^(3\d+)"}}
* <span>yep</span>
* {{else}}
* <span>nope</span>
* {{/lex}}
*
* <span>{{rex path "(\\)?\/" " - " "revert"}}</span>
*/
Handlebars.registerHelper('lex', function(key, regexp, options) {
return new RegExp(regexp, 'gi').test(key) ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('rex', function(key, regexp, value, param, options) {
var regexp = new RegExp(regexp, 'gi'), match = key.match(regexp)[0];
if (param && param === 'revert') {
key = (key.split(match).reverse().join(match).substr(-1) === match ? key.substr(0, key.length - 1) : null);
} else {
key = (key.substr(-1) === match ? key.substr(0, key.length - 1) : null);
}
return key.replace(regexp, value).trim();
});
/**
* Handlebars comparison helpers.
*
* Usage:
* {{#ife key 10}}
* <span>yep</span>
* {{else}}
* <span>nope</span>
* {{/ife}}
*/
Handlebars.registerHelper('ife', function(key, value, options) {
return key == value ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('unlesse', function(key, value, options) {
return key != value ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('ifgt', function(key, value, options) {
return key > value ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('ifgte', function(key, value, options) {
return key >= value ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('iflt', function(key, value, options) {
return key < value ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('iflte', function(key, value, options) {
return key <= value ? options.fn(this) : options.inverse(this);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment