simple-factory

One factory method to rule them all.

Usage no npm install needed!

<script type="module">
  import simpleFactory from 'https://cdn.skypack.dev/simple-factory';
</script>

README

Build Status Coverage Status

simple-factory

A factory to create factories, sounds weird right? But it has it's uses!

Install

Via npm, saving it as a dependency.

npm i simple-factory --save
var simpleFactory = require('simple-factory');

Uses

1. Lightweight factories

A common pattern would go along the lines of:

//Thing.js

function Thing(stuff) {
  this.stuff = stuff;
}

module.exports = function factory(stuff) {
  return new Thing(stuff);
};

This and many cases like it can be shortened to:

module.exports = simpleFactory(Thing);

2. Avoiding the new keyword

Some people don't like using the new keyword in javascript, but there are some cases where it's unavoidable. For example, the native Date object, without the new keyword it will only return a string.

Simple-factory can be used to create a date factory:

var dateFactory = simpleFactory(Date);

assert(dateFactory() instanceof Date); // -> true

This also opens the door to the call and apply methods as the dateFactory is just a function.

assert(dateFactory.apply(null, [2015, 2, 10]) instanceof Date); // -> true

3. Factory validators

In some senarios you will want to validate the input data before creating the object:

function validate(stuff) {
  return stuff.plentiful;
}

module.exports = function factory(stuff) {
  return validate(stuff) ? new Thing(stuff) : null;
};

This can be shortened to:

//Same two functions defined above
module.exports = simpleFactory(Thing, validate);

4. Mapping to objects succinctly

We want to map an array of basic objects, transforming them into instances of our class - Person.

function Person(config) {
  this.name = config.name;
  this.age = config.age;
}

The data.

var items = [
  {
    name: 'John',
    age: 34
  },
  {
    name: 'Dick',
    age: 55
  },
  {
    name: 'Harry',
    age: 89
  }
];

The glue.

var simpleFactory = require('simple-factory');
var people = items.map(simpleFactory(Person));

//test
assert(people.every(function(person) {
  return person instanceof Person;
})); // -> true