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 🙏

© 2024 – Pkg Stats / Ryan Hefner

easy-constructor

v0.9.0

Published

JavaScript class constructors without the boilerplate

Downloads

617

Readme

Write JavaScript class constructors in a single line of code.

  • Fully type-safe: TypeScript inference that feels like magic :mage:
  • Write classes in half the code == half the bugs :bug: == half the bundle size :package:
  • No more positional arguments. Enjoy named arguments :love_letter:

Let me show you an example. :point_down:

// Before

class ExampleClass {
  property1: string;
  property2: number;
  property3: boolean;
  property4: string;

  // So much boilerplate!
  constructor(
    property1: string,
    property2: number,
    property3: boolean,
    property4: string,
  ) {
    // Feels like I'm writing the same thing twice.
    this.property1 = property1;
    this.property2 = property2;
    this.property3 = property3;
    this.property4 = property4;
  }
}

// So many positional arguments. I can't remember what they are!
const exampleInstance = new ExampleClass("Hello", 42, true, "World");
// After

import { easyConstructor } from "easy-constructor";

class ExampleClass {
  property1!: string;
  property2!: number;
  property3!: boolean;
  property4!: string;

  // Just one line!
  static create = easyConstructor(ExampleClass);
}

// Named arguments! 🎉
const exampleInstance = ExampleClass.create({
  property1: "Hello",
  property2: 42,
  property3: true,
  property4: "World",
});

Installation

npm i easy-constructor

Advanced usage

Optional fields and default values

The easyConstructor function treats all class fields as required by default. To make them optional, add them in the optional array.

import { easyConstructor } from "easy-constructor";

class ExampleClass {
  property1!: string;
  property2!: number;
  property3?: boolean;
  property4: string = "default";

  static create = easyConstructor(ExampleClass, {
    optional: ["property3", "property4"],
  });
}
const exampleInstance = ExampleClass.create({
  property1: "Hello",
  property2: 42,
});

TypeScript will infer the property names and provide auto-completion.

Omit variables and custom constructor

You can combine easyConstructor with a custom constructor, if you need to initialize some variables with custom logic.

To exclude variables from the easy constructor, use the omit array.

class ExampleClass {
  property1!: string;
  property2!: number;
  property3!: boolean;
  property4: number;

  static create = easyConstructor(ExampleClass, {
    omit: ["property4"],
  });

  // Custom constructor
  constructor(property4: string) {
    this.property4 = property4 * 2;
  }
}

const exampleInstance = ExampleClass.create(
  // Easy constructor arguments
  {
    property1: "Hello",
    property2: 42,
    property3: true,
  },
  // Custom constructor arguments
  100,
);

Getters and setters

Easy Constructor works with getters and setters too.

class ExampleClass {
  property1!: string;

  get property2() {
    return this.property1.length;
  }

  static create = easyConstructor(ExampleClass, {
    // Omit getter and setter properties
    // from the easy constructor
    omit: ["property2"],
  });
}

const exampleInstance = ExampleClass.create({
  property1: "Hello",
});

Limitations

Does not support inheritance.

Type inference only works with public fields.

The properties from the easy constructor are assigned after the custom constructor is called. This means that the custom constructor can't access the properties of the easy constructor.

When should I (not) use Easy Constructor? :thinking:

Easy Constructor is simple, lightweight, and designed to do one thing very well: remove boilerplate from class constructors.

It is designed to replace 99% of the constructors in your app, where you are just assigning arguments to properties. However, if you have a very complex constructor, with hefty initialization logic, you should stick to the traditional constructor or use a factory function.

Contributors

Meme gallery

Memes related to easy-constructor. Express the pain of creating class constructors in vanilla JavaScript. Want to contribute a meme? Open a PR!

from Imgflip Meme Generator

from Imgflip Meme Generator

from Imgflip Meme Generator

💙 This package was templated with create-typescript-app.