factory-js

Factory for building Javascript objects. Good for test fixtures.

Include it to your project

<script src="https://raw2.github.com/HackerOfDreams/factory-js/master/dist/factory.min.js"></script>

Usage

var note,
  Note = function Note() {
    this.title = "";
  };
Factory.define("note", Note).defaults({title:"A good note"});
note = Factory.build("note");

Define an Object

Factory.define("type", ConstructorFunction).defaults(defaultObjProperties);

The constructor function is optional

Factory.define("type").defaults(defaultObjProperties);

Build a defined Object

var foo;
Factory.define("foo").defaults({name:"bar"});
foo = Factory.build("foo");

You can override properties of defined objects

var foo;
Factory.define("foo").defaults({name:"bar"});
foo = Factory.build("foo", {name: "I'm foo"});

Note: If the property doesn't exist, it will be created

Build Lists

var notes;
Factory.define("note", NoteConstructor).defaults({id:0, title: "Note #0"})
.sequence("id")
.sequence("title", function(i) { return "Note #" + i });
notes = Factory.buildList("note", 10);

Clean

Cleans the Factory's defined constructors

Factory.clean();

Built With

Share this project:

Updates