Permalink
Browse files

added fit function to Number prototype

  • Loading branch information...
1 parent 33ed965 commit 07fd4129b06914653cc7f0d733b698a7501c6196 @juzna committed Jan 13, 2011
Showing with 20 additions and 1 deletion.
  1. +20 −1 src/prototype/lang/number.js
@@ -145,6 +145,24 @@ Object.extend(Number.prototype, (function() {
return Math.floor(this);
}
+ /**
+ * Number#floor() -> Number
+ *
+ * Fit number to boundaries
+ *
+ * Example:
+ * 5.fit(1,10) -> 5
+ * 20.fit(1,10) -> 10
+ **/
+ function fit(min, max) {
+ if(min >= max) return this;
+ else {
+ if(this > max) return max;
+ else if(this < min) return min;
+ else return this;
+ }
+ }
+
return {
toColorPart: toColorPart,
succ: succ,
@@ -153,6 +171,7 @@ Object.extend(Number.prototype, (function() {
abs: abs,
round: round,
ceil: ceil,
- floor: floor
+ floor: floor,
+ fit: fit
};
})());

0 comments on commit 07fd412

Please sign in to comment.