linkedin/dustjs — pull request #93 by rodw (2012-07-16 18:59:14+00:00)
Title: make `tap` helper work with function-valued context variables; add tests

This pull request addresses a problem with the `tap` function recently added to dust-helpers.js.

Tap doesn't always work properly when it encounters references to context keys that resolve to *functions* as opposed to scalar values.

For instance, given a base context object that includes a *function*:

{ foo: function() { return "bar"; } }

Dust will generally resolve a reference to `foo` by evaluating the function. I.e. the template `{foo}` will resolve to `bar`.

In some circumstances the `tap` function doesn't quite do that--yielding an error or a blank/undefined value.

Specifically, suppose we're creating a helper function named `example` that accepts an inline-parameter named `value`. We'd like to use the `tap` helper function (helper-helper-function?) to allow `example` to handle things like:

{@example value="a literal string"/}

{@example value=an_object_reference/}

{@example value="a literal string containing {an_object_reference}"/}

etc.

in a dust-like way.But while tap works as expected for

{@example value=an_object_reference/}

when `an_object_reference` resolves to a *string*, it gives an error with `an_object_reference` resolves to a JavaScript *function*.

The underlying cause for this is that the `tap` method uses `typeof input === 'function'` to identify the input values that need to be "rendered" with dust, but not every input function is a dust `body`-style function.

The change in this pull request is to check to see if `input` is a zero-argument function, in which case `tap` evaluates the function (i.e. `output = input()`) rather than trying to tap and render it as a dust template.

Unit tests for several `tap` cases are included in the patch.