Defiant – Tuple and Record Utilities

A small utility module for working with tuples (frozen arrays) and records (frozen objects) in JavaScript / TypeScript.

The module emphasizes immutability and provides helpers for creation, type checks, deep equality, and conversion.


Overview

Version: 1.0.0

Defiant provides:

The module is implemented in TypeScript-compatible JavaScript and relies only on standard ECMAScript APIs.


Design Notes


Autotest

     


How to import

Import directly as a local module:

import { Tuple, Record } from "./defiant.js";

Import default directly as a local module:

import { default as defiant } from "./defiant.js";

Import defaultExport directly as a local module:

import { defaultExport as defiant } from "./defiant.js";

Exports

export { Tuple, Record };
export default { Tuple, Record };

Tuple Utilities

Tuples are implemented as frozen arrays.

Tuple(...values)

Creates a frozen array (tuple) from the given values.

const t = Tuple(1, "a", true);
// readonly [1, "a", true]

Signature

Tuple<T extends readonly unknown[]>(...values: T): T

Tuple.of(...values)

Alias for Tuple(...).

const t = Tuple.of(1, 2, 3);

Tuple.from(iterable)

Creates a tuple from an iterable or array-like object.

const t = Tuple.from(new Set([1, 2, 3]));

Signature

Tuple.from<T extends readonly unknown[]>(
  iterable: Iterable<unknown> | ArrayLike<unknown>): T

Tuple.isTuple(value)

Checks whether a value is a frozen array.

Tuple.isTuple(Object.freeze([1, 2])); // true
Tuple.isTuple([1, 2]);                // false

Signature

Tuple.isTuple(value: unknown): value is readonly unknown[]

Tuple.isEqual(x, y)

Performs a deep equality check between two values.

Characteristics

  • NaN === NaN -> true
  • -0 and +0 are treated as equal
  • Recursively compares:
  • Arrays (length and element order)
  • Objects (prototype, keys, and values)
  • Works for nested structures
  • Tuple.isEqual(
      Tuple(1, { a: 2 }),
      Tuple(1, { a: 2 })
    ); // true

    Signature

    Tuple.isEqual<T extends readonly unknown[]>(x: T, y: T): boolean

    Tuple.toArray(value)

    Converts a tuple to a mutable array (shallow copy).

    const arr = Tuple.toArray(Tuple(1, 2, 3));

    Note: Nested objects are still shared.

    Signature

    Tuple.toArray<T extends readonly unknown[]>(value: T): unknown[]

    Record Utilities

    Records are implemented as frozen objects (shallow freeze).


    Record(object)

    Creates a frozen copy of an object.

    const r = Record({ a: 1, b: 2 });

    Characteristics

    Signature

    Record<T extends object>(O: T): T

    Record.fromEntries(entries)

    Creates a record from an iterable of key–value pairs.

    const r = Record.fromEntries([
      ["a", 1],
      ["b", 2],
    ]);

    Signature

    Record.fromEntries<T extends object>(
      entries: Iterable<readonly [keyof T, unknown]>
    ): T

    Record.fromObject(object)

    Alias for Record(object).

    const r = Record.fromObject({ x: 10 });

    Record.isRecord(value)

    Checks whether a value is a frozen object.

    Record.isRecord(Object.freeze({ a: 1 })); // true
    Record.isRecord({ a: 1 });                // false

    Signature

    Record.isRecord(value: unknown): value is object

    Record.isEqual(x, y)

    Alias for Tuple.isEqual.

    Record.isEqual({ a: 1 }, { a: 1 }); // true

    Signature

    Record.isEqual(x: readonly unknown, y: readonly unknown): boolean

    Record.toObject(value)

    Converts a record to a mutable object (shallow copy).

    const obj = Record.toObject(Record({ a: 1 }));

    Signature

    Record.toObject<T extends object>(value: T): object

    License

    https://opensource.org/licenses/MIT

    MIT License

    SPDX short identifier: MIT

    Copyright (c) 2026 Ferenc Czigler

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


    Copyright (c) 2026 Ferenc Czigler https://github.com/Serrin