Skip to content

Commit c9171a2

Browse files
committed
Update return type for rules
1 parent 5c464b3 commit c9171a2

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,23 +336,43 @@ A rule also has several components:
336336

337337
### Returning a `RuleValue`
338338

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

341+
```typescript
342+
export type RuleValue = undefined | boolean | [string?, number?]
343+
```
344+
345+
If a rule is passing/hasn't been violated:
346+
347+
```typescript
348+
return
349+
return false
350+
```
351+
352+
If a rule has been violated:
353+
354+
```typescript
355+
return true
341356
```
342-
RuleValue = [boolean, message, points]
357+
358+
Or, if you want to return a custom message:
359+
360+
```typescript
361+
return ['message']
343362
```
344363

364+
Or, if you want to return a message and points:
365+
345366
```typescript
346-
// The true/false MUST be there, it's if the rule was violated or not
347-
return [true]
367+
return ['message', points]
368+
```
348369

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

353-
// This returns that it was violated, uses the default message, but changes
354-
// the points returned to -10
355-
return [true, null, -10]
372+
```typescript
373+
return ['', points]
356374
```
357375

376+
`points` and `message` are optional - if omitted, they're pulled from the `meta` block
377+
358378
### Helper Methods

src/rules/example.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ export default class ExampleRule implements Rule {
2222
return Acars.ViolatedAfterDelay(
2323
this.meta.name,
2424
this.meta.delay_time,
25-
() => {
25+
(): RuleValue => {
2626
if (data.onGround) {
27-
return [false]
27+
return
2828
}
2929

30-
return [true, 'The example was violated!', this.meta.points]
30+
return ['The example was violated!', this.meta.points]
3131
},
3232
)
3333
}

src/types/rule.d.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,24 @@ import type { Pirep, Telemetry } from '../types'
99
* Return it as a tuple.
1010
*
1111
* If a rule is passing/hasn't been violated:
12-
* return [false]
13-
*
12+
* return false
13+
* // or:
14+
* return [false]
15+
*a
1416
* If a rule has been violated:
17+
* return true
18+
* // or, if you want to return a custom message:
19+
* return ['message']
20+
* // or, if you want to return a message and points:
21+
* return ['message', points]
1522
*
16-
* return [true, message, points ]
23+
* If you want to return just the points, you can return:
24+
* return ['', points]
1725
*
1826
* points and message are optional - if omitted, they're pulled from
1927
* the 'meta' block
2028
*/
21-
export type RuleValue = [boolean, string?, number?]
29+
export type RuleValue = undefined | null | boolean | [string?, number?]
2230

2331
/**
2432
*

0 commit comments

Comments
 (0)