Code Appendix
Worked code snippets for the places in Informer where you can write a little JavaScript: Calculated Fields, Power Script Flow Steps, and Web Query response parsers. Use them as starting points and adapt the Field aliases to your data.
Where code runs in Informer
| Context | What you write | Key variables |
|---|---|---|
| Calculated Fields | A JavaScript expression that produces a Field's value, one row at a time. The value of its last line becomes the Field's value. | $record['alias'] for the current row's Fields (read only); moment() for dates; _ for lodash. |
| Power Script Flow Steps | JavaScript that runs once per record as part of a Query's flow, and can change the record in place. | $record for the current row; $local to carry values across rows. |
| Web Query response parsers | JavaScript that turns a web service's response into records. | response for the payload; push() to emit rows; next() to send another request. |
The script sandbox exposes a consistent set of globals, including $record (the current row), $local (a place to keep state across rows), $field/$inputs, and saved functions, so the same patterns carry from one context to the next. See Reserved Power Script variables for the full list.
A quick example
Formatting a date Field with Moment.js in a Calculated Field:
var date = $record['fieldAliasGoesHere'];
var formattedDate = moment(date).format('MM-DD-YYYY');
formattedDate;
In this section
| Chapter | What's in it |
|---|---|
| JavaScript Examples - Strings | Take parts of a string, add or strip characters, pad, and split. |
| JavaScript Examples - Dates | Compare, format, add to or subtract from, and take apart dates. |
| JavaScript Examples - Arrays | Append, remove, sort, and join, plus splitting and pivoting multi-valued Fields into columns. |
| Power Script Examples | Grand totals and per-group totals with $local and Flush, and comparing two dates. |
| Web Query Custom Response Parsers | Follow-up requests with next(), pagination, token retrieval, and unioning Datasets through the REST API. |
Where do I look when...
| You want to... | Start here |
|---|---|
| Show only part of a text value | How to display parts of a string |
| Format a phone number, or pad an ID with leading zeros | How to manipulate a string |
| Show a date as text in a set pattern | How to format a date |
| Calculate a due date or an expiration date | How to add or subtract from a date |
| Flag records where two dates differ | How to compare two dates in a Power Script |
| Spread a multi-valued Field across columns | How to split a multi-valued field into separate columns |
| Add a grand total, or a total per group, to every row | How to calculate a sum total aggregate via a Power Script, then How to aggregate values on specific groups of data |
| Load every page from an API that pages its results | Handling Pagination in Web Queries |
| Call an API that needs a short-lived access token | Automating Token Retrieval in Web Queries |
| Combine several Datasets into one | Unioning Two Informer Datasets Using a Web Query and the Informer REST API |