Skip to content

Commit 6eaa1a3

Browse files
committed
Improves error messages.
1 parent 710f47d commit 6eaa1a3

File tree

10 files changed

+26
-22
lines changed

10 files changed

+26
-22
lines changed

src/core/Capability.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { Capability as ICapability, Property as IProperty } from '../types';
22

3-
import { bytesToChunks, bytesToInt, hexToUint8Array, isEmptyObject, isObject } from '../utils';
3+
import {
4+
bytesToChunks,
5+
bytesToInt,
6+
getArray,
7+
hexToUint8Array,
8+
isEmptyObject,
9+
isObject,
10+
} from '../utils';
411

512
import { InvalidCommandError, JSONError } from './Error';
613
import { NamedEntity } from './NamedEntity';
@@ -95,9 +102,7 @@ export abstract class Capability extends Serializable implements NamedEntity {
95102
}
96103

97104
for (const [name, components] of Object.entries(payload)) {
98-
const componentsArray = (Array.isArray(components) ? components : [components]).filter(
99-
(value) => value !== null,
100-
);
105+
const componentsArray = getArray(components).filter((value) => value !== null);
101106

102107
if (componentsArray.length) {
103108
for (const components of componentsArray) {
@@ -119,21 +124,17 @@ export abstract class Capability extends Serializable implements NamedEntity {
119124
}
120125

121126
public getProperty(name: string): Property | undefined {
122-
const property = this.properties[name];
123-
return Array.isArray(property) ? property[0] : property;
127+
const [property] = getArray(this.properties[name]);
128+
return property;
124129
}
125130

126131
public getProperties(name: string) {
127-
const property = this.properties[name] || [];
128-
return Array.isArray(property) ? property : [property];
132+
return getArray(this.properties[name] || []);
129133
}
130134

131135
public getPropertiesArray() {
132136
return Object.values(this.properties).reduce<Property[]>(
133-
(allProperties, properties) => [
134-
...allProperties,
135-
...(Array.isArray(properties) ? properties : [properties]),
136-
],
137+
(allProperties, properties) => [...allProperties, ...getArray(properties)],
137138
[],
138139
);
139140
}
@@ -185,9 +186,7 @@ export abstract class Capability extends Serializable implements NamedEntity {
185186

186187
const currentValue = this.properties[name];
187188
if (property.multiple && currentValue) {
188-
(this.properties[name] = Array.isArray(currentValue) ? currentValue : [currentValue]).push(
189-
property,
190-
);
189+
(this.properties[name] = getArray(currentValue)).push(property);
191190
} else {
192191
this.properties[name] = property;
193192
}

src/core/Command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class Command {
2626

2727
const type = CommandType[capitalize(name) as keyof typeof CommandType];
2828
if (type === undefined) {
29-
throw new Error(`Unknown command type: ${name}`);
29+
throw new Error(`Unknown command type: ${name}.`);
3030
}
3131

3232
return new Command(

src/factories/CapabilityFactory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ClassList } from '../capabilities/classes';
2+
23
export class CapabilityFactory {
34
public static createFromIdentifier(msb: number, lsb: number) {
45
const Capability = ClassList.find(

src/utils/misc.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { isFinite, isPlainObject } from 'lodash';
22

3+
export function getArray<T>(value: T | T[]): T[] {
4+
return Array.isArray(value) ? value : [value];
5+
}
6+
37
export function isArrayOfNumbers(value: unknown): value is number[] {
48
return Array.isArray(value) && value.every(isInteger);
59
}

src/values/BytesValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class BytesValue extends Value<number[]> {
1919
if (value && isArrayOfNumbers(value)) {
2020
this.setValue(value);
2121
} else {
22-
throw new JSONError('Value must be array of numbers.');
22+
throw new JSONError('Value must be an array of numbers.');
2323
}
2424

2525
return this;

src/values/FloatValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class FloatValue extends Value<number> {
2323
if (isNumber(value)) {
2424
this.setValue(value);
2525
} else {
26-
throw new JSONError('Value must be number.');
26+
throw new JSONError('Value must be a number.');
2727
}
2828

2929
return this;

src/values/IntegerValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class IntegerValue extends Value<number> {
2323
if (isInteger(value)) {
2424
this.setValue(value);
2525
} else {
26-
throw new JSONError('Value must be integer.');
26+
throw new JSONError('Value must be an integer.');
2727
}
2828

2929
return this;

src/values/StringValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class StringValue extends Value<string> {
2121
if (isString(value)) {
2222
this.setValue(value);
2323
} else {
24-
throw new JSONError('Value must be string.');
24+
throw new JSONError('Value must be a string.');
2525
}
2626

2727
return this;

src/values/TimestampValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class TimestampValue extends Value<Date> {
2626
if (isString(value)) {
2727
this.setValue(new Date(value));
2828
} else {
29-
throw new JSONError('Value must be string in ISO date-time format.');
29+
throw new JSONError('Value must be a string in ISO date-time format.');
3030
}
3131

3232
return this;

src/values/UnitValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class UnitValue extends Value<UnitValueData, UnitValueDataSetter> impleme
5757
if (isObject(value) && isNumber(value.value)) {
5858
this.setValue(value as UnitValueDataSetter);
5959
} else {
60-
throw new Error('Value must be number.');
60+
throw new Error('Value must be a number.');
6161
}
6262
} catch (e) {
6363
throw new JSONError(e);

0 commit comments

Comments
 (0)