Numeric Comparisons

We can also use T6 to run comparisons on numeric values. The following example shows a few equivalents to using the math tests (e.g. gt) as compared to the standard isTrue() and isFalse() tests. Using the math tests are preferred for 2 reasons.

  1. More descriptive default exception message
  2. Forces the developer to provide 2 explicit inputs into the test (isTrue() and isFalse() only require a single boolean input)

Follow these steps to create a new project workspace and install the t6 dependency to run this example.

# Create and open project folder
mkdir Math_Comparisons_demo
cd Math_Comparisons_demo
# Initialize project and install dependencies
npm init -y
npm i t6@1.1.9
# Create and open source file
touch "Math Comparisons.mjs"
open "Math Comparisons.mjs"

Copy and paste this source code into Math Comparisons.mjs.

import { T6 } from 't6';

// 5 is greater than 4
T6.gt(5, 4);
T6.isTrue(5 > 4);

// 2 is less than or equal to 2
T6.le(2, 2);
T6.isTrue(2 <= 2);

// 3 is not equal to 0
T6.ne(3, 0)
T6.isTrue(3 !== 0);
T6.isFalse(3 === 0);

// Show that all tests passed.
console.log('All tests passed!');

In Math_Comparisons_demo/, execute Math Comparisons.mjs with NodeJS to generate an output.

node "Math Comparisons.mjs"

You should expect to see an output similar to the one below.

All tests passed!
MMNEPVFCICPMFPCPTTAAATR