diff --git a/src/importer/index.ts b/src/importer/index.ts index 5874c2b..a2bf4be 100644 --- a/src/importer/index.ts +++ b/src/importer/index.ts @@ -3,6 +3,14 @@ import { adapters } from "../adapters"; import { DummyOpenHAB, OpenHAB } from "../openhab"; +const createFilter = (filter?: string) => { + if(!filter) { + return (_: Thing) => true; + } + + return new Function("$", `const v = ${filter}; if(typeof v !== 'boolean') throw new Error("Filter should be of boolean type"); return v;`) as ((_: Thing) => true); +}; + export const importThings = async (config: Config, options: ImportOptions) => { const OH = options.dryRun ? DummyOpenHAB : OpenHAB; const openhab = new OH(config.openHAB); @@ -24,9 +32,7 @@ export const importThings = async (config: Config, options: ImportOptions) => { const existingThings = await openhab.getThings(); const existingThingsUIDs = existingThings.map(t => t.UID); - const filter: (thing: Thing) => boolean = options.where - ? (new Function("$", `return ${options.where}`)) as ((_: Thing) => true) - : (_: Thing) => true; + const filter = createFilter(options.where); const thingsToImport = things .filter(t => !existingThingsUIDs.includes(t.UID)) @@ -39,4 +45,4 @@ export const importThings = async (config: Config, options: ImportOptions) => { await openhab.createThings(...thingsToImport); return thingsToImport; -} \ No newline at end of file +}