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

builder-pattern-2

v1.0.1

Published

Implementation of the Builder Pattern for both Javascript/Typescript and compatible with ES5/ES6 classes

Downloads

7

Readme

builder-pattern-2

Implementation of the Builder Pattern for both Javascript/Typescript and compatible with ES5/ES6 classes.

Why This Library?

I have been using NestJs for some time now which I love. The framework is relying heavily on OOP and DDD, and I was looking for a clean and convenient way to instantiate my Domain Models.

The Builder Pattern, as implemented by the Java library Lombok, gave me the idea to transpose it to Typescript.

To learn more about this library, see the section Articles.

Installation

Install the package using npm

npm i builder-pattern-2

or yarn

yarn add builder-pattern-2

Features

  • Automatic generation of the Builder class.
  • Auto-completion of the Builder's methods and their typing.
  • Compatible with pure Javascript and Typescript.
  • Compatible with ES5/ES6 classes.

Usage

Generate the Builder of a class using the function createBuilderClass.

Its setters are based on the parameter names of the constructor of the class. For Example, if the constructor has a parameter id, the Builder will expose a setter setId.

Typescript Example

import { Builder, createBuilderClass } from 'builder-pattern-2';

/**
 * 1. Create your domain model.
 */
class MrMeeseeks {
  constructor(private readonly goal: string, private readonly lifespan: number) {}

  public getGoal() {
    return this.goal;
  }

  public getLifespan() {
    return this.lifespan;
  }
}

/**
 * 2. Describe its constructor parameters.
 */
interface MrMeeseeksCtor {
  goal: string;
  lifespan: number;
}

/**
 * 3. Generate the Builder class.
 */
const MrMeeseeksBuilder: Builder<MrMeeseeks, MrMeeseeksCtor> = createBuilderClass<MrMeeseeks, MrMeeseeksCtor>(MrMeeseeks);

/**
 * 4. Use it!
 */
const builder = new MrMeeseeksBuilder();

const mr = builder.setGoal('Open jam jar').setLifespan(12).build();

console.log(mr.getGoal()); // 'Open jam jar'
console.log(mr.getLifespan()); // 12

Javascript ES5 Example

const { createBuilderClass } = require('builder-pattern-2');

/**
 * 1. Create your domain model.
 */
function MrMeeseeks(goal, lifespan) {
  this.goal = goal;
  this.lifespan = lifespan;

  this.getGoal = function () {
    return this.goal;
  };

  this.getLifespan = function () {
    return this.lifespan;
  };
}

/**
 * 2. Generate the Builder class.
 */
const MrMeeseeksBuilder = createBuilderClass(MrMeeseeks);

/**
 * 3. Use it!
 */
const builder = new MrMeeseeksBuilder();

const mr = builder.setGoal('Open jam jar').setLifespan(12).build();

console.log(mr.getGoal()); // 'Open jam jar'
console.log(mr.getLifespan()); // 12

Articles

Learn more about the implementation of this library on my tech blog:

License

MIT