Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-factory

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-factory

Build/share/create reusable factories

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
818
increased by18.9%
Maintainers
1
Weekly downloads
 
Created
Source

Factory

Create and distribute test fixtures/factories.

Usage

All examples assume you have required object-factory like this:

var Factory = require('object-factory');

Basic task syntax

function Event(props) {
  this.title = props.title;
  this.location = props.location;
}

var EventFactory = new Factory({
  object: Event
  properties: {
    // define defaults
    title: 'Amazing Event',
    location: 'Bahamas'
  }
});

// create an object with the attributes of the factory but not an
// instance of the Event class
var event = EventFactory.build({ 
  title: 'xxx' 
});

// Create an instance of the event class
var event = EventFactory.create({ 
  title: 'xxx' 
});

Options for factories

When creating factories there are various options that can be passed.

.properties

new Factory({ 
  properties: {
    key: 'default value
    '
  } 
});

The .properties property (sorry) specify the default values for a given factory.

.object


var MyThing = new Factory({ 
  object: ThingWithConstructorThatAcceptsObjects
});

As the fictional object might suggest object is the object that the factories properties are passed into...

// This operation

MyThing.create({ xfoo: true });

// Translates to this
new ThingWithConstructorThatAcceptsObjects({ xfoo: true })

.onbuild

The onbuild property will be called if given before the generated properties are passed to the constructor .object.

var BuildMe = new Factory({
  onbuild: function(builtObject) {
    // use this to customize the output of your factory for dynamic
    // values, etc...
  }
})

.oncreate

The oncreate property will be called if given after the generated properties are passed to the constructor .object.

var BuildMe = new Factory({
  object: Xfoo
  oncreate: function(object) {
    // (object instanceof Xfoo) === true
  }
})

Composing factories

You can't create abritrarty depth in a factory. Each factory must be one object deep but multiple factories can be referenced as properties to create this nesting.


var Person = new Factory({
  properties: {
    name: 'James Lal'  
  }
});

var Event = new Factory({
  properties: {
    // define defaults
    title: 'Amazing Event',
    location: 'Bahamas',
    person: Person
  }
});

Inheritance

Factories can inherit from other factories:


var Developer = Person.extend({
  properties: {
    OCD: true  
  }
});

Testing Factories

object factory ships with a object-factory-viewer binary which will pretty print the output of your factory given a module.

// xfoo.js
module.exports = new Factory({
  properties: { xfoo: 'foo' }
});
./node_modules/.bin/object-factory-viewer xfoo.js
# will output the pretty printed (util.inspect) version of the factory.

If your not using .onbuild or .oncreate then this is a great way to test the output of your factories. This serves as a good sanity check (and could be used as documentation too).

Keywords

FAQs

Package last updated on 31 Dec 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc