Skip to content

Commit

Permalink
feat(lib/utils.js): Add clamp function
Browse files Browse the repository at this point in the history
Added function which clamps a numeric value to a range.
  • Loading branch information
plroebuck committed Nov 9, 2018
1 parent 5d4ac5f commit 20083cb
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,17 @@ exports.isPromise = function isPromise(value) {
return typeof value === 'object' && typeof value.then === 'function';
};

/**
* Clamps a numeric value to an inclusive range.
*
* @param {number} value - Value to be clamped.
* @param {numer[]} range - Two element array specifying [min, max] range.
* @returns {number} clamped value
*/
exports.clamp = function clamp(value, range) {
return Math.min(Math.max(value, range[0]), range[1]);
};

/**
* It's a noop.
* @api
Expand Down

0 comments on commit 20083cb

Please sign in to comment.