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 = ; { console; console;} { console; console;} { console; console;} MyClassprototype = { console; }; { console; console;} MyOtherClassprototype = { console; };
Calling a Function
code:
reflekt;
output:
foo duckbar sauce
Calling a Function Using the Resolver
code:
reflekt;
output:
foo duckbar sauce
Calling a Function and Overriding Argument Names
code:
reflekt;
output:
foo ducksbar ducks
Calling Multiple Functions Using the Same Resolver
code:
var myCaller = reflektcaller foo: 'duck' bar: 'sauce' ;;;
output:
foo duckbar saucefoo duckbar sauce
Constructing an Object
code:
var myInstance = reflekt;console;myInstance;
output:
foo duckbar saucetruename stan
Constructing an Object Using the Resolver
code:
var myInstance = reflekt;
output:
foo duckbar sauce
Constructing Multiple Objects with the Same Resolver
code:
var myConstructor = reflektconstructor foo: 'duck' bar: 'sauce' ;var myInstance = ;var myOtherInstance = ;
output:
foo duckbar saucefoo duckbar sauce
Constructing the Same Object Multiple Times Using the Resolver
code:
var myConstructor = reflektconstructor MyClass: MyClass foo: 'duck' bar: 'sauce' ;var myInstance = ;var myOtherInstance = ;
output:
foo duckbar saucefoo duckbar 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])
- ~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() ⇒
function
reflekt.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) ⇒
Object
| undefined
ObjectResolver~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 |
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 |
Object
| undefined
ObjectResolver~get(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 |
Boolean
ObjectResolver~has(name) ⇒ 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 |
Boolean
reflekt.any(items, callback) ⇒ 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 |
Object
reflekt.call(fn, [resolver], [context]) ⇒ 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 |
function
reflekt.caller([resolver]) ⇒ 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 |
Object
reflekt.construct(klass, [resolver], [context]) ⇒ 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 |
function
reflekt.constructor([resolver]) ⇒ 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 |
Object
reflekt.decorate(fn, [resolver], [context]) ⇒ 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 |
Boolean
reflekt.every(items, callback) ⇒ 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 |
Boolean
reflekt.has(fn, args, [allOrNone]) ⇒ 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. |
Array
reflekt.injections(fn, [resolver]) ⇒ 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 |
Boolean
reflekt.isKind(item, kind) ⇒ 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]' |
Boolean
reflekt.isArray(item) ⇒ 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 |
Boolean
reflekt.isBoolean(item) ⇒ 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 |
Boolean
reflekt.isObject(item) ⇒ 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 |
Boolean
reflekt.isString(item) ⇒ 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 |
Array
reflekt.parse(fn) ⇒ 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 |