reflekt can call functions and construct new object, injecting any arguments required. This is achieved by converting the function to a string and using regex to parse out the arguments. Each argument is then resolved using a defined resolver, which can either be an object containing key/value pairs of names and values to pass, or a function which takes the name of the argument as a parameter and returns the argument to pass.
Install using NPM:
npm install reflekt --save
This covers the basic usage of reflekt, for more detailed information consult the API section below.
NOTE: for brevity's sake, all examples should be assumed to begin with:
var reflekt = require('reflekt');
function myFunc(foo, bar) {
console.log('foo', foo);
console.log('bar', bar);
}
function myOtherFunc(foo, bar) {
console.log('foo', foo);
console.log('bar', bar);
}
function MyClass(foo, bar) {
console.log('foo', foo);
console.log('bar', bar);
}
MyClass.prototype = {
something: function(name) {
console.log('name', name || 'stan');
}
};
function MyOtherClass(foo, bar) {
console.log('foo', foo);
console.log('bar', bar);
}
MyOtherClass.prototype = {
something: function(name) {
console.log('name', name || 'smith');
}
};
code:
reflekt.call(myFunc, { foo: 'duck', bar: 'sauce' });
output:
foo duck
bar sauce
code:
reflekt.call('myFunc', { myFunc: myFunc, foo: 'duck', bar: 'sauce' });
output:
foo duck
bar sauce
code:
reflekt.call([ 'bar', 'bar', myFunc ], { bar: 'ducks' });
output:
foo ducks
bar ducks
code:
var myCaller = reflekt.caller({ foo: 'duck', bar: 'sauce' });
myCaller(myFunc);
myCaller(myOtherFunc);
output:
foo duck
bar sauce
foo duck
bar sauce
code:
var myInstance = reflekt.construct(MyClass, { foo: 'duck', bar: 'sauce' });
console.log(myInstance instanceof MyClass);
myInstance.something();
output:
foo duck
bar sauce
true
name stan
code:
var myInstance = reflekt.construct('MyClass', { MyClass: MyClass, foo: 'duck', bar: 'sauce' });
output:
foo duck
bar sauce
code:
var myConstructor = reflekt.constructor({ foo: 'duck', bar: 'sauce' });
var myInstance = myConstructor(MyClass);
var myOtherInstance = myConstructor(MyOtherClass);
output:
foo duck
bar sauce
foo duck
bar sauce
code:
var myConstructor = reflekt.constructor({ MyClass: MyClass, foo: 'duck', bar: 'sauce' });
var myInstance = myConstructor('MyClass');
var myOtherInstance = myConstructor('MyClass');
output:
foo duck
bar sauce
foo duck
bar sauce
- reflekt
- .ObjectResolver() ⇒
function
- ~resolve(name) ⇒
Object
|undefined
- ~add(name, [value], [lifetime])
- ~get(name) ⇒
Object
|undefined
- ~has(name) ⇒
Boolean
- ~remove(name)
- ~set(name, [value], [lifetime])
- ~resolve(name) ⇒
- .any(items, callback) ⇒
Boolean
- .call(fn, [resolver], [context]) ⇒
Object
- .caller([resolver]) ⇒
function
- .construct(klass, [resolver], [context]) ⇒
Object
- .constructor([resolver]) ⇒
function
- .decorate(fn, [resolver], [context]) ⇒
Object
- .every(items, callback) ⇒
Boolean
- .has(fn, args, [allOrNone]) ⇒
Boolean
- .injections(fn, [resolver]) ⇒
Array
- .isKind(item, kind) ⇒
Boolean
- .isArray(item) ⇒
Boolean
- .isBoolean(item) ⇒
Boolean
- .isObject(item) ⇒
Boolean
- .isString(item) ⇒
Boolean
- .parse(fn) ⇒
Array
- .ObjectResolver() ⇒
creates a new ObjectResolver
Kind: static method of reflekt
Returns: function
- the created ObjectResolver. see ObjectResolver~resolve.
Params: Object
[items] - the items to initially store when creating the ObjectResolver
- .ObjectResolver() ⇒
function
- ~resolve(name) ⇒
Object
|undefined
- ~add(name, [value], [lifetime])
- ~get(name) ⇒
Object
|undefined
- ~has(name) ⇒
Boolean
- ~remove(name)
- ~set(name, [value], [lifetime])
- ~resolve(name) ⇒
attempts to resolve an item with the given name.
NOTE: the ObjectResolver is callable directly, which is an alias to this function.
Kind: inner method of ObjectResolver
Returns: Object
| undefined
- the resolved item, or undefined if it was not found
Param | Type | Description |
---|---|---|
name | String |
the name of the item to add |
adds an item using the given name, value and lifetime
Kind: inner method of ObjectResolver
Param | Type | Description |
---|---|---|
name | String | Object |
the name of the item to add. if an object is passed all items will be added |
[value] | Object |
the value of the item to store |
[lifetime] | Integer |
how many times the item can be resolved before being removed automatically |
attempts to resolve an item with the given name.
NOTE: the ObjectResolver is callable directly, which is an alias to this function.
Kind: inner method of ObjectResolver
Returns: Object
| undefined
- the resolved item, or undefined if it was not found
Param | Type | Description |
---|---|---|
name | String |
the name of the item to add |
checks if the item exists in the resolver
Kind: inner method of ObjectResolver
Returns: Boolean
- true if the ObjectResolver has the item, false otherwise
Param | Type | Description |
---|---|---|
name | String |
the name of the item to check |
removes an item with the given name from the ObjectResolver
Kind: inner method of ObjectResolver
Param | Type | Description |
---|---|---|
name | String | Array |
the name(s) of the item to remove |
adds an item using the given name, value and lifetime
Kind: inner method of ObjectResolver
Param | Type | Description |
---|---|---|
name | String | Object |
the name of the item to add. if an object is passed all items will be added |
[value] | Object |
the value of the item to store |
[lifetime] | Integer |
how many times the item can be resolved before being removed automatically |
calls the callback on each item in the array, stopping when the first callback returns true
Kind: static method of reflekt
Returns: Boolean
- true if any calls return true, or false otherwise
Param | Type | Description |
---|---|---|
items | Array |
the items to call the callback with |
callback | function |
the function to call with each item |
calls the function using the given resolver and context
Kind: static method of reflekt
Returns: Object
- the result of the function call
Param | Type | Description |
---|---|---|
fn | function | String | Array |
the function to call |
[resolver] | function | Object |
the resolver to use to resolve the function's arguments |
[context] | Object |
the context to call the function in |
creates a function that takes a function/string and a context, calling the function in the given context using the given resolver
Kind: static method of reflekt
Returns: function
- the function that calls other functions
Param | Type | Description |
---|---|---|
[resolver] | function | Object | Array |
the resolver to use to resolve the function's arguments |
constructs a new copy of the given klass using the given resolver and context
Kind: static method of reflekt
Returns: Object
- the result of the call to the class constructor
Param | Type | Description |
---|---|---|
klass | function | String |
the object to construct a new copy of |
[resolver] | function | Object | Array |
the resolver to use to resolve the class constructor's arguments |
[context] | Object |
the context to call the class constructor in |
creates a function that takes a class and context, creating a new copy of the class in the given context using the given resolver
Kind: static method of reflekt
Returns: function
- the function that constructs other objects
Param | Type | Description |
---|---|---|
[resolver] | function | Object | Array |
the resolver to use to resolve the class constructor's arguments |
creates a function that calls the given function using the given resolver in the given context
Kind: static method of reflekt
Returns: Object
- a function that takes no arguments and returns the result of calling the given function
Param | Type | Description |
---|---|---|
fn | function | String |
the function to resolve the arguments for |
[resolver] | function | Object | Array |
the resolver to use to resolve the function's arguments |
[context] | Object |
the context to call the function in |
calls the callback on each item in the array
Kind: static method of reflekt
Returns: Boolean
- true if all calls return true, or false otherwise
Param | Type | Description |
---|---|---|
items | Array |
the items to call the callback with |
callback | function |
the function to call with each item |
checks if the given function has the given argument(s)
Kind: static method of reflekt
Returns: Boolean
- true if the argument(s) are found in the function signature, false otherwise
Param | Type | Description |
---|---|---|
fn | function |
the function to check |
args | String | Array |
the args to check |
[allOrNone] | Boolean |
if true, all args given must be present. if false, any of the args may be present. the default is true. |
resolves the function's arguments using the given resolver
Kind: static method of reflekt
Returns: Array
- the functions resolved arguments, in order of appearance in the function's signature
Param | Type | Description |
---|---|---|
fn | function | String | Array |
the function to resolve the arguments for |
[resolver] | function | Object | Array |
the resolver(s) to use to resolve the function's arguments |
checks if the given item is of the given type by calling Object.toString
on the item and doing an comparison between
the result and the given kind
Kind: static method of reflekt
Returns: Boolean
- true if the item is of the same type, false otherwise
Param | Type | Description |
---|---|---|
item | Object |
the item to check the type of |
kind | String |
the type expected, in the form of '[object Object]' |
checks if the given item is an array
Kind: static method of reflekt
Returns: Boolean
- true if the item is an array, false otherwise
Param | Type | Description |
---|---|---|
item | Object |
the item to check the type of |
checks if the given item is a boolean
Kind: static method of reflekt
Returns: Boolean
- true if the item is a boolean, false otherwise
Param | Type | Description |
---|---|---|
item | Object |
the item to check the type of |
checks if the given item is an object
Kind: static method of reflekt
Returns: Boolean
- true if the item is an object, false otherwise
Param | Type | Description |
---|---|---|
item | Object |
the item to check the type of |
checks if the given item is a string
Kind: static method of reflekt
Returns: Boolean
- true if the item is a string, false otherwise
Param | Type | Description |
---|---|---|
item | Object |
the item to check the type of |
parses the function's arguments, returning an array of the arguments found
Kind: static method of reflekt
Returns: Array
- the functions arguments, in order of appearance in the function's signature
Param | Type | Description |
---|---|---|
fn | function | String |
the function to parse the arguments for |