function-proxy
Proxy functions with functions for modifying arguments, intercepting errors, or spying
Spies
spy(fn, spyFn) aka mux
useful for tests, to ensure a function is invoked
var spy = spy; request; { console; // 200} { console; // 200}
spyOnMethod(obj, methodName, spyFn)
useful for tests, to ensure an object method is invoked
var spyOnMethod = spyOnMethod;var utils = {...};utils; // returns true like normal { console; // test}
spyOnClassMethod(obj, methodName, spyFn)
useful for tests, to ensure a Class method is invoked
var spyOnClassMethod = spyOnClassMethod;var Animal = ;var animal = ; ; animal;console; // true, method worked like normal { console; // true, spy was also invoked}
Error Intercept
intercept(fn, onError) aka int
useful for calling back a common onError function
only invokes onError if error exists
var intercept = intercept;var newFn = ; { console; // boom} {...}; // was never invoked
only invokes fn if error does not exist, also does not pass error to fn
var intercept = intercept;var newFn = ; { // notice no error arg console; // hello} {...}; // was never invoked
invokes fn with error if dontSliceError arg is true
var intercept = intercept;var dontSliceError = true;var newFn = // true indicates pass on error to fn; { // notice HAS error arg // err will always not exist here. console; // hello} {...}; // was never invoked
Transform Args like Arrays
sliceArgs(fn, start, end) aka slice
allows you to slice off arguments you dont care for (Array.prototype.slice for arguments)
var sliceArgs = sliceArgs;var newFn = ; { console; // { 0:'foo' } , 'bar' and 'baz' were not passed};
spliceArgs(fn, index, howMany /*, elementsToInsert.. */) aka splice
allows you to splice arguments (remove and/or add add args), (Array.prototype.splice for arguments)
var spliceArgs = spliceArgs;var newFn = ; { console; // { 0:'inserted', 1:'bar' }, overrode foo with splice};
mapArgs(fn, mapFn [, includeError]) aka map
allows you to map arguments to new values before passing them along
var mapArgs = mapArgs;var newFn = ; { console; // { 0:null, 1:1, 2:2 }, args were parsed from Strings to Integers};
var mapArgs = mapArgs;var includeError = true; // includeError specifies not to skip the error arg (args[0])var newFn = ; { console; // { 0:1, 1:2 }, args were parsed from Strings to Integers};
filterArgs(fn, filterFn [, includeError]) aka filter
allows you to filter (remove) arguments passed
var filterArgs = filterArgs;var newFn = ; { console; // { 0:null, 1:'foo', 2:'bar' }, null args were ignored};
var filterArgs = filterArgs;var newFn = ; { console; // { 0:'foo', 1:'bar' }, null args were ignored};
tjmehta/map-utils)
Map Args Transforms (frompickArgs(fn, keys..., includeError) aka pick
picks keys off each arg before passing
omitArgs(fn, keys..., includeError) aka omit
omits keys off each arg before passing
setArgs(fn, setObj, includeError) aka set
setArgs(fn, key, value, includeError)
sets keys and values on each arg before passing
unsetArgs(fn, key, value, includeError) aka unset
unsets keys on each arg before passing
pluckArgs(fn, key, includeError) aka pluck
allows you map each arg to a key on the arg
mixed example
var pluckArgs = pluckArgs;var sliceArgs = sliceArgs;request; { console; // 200, yay google is up};
License
MIT