Skip to content

Commit

Permalink
feat: Make lookup commands to TuYa devices case insensitive. Koenkk/z…
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jun 1, 2023
1 parent 2842729 commit 1b99bf2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/lib/tuya.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,7 @@ export class Bitmap extends Base {
export const valueConverterBasic = {
lookup: (map: {[s: (string)]: number | boolean | Enum | string}) => {
return {
to: (v: string) => {
if (map[v] === undefined) throw new Error(`Value '${v}' is not allowed, expected one of ${Object.keys(map)}`);
return map[v];
},
to: (v: string) => getFromLookup(v, map),
from: (v: number) => {
const value = Object.entries(map).find((i) => i[1].valueOf() === v);
if (!value) throw new Error(`Value '${v}' is not allowed, expected one of ${Object.values(map)}`);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function assertNumber(value: unknown, property: string): asserts value is

export function getFromLookup<V>(value: unknown, lookup: {[s: string | number]: V}): V {
assertString(value, `Expected string got: '${value}' (${typeof(value)})`);
const result = lookup[value.toLowerCase()];
const result = lookup[value.toLowerCase()] ?? lookup[value.toUpperCase()];
if (!result) throw new Error(`Expected one of: ${Object.keys(lookup).join(', ')}, got: '${value}'`);
return result;
}

0 comments on commit 1b99bf2

Please sign in to comment.