/
decorate.js
50 lines (46 loc) · 1.32 KB
/
decorate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const DECORATED_KEY = require('../deps/meta/decorated')
const meta = require('../deps/meta')
/**
* decorates a parent when the argument is provided
* BUT THE FUNCTIONS WILL STILL BE SCOPED TO CURRENT PARENT
* for easy factory chaining
*
* @since 4.0.0-alpha.1
* @memberOf MethodChain
* @param {Object} parentToDecorate object to put the method on instead
* @return {MethodChain} @chainable
*
* @see MethodChain
*
* @TODO this is more like a preset since it *adds* plugins?
* more of methodFactory now
*
* @example
*
* const chain = new Chain()
* const obj = {}
* chain.method('ehOh').decorate(obj).build()
* typeof obj.ehOh
* //=> 'function'
*
*/
module.exports = function(parentToDecorate) {
// @TODO is objStrict?
// if (parentToDecorate) {
this.target(parentToDecorate)
// can use this to "undecorate"
// if (!parentToDecorate.meta) <- checks already inside of meta()
parentToDecorate.meta = meta(parentToDecorate)
// default returns result of calling function,
// else .parentToDecorate
return this.plugin(function(name, parent) {
parentToDecorate.meta(DECORATED_KEY, name)
// @NOTE: so we can return...
/* prettier-ignore */
return this
.returns(function returnsFunction(result) {
return result || parentToDecorate
})
.callReturns(true)
})
}