Created
March 23, 2017 18:33
-
-
Save BTMPL/4a29b87bb494f9cdbada1f20213e64f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test(params) { | |
return { | |
type: 'TEST', | |
payload: params | |
} | |
} | |
const metaizeThis = (fn) => { | |
fn.addMeta = function(meta) { | |
if(!this._originalFunction) this._originalFunction = this; | |
this._originalFunction._meta = {...this._originalFunction._meta, ...meta} | |
const wrapped = (...params) => { | |
const result = this(...params); | |
result.meta = {...this._meta,...meta}; | |
return result | |
} | |
wrapped.addMeta = this.addMeta; | |
wrapped._originalFunction = this._originalFunction; | |
wrapped._meta = this._originalFunction._meta; | |
return wrapped; | |
} | |
return fn; | |
} | |
const wm = metaizeThis(test).addMeta({test: 1}).addMeta({ook: 2}).addMeta({lue: 42}); | |
console.log(wm(1)); | |
/* | |
Object { | |
"meta": Object { | |
"lue": 42, | |
"ook": 2, | |
"test": 1 | |
}, | |
"payload": 1, | |
"type": "TEST" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment