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

@timreynolds/autofixture

v2.0.0-0

Published

JS port of the C# AutoFixture library

Downloads

277

Readme

AutoFixture.js

Build Status Maintainability

AutoFixture.js is based off of Mark Seeman's AutoFixture for .NET. The AutoFixture.js syntax attempts to match that of AutoFixture as much as possible where reasonable. You'll find the syntax and functionality applicable to JavaScript very similar and easy to understand.

Quick Start

npm install @timreynolds/autofixture

General Syntax

For the examples below, I'm going to assume that autofixture has already been require'd and assigned to the fixture variable.

The general syntax provided by AutoFixture.js follows:

const AutoFixture = require('@timreynolds/autofixture');
const fixture = new AutoFixture();
const instance = fixture.Create(ConstructorFunction[, args]);

What that means is that most of the constructor functions for the builtin JavaScript types are available.

String Creation

To create a string we'd do the following:

const myString = fixture.create(String);
// myString will look like a guid, e.g., '44CDC249-EAA2-4CC4-945C-B52475B0B0A9'

As with AutoFixture, we can also create a string that's prefixed by a value of our choosing. That's done by passing in a string:

const prefixedString = fixture.create("prefix");
// myString will look like a guid with our prefix. e.g.: 'prefix44CDC249-EAA2-4CC4-945C-B52475B0B0A9

Number Creation

To create a Number we'd do the following:

const myNumber = fixture.create(Number);
// myNumber will be a number between [0, 1). E.g., 0.59147235396

If we'd like a negative number or a larger number we can provide a multiplier:

const myNegativeNumber = fixture.create(Number, -1);
// myNegativeNumber will be between (-1, 0]. E.g., -0.7982537387376
const myLargeNumber = fixture.create(Number, 500);
// mylargeNumber will be between [0, 500). E.g., 423.8746491657
const myLargeNegativeNumber = fixture.create(Number, -700);
// myLargeNegativeNumber will be between (-700, 0]. E.g., -672.451987454916

Boolean Creation

To create a Boolean we can do the following:

const myBoolean = fixture.create(Boolean);
// myBoolean will be either true or false.

Object Creation

Creation From a Like Object

In JavaScript we frequently use objects and object literals and care not whether the object was created with a constructor function. AutoFixture.js handles this by using a provided object as a template, or specimen, for the object to be created.

To create an object that's similar to an existing object, just pass the object in to the create method:

const myObj = fixture.create({ prop1: "a string", prop2: 1.234, prop3: true });
/*
myObj will look something like the following: 
{
  prop1: 'prop121032407-9216-404A-9F6A-021E8766AF21',
  prop2: 0.2518655981465,
  prop3: true
}
*/

Each of the property values are randomly chosen from the set of allowable values. In the case of properties that are of type string the string value will be prefixed by the property name to allow the values to be easily distinguishable.

Building Objects

When writing tests cases you often need to exclude a property or set it to a predetermined value. This is achived by using the ObjectBuilder exposed in AutoFixture.js via build().

The builder provides the following methods;

  • like(template: {}): IObjectBuilder
  • without(propName: objectPath): IObjectBuilder
  • with(propName: objectPath, value: any): IObjectBuilder
const myObj = fixture
  .build()
  .like({ prop1: "a string", prop2: 1.234, prop3: true })
  .without("prop1")
  .with("prop4", "added property")
  .create();
/*
myObj will look something like the following: 
{
  prop2: 0.2518655981465,
  prop3: true,
  prop4: 'added property'
}
*/

Object paths as supported by object-path can be used for with and without methods on the builder.

Creation From a Constructor Function

Just as the builtin constructor functions can be used to create strings, numbers, and booleans, custom constructor functions can be used to create other objects.

function MyObjectType() {
  this.prop1 = "";
  this.prop2 = "";
  this.prop3 = 0;
  this.prop4 = false;
}
const myObj = fixture.create(MyObjectType);
/*
myObj will look something like the following:
{
  prop1: 'prop121032407-9216-404A-9F6A-021E8766AF21',
  prop2: 'prop27237F916-AAB4-40CF-814E-8BEC7181A70C',
  prop2: 0.98712634589712,
  prop3: true
}
*/

As with Like-Object creation described above, the instance values are assigned based on their respective property types. Because prop and prop2 are strings, they were assigned random values prefixed by the property name.

Creating Many

When you're required to create an array the create method can be substituted for createMany.

This method takes an additional optional parameter for the number of items to be generated. By default his is set to a random number between 3 and 10.

const stringArray = fixture.createMany(String, 3);

Type Definitions

Due to the dynamic return types of the library only relativly basic type definitions are included. These will be improved overtime from real world usage.

Acknowledgements

This library is a TypeScript rewrite of NextITCorp/AutoFixture.js. Credit to them for the original work which wasn't published to NPM.