applyr

0.0.4 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i applyr

Weekly Downloads

6

Version

0.0.4

License

none

Last publish

Collaborators

  • gordlea