Skip to content

Commit

Permalink
Update return type for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Sep 24, 2024
1 parent 5c464b3 commit c9171a2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
40 changes: 30 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,43 @@ A rule also has several components:

### Returning a `RuleValue`

The return value is an tuple, with one mandatory value, and 2 optional values:
The return value has multiple possible values, sending on

```typescript
export type RuleValue = undefined | boolean | [string?, number?]
```
If a rule is passing/hasn't been violated:
```typescript
return
return false
```

If a rule has been violated:

```typescript
return true
```
RuleValue = [boolean, message, points]

Or, if you want to return a custom message:

```typescript
return ['message']
```

Or, if you want to return a message and points:

```typescript
// The true/false MUST be there, it's if the rule was violated or not
return [true]
return ['message', points]
```

// Return a message along with the state. If not set or null, it will
// use `this.meta.message` as the default
return [true, message]
If you want to return just the points, you can return:

// This returns that it was violated, uses the default message, but changes
// the points returned to -10
return [true, null, -10]
```typescript
return ['', points]
```

`points` and `message` are optional - if omitted, they're pulled from the `meta` block

### Helper Methods
6 changes: 3 additions & 3 deletions src/rules/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export default class ExampleRule implements Rule {
return Acars.ViolatedAfterDelay(
this.meta.name,
this.meta.delay_time,
() => {
(): RuleValue => {
if (data.onGround) {
return [false]
return
}

return [true, 'The example was violated!', this.meta.points]
return ['The example was violated!', this.meta.points]
},
)
}
Expand Down
16 changes: 12 additions & 4 deletions src/types/rule.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ import type { Pirep, Telemetry } from '../types'
* Return it as a tuple.
*
* If a rule is passing/hasn't been violated:
* return [false]
*
* return false
* // or:
* return [false]
*a
* If a rule has been violated:
* return true
* // or, if you want to return a custom message:
* return ['message']
* // or, if you want to return a message and points:
* return ['message', points]
*
* return [true, message, points ]
* If you want to return just the points, you can return:
* return ['', points]
*
* points and message are optional - if omitted, they're pulled from
* the 'meta' block
*/
export type RuleValue = [boolean, string?, number?]
export type RuleValue = undefined | null | boolean | [string?, number?]

/**
*
Expand Down

0 comments on commit c9171a2

Please sign in to comment.