propo
This module exposes a single function, a functional helper to define chained getter / setter on an Object.
Eases the process of building a chaining API.
Usage
On a prototype.
var prop = ; {}'src';'dest';'output'; var stuff = ; stuff ; console;// => file.json console;// => file.json
Hash / Object.create
var obj = {};
prop(obj)('src');
prop(obj)('dest');
prop(obj)('output');
Object.create(obj)
.src('file.json')
.dest('tmp/output.json')
.output('log/stdout.log')
Using a forEach iterator
['url', 'auth', 'file', 'action'].forEach(prop(Stuff.prototype));
Getter / Setter strategy
A way to get and set props in and out of an object.
- _ - Uses "private-like" properties with a leading
_
. Ex.obj._src
- attr - Uses a hash object named
attributes
to hold props. Ex.obj.attributes.src
Custom:
// Using this.options instead
function opts(name, value) {
this.options = this.options || {};
if (!value) return this.options[name];
this.options[name] = value;
return this;
}
var obj = {};
prop(obj, { strategy: opts })('file');
// Set
obj.file('file.json');
console.log(obj.options.file);
// => file.json
Validation
Used before setting a property, a simple function returning true on proper input.
{ if !value return; if !valuename return; if !valuepass return; return true;} var obj = {};'auth'; // Wrong input are noopobj; console;// => undefined // Ok inputobj; console;// => { name: 'foo', pass: 'bar' });