function.signature.extender
v0.0.2
Published
Allows to extend the signature of any function with new arguments.
Downloads
3
Readme
function.signature.extender
Allows to extend the signature of any function with new arguments.
The new arguments are appended at the start of the signature:
fx(arg1, arg2) ==> add new1 ==> fx(new1, arg1, arg2)
Use case: you write a automaton plugin extending a application
object using state templates that overwrite the app methods when they are activated. A state
method is added to
query the current state, but the object has no changeToState
method:
- methods should still run in app context in order to have
this
still referencing the app Object - A
state
method is added to the app Object to query the current state, but the object has nochangeToState
method - user of app should not be able to change the state themselves - developers should be able to use changeState in their state method bodies, but should not be bothered with handling it
Context and closures are useless here, we have to inject the changeToState
at runtime to make it available in the body.
// developer writes
function stateMethod (arg1, arg2) {
if (condition) {
changeToState(xyz); // should to an error here
}
}
// loading state modifies signature (purpose of the function.signature.extender)
function changedStateMethod (changeToState, arg1, arg2) {
if (condition) {
changeToState(xyz); // changeToState now exists in run context
}
}
// call to someMethod does
app.someMethod = function () {
arguments[arguments.length] = automatonLib.changeToState;
return changedStateMethod.apply(arguments); // code runs without error
}
Usage
Set dependency
npm install function.signature.extender --save
Extend function signature
var signatureExtender = require('function.signature.extender');
// add one new argument
var newSimpleFunction = signatureExtender(simpleFunction, 'myNewArgument');
// add multiple new arguments
var newMultiFunction = signatureExtender(multiFunction, ['myNewArgument', 'andAnother]);
Inject value
arguments[arguments.length] = 'value for myNewArgument';
newSimpleFunction.apply(arguments);
arguments[arguments.length] = 'value for andAnother';
newMultiFunction.apply(arguments);
Copyright (c) 2014 Luscus ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.