Skip to main content

Num module

📖 Description​

The Num module contains helper functions related to numbers.

Rationale

The functions in this module are primitive and probably widely available in many other open-source packages on npm. So why are they included in the Tripetto Runner library? That's because the Tripetto Runner library doesn't have any dependency on other npm packages. So all the code needed to run a Tripetto form is included and these functions are simply required. Since they are part of the package, they are exported so you can use them too. For example when building custom blocks.

▶️ Functions​


🔧 ceil​

Round a floating point number upward to its nearest integer.

Signature​

ceil(n: number): number

Parameters​

NameTypeOptionalDescription
nnumberNoSpecifies the input number.

Return value​

Returns the ceil number.

Example​

import { Num } from "@tripetto/runner";

Num.ceil(1.6); // Returns `2`

🔧 conform​

Conforms the supplied number to the specified precision.

Signature​

conform(n: number, precision: number): number

Parameters​

NameTypeOptionalDescription
nnumberNoSpecifies the input number.
precisionnumberNoSpecifies the number of decimals.

Return value​

Returns the conformed number.

Example​

import { Num } from "@tripetto/runner";

Num.conform(1.1235, 3); // Returns `1.124`
Num.conform(1.1235, 2); // Returns `1.12`

🔧 floor​

Round a floating point number downward to its nearest integer.

Signature​

floor(n: number): number

Parameters​

NameTypeOptionalDescription
nnumberNoSpecifies the input number.

Return value​

Returns the floored number.

Example​

import { Num } from "@tripetto/runner";

Num.floor(1.6); // Returns `1`

🔧 format​

Formats a number by inserting thousands and decimal separators while taking rounding into account.

Signature​

format(
input: number | string,
precision?: number | "auto",
separator?: string,
decimal?: string,
minus?: string
): string

Parameters​

NameTypeOptionalDescription
inputnumber | stringNoSpecifies the input value as a number or string.
precisionnumber | "auto"YesSpecifies the precision (default is 0). Set it to auto to keep the precision of the supplied input.
separatorstringYesSeparator sign (default is ,).
decimalstringYesDecimal sign (default is .).
minusstringYesMinus sign (default is -).

Return value​

Returns the formatted number as a string.

Example​

import { Num } from "@tripetto/runner";

Num.format("1000.235", 2); // Returns `1,000.24`
Num.format("1000.235", "auto"); // Returns `1,000.235`

🔧 inRange​

Checks if the given value is within the specified range.

Signature​

inRange(n: number, min: number, max: number, edgeMin?: boolean, edgeMax?: boolean): boolean

Parameters​

NameTypeOptionalDescription
nnumberNoSpecifies the input number.
minnumberNoSpecifies the minimum value.
maxnumberNoSpecifies the maximum value.
edgeMinbooleanYesSpecifies if the edge of the min value is allowed (default is true).
edgeMaxbooleanYesSpecifies if the edge of the max value is allowed (default is false).

Return value​

Returns true if the value is in range.

Example​

import { Num } from "@tripetto/runner";

Num.inRange(5, 5, 10); // Returns `true`
Num.inRange(0, 5, 10); // Returns `false`

🔧 max​

Compares two numbers and returns the highest value.

Signature​

max(a: number, b: number): number

Parameters​

NameTypeOptionalDescription
anumberNoSpecifies the input number A.
bnumberNoSpecifies the input number B.

Return value​

Returns the number with the highest value.

Example​

import { Num } from "@tripetto/runner";

Num.max(1, 2); // Returns `2`

🔧 maxL​

Compares the supplied arguments returns the highest value.

Signature​

maxL(...arguments: number[]): number

Parameters​

NameTypeOptionalDescription
argumentsnumber[]NoArguments to compare.

Return value​

Returns the number with the highest value.

Example​

import { Num } from "@tripetto/runner";

Num.maxL(1, 2, 5, 3); // Returns `5`

🔧 min​

Compares two numbers and returns the lowest value.

Signature​

min(a: number, b: number): number

Parameters​

NameTypeOptionalDescription
anumberNoSpecifies the input number A.
bnumberNoSpecifies the input number B.

Return value​

Returns the number with the lowest value.

Example​

import { Num } from "@tripetto/runner";

Num.min(1, 2); // Returns `1`

🔧 minL​

Compares the supplied arguments returns the lowest value.

Signature​

minL(...arguments: number[]): number

Parameters​

NameTypeOptionalDescription
argumentsnumber[]NoArguments to compare.

Return value​

Returns the number with the lowest value.

Example​

import { Num } from "@tripetto/runner";

Num.minL(1, 2, 5, 3); // Returns `1`

🔧 negative​

Checks if the supplied number is negative and returns this negative value or 0 if the value is positive.

Signature​

negative(n: number): number

Parameters​

NameTypeOptionalDescription
nnumberNoSpecifies the input number.

Return value​

Returns n if the value is negative otherwise 0.

Example​

import { Num } from "@tripetto/runner";

Num.negative(1); // Returns `0`
Num.negative(-1); // Returns `-1`

🔧 positive​

Checks if the supplied number is positive and returns this positive value or 0 if the value is negative.

Signature​

positive(n: number): number

Parameters​

NameTypeOptionalDescription
nnumberNoSpecifies the input number.

Return value​

Returns n if the value is positive otherwise 0.

Example​

import { Num } from "@tripetto/runner";

Num.positive(1); // Returns `1`
Num.positive(-1); // Returns `0`

🔧 range​

Adjusts a number so it is in range between the specifies minimum and maximum.

Signature​

range(n: number, min: number, max?: number): number

Parameters​

NameTypeOptionalDescription
nnumberNoSpecifies the input number.
minnumberNoSpecifies the minimum value.
maxnumberYesSpecifies the maximum value.

Return value​

Returns the ranged number.

Example​

import { Num } from "@tripetto/runner";

Num.range(20, 5, 10); // Returns `10`
Num.range(0, 5, 10); // Returns `5`

🔧 round​

Round a floating point number to the nearest integer.

Signature​

round(n: number): number

Parameters​

NameTypeOptionalDescription
nnumberNoSpecifies the input number.

Return value​

Returns the rounded number.

Example​

import { Num } from "@tripetto/runner";

Num.round(1.49); // Returns `1`
Num.round(1.5); // Returns `2`