From c9171a29d66026917ae6707ac554f419240422ac Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 24 Sep 2024 13:42:58 -0500 Subject: [PATCH] Update return type for rules --- README.md | 40 ++++++++++++++++++++++++++++++---------- src/rules/example.ts | 6 +++--- src/types/rule.d.ts | 16 ++++++++++++---- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 08c037b..2255559 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/rules/example.ts b/src/rules/example.ts index 73f04c3..50e5077 100644 --- a/src/rules/example.ts +++ b/src/rules/example.ts @@ -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] }, ) } diff --git a/src/types/rule.d.ts b/src/types/rule.d.ts index 8469bd2..c4cec9d 100644 --- a/src/types/rule.d.ts +++ b/src/types/rule.d.ts @@ -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?] /** *