factorify

Better object factories.

factorify

Better object factories.

Install with component(1):

$ component install mikanda/factorify
var factory = require('factorify');
 
// build factory 
 
var User = factory()
  .sequence('seq', 'id-')
  .attr('name', function(){
    return randomName();
  });
 
// build object using factory 
 
var object = new User();
console.log(object.name);  // -> 'generated random name' 

Makes this attribute a sequence number. Optional prefix is prepended to the number.

Defines a new attribute for which the value is calculated using fn.

Initialize new object. defs is an optional map to override attributes.

var User = factory()
  .attr('name', function(){
    return 'my-name';
  });
var user = new User({ name: 'my-other-name' });
console.log(user.name)  // -> 'my-other-name' 

MIT