reflekt v0.4.1
reflekt
Overview
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.
Installation
Install using NPM:
npm install reflekt --save
Examples / Quickstart
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');
}
};
Calling a Function
code:
reflekt.call(myFunc, { foo: 'duck', bar: 'sauce' });
output:
foo duck
bar sauce
Calling a Function Using the Resolver
code:
reflekt.call('myFunc', { myFunc: myFunc, foo: 'duck', bar: 'sauce' });
output:
foo duck
bar sauce
Calling a Function and Overriding Argument Names
code:
reflekt.call([ 'bar', 'bar', myFunc ], { bar: 'ducks' });
output:
foo ducks
bar ducks
Calling Multiple Functions Using the Same Resolver
code:
var myCaller = reflekt.caller({ foo: 'duck', bar: 'sauce' });
myCaller(myFunc);
myCaller(myOtherFunc);
output:
foo duck
bar sauce
foo duck
bar sauce
Constructing an Object
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
Constructing an Object Using the Resolver
code:
var myInstance = reflekt.construct('MyClass', { MyClass: MyClass, foo: 'duck', bar: 'sauce' });
output:
foo duck
bar sauce
Constructing Multiple Objects with the Same Resolver
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
Constructing the Same Object Multiple Times Using the Resolver
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
API Reference
reflekt
- reflekt
- .ObjectResolver() ⇒ function
- ~resolve(name) ⇒ Object | undefined
- ~add(name, [value], [lifetime])
- ~get(name) ⇒ Object | undefined
- ~has(name) ⇒ Boolean
- ~remove(name)
- ~set(name, [value], [lifetime])
- .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() ⇒ function
reflekt.ObjectResolver() ⇒ function
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])
ObjectResolver~resolve(name) ⇒ Object | undefined
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 |
ObjectResolver~add(name, value, lifetime)
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 |
ObjectResolver~get(name) ⇒ Object | undefined
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 |
ObjectResolver~has(name) ⇒ Boolean
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 |
ObjectResolver~remove(name)
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 |
ObjectResolver~set(name, value, lifetime)
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 |
reflekt.any(items, callback) ⇒ Boolean
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 |
reflekt.call(fn, resolver, context) ⇒ Object
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 |
reflekt.caller(resolver) ⇒ function
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 |
reflekt.construct(klass, resolver, context) ⇒ Object
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 |
reflekt.constructor(resolver) ⇒ function
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 |
reflekt.decorate(fn, resolver, context) ⇒ Object
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 |
reflekt.every(items, callback) ⇒ Boolean
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 |
reflekt.has(fn, args, allOrNone) ⇒ Boolean
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. |
reflekt.injections(fn, resolver) ⇒ Array
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 |
reflekt.isKind(item, kind) ⇒ Boolean
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' |
reflekt.isArray(item) ⇒ Boolean
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 |
reflekt.isBoolean(item) ⇒ Boolean
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 |
reflekt.isObject(item) ⇒ Boolean
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 |
reflekt.isString(item) ⇒ Boolean
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 |
reflekt.parse(fn) ⇒ Array
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 |
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago