from

Validates an unknown value as a matrix and returns a key-ascending shallow clone.

import { Matrix } from "@teakit/matrix";

// The boundary for external input: validate, then clone.
Matrix.from({ "2025": [1, 2], "2024": [3] });
// { "2024": [3], "2025": [1, 2] }  (keys ascending)

// Outer object and each row are new; cell references are preserved.
const cell = { v: 1 };
Matrix.from({ a: [cell] }).a[0] === cell; // true

// Non-matrix input throws.
Matrix.from(new Map()); // throws MatrixError "MATRIX_INVALID_MATRIX"

// An empty object is a valid (empty) matrix.
Matrix.from({}); // {}

API Reference

Signature

Matrix.from<T = unknown, TKey extends string = string>(
  value: unknown,
): Matrix<T, TKey>;

Parameters

ParameterTypeRequiredNotes
valueunknownYesMust be a plain object whose own enumerable string keys all map to dense arrays.

Returns

A new matrix with keys ascending and each row copied. Cell values are not deep-copied. Symbol keys and non-enumerable properties are dropped.

Throws

  • MATRIX_INVALID_MATRIX — value is not a plain object, a row is not an array, a row is sparse, or input is an array / function / Date / Map / Set / class instance.

Agent Contract

FieldValue
Kindstatic helper
Canonical namefrom
AliasesNone
Mutates inputsNo
ReturnsMatrix<T, TKey>

Agent Notes

  • Use from at the boundary of unknown/external data; for copying a value you already know is a matrix, use clone.
  • For a non-cloning check or assertion, use isMatrix / assertMatrix.
  • Represent missing cells as explicit undefined; sparse arrays are rejected.