ZEST / base.resolver / Module: base-resolver/utils

base-resolver/utils

The base-resolver/utils module has utility functions for parsing expressions, normalizing dependencies and validations

Source:

Requires

Methods

<static> clone(obj) → {*}

This function clones and creates a new copy of the object.

Parameters:
Name Type Description
obj *

any object

Source:
Returns:

cloned object

Type
*

<static> getDependencies(callback) → {Array.<string>}

Extracts arguments from a function and makes a list of dependencies. The function parameter is transformed from camel-case to - and . separated, and an expression is created which evaluates them in sequence, cascading, till a match is found.
eg. databaseMongoLocal as a parameter dependency will try to resolve to databaseMongoLocal. If there are no components with that name, database-mongo-local will be tried. If database-mongo-local is also not resolved, database.mongo.local will be tried. The first resolution will be taken as the value and if none of them resolves, the component will fail to resolve.

Parameters:
Name Type Description
callback callback

the function to be inspected

Source:
Returns:

an array of dependency expressions in the same sequence as the parameters

Type
Array.<string>
Examples
utils.getDependencies(function (x, y, z) {
     ...
});
// this will return ['x', 'y', 'z']
utils.getDependencies(function (dataStore, userProvider) {
     ...
});
// This should return
[
     'dataStore|data-store|data.store',
     'userProvider|user-provider|user.provider'
]

<static> isInjectorArray(obj) → {boolean}

This function checks if an array is an injector array or not. An injector array is a valid form of zest component representation which is an array with the last element of type function and other elements of type string. The strings in the array is mapped to components and are injected as parameters to the function in the same sequence.

Parameters:
Name Type Description
obj *

any object

Source:
Returns:

true if the object is an injector array. false otherwise.

Type
boolean

<static> resolveExpression(expression, resolverFunction) → {external:q}

Resolves any expression with modifiers. Modifiers are used to add logic to dependency injection. There are 4 kinds of modifiers. They are listed in their order of execution priority below:<

  • # (parameter modifier) will pass parameters to the component that can be used to construct the options object. See Configuring Resolver for more details on parameter usage.
  • ! (immediate modifier) will mark a dependency as immediate. When a dependency is immediate, resolver will not wait for it to resolve, but instead, pass a promise ( which will get resolved when the immediate dependency resolves ) to the component factory function. Immediate is discussed in detail in the Circular Dependencies section.
  • | (OR modifier) will inject the first resolvable component
  • ? (optional modifier) will silently pass undefined if resolution fails

If the expression is a primitive non string type, it will be returned as-is. All strings will be considered as expressions and resolved. For objects or arrays, resolution will happen recursively.

Parameters:
Name Type Description
expression string

the expression to evaluate

resolverFunction module:base-resolver/utils~ResolverCallback

the resolver function to be called.

Source:
Returns:

a promise that resolves to the evaluated value of the expression

Type
external:q
Examples
utils.resolveExpression('a|b#c|d', function (expression, params, expressionWithParams) {
         ...
}
// the ResolverCallback will be called with these arguments
// first call
 a, [], a
// second call
 b, [c], b#c
// third call
 d, [], d
// if any of the calls return a non undefined value or do not throw an Error, the next call will not happen
// if none of the callbacks return a defined value, the promise returned will reject with error
utils.resolveExpression('a|b#c/#c1#c2|d?', function (expression, params, expressionWithParams) {
         ...
}
// the ResolverCallback will be called with these arguments
// first call
 a, [], a
// second call
 b, [c#c1, c2], b#c/#c1#c2
// third call
 d, [], d
// #, |, ? and / can be escaped with a / as shown in the second call
// if any of the calls return a non undefined value or do not throw an Error, the next call will not happen
// if none of the callbacks return a defined value, the promise returned will still resolve with undefined
// because theoptional flag (?) is specified

Type Definitions

ResolverCallback(expression, params, expressionWithParams, immediate)

The ResolverCallback is used in resolveExpression function as a parameter. This callback is called for every part of the expression to get its value. The values are then consolidated and a result is obtained.

Parameters:
Name Type Argument Description
expression string

the actual expression on which the resolver is to be called

params Array.<string>

the list of parameters to be sent to the configurations object

expressionWithParams string

the original expression. This can be used for caching results.

immediate boolean <optional>

if true, the callback should not return a promise. This is useful when resolving a circular dependency

Source:
See: