0.0.4 • Published 9 years ago

applyr v0.0.4

Weekly downloads
18
License
-
Repository
github
Last release
9 years ago

applyr.js

A module for node.js.

A simple way to apply properties from one object to another, and optionally supply defaults to use if the from object is missing properties.

Useful for using config objects in constructors.

Examples

var Applyr = require('applyr')

var config = {
  port: 7215,
  name: "Fredrick"
}

var MainObject = function(objectConfig) {
  Applyr.applyConfigTo(this, objectConfig);
}
var mo = new MainObject(config);

console.log(JSON.stringify(mo));
//output: {"port":7215,"name":"Fredrick"}

//now if we want to use defaults
var configNoPort = {
	name: 'Jason'
}

var MainObjectWithDefaults = function(objectConfig) {
	var defaults = {
		port: 8888
	}
	Applyr.applyConfigTo(this, objectConfig, defaults);
}
var moDefaults1 = new MainObjectWithDefaults(configNoPort);

console.log(JSON.stringify(moDefaults1));
//output: {"port":8888,"name":"Jason"}

//now what happens if the config has port and so does defaults:
var moDefaults2 = new MainObjectWithDefaults(config);

console.log(JSON.stringify(moDefaults2));
//output: {"port":7215,"name":"Fredrick"}

//the object gets the value from the config, ignoring the default
0.0.4

9 years ago

0.0.3

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago