diff --git a/helpers/3p/math.js b/helpers/3p/math.js index 686d434b..382d164a 100644 --- a/helpers/3p/math.js +++ b/helpers/3p/math.js @@ -134,4 +134,61 @@ helpers.avg = function() { // remove handlebars options object args.pop(); return exports.sum(args) / args.length; -}; \ No newline at end of file +}; + +/** + * Return the min of `a` and `b`. + * ```handlebars + * {{min 1 5}} + * //=> '1' + * ``` + * + * @param {Number} `a` + * @param {Number} `b` + * @api public + */ + +helpers.min = function(a, b) { + return a < b ? a : b; +} + + +/** + * Return the max of `a` and `b`. + * ```handlebars + * {{max 1 5}} + * //=> '5' + * ``` + * + * @param {Number} `a` + * @param {Number} `b` + * @api public + */ + +helpers.max = function(a, b) { + return a > b ? a : b; +} + + +/** + * Return the value `test` constrained to the provided `min` and `max`. + * If the value `test` falls outside of the provided `min` or `max`, the respective bounds will be returned instead. + * + * ```handlebars + * {{clamp 3 2 4}} + * //=> '3' + * {{clamp 10 2 4}} + * //=> '4' + * {{clamp -10 2 4}} + * //=> '2' + * ``` + * + * @param {Number} `test` + * @param {Number} `min` + * @param {Number} `max` + * @api public + */ + +helpers.clamp = function(test, min, max) { + return helpers.max(min, helpers.min(test, max)); +}