function size
size(x: unknown): array

Size of an N-D array.

Determines the size of an N-dimensional array, where a number is treated as a 1x1 array, a 1-D array as 1xN, and a matrix as MxN. It handles strings by returning their length as 1xN.

Examples

Size of a 4D array

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

assertEquals(size([[[[5, 6, 5], [7, 8, -1]]]]), [1, 1, 2, 3]);

Size of a 2D matrix

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

assertEquals(size([[3, 2, 7], [4, 5, 6]]), [2, 3]);

Size of a 1D array

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

assertEquals(size([5, 4, 4]), [1, 3]);

Size of a scalar

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

assertEquals(size(5), [1, 1]);

Size of a string

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

assertEquals(size('ubique'), [1, 6]);

Size of a 1x2 array of strings

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

assertEquals(size([['first', 'second']]), [1, 2]);

Size of an empty array

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

assertEquals(size([]), [0, 0]);

Parameters

x: unknown

The input whose size is to be determined.

Return Type

An array of dimensions representing the size of the input.

Throws

If no input is provided or if the input type is unknown.