-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #585 from maxwroc/BatchRenRegex
Support regex on bulk_rename
- Loading branch information
Showing
6 changed files
with
177 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { getRegexFromString, log } from "./utils"; | ||
|
||
/** | ||
* Functions to check if filter condition is met | ||
*/ | ||
const operatorHandlers: { [key in FilterOperator]: (val: string | number | undefined, expectedVal: string | number) => boolean } = { | ||
"exists": val => val !== undefined, | ||
"contains": (val, searchString) => val !== undefined && val.toString().indexOf(searchString.toString()) != -1, | ||
"=": (val, expectedVal) => val == expectedVal, | ||
">": (val, expectedVal) => Number(val) > Number(expectedVal), | ||
"<": (val, expectedVal) => Number(val) < Number(expectedVal), | ||
">=": (val, expectedVal) => Number(val) >= Number(expectedVal), | ||
"<=": (val, expectedVal) => Number(val) <= Number(expectedVal), | ||
"matches": (val, pattern) => { | ||
if (val === undefined) { | ||
return false; | ||
} | ||
|
||
pattern = pattern.toString() | ||
|
||
let exp = getRegexFromString(pattern); | ||
if (!exp && pattern.includes("*")) { | ||
exp = new RegExp("^" + pattern.replace(/\*/g, ".*") + "$"); | ||
} | ||
|
||
return exp ? exp.test(val.toString()) : val === pattern; | ||
} | ||
} | ||
|
||
/** | ||
* Filter class | ||
*/ | ||
export class Filter { | ||
|
||
/** | ||
* Whether filter is permanent. | ||
* | ||
* Permanent filters removes entities/batteries from collections permanently | ||
* instead of making them hidden. | ||
*/ | ||
get is_permanent(): boolean { | ||
return this.config.name != "state"; | ||
} | ||
|
||
constructor(private config: IFilter) { | ||
|
||
} | ||
|
||
/** | ||
* Checks whether entity meets the filter conditions. | ||
* @param entity Hass entity | ||
* @param state State override - battery state/level | ||
*/ | ||
isValid(entity: any, state?: string): boolean { | ||
const val = this.getValue(entity, state); | ||
return this.meetsExpectations(val); | ||
} | ||
|
||
/** | ||
* Gets the value to validate. | ||
* @param entity Hass entity | ||
* @param state State override - battery state/level | ||
*/ | ||
private getValue(entity: any, state?: string): string | undefined { | ||
if (!this.config.name) { | ||
log("Missing filter 'name' property"); | ||
return; | ||
} | ||
|
||
if (this.config.name.indexOf("attributes.") == 0) { | ||
return entity.attributes[this.config.name.substr(11)]; | ||
} | ||
|
||
if (this.config.name == "state" && state !== undefined) { | ||
return state; | ||
} | ||
|
||
return (<any>entity)[this.config.name]; | ||
} | ||
|
||
/** | ||
* Checks whether value meets the filter conditions. | ||
* @param val Value to validate | ||
*/ | ||
private meetsExpectations(val: string | number | undefined): boolean { | ||
|
||
let operator = this.config.operator; | ||
if (!operator) { | ||
if (this.config.value === undefined) { | ||
operator = "exists"; | ||
} | ||
else { | ||
const expectedVal = this.config.value.toString(); | ||
const regex = getRegexFromString(expectedVal); | ||
operator = expectedVal.indexOf("*") != -1 || regex ? | ||
"matches" : | ||
"="; | ||
} | ||
} | ||
|
||
const func = operatorHandlers[operator]; | ||
if (!func) { | ||
log(`Operator '${this.config.operator}' not supported. Supported operators: ${Object.keys(operatorHandlers).join(", ")}`); | ||
return false; | ||
} | ||
|
||
return func(val, this.config.value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Filter } from "../../src/filter"; | ||
import { HomeAssistantMock } from "../helpers"; | ||
|
||
describe("Filter", () => { | ||
test.each([ | ||
["Bedroom motion battery level", "*_battery_level", true], | ||
["Bedroom motion battery level", "/_battery_level$/", true], | ||
["Bedroom motion battery level", "*_battery_*", true], | ||
["Bedroom motion battery level", "*_battery_", false], | ||
["Bedroom motion", "*_battery_level", false], | ||
["Bedroom motion", "/BEDroom_motion/", false], | ||
["Bedroom motion", "/BEDroom_motion/i", true], | ||
])("returns correct validity status (matches func)", (entityName: string, filterValue: string, expectedIsVlid: boolean) => { | ||
const hassMock = new HomeAssistantMock(); | ||
|
||
const entity = hassMock.addEntity(entityName, "90"); | ||
|
||
const filter = new Filter({ name: "entity_id", value: filterValue }); | ||
const isValid = filter.isValid(entity); | ||
|
||
expect(filter.is_permanent).toBeTruthy(); | ||
expect(isValid).toBe(expectedIsVlid); | ||
}) | ||
}); |