Injectors
Injectors are operations that inject values. They can be used
with any of the compose-*
functions wherever an Operation
object is required:
They are the necessary leaf nodes in the operation tree:
Here is how the above example works. Check the HTML page to see where the injectors are getting their values.
The second panel shows the JS/JSON object generated from the nested
operation/injector constructors. This object is then passed to the composeOperators
function, which returns a function that takes an optional arg
(used by FromArgument
) and returns the result of the
calculation.
Here we apply it immediately, passing it the value, 5. The result of the calculation can be seen in the console below.
Types of Injectors
-
Constant
-
The
Constant
constructor is a curried function used to generate anoperation
configuration that injects a known value, which is included in the configuration object. - The first parameter is the datatype to be returned, which defaults to "Number". The second parameter is the constant value to be returned.
-
FromArgument
-
The
FromArgument
constructor is the simplest of the injector constructors. It creates an operation configuration that tells the operation to use the optional argument passed to the function returned fromcomposeOperators
. - It takes a single parameter, the datatype to be returned, which defaults to "Number".
-
FromElement
-
The
FromElement
injector is used to inject values from HTML elements on the page, such as form inputs. As with all the operation constructors, it is a curried function. - The first parameter is the datatype to be returned, which defaults to "Number".
-
The second parameter,
source
, is an object from which a CSS selector is created. Keys includeform
(the ID of the form),id
(the ID of the element),name
(of the form input),tagName
(of the element), andselector
, which sets a selector directly and overrides all other keys. -
The selector is used to obtain the HTML element (using
document.querySelector
). Depending on the type of element returned, the value is extracted frominnerHTML
or thevalue
attribute. -
FromLocalStorage
-
The
FromLocalStorage
injector is used to retrieve values from the browser's local storage. - Values in local storage are stored as strings. The first parameter is the datatype to which the value retrieved will be cast. It defaults to "Number".
- The second parameter is the key of the value stored in local storage.
-
FromLookup
-
The
FromLookup
injector is used to lookup values stored as<data>
elements on the page, although hidden inputs could also be used if the data is to be included in a form submission. -
The first parameter is the datatype to be returned, which defaults to
"Json". In this instance,
JSON.parse
will be used on the returned value. But "Number", "Integer", "String", etc. are also options, in which case the return value will be cast accordingly. -
The second parameter is the
id
of the<data>
or<input type="hidden">
element. -
FromLookupTable
-
The
FromLookupTable
injector is used to lookup values stored as tables (multi-dimensional arrays) JSON "stringified" in<data>
elements on the page, although hidden inputs could also be used if the data is to be included in a form submission. -
The first parameter is the datatype to be returned, which defaults to
"Json". In this instance,
JSON.parse
will be used on the returned value. But "Number", "Integer", "String", etc. are also options, in which case the return value will be cast accordingly. -
The second parameter is a configuration object which determines how the
data is to be extracted from the table. This must contain a
source
key whose value is a source configuration object as described inFromElement
above. -
The
source
configuration must also includecolumn
andtest
keys whose values are operations themselves. In other words, the column of the table (one-based) from which to get the value is determined by an operation, the simplest of which would beConstant("Integer")(2)
, the second column. But this could also use aTernary
operation or even nested ternaries. -
The
test
key is used to determine the row in which the desired value is located. Most often, this will use aFromArgument
operation, with the current value to be looked up passed in as an argument at runtime. -
FromQueryString
-
The
FromQueryString
injector is used to retrieve values from the query (search) string in the page URL, e.g.,sitebender.org?age=42
. -
Values in the query/search string are returned as strings. The first
parameter to
FromQueryString
is the datatype to which the value retrieved will be cast. It defaults to "Number". - The second parameter is the key of the value to be found in the query string.
-
FromSessionStorage
-
The
FromSessionStorage
injector is used to retrieve values from the browser's session storage. - Values in session storage are stored as strings. The first parameter is the datatype to which the value retrieved will be cast. It defaults to "Number".
- The second parameter is the key of the value stored in session storage.
-
FromUrlParameter
-
The
FromUrlParameter
injector is used to retrieve values from the path in the page URL, e.g.,sitebender.org/path/to/page
. -
Values in the path are returned as strings. The first parameter to
FromUrlParameter
is the datatype to which the value retrieved will be cast. It defaults to "Number". -
The second parameter is the number of the path segment where the value is
to be found. These are zero-based. For example, if the segment is
1
and the URL issitebender.org/path/to/page
, thenFromUrlParameter("String")(1)
will create an operation that, when composed and called, will returnlbrace; right: "to" }
.