From f0f8adaac48fb89997cbcdd89d694a05272b2da8 Mon Sep 17 00:00:00 2001 From: Jordan Ramsay <34638a@users.noreply.github.com> Date: Fri, 7 Feb 2025 16:19:36 +1000 Subject: [PATCH 1/2] Update math.js to include min, max, and clamp functions Include of simple math helpers for inline calculations. All support Number types of Int and Float, but not edge case numbers such as NaN, +-infinity, or undefined. Min: given `A` and `B` return the smaller of the two on a standard number line. Max: given `A` and `B` return the larger of the two on a standard number line. Clamp: given `test`, `min` and `max` return the tested value if it falls within the range of `min` and `max` such that `min < test < max` is satisfied on a standard number line. If the constraint is not satisfied, return the closest bounding constraint. --- helpers/3p/math.js | 59 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/helpers/3p/math.js b/helpers/3p/math.js index 686d434b..bc00fb9c 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(text, max)); +} From d620b5366a4a2517983b806c845f8cce0d374fa6 Mon Sep 17 00:00:00 2001 From: Jordan Ramsay <34638a@users.noreply.github.com> Date: Wed, 12 Feb 2025 11:59:08 +1000 Subject: [PATCH 2/2] Update math.js Amendment - Typo, Testing Protocol ``` {{log (min 1 5)}} {{! Resolved to 1}} {{log (max 1 5)}} {{! Resolved to 5}} {{log (clamp 6 1 5)}} {{! Resolved to 5}} {{log (clamp -6 1 5)}} {{! Resolved to 1}} {{log (clamp 3 1 5)}} {{! Resolved to 3}} ``` --- helpers/3p/math.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/3p/math.js b/helpers/3p/math.js index bc00fb9c..382d164a 100644 --- a/helpers/3p/math.js +++ b/helpers/3p/math.js @@ -190,5 +190,5 @@ helpers.max = function(a, b) { */ helpers.clamp = function(test, min, max) { - return helpers.max(min, helpers.min(text, max)); + return helpers.max(min, helpers.min(test, max)); }