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

makery

v0.2.0

Published

Factory-style (via blueprints) object creation for tests.

Downloads

5

Readme

makery.js

Factory-style (via blueprints) object creation for tests. Inspired by Ruby Machinist.

Makery is a library that allows you to create objects with default values (and possibly overriding those) via the definition of blueprints.

Makery works really well for creating Backbone models, but that is not the only use case. Makery can be used to create any kind of objects from a constructor.

How to install (web)

This library has Underscore as a single dependency. Simply put the underscore script before this one.

How to install (node)

npm install makery

How to use

Makery has a short syntax for constructors that have a single options object literal as a parameter. This is a very common way to create "Models", "Views" and other entities in most frameworks out there.

Define the blueprints for the constructor

Makery.blueprint(MyConstructor, {
  aProperty: "Default value for property",
  someOtherProperty: false
});

Now create the object

var obj = MyConstructor.make();
obj.aProperty; // "Default value for property"
obj.someOtherProperty; // false

You can override the defaults

var obj = MyConstructor.make({
  someOtherProperty: true
});
obj.aProperty; // "Default value for property"
obj.someOtherProperty; // true

You can use functions to build up the properties in the blueprint. This is useful for creating new instances each time for a property.

Makery.blueprint(MyConstructor, {
  aProperty: function() { return [1, 2, 3]; },
  someOtherProperty: false
});

var obj = MyConstructor.make();
obj.aProperty; // [1, 2, 3]

Inside the function properties in the blueprint, you have access to helper functions, available directly though this. Available helpers:

  • unique: gives you an incremental value each time, ideal for ids or unique properties. Optionally pass a prefix, to create unique properties of the type of 'prefixid'.
Makery.blueprint(MyConstructor, {
  id: function() { return this.unique(); }
});

var obj = MyConstructor.make();
obj.id; // 0
var anotherObj = MyConstructor.make();
anotherObj.id; // 1

Also there is an afterCreation hook available in the blueprints, to do any kind of action with the just created object.

Makery.blueprint(MyConstructor, {
  aProperty: "The value of this property",
  afterCreation: function(obj) {
    obj.otherProperty = "Another value";
  }
});

var obj = MyConstructor.make();
obj.aProperty; // "The value of this property"
obj.otherProperty; // "Another value"

Multiple blueprints for the same constructor can be defined. An unnamed blueprint will be the default one, but you can name blueprints and specify that name when making objects to use them. Named blueprints will also use the default blueprint as a fallback for not defined properties.

Makery.blueprint(MyConstructor, {
  aProperty: "This is the default blueprint",
  anotherProperty: "Still from the default blueprint"
});

Makery.blueprint(MyConstructor, "another blueprint", {
  aProperty: "This is another blueprint"
});

var obj = MyConstructor.make();
obj.aProperty; //"This is the default blueprint"
obj.anotherProperty; //"Still from the default blueprint"

obj = MyConstructor.make("another blueprint");
obj.aProperty; //"This is another blueprint"
obj.anotherProperty; //"Still from the default blueprint"

Makery has a little more verbose way to define a blueprint, by passing a function as the last parameter of the definition. The return value of that function must be the parameter to use for the constructor.

Makery.blueprint(MyConstructor, function() {
  return {
    aProperty: "Some value",
    anotherProperty: "Another value"
  };
});

var obj = MyConstructor.make();
obj.aProperty; //"Some value"
obj.anotherProperty; //"Another value"

You can use this inside the function to get access to the helpers provided.

Makery.blueprint(MyConstructor, function() {
  return {
    id: this.unique(),
    aProperty: "Some value",
    anotherProperty: "Another value"
  };
});

var obj = MyConstructor.make();
obj.id; //1
obj.aProperty; //"Some value"
obj.anotherProperty; //"Another value"

With this "more verbose" way to define blueprints, you can work with constructors that have more than one parameter. To do this, just return an array with the parameters in the correct order for that constructor.

function MyMultiParamConstructor(param1, param2) {
  this.param1 = param1;
  this.param2 = param2;
}

Makery.blueprint(MyMultiParamConstructor, function() {
  return ["some param1 value", "some param2 value"];
});

var obj = MyMultiParamConstructor.make();
obj.param1; //"some param1 value"
obj.param2; //"some param2 value"

Changelog

v0.2.0

  • Named blueprints fallback to default blueprint if defined
  • Multiple parameters

v0.1.0

  • Basic blueprints functionality
  • Support for afterCreation hook
  • Support for function properties
  • Named blueprints