0.4.1 • Published 8 years ago

reflekt v0.4.1

Weekly downloads
8
License
ISC
Repository
github
Last release
8 years ago

reflekt

view on npm view on npm Dependency Status Build Status Coverage Status Code Climate

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

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~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

ParamTypeDescription
nameStringthe 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

ParamTypeDescription
nameString | Objectthe name of the item to add. if an object is passed all items will be added
valueObjectthe value of the item to store
lifetimeIntegerhow 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

ParamTypeDescription
nameStringthe 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

ParamTypeDescription
nameStringthe name of the item to check

ObjectResolver~remove(name)

removes an item with the given name from the ObjectResolver

Kind: inner method of ObjectResolver

ParamTypeDescription
nameString | Arraythe 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

ParamTypeDescription
nameString | Objectthe name of the item to add. if an object is passed all items will be added
valueObjectthe value of the item to store
lifetimeIntegerhow 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

ParamTypeDescription
itemsArraythe items to call the callback with
callbackfunctionthe 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

ParamTypeDescription
fnfunction | String | Arraythe function to call
resolverfunction | Objectthe resolver to use to resolve the function's arguments
contextObjectthe 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

ParamTypeDescription
resolverfunction | Object | Arraythe 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

ParamTypeDescription
klassfunction | Stringthe object to construct a new copy of
resolverfunction | Object | Arraythe resolver to use to resolve the class constructor's arguments
contextObjectthe 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

ParamTypeDescription
resolverfunction | Object | Arraythe 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

ParamTypeDescription
fnfunction | Stringthe function to resolve the arguments for
resolverfunction | Object | Arraythe resolver to use to resolve the function's arguments
contextObjectthe 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

ParamTypeDescription
itemsArraythe items to call the callback with
callbackfunctionthe 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

ParamTypeDescription
fnfunctionthe function to check
argsString | Arraythe args to check
allOrNoneBooleanif 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

ParamTypeDescription
fnfunction | String | Arraythe function to resolve the arguments for
resolverfunction | Object | Arraythe 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

ParamTypeDescription
itemObjectthe item to check the type of
kindStringthe 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

ParamTypeDescription
itemObjectthe 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

ParamTypeDescription
itemObjectthe 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

ParamTypeDescription
itemObjectthe 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

ParamTypeDescription
itemObjectthe 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

ParamTypeDescription
fnfunction | Stringthe function to parse the arguments for

Analytics

0.4.1

8 years ago

0.4.0

8 years ago

0.3.9

9 years ago

0.3.8

9 years ago

0.3.7

9 years ago

0.3.6

9 years ago

0.3.5

9 years ago

0.3.4

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.13

9 years ago

0.1.12

9 years ago

0.1.11

9 years ago

0.1.10

9 years ago

0.1.9

9 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago