Skip to content

Commit

Permalink
Use ts for rhai codeblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziosestito committed Jan 16, 2023
1 parent 5fe0ecc commit b626af3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
26 changes: 13 additions & 13 deletions guides/expression_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Comparing two values of _different_ data types defaults to `false`.

The exception is `!=` (not equals) which defaults to `true`. This is in line with intuition.

```rust
```ts
42 > "42"; // false: i64 cannot be compared with string
42 <= "42"; // false: i64 cannot be compared with string
ts == 42; // false: different types cannot be compared
Expand All @@ -57,7 +57,7 @@ if the first one already proves the condition wrong.

Single boolean operators `&` and `|` always evaluate both operands.

```rust
```ts
a() || b(); // b() is not evaluated if a() is true
a() && b(); // b() is not evaluated if a() is false
a() | b(); // both a() and b() are evaluated
Expand All @@ -68,7 +68,7 @@ a() & b(); // both a() and b() are evaluated

`if` statements follow C syntax.

```rust
```ts
if foo(x) {
print("It's true!");
} else if bar == baz {
Expand All @@ -87,7 +87,7 @@ if foo(x) {
> one statement inside the branch.
> Like Rust, there is no ambiguity regarding which `if` clause a branch belongs to.
>
> ```rust
> ```ts
> // not C!
> if (decision) print(42);
> // ^ syntax error, expecting '{'
Expand All @@ -98,7 +98,7 @@ if foo(x) {
`if` statements can also be used as _expressions_, replacing the `? :` conditional
operators in other C-like languages.
```rust
```ts
// The following is equivalent to C: int x = 1 + (decision ? 42 : 123) / 2;
let x = 1 + if decision { 42 } else { 123 } / 2;
x == 22;
Expand All @@ -117,7 +117,7 @@ Array literals are built within square brackets `[` ... `]` and separated by com
>
> `[` _value_`,` _value_`,` ... `,` _value_ `,` `]` `// trailing comma is OK`
```rust
```ts
let some_list = [1, 2, 3];

let another_list = ["foo", "bar", 42];
Expand All @@ -129,7 +129,7 @@ Like C, arrays are accessed with zero-based, non-negative integer indices:

> _array_ `[` _index position from 0 to length−1_ `]`
```rust
```ts
let some_list = ["foo", "bar", 42];

let second_element = some_list[1];
Expand All @@ -144,7 +144,7 @@ _last_ element.

> _array_ `[` _index position from −1 to −length_ `]`
```rust
```ts
let some_list = ["foo", "bar", 42];

let second_element = some_list[-2];
Expand All @@ -163,7 +163,7 @@ let last_element = some_list[-1];

Examples

```rust
```ts
let some_list = [1, 2, 3, 4, "foo", "bar"];

let foo = some_list.get(4); // "foo"
Expand Down Expand Up @@ -191,7 +191,7 @@ commas `,`:
>
> `#{` _property_ `:` _value_`,` ... `,` _property_ `:` _value_ `,` `}` `// trailing comma is OK`
```rust
```ts
let some_map = #{ // map literal with 2 properties
foo: 42,
bar: "hello",
Expand All @@ -204,7 +204,7 @@ The _dot notation_ allows to access properties by name.

> _object_ `.` _property_
```rust
```ts
let some_map = #{ // map literal with 2 properties
foo: 42,
bar: "hello",
Expand All @@ -219,7 +219,7 @@ some_map.bar // "hello"

Trying to read a non-existing property returns an error.

```rust
```ts
let some_map = #{ // map literal with 2 properties
foo: 42,
bar: "hello",
Expand All @@ -230,7 +230,7 @@ some_map.another_property // returns "Property not found: another_property

### A more complex example

```rust
```ts
let some_map = #{ // map literal with 2 properties
foo: 42,
bar: "hello",
Expand Down
16 changes: 8 additions & 8 deletions guides/rhai_expressions_cheat_sheet.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ In this cheatsheet are grouped the most frequent manipulations that can be done

#### Filtering an array

```rust
```ts
[1, 2, 3].filter(|value| value % 2 == 0)
=> [2]
```

#### Finding the index of an element inside an array

```rust
```ts
["wow", "check"].index_of(|value| value == "check")
=> 1
```

#### Finding an element inside an array

```rust
```ts
let nodelist = [
#{resource_id: 5, name: "foo"},
#{resource_id: 12, name: "bar"}
Expand All @@ -35,7 +35,7 @@ nodelist.find(|value| value.resource_id == 5)

#### Checking if an expression is true for every element of an array

```rust
```ts
[2, 4, 6].all(|value| value % 2 == 0)
=> true
```
Expand All @@ -44,21 +44,21 @@ nodelist.find(|value| value.resource_id == 5)

#### Get only keys (returns an array)

```rust
```ts
#{a: 1, b: 2}.keys()
=> ["a", "b"]
```

#### Get only values (returns an array)

```rust
```ts
#{a: 1, b: 2}.values()
=> [1, 2]
```

#### Check if a map has more than 5 keys which name starts with "ring"

```rust
```ts
let map = #{
ring_one: 12,
ring_two: 34,
Expand All @@ -75,7 +75,7 @@ map.keys().filter(|prop| prop.starts_with("ring")).len() >= 5

#### Splitting a string

```rust
```ts
"a;b;c".split(";")
=> ["a", "b", "c"]
```

0 comments on commit b626af3

Please sign in to comment.