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 = ;
Uses
1. Lightweight factories
A common pattern would go along the lines of:
//Thing.js { thisstuff = stuff;} module { return stuff;};
This and many cases like it can be shortened to:
moduleexports = ;
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 = ; ; // -> true
This also opens the door to the call
and apply
methods as the dateFactory is just a function.
; // -> true
3. Factory validators
In some senarios you will want to validate the input data before creating the object:
{ return stuffplentiful;} module { return ? stuff : null;};
This can be shortened to:
//Same two functions defined abovemoduleexports = ;
4. Mapping to objects succinctly
We want to map an array of basic objects, transforming them into instances of our class - Person.
{ thisname = configname; thisage = configage;}
The data.
var items = name: 'John' age: 34 name: 'Dick' age: 55 name: 'Harry' age: 89 ;
The glue.
var simpleFactory = ;var people = items; //test; // -> true