Skip to content

8. Miscellaneous

CookieShade edited this page Aug 11, 2016 · 4 revisions

Miscellaneous

Other general utility functions

BETA.assert(assertion, message)

Throws an Error with the provided message if assertion evaluates to false. Useful for debugging and error-checking.

BETA.assert(typeof x !== "undefined", "That's an error!"); // Throws an error if x is undefined

BETA.isNumber(val)

Returns true if a value is a number, and not NaN.

var isNum = BETA.isNumber(NaN); // false
var isNum = BETA.isNumber(Infinity); // true

BETA.mod(x, y)

Returns x modulo y. Similar to the % operator, but treats negative remainders correctly.

BETA.clamp(val, a, b)

Clamps val to the range [a, b]. Numbers above the range return the highest bound of the range, while numbers below the range return the lowest bound. Numbers within the range are unaffected.

var clamped = BETA.clamp(5, 0, 1); // 1, as 5 is outside [0, 1]
var unaffected = BETA.clamp(0.6, 0, 1); // 0.6, as 0.6 is in [0, 1]