StructureJS

0.15.2

A class based utility library for building modular and scalable web platform applications. Features opt-in classes and utilities which provide a solid foundation and toolset to build your next project.

MathUtil Class

Defined in: ts/util/MathUtil.ts:1
Module: util
Parent Module: StructureJS

A helper class to do calculations and conversions.

Methods

constrain
(
  • num
  • min
  • max
)
Number

Returns a number constrained between min and max.

Parameters:

Returns:

Example:

 MathUtil.constrain(12, 3, 20);
 // 12

 MathUtil.constrain(22, 3, 20);
 // 20

 MathUtil.constrain(0, 3, 20);
 // 3

degreesToRadians
(
  • degrees
)
Number

Converts degrees to radians.

Parameters:

Returns:

Example:

 MathUtil.degreesToRadians(90);
 // 1.5707963267948966

 MathUtil.degreesToRadians(180);
 // 3.141592653589793

factorial
(
  • num
)
Number

Calculate the factorial of the integer.

Parameters:

Returns:

Example:

 MathUtil.factorial(5);
 // 120

 MathUtil.factorial(9);
 // 362880

getDivisors
(
  • num
)
Array.

Return an array of divisors of the integer.

Parameters:

Returns:

Array.:

Example:

isEven
(
  • num
)
Boolean

Check if number is even (convert to Integer if necessary).

Parameters:

Returns:

Example:

 MathUtil.isEven(2);
 // true

 MathUtil.isEven(3);
 // false

isNegative
(
  • num
)
Boolean

Check if number is negative.

Parameters:

Returns:

Example:

 MathUtil.isNegative(23);
 // false

 MathUtil.isNegative(-23);
 // true

isOdd
(
  • num
)
Boolean

Check if number is odd (convert to Integer if necessary).

Parameters:

Returns:

Example:

 MathUtil.isOdd(2);
 // false

 MathUtil.isOdd(3);
 // true

isPositive
(
  • num
)
Boolean

Check if number is positive (zero is positive).

Parameters:

Returns:

Example:

 MathUtil.isPositive(23);
 // true

 MathUtil.isPositive(-23);
 // false

isPrime
(
  • num
)
Boolean

Check if number is Prime (divisible only by itself and one).

Parameters:

Returns:

Example:

 MathUtil.isPrime(4);
 // false

 MathUtil.isPrime(5);
 // true

map
(
  • num
  • min1
  • max1
  • min2
  • max2
)
Number

Re-maps a number from one range to another. The output is the same as inputing the result of rangeToPercent() numbero percentToRange(). Example: num = 10, min1 = 0, max1 = 100, min2 = 0, max2 = 50 // outputs 5

Parameters:

Returns:

Example:

 MathUtil.map(10, 0, 100, 0, 50);
 // 5

percentToRange
(
  • percent
  • min
  • max
)
Number

Returns the number that corresponds to the percentage in a given range. Example: percent = 0.5 range 10 to 20 // outputs 15

Parameters:

Returns:

Example:

 MathUtil.percentToRange(0.5, 10, 20);
 // 15

radiansToDegrees
(
  • radians
)
Number

Converts radians to degrees.

Parameters:

Returns:

Example:

 MathUtil.radiansToDegrees(1.5707963267948966);
 // 90

 MathUtil.radiansToDegrees(3.141592653589793);
 // 180

randomRange
(
  • min
  • max
  • [wholeNumber=true]
)
Number

Returns a random number between min and max.

Parameters:

Returns:

Example:

rangeToPercent
(
  • num
  • min
  • max
  • constrainMin
  • constrainMax
)
Number

Returns the percentage of a number in a given range. Example: num = 15 range 10 to 20 // outputs 0.5

Parameters:

Returns:

Example:

 MathUtil.rangeToPercent(15, 10, 20);
 // 0.5

sign
(
  • num
)
Number

Returns 1 if the value is >= 0. Returns -1 if the value is < 0.

Parameters:

Returns:

Example:

 MathUtil.sign(23);
 // 1

 MathUtil.sign(-23);
 // -1

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index