Skip to content

Commit

Permalink
[rules] Rule Builder: Fix type defs for .if() and .then()
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 committed Aug 14, 2024
1 parent cadda4d commit 7b46659
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 37 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,8 @@ Additionally, all the above triggers have the following functions:
- `if(optionalFunction)`
- `.stateOfItem(itemName)`
- `is(state)`
- `isOn()`
- `isOff()`
- `in(state...)`
#### Rule Builder Operations
Expand Down
49 changes: 33 additions & 16 deletions src/rules/condition-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class ConditionBuilder {
/**
* Condition of an item in determining whether to process rule.
*
* @memberof ConditionBuilder
* @param {string} itemName the name of the item to assess the state
* @returns {ItemStateConditionConf} the operation config
*/
Expand All @@ -49,8 +48,7 @@ class ConditionBuilder {
}

/**
* {RuleBuilder} RuleBuilder conditions
* @memberof ConditionBuilder
* {@link RuleBuilder} RuleBuilder conditions
*/
class ConditionConf {
constructor (conditionBuilder) {
Expand All @@ -59,10 +57,9 @@ class ConditionConf {
}

/**
*
* @param {*} [fn] an optional function
* @returns ConditionBuilder
*/
* @param {*} [fn] an optional function
* @returns {operations.OperationBuilder}
*/
then (fn) {
return this.conditionBuilder._then(this, fn);
}
Expand All @@ -71,8 +68,7 @@ class ConditionConf {
/**
* Condition that wraps a function to determine whether if passes
*
* @memberof ConditionBuilder
* @extends ConditionBuilder.ConditionConf
* @extends ConditionConf
* @hideconstructor
*/
class FunctionConditionConf extends ConditionConf {
Expand Down Expand Up @@ -102,8 +98,7 @@ class FunctionConditionConf extends ConditionConf {
/**
* Condition that wraps a function to determine whether if passes
*
* @memberof ConditionBuilder
* @extends ConditionBuilder.ConditionConf
* @extends ConditionConf
* @hideconstructor
*/
class ItemStateConditionConf extends ConditionConf {
Expand All @@ -114,25 +109,47 @@ class ItemStateConditionConf extends ConditionConf {
}

/**
* Checks if item state is equal to value
* @param {*} value
* @returns {this}
*/
* Checks if Item state is equal to given value.
*
* @param {string} value
* @return {ItemStateConditionConf}
*/
is (value) {
this.values = [value];
return this;
}

/**
* Checks if the Item state is ON.
*
* @return {ItemStateConditionConf}
*/
isOn () {
this.is('ON');
return this;
}

/**
* Checks if the Item state is OFF.
*
* @return {ItemStateConditionConf}
*/
isOff () {
this.is('OFF');
return this;
}

/**
* Checks if item state matches any array of values
* @param {...any} values
* @returns {this}
* @return {ItemStateConditionConf}
*/
in (...values) {
this.values = values;
return this;
}

/** @private */
check (...args) {
const item = items.getItem(this.item_name);
if (typeof item === 'undefined' || item === null) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/trigger-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class TriggerConf {
/**
* Move to the rule condition
*
* @param {*} fn the optional function to execute
* @param {*} [fn] the optional function to execute
* @returns {conditions.ConditionBuilder}
*/
if (fn) {
Expand Down
57 changes: 41 additions & 16 deletions types/rules/condition-builder.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/**
* Condition that wraps a function to determine whether if passes
*
* @memberof ConditionBuilder
* @extends ConditionBuilder.ConditionConf
* @extends ConditionConf
* @hideconstructor
*/
export class FunctionConditionConf {
export class FunctionConditionConf extends ConditionConf {
/**
* Creates a new function condition. Don't call directly.
*
Expand All @@ -26,28 +25,41 @@ export class FunctionConditionConf {
/**
* Condition that wraps a function to determine whether if passes
*
* @memberof ConditionBuilder
* @extends ConditionBuilder.ConditionConf
* @extends ConditionConf
* @hideconstructor
*/
export class ItemStateConditionConf {
export class ItemStateConditionConf extends ConditionConf {
constructor(itemName: any, conditionBuilder: any);
/** @private */
private item_name;
/**
* Checks if item state is equal to value
* @param {*} value
* @returns {this}
*/
is(value: any): this;
values: any[];
* Checks if Item state is equal to given value.
*
* @param {string} value
* @return {ItemStateConditionConf}
*/
is(value: string): ItemStateConditionConf;
values: any[] | string[];
/**
* Checks if the Item state is ON.
*
* @return {ItemStateConditionConf}
*/
isOn(): ItemStateConditionConf;
/**
* Checks if the Item state is OFF.
*
* @return {ItemStateConditionConf}
*/
isOff(): ItemStateConditionConf;
/**
* Checks if item state matches any array of values
* @param {...any} values
* @returns {this}
* @return {ItemStateConditionConf}
*/
in(...values: any[]): this;
check(...args: any[]): boolean;
in(...values: any[]): ItemStateConditionConf;
/** @private */
private check;
}
/**
* Condition that wraps a function to determine whether if passes
Expand All @@ -72,12 +84,25 @@ export class ConditionBuilder {
/**
* Condition of an item in determining whether to process rule.
*
* @memberof ConditionBuilder
* @param {string} itemName the name of the item to assess the state
* @returns {ItemStateConditionConf} the operation config
*/
stateOfItem(itemName: string): ItemStateConditionConf;
condition: ItemStateConditionConf;
}
/**
* {@link RuleBuilder} RuleBuilder conditions
*/
declare class ConditionConf {
constructor(conditionBuilder: any);
/** @private */
private conditionBuilder;
/**
* @param {*} [fn] an optional function
* @returns {operations.OperationBuilder}
*/
then(fn?: any): operations.OperationBuilder;
}
import operations = require("./operation-builder");
export {};
//# sourceMappingURL=condition-builder.d.ts.map
2 changes: 1 addition & 1 deletion types/rules/condition-builder.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions types/rules/trigger-builder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,10 @@ declare class TriggerConf {
/**
* Move to the rule condition
*
* @param {*} fn the optional function to execute
* @param {*} [fn] the optional function to execute
* @returns {conditions.ConditionBuilder}
*/
if(fn: any): conditions.ConditionBuilder;
if(fn?: any): conditions.ConditionBuilder;
}
/**
* Time of day based trigger
Expand Down
2 changes: 1 addition & 1 deletion types/rules/trigger-builder.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7b46659

Please sign in to comment.