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

test-factory

v0.0.1

Published

A library for create test-data factories

Downloads

5

Readme

Factory.js

A library for create test-data factories

Build Status

Usage

function Game(data) {
    this.id = data.id;
    this.isOver = data.isOver;
    this.createAt = data.createAt;
    this.randomSeed = data.randomSeed;
    this.players = data.players;
}

function Player(data) {
    this.id = data.id;
    this.name = data.name;
}

var playerFactory = factory(function (name) {
    var id = this.sequence();
    name = name || 'Player ' + id;
    return new Player({
        id: id,
        name: name
    });
});

var gameFactory = factory(function () {
    var players = playerFactory.create(2);
    players.push(playerFactory('Awesome player'));
    return new Game({
        id: this.sequence(),
        isOver: false,
        createAt: new Date(),
        randomSeed: Math.random(),
        players: players
    });
});

You can now build a new game the following way:

var game = gameFactory();

which returns a Game instance with the following data:

id: 0,
isOver: true,
createdAt: Wed Apr 03 2013 21:56:16 GMT+0200 (CEST),
randomSeed: 0.672447538934648,
players: [
    { id: 0, name: 'Player 0' },
    { id: 1, name: 'Player 1' },
    { id: 2, name: 'Awesome player' }
]

Create a new factory

You create a new factory the following way:

var personFactory = factory(function () {
    return new Person({ name: 'Person' + this.sequence() });
});

This effectively binds the given function to a factory instance that provides support for sequencing. Furthermore the given function is decorated with methods for creating multiple instances and reset the sequence of the factory.

Creating an instance using the factory

You create a new instance by calling the factory:

var person0 = personFactory();
var person1 = personFactory();

person0 will be named Person0 and person1 will be named Person1.

Creating multiple instances using the factory

You can create multiple instances using the create method on the factory:

var persons = personFactory.create(2);

This will create a persons array containing two persons. person[0] will be named Person0 and person[1] will be named Person1.

Parameterized factories

You can parameterize you factory the following way:

var personFactory = factory(function (name) {
    name = name || 'Person' + this.sequence();
    return new Person({ name: name });
});

When you call the personFactory with a name it will return a person with that name; otherwise it will default to the sequenced name.

var person0 = personFactory();
var person1 = personFactory('foo');

person0 will be named Person0 and person1 will be named Foo.

If you use the create method to create multiple instances it call the factory method with no parameters.

Combining factory methods

You are free to combine factory methods as you are pleased. But be aware not to create cyclic recursion.

var dogFactory = factory(function () {
    return new Dog({ name: 'Dog' + this.sequence() });
});

var personFactory = factory(function () {
    return new Person({ 
        name: 'Person' + this.sequence(), 
        dog: dogFactory();
    });
});

Every person you create with the personFactory will have a dog instance associated.

var person0 = personFactory();
var person1 = personFactory();

assert(person0.name === 'Person0');
assert(person0.dog.name === 'Dog0');

assert(person1.name === 'Person1');
assert(person1.dog.name === 'Dog1');

Resetting the sequence of a factory

You can reset the sequencing of a factory the following way:

var personFactory = factory(function () {
    return new Person({ name: 'Person' + this.sequence() });
});

var person0 = personFactory();
personFactory.reset();
var person1 = personFactory();

person0 will be named Person0 and person1 will be named Person0.

Contributors

Gert Sønderby (@gertsonderby)

License

Copyright 2013 Sune Simonsen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.