delegates
Advanced tools
Comparing version 0.0.3 to 0.1.0
0.1.0 / 2014-10-17 | ||
================== | ||
* adds `.fluent()` to api | ||
0.0.3 / 2014-01-13 | ||
@@ -3,0 +8,0 @@ ================== |
28
index.js
@@ -23,2 +23,3 @@ | ||
this.setters = []; | ||
this.fluents = []; | ||
} | ||
@@ -96,2 +97,27 @@ | ||
return this; | ||
}; | ||
}; | ||
/** | ||
* Delegator fluent accessor | ||
* | ||
* @param {String} name | ||
* @return {Delegator} self | ||
* @api public | ||
*/ | ||
Delegator.prototype.fluent = function (name) { | ||
var proto = this.proto; | ||
var target = this.target; | ||
this.fluents.push(name); | ||
proto[name] = function(val){ | ||
if ('undefined' != typeof val) { | ||
this[target][name] = val; | ||
return this; | ||
} else { | ||
return this[target][name]; | ||
} | ||
}; | ||
return this; | ||
}; |
{ | ||
"name": "delegates", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"repository": "visionmedia/node-delegates", | ||
@@ -13,2 +13,2 @@ "description": "delegate methods and accessors to another property", | ||
"license": "MIT" | ||
} | ||
} |
@@ -47,4 +47,49 @@ | ||
# API | ||
## Delegate(proto, prop) | ||
Creates a delegator instance used to configure using the `prop` on the given | ||
`proto` object. (which is usually a prototype) | ||
## Delegate#method(name) | ||
Allows the given method `name` to be accessed on the host. | ||
## Delegate#getter(name) | ||
Creates a "getter" for the property with the given `name` on the delegated | ||
object. | ||
## Delegate#setter(name) | ||
Creates a "setter" for the property with the given `name` on the delegated | ||
object. | ||
## Delegate#access(name) | ||
Creates an "accessor" (ie: both getter *and* setter) for the property with the | ||
given `name` on the delegated object. | ||
## Delegate#fluent(name) | ||
A unique type of "accessor" that works for a "fluent" API. When called as a | ||
getter, the method returns the expected value. However, if the method is called | ||
with a value, it will return itself so it can be chained. For example: | ||
```js | ||
delegate(proto, 'request') | ||
.fluent('query') | ||
// getter | ||
var q = request.query(); | ||
// setter (chainable) | ||
request | ||
.query({ a: 1 }) | ||
.query({ b: 2 }); | ||
``` | ||
# License | ||
MIT | ||
MIT |
@@ -78,2 +78,18 @@ | ||
}) | ||
}) | ||
}) | ||
describe('.fluent(name)', function () { | ||
it('should delegate in a fluent fashion', function () { | ||
var obj = { | ||
settings: { | ||
env: 'development' | ||
} | ||
}; | ||
delegate(obj, 'settings').fluent('env'); | ||
obj.env().should.equal('development'); | ||
obj.env('production').should.equal(obj); | ||
obj.settings.env.should.equal('production'); | ||
}) | ||
}) |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6272
171
95
1