Skip to content

Instantly share code, notes, and snippets.

@BTMPL
Created March 23, 2017 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BTMPL/4a29b87bb494f9cdbada1f20213e64f5 to your computer and use it in GitHub Desktop.
Save BTMPL/4a29b87bb494f9cdbada1f20213e64f5 to your computer and use it in GitHub Desktop.
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