function ismatrix
ismatrix(x: unknown): x is matrix

True for matrix (2D array with consistent row lengths).

Returns true if the input is a 2D array (array of arrays) where all subarrays have the same length.

Examples

Valid matrix with one row

import { assertEquals } from "jsr:@std/assert";

assertEquals(ismatrix([[1, 3, 4]]), true);

Valid matrix with multiple rows

import { assertEquals } from "jsr:@std/assert";

assertEquals(ismatrix([[1], [3], [4]]), true);

Invalid matrix due to varying row lengths

import { assertEquals } from "jsr:@std/assert";

assertEquals(ismatrix([[1, 2], [3, 4, 5]]), false);

Valid matrix with mixed element types

import { assertEquals } from "jsr:@std/assert";

assertEquals(ismatrix([[1, 2], [3, '4']]), true);

Empty array (not a matrix)

import { assertEquals } from "jsr:@std/assert";

assertEquals(ismatrix([]), false);

Empty matrix with one empty row

import { assertEquals } from "jsr:@std/assert";

assertEquals(ismatrix([[]]), true);

Parameters

x: unknown

The input to check.

Return Type

x is matrix

Returns true if x is a valid matrix.