@lleon/object-builders
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

@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

Package Sidebar

Install

npm i @lleon/object-builders

Weekly Downloads

4

Version

2.0.2

License

MIT

Unpacked Size

42 kB

Total Files

49

Last publish

Collaborators

  • lleon