Skip to content

Commit

Permalink
Update math.js to include min, max, and clamp functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
34638a authored Feb 7, 2025
1 parent 4fbc7b7 commit f0f8ada
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion helpers/3p/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,61 @@ helpers.avg = function() {
// remove handlebars options object
args.pop();
return exports.sum(args) / args.length;
};
};

/**
* 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));
}

0 comments on commit f0f8ada

Please sign in to comment.