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

@brunoss/mock-builder

v1.0.11

Published

Mock Builder for FakerJS and Vitest

Downloads

83

Readme

mock-builder

GitHub Workflow Status GitHub npm npm JSR Bundle Size Bundle Size GitHub commit activity GitHub last commit codecov

Overview

The mock-builder is a TypeScript library designed to help you create objects based on a specified shape. Each property in the shape can be either a static value or a function that returns a value. The library emphasizes immutability, ensuring that each modification creates a new instance, thereby avoiding side effects and promoting predictable, maintainable code.

Features

  • Immutable Design: Ensures that every operation returns a new instance, avoiding unintended side effects.
  • Flexible Shape Definition: Supports both static values and functions that generate values.
  • TypeScript Support: Fully typed for a robust development experience.
  • Chainable API: Easily chain methods for building complex objects.

Installation

Install the package via npm:

npm i -D @brunoss/mock-builder

Usage

Basic Usage

import { Builder } from 'immutable-builder';

// Define a shape for the object
const builder = new Builder({
  id: () => Math.floor(Math.random() * 100),
  name: 'defaultName'
});

// Build the object
const myObject = builder.build();

console.log(myObject);
// Output: { id: 42, name: 'defaultName' }

Applying Default Values

const builder = new Builder({
  id: 1,
  name: 'Alice'
});

// Apply default values
const updatedBuilder = builder.applyDefaultValues({
  age: () => 30, // Function executed during build
  isActive: true
});

const myObject = updatedBuilder.build();
console.log(myObject);
// Output: { id: 1, name: 'Alice', age: 30, isActive: true }

Setting Values Conditionally

const builder = new Builder({
  id: 1,
  name: 'Alice'
});

// Set values based on a condition
const updatedBuilder = builder.setConditionalValue(
  'isAdmin',
  userType === 'admin',
  true,
  false
);

const myObject = updatedBuilder.build();
console.log(myObject);
// Output: { id: 1, name: 'Alice', isAdmin: false }

API

Builder

constructor(shape: Shape<T>)

  • Creates a new Builder instance.
  • Parameters:
    • shape: An object where each key is a property of the resulting object and can be either a value or a function that returns a value.

build(): T

  • Constructs an object based on the shape provided during instantiation.
  • Returns: An object of type T with all values resolved.

withValue<K extends keyof T>(prop: K, value: T[K]): Builder<T>

  • Sets a specific property of the shape to always return a given value.
  • Parameters:
    • prop: The property name to set.
    • value: The value that the property should always return.
  • Returns: A new Builder instance.

setConditionalValue<K extends keyof T>(prop: K, condition: boolean, trueValue: T[K], falseValue: T[K]): Builder<T>

  • Sets a property based on a condition.
  • Parameters:
    • prop: The property name to set.
    • condition: The condition to evaluate.
    • trueValue: The value if the condition is true.
    • falseValue: The value if the condition is false.
  • Returns: A new Builder instance.

applyDefaultValues(defaults: Partial<Shape<T>>): Builder<T>

  • Applies default values to undefined properties. If a default value is a function, it runs during build() to get the final value.
  • Parameters:
  • defaults: An object containing default values for properties.
  • Returns: A new Builder instance.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request or open an Issue on GitHub.

Credits

based on fluentbuilder