Decorate any Javascript object with a convenient builder, which returns an immutable object with getters.
var StudentClass = function(){
this.name = "Some default name";
this.age = undefined;
this.address = {};
this.prettyName = function(){};
};
var StudentClassBuilder = BuilderDecorator(StudentClass);
var student = StudentClassBuilder()
.name("John")
.age(17)
.address({postcode: "90210"})
.prettyName(function(){ return "Hi, I'm " + this.name() + "!"; })
.build();
student.name(); // "John"
student.age(); // 17
student.address(); // {postcode: "90210"}
student.prettyName(); // function(){ return "Hi, I'm " + this.name() + "!"; }
var BuilderDecorator = require('js-builder-decorator').BuilderDecorator;
var StudentClassBuilderLocked = BuilderDecorator(StudentClass, {lockFunctionsAfterBuild: true});
var student = StudentClassBuilderLocked()
.name("John")
.prettyName(function(){ return "Hi, I'm " + this.name() + "!"; })
.build();
student.name(); // "John"
student.prettyName(); // "Hi, I'm John!"
// Throwing exception if any field isn't set
var StudentClassBuilderNoNulls = BuilderDecorator(StudentClass, {allFieldsMustBeSet: true});
try {
var student = StudentClassBuilderNoNulls().build(); // This throws an exception
} catch (E) {
console.log(E); // The following fields were not set: name,age,address,prettyName
}
If you have Node.js installed, run npm install js-builder-decorator
in your project directory.
Else, you can download the latest version from Github here.