Skip to content

Commit

Permalink
Demonstrate how to use IIFEs to write complex expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
SydneyUni-Jim committed Jan 15, 2025
1 parent 9c73469 commit f825b9c
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions docs/code/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,18 @@ This expression:

### Example: Writing longer JavaScript

An expression contains one line of JavaScript. This means you cannot do things like variable assignments or multiple standalone operations.
You can do things like variable assignments or multiple statements in an expression, but you need to wrap your code using the syntax for an IIFE (immediately invoked function expression).

Check warning on line 82 in docs/code/expressions.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [from-write-good.Weasel] 'immediately' is a weasel word! Raw Output: {"message": "[from-write-good.Weasel] 'immediately' is a weasel word!", "location": {"path": "docs/code/expressions.md", "range": {"start": {"line": 82, "column": 147}}}, "severity": "WARNING"}

To understand the limitations of JavaScript in expressions, and start thinking about workarounds, look at the following two pieces of code. Both code examples use the Luxon date and time library to find the time between two dates in months, and encloses the code in handlebar brackets, like an expression.

However, the first example isn't a valid n8n expression:


```js
// This example is split over multiple lines for readability
// It's still invalid when formatted as a single line
{{
function example() {
let end = DateTime.fromISO('2017-03-13');
let start = DateTime.fromISO('2017-02-13');
let diffInMonths = end.diff(start, 'months');
return diffInMonths.toObject();
}
example();
}}
```


While the second example is valid:
The following code use the Luxon date and time library to find the time between two dates in months. It is included in both the handlebar brackets and the IIFE syntax.

Check failure on line 84 in docs/code/expressions.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [from-microsoft.Contractions] Use 'it's' instead of 'It is'. Raw Output: {"message": "[from-microsoft.Contractions] Use 'it's' instead of 'It is'.", "location": {"path": "docs/code/expressions.md", "range": {"start": {"line": 84, "column": 102}}}, "severity": "ERROR"}

Check warning on line 84 in docs/code/expressions.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [from-write-good.TooWordy] 'It is' is too wordy. Raw Output: {"message": "[from-write-good.TooWordy] 'It is' is too wordy.", "location": {"path": "docs/code/expressions.md", "range": {"start": {"line": 84, "column": 102}}}, "severity": "WARNING"}

Check warning on line 84 in docs/code/expressions.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [from-write-good.Passive] 'is included' may be passive voice. Use active voice if you can. Raw Output: {"message": "[from-write-good.Passive] 'is included' may be passive voice. Use active voice if you can.", "location": {"path": "docs/code/expressions.md", "range": {"start": {"line": 84, "column": 105}}}, "severity": "WARNING"}


```js
{{DateTime.fromISO('2017-03-13').diff(DateTime.fromISO('2017-02-13'), 'months').toObject()}}
{{(()=>{
let end = DateTime.fromISO('2017-03-13');
let start = DateTime.fromISO('2017-02-13');
let diffInMonths = end.diff(start, 'months');
return diffInMonths.toObject();
})()}}
```

## Common issues
Expand Down

0 comments on commit f825b9c

Please sign in to comment.