Skip to main content

Arrays

An array is classified as a table of incrementing number keys that start at 1, and without any holes.

In typescript, the distinction between an object and an array is pretty black and white, while in lua, this distinction is usually lost.

@rbxts/expect attempts to rectify this by providing helper methods for checking arrays.

API

Is an Array

You can use the array method to check if a provided value is actually an array.

import { expect } from "@rbxts/expect";

expect([1, 2, 3]).to.be.an.array();

Example error

Expected '{"name": "Daymon"}' to be an array, but it had a non number key 'name' (string)

Array type

You can use an overload on the array method to check if an array contains elements all of a certain type.

This includes support for checkable types, t checks, and even user-defined callbacks.

import { expect, TypeCheckCallback } from "@rbxts/expect";
import t from "@rbxts/t";

const isNumber: TypeCheckCallback = (value) => {
return typeOf(value) === "number";
};

expect([1, 2, 3]).to.be.an.arrayOf("number");
expect([1, 2, 3]).to.be.an.arrayOf(t.number);
expect([1, 2, 3]).to.be.an.arrayOf(isNumber);

Example error

Expected '[1,2,3]' to be an array of type 'string', but there was an element that was a 'number'

Index: 1
Value: 1

Includes

You can use the includes method to check if a value is present in the array.

import { expect } from "@rbxts/expect";

expect([1, 2, 3]).to.include(4);

Example error

Expected '[1,2,3]' to include '4', but it was missing

Empty

You can use the empty method to check if an array contains any elements.

import { expect } from "@rbxts/expect";

expect([]).to.be.empty();

Example error

Expected '[1,2,3]' to be empty, but it had 3 elements

Length

You can use the length method to check if an array has a certain amount of elements.

import { expect } from "@rbxts/expect";

expect([1, 2, 3]).to.have.a.lengthOf(3);

Example error

Expected '[1,2]' to have exactly '3' element(s), but it actually had '2'

Deep equal

You can use the deepEqual method to check if two arrays are deeply equal to one another.

import { expect } from "@rbxts/expect";

expect([1, 2, 3]).to.deepEqual([1, 2, 3]);

Example error

Expected '[1,2]' to deep equal '[1]', but there were extra elements

Expected: '[1]'
Actual: '[1,2]'
Extra Elements: '[2]'

Starts with

You can use the startsWith method to check if an array starts with another array.

import { expect } from "@rbxts/expect";

expect([1, 2, 3]).to.startWith([1, 2]);

Example error

Expected '[1,2,3]' to be an array that starts with '[1,4,5]', but it was missing 2 elements

Missing: '[4,5]'

Ends with

You can use the endsWith method to check if an array ends with another array.

import { expect } from "@rbxts/expect";

expect([1, 2, 3]).to.endWith([2, 3]);

Example error

Expected '[1,2,3,4]' to be an array that ends with '[1,2,4]', but it was missing 2 elements

Missing: '[1,2]'

All

You can use the all method to check if all the elments in an array pass some condition.

import { expect } from "@rbxts/expect";

expect([2, 4, 6]).to.all((it) => it % 2 === 0);
expect(["Bryce", "Bryan"]).to.all(`start with "Bry"`, (it) => startsWith(it, "Bry"));

Example error

Expected '["Bryce", "Bryan", "Daymon"]' to all start with "Bry", but there was an element that failed the check

Index: 3
Value: "Daymon"

Some

You can use the some method to check if an array passes some condition.

import { expect } from "@rbxts/expect";

expect([1, 2, 3]).to.have.some((it) => it % 2 === 0);
expect(["Daymon", "Bryan"]).to.have.some(`starts with "Bry"`, (it) => startsWith(it, "Bry"));

Example error

Expected '["Byron", "Bryan"]' to have atleast one element that starts with "Day"

Contain exactly

You can use the containsExactly method to check if an array deeply contains all of the elements in another array, with no regard for order and without any extra elements.

import { expect } from "@rbxts/expect";

expect([1, 2, 3]).to.containExactly([3, 2, 1]);

Example error

Expected '[1,2]' to contain exactly '[1,2,3]', but it was missing elements

Missing elements: '[3]'

Contain exactly in order

You can use the containsExactlyInOrder method to check if an array deeply contains all of the elements in another array, without any extra elements and in the same order.

import { expect } from "@rbxts/expect";

expect([1, 2, 3]).to.containsExactlyInOrder([1, 2, 3]);

Example error

Expected '[1,2,3]' to contain exactly '[1,3,2]', but it had a different value for the element at '[1]'

Expected [1]: '3'
Actual [1]: '2'

All of

Unique

Sorted