Skip to content

Commit

Permalink
Boolean values for numeric formats are now properly converted
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Oct 8, 2020
1 parent 074ede1 commit df0761d
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/lib/Characteristic.ts
Expand Up @@ -907,15 +907,19 @@ export class Characteristic extends EventEmitter {
}
break;
case Formats.INT: // 32-bit signed int
if (typeof value !== "number") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value !== "number") {
return false;
}

numericMin = maxWithUndefined(this.props.minValue, -2147483648);
numericMax = minWithUndefined(this.props.maxValue, 2147483647);
break;
case Formats.FLOAT:
if (typeof value !== "number") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value !== "number") {
return false;
}

Expand All @@ -927,31 +931,39 @@ export class Characteristic extends EventEmitter {
}
break;
case Formats.UINT8:
if (typeof value !== "number") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value !== "number") {
return false;
}

numericMin = maxWithUndefined(this.props.minValue, 0);
numericMax = minWithUndefined(this.props.maxValue, 255);
break;
case Formats.UINT16:
if (typeof value !== "number") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value !== "number") {
return false;
}

numericMin = maxWithUndefined(this.props.minValue, 0);
numericMax = minWithUndefined(this.props.maxValue, 65535);
break;
case Formats.UINT32:
if (typeof value !== "number") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value !== "number") {
return false;
}

numericMin = maxWithUndefined(this.props.minValue, 0);
numericMax = minWithUndefined(this.props.maxValue, 4294967295);
break;
case Formats.UINT64:
if (typeof value !== "number") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value !== "number") {
return false;
}

Expand Down Expand Up @@ -1044,7 +1056,9 @@ export class Characteristic extends EventEmitter {
}
}
case Formats.INT: {
if (typeof value === "string") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value === "string") {
console.warn(`[${this.displayName}] characteristic was supplied illegal value: string instead of number. Supplying illegal values will throw errors in the future!`);
value = parseInt(value, 10);
} else if (typeof value !== "number") {
Expand All @@ -1056,7 +1070,9 @@ export class Characteristic extends EventEmitter {
break;
}
case Formats.FLOAT: {
if (typeof value === "string") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value === "string") {
console.warn(`[${this.displayName}] characteristic was supplied illegal value: string instead of float. Supplying illegal values will throw errors in the future!`);
value = parseFloat(value);
} else if (typeof value !== "number") {
Expand All @@ -1072,7 +1088,9 @@ export class Characteristic extends EventEmitter {
break;
}
case Formats.UINT8: {
if (typeof value === "string") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value === "string") {
console.warn(`[${this.displayName}] characteristic was supplied illegal value: string instead of number. Supplying illegal values will throw errors in the future!`);
value = parseInt(value, 10);
} else if (typeof value !== "number") {
Expand All @@ -1084,7 +1102,9 @@ export class Characteristic extends EventEmitter {
break;
}
case Formats.UINT16: {
if (typeof value === "string") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value === "string") {
console.warn(`[${this.displayName}] characteristic was supplied illegal value: string instead of number. Supplying illegal values will throw errors in the future!`);
value = parseInt(value, 10);
} else if (typeof value !== "number") {
Expand All @@ -1096,7 +1116,9 @@ export class Characteristic extends EventEmitter {
break;
}
case Formats.UINT32: {
if (typeof value === "string") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value === "string") {
console.warn(`[${this.displayName}] characteristic was supplied illegal value: string instead of number. Supplying illegal values will throw errors in the future!`);
value = parseInt(value, 10);
} else if (typeof value !== "number") {
Expand All @@ -1108,7 +1130,9 @@ export class Characteristic extends EventEmitter {
break;
}
case Formats.UINT64: {
if (typeof value === "string") {
if (typeof value === "boolean") {
value = value? 1: 0;
} if (typeof value === "string") {
console.warn(`[${this.displayName}] characteristic was supplied illegal value: string instead of number. Supplying illegal values will throw errors in the future!`);
value = parseInt(value, 10);
} else if (typeof value !== "number") {
Expand Down

0 comments on commit df0761d

Please sign in to comment.