npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ludeschersoftware/struct

v1.1.0

Published

A lightweight, type-safe, and immutable data structure utility for JavaScript and TypeScript.

Readme

A lightweight, type-safe, and immutable data structure utility for JavaScript and TypeScript.
Built around a single abstract class Struct, this package allows you to define strict, validated, and readonly objects with minimal boilerplate.


🚀 Features

  • ✅ Type-safe object construction
  • ✅ Custom validators for each field
  • ✅ Automatic default values
  • ✅ Immutable and readonly instances
  • ✅ Clear error messages for invalid input

📦 Installation

npm install @ludeschersoftware/struct

🧠 Concept

The core idea is to define a subclass of Struct, specify default values and validators, and use the .of() method to create validated instances.

Only the Struct class is exposed publicly—it's the default export of the package.


🛠️ Basic Usage

1. Define Your Struct

import Struct from "@ludeschersoftware/struct";

class SoilStruct extends Struct {
  public row!: number;
  public topf!: string;
  public woche!: number;
  public amount!: number;

  private constructor() {
    super();
  }

  public static of(data: Partial<SoilStruct> = {}) {
    return this.create<SoilStruct>(
      {
        row: 0,
        topf: "",
        woche: 0,
        amount: 0,
      },
      {
        row: (raw: any) =>
          typeof raw === "number"
            ? raw
            : (() => {
                throw new TypeError("row must be a number");
              })(),
        topf: (raw: any) =>
          typeof raw === "string"
            ? raw
            : (() => {
                throw new TypeError("topf must be a string");
              })(),
        woche: (raw: any) =>
          typeof raw === "number"
            ? raw
            : (() => {
                throw new TypeError("woche must be a number");
              })(),
        amount: (raw: any) =>
          typeof raw === "number"
            ? raw
            : (() => {
                throw new TypeError("amount must be a number");
              })(),
      },
      data
    );
  }
}

2. Create an Instance

const soil = SoilStruct.of({
  row: 5,
  topf: "A3",
  woche: 28,
  amount: 120,
});

console.log(soil.row); // 5
console.log(soil.topf); // "A3"

3. Immutability & Validation

soil.row = 10; // ✅ Allowed: row is a defined field with a validator
soil.newField = "oops"; // ❌ Throws: Cannot add new property
soil.amount = "high"; // ❌ Throws: amount must be a number

🧪 Validator Syntax

You can use:

  • Primitive constructors: String, Number, Boolean
  • Custom functions: (x: unknown) => T
  • Arrays of validators: [String] for arrays of strings

Example:

{
    name: String,
    tags: [String],
    active: Boolean,
    age: (x) => {
        if (typeof x !== "number" || x < 0) throw new TypeError("Invalid age");
        return x;
    }
}

🔒 Struct Behavior

The returned object is a generic object with getter|setter's that enforces:

  • Field existence
  • Type validation on assignment
  • Readonly structure (no new fields, no deletion)
  • Prevents extensions and prototype manipulation
  • Only data properties are allowed, methods are disallowed. For helper logic, use static functions or external utility classes.

🧬 Advanced Features

instanceof Support

Structs created via .of() behave like native class instances:

const soil = SoilStruct.of();
soil instanceof SoilStruct; // ✅ true

This is achieved by dynamically patching the class's Symbol.hasInstance method. It ensures that instanceof checks work correctly even though instances are constructed via a static factory method.


Contributing

  1. Fork the repo
  2. Create a feature branch
  3. Add tests under tests/
  4. Submit a PR

License

MIT © Johannes Ludescher