Comparing version 1.1.0 to 1.2.0
@@ -23,3 +23,2 @@ import { ExternalTokenizer, LRParser } from '@lezer/lr'; | ||
const CHAR_TABLE = { | ||
@@ -26,0 +25,0 @@ '{': 123, |
@@ -205,3 +205,3 @@ 'use strict'; | ||
try { | ||
return feelin.evaluate(feel, context); | ||
return feelin.evaluate(`string(${feel})`, context); | ||
} | ||
@@ -218,7 +218,9 @@ catch { | ||
case 'FeelBlock': { | ||
const feel = node.content; | ||
try { | ||
return feelin.evaluate(node.content, context); | ||
return feelin.evaluate(`string(${feel})`, context); | ||
} | ||
catch { | ||
return errorHandler(new Error(`FEEL expression ${node.content} couldn't be evaluated`)); | ||
catch (e) { | ||
return errorHandler(new Error(`FEEL expression ${feel} couldn't be evaluated`)); | ||
} | ||
@@ -225,0 +227,0 @@ } |
{ | ||
"name": "feelers", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "FEELers grammar and editor for the Lezer parser system.", | ||
@@ -39,3 +39,3 @@ "main": "dist/index.js", | ||
"@bpmn-io/cm-theme": "^0.1.0-alpha.2", | ||
"@bpmn-io/feel-lint": "^1.0.0", | ||
"@bpmn-io/feel-lint": "^1.1.0", | ||
"@codemirror/autocomplete": "^6.10.1", | ||
@@ -51,3 +51,3 @@ "@codemirror/commands": "^6.3.0", | ||
"@lezer/markdown": "^1.1.0", | ||
"feelin": "^1.2.0", | ||
"feelin": "^2.3.0", | ||
"lezer-feel": "^1.2.0", | ||
@@ -54,0 +54,0 @@ "min-dom": "^4.1.0" |
115
README.md
@@ -21,7 +21,118 @@ # feelers | ||
## Usage | ||
... | ||
Feelers is a string templating tool, and will return string text or error. | ||
```js | ||
import { evaluate } from 'feelers'` | ||
``` | ||
A simple string will always be returned as-is. | ||
```js | ||
evaluate("My simple string"); | ||
// "My simple string" | ||
``` | ||
If your string is prefixed with an =, it will be evaluated as a single FEEL expression wrapped in a string conversion function. | ||
```js | ||
const context = { secondNumber: 12 }; | ||
evaluate("= 2 + secondNumber", context); | ||
// "14" | ||
``` | ||
Finally, if your string features feelers language features, the templating engine takes over. | ||
```js | ||
const context = { user: "Dave" }; | ||
evaluate("I'm sorry {{user}}, I'm afraid I can't do that.", context); | ||
// I'm sorry Dave, I'm afraid I can't do that. | ||
``` | ||
## Feelers templating language features | ||
... | ||
### Inserts | ||
The simplest feature of feelers is inserting FEEL evaluations within your text. You may provide a variable context for the underlying FEEL engine to reference. Within these scopes, you have access to all features of the FEEL engine. | ||
```js | ||
const context = { user: "Dave", age: 24, hobbies: [ "surfing", "coding" ] }; | ||
evaluate(`Hey there {{user}}, welcome. {{if age >= 18 then "Have a beer!" else "Here's some apple juice."}}`, context); | ||
// Hey there Dave. Have a beer! | ||
evaluate(`Hobbies: {{hobbies}}`, context); | ||
// Hobbies: ["surfing", "coding"] | ||
evaluate(`{{user}}-{{user}}-{{user}}`, context); | ||
// Dave-Dave-Dave | ||
``` | ||
### Conditional sections | ||
To simply display a section of the template, you may use a conditional section. While this can already be achieved via feel itself using `if then else`, this syntax is a lot easier to manage for large sections. | ||
```js | ||
const conditionExample = `{{#if count(users) > 1}}There are multiple users{{/if}} | ||
{{#if false}}This should not display{{/if}} | ||
{{#if true}}This should display{{/if}}`; | ||
const context = { users: [ "Bob", "Dave" ] }; | ||
evaluate(conditionExample, context); | ||
// There are multiple users | ||
// This should display | ||
``` | ||
### Loops | ||
To handle dealing with arrays of data graciously, you can make use of loop tags. A special variable `this` is create granting you access to the current loop's element. | ||
```js | ||
const context = { user: "Dave", age: 24, hobbies: [ "surfing", "coding" ] }; | ||
const hobbyExpression = `{{user}}'s hobbies are: | ||
{{#loop hobbies}} | ||
- {{this}} | ||
{{/loop}}`; | ||
evaluate(hobbyExpression, context); | ||
/// Dave's hobbies are: | ||
/// - surfing | ||
/// - coding | ||
``` | ||
Loops may be nested when dealing with more complex data. When `this` is an object, you may access its variables directly within the loop. Although `this.name` would also work in the below example | ||
```js | ||
const context = { | ||
users: [ | ||
{ | ||
name: "Bob", | ||
hobbies: [ "building", "wearing hardhats" ] | ||
}, | ||
{ | ||
name: "Dave", | ||
hobbies: [ "surfing", "coding" ] | ||
} | ||
] | ||
} | ||
const complexLoops = `{{#loop users}} | ||
{{name}}'s hobbies: | ||
{{#loop hobbies}} | ||
- {{this}} | ||
{{/loop}} | ||
{{/loop}} | ||
` | ||
evaluate(complexLoops, context); | ||
// Bobs's hobbies: | ||
// - building | ||
// - wearing hardhats | ||
// Dave's hobbies: | ||
// - surfing | ||
// - coding | ||
``` | ||
> Loops actually create 4 helper variables: `this`, `parent`, `_this_` and `_parent_`. Parent refers to the context just outside of your loop, in case you need to refer to it. The underscored variants are fallbacks in case your context include variables named `this` and `parent`. | ||
## Build and run | ||
@@ -28,0 +139,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63668
1425
177
+ Addedfeelin@2.3.0(transitive)
- Removedfeelin@1.2.0(transitive)
Updated@bpmn-io/feel-lint@^1.1.0
Updatedfeelin@^2.3.0