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

@lleon/object-builders

v2.0.2

Published

Utilities for building shaped objects and classes

Downloads

58

Readme

@lleon/object-builders

npm (scoped) Test Coverage Status semantic-release code style: prettier

Library to easily create object builders, useful for testing or for generating seed data.

This library requires ES Proxies to work. So be sure your browser or the node version you're using supports proxies.

Installation

yarn add @lleon/object-builders

# or using npm...
npm install @lleon/object-builders

fromInterface()

Creates a new object builder typed from an interface

import { fromInterface } from '@lleon/object-builders';

interface User {
  name: string;
}

const user = fromInterface<User>().setName('John').build();

console.log(user.name); // prints "John"

fromClassObject()

Creates a new object builder from the given class. The properties that can be set are the same properties as the class has excluding methods.

When using this builder object is instantiated using Object.create so the function constructor is never called.

This is a good builder when using in conjunction with class-transformer and class-validator

import { fromClassObject } from '@lleon/object-builders';
import { IsString } from 'class-validator';

class User {
  @IsString()
  name!: string;
}

const user = fromClassObject(User).setName('John').build();

console.log(user instanceof User); // prints `true`
console.log(user.name); // prints "John"

fromClassConstructor()

Creates a new object builder from the given class. The class must receive an object as unique argument. In this case the object is instantiated using the new operator

import { fromClassConstructor } from '@lleon/object-builders';

class User {
  givenName: string;
  familyName: string;

  constructor({ firstName, lastName }: { firstName: string; lastName: string }) {
    this.givenName = firstName;
    this.familyName = lastName;
  }
}

// the available builder properties are the properties that receives the constructor
const user = fromClassConstructor(User).setFirstName('John').setLastName('Doe').build();

console.log(user instanceof User); // prints `true`
console.log(user.givenName); // prints "John"
console.log(user.familyName); // prints "Doe"

fromFactory()

Creates a new builder using the given factory function. Using a factory allow you to return any type you want not just objects

import { fromFactory } from '@lleon/object-builders';

const factory = (properties: Partial<{ userName: string }>): string => {
  return properties.userName || '';
};

const userName = fromFactory(factory).setUserName('john.doe1').build();

console.log(userName === 'john.doe1'); // prints `true`

Development

The project use husky and lint-staged for linting and fixing possible errors on source code before commit

yarn build

Concurrently run build:commonjs and build:esm

yarn build:commonjs

Compile typescript files from the src folder inside the lib folder

yarn build:esm

Compile typescript files from the src folder inside the esm folder using ES modules

yarn clean

Remove the following directories/files

  • lib
  • esm
  • reports

yarn test

Run tests files inside the tests and src folders that matches the following patterns. Exit with code > 0 on error

  • *.test.ts
  • *.spec.ts

yarn cover

For some reason this script does not work when used with yarn so it is needed to launch with npm npm run cover

The same as as yarn test and generates coverages reports in reports/coverage. Exit with code > 0 on error

yarn lint

Check eslint errors according to .eslintrc

yarn lint:fix

Run yarn lint applying fixes and run prettier on every typescript file