function squeeze
squeeze<T>(x: NestedArray<T>): NestedArray<T>

Removes singleton dimensions from arrays or matrices.

This function simplifies the shape of an array or matrix by removing singleton dimensions (dimensions of size 1). If the input is a number or a string, it returns the input as-is, since they don't have dimensions to squeeze.

Examples

Squeeze deeply nested arrays

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

assertEquals(squeeze([[[[[8]]]]]), [[8]]);

Squeeze nested arrays with multiple elements

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

assertEquals(squeeze([[[[3, 4, 5]]]]), [[3, 4, 5]]);

Squeeze nested arrays with 2D content

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

assertEquals(squeeze([[[[[['31-12-2014', '31-01-2015'], ['15-02-2015', '01-03-2015']]]]]]), [
  ['31-12-2014', '31-01-2015'],
  ['15-02-2015', '01-03-2015']
]);

Input is a number (no squeezing needed)

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

assertEquals(squeeze(42), 42);

Input is a string (no squeezing needed)

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

assertEquals(squeeze("hello"), "hello");

Type Parameters

Parameters

The input to squeeze

Return Type

The squeezed input with singleton dimensions removed