Skip to content

Commit 1f70b4f

Browse files
committed
Unifies value constructors and implements NamedEntity for all values
1 parent b455086 commit 1f70b4f

File tree

12 files changed

+79
-24
lines changed

12 files changed

+79
-24
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@highmobility/auto-api-javascript",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Auto API for JavaScript - the parsing library for the Auto API vehicle data model",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/factories/ValueFactory.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const ValueConstructors: Partial<Record<TypeDefinitionType | string, ValueConstr
4343

4444
export class ValueFactory {
4545
public static createFromDefinition(definition: TypeDefinition): InstanceType<ValueConstructor> {
46-
const { customType, event, size, type, unitType } = definition;
46+
const { customType, event, type, unitType } = definition;
4747

4848
if (customType || event) {
4949
return ValueFactory.createFromDefinition(Configuration.getTypeDefinitionFromRef(definition));
@@ -58,16 +58,14 @@ export class ValueFactory {
5858
switch (ValueConstructor) {
5959
case CustomValue:
6060
case EnumValue:
61-
return new ValueConstructor(definition);
6261
case DoubleValue:
6362
case FloatValue:
6463
case IntegerValue:
6564
case UintValue:
66-
return new (ValueConstructor as NumericValueConstructor)(size);
6765
case BytesValue:
6866
case StringValue:
6967
case TimestampValue:
70-
return new ValueConstructor();
68+
return new ValueConstructor(definition);
7169
default:
7270
throw new Error(`Unknown value type: ${type}`);
7371
}

src/utils/encoding.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const base64ToByteArray = (base64String: string) =>
2424
(atob(base64String) || '').split('').map((c) => c.charCodeAt(0));
2525

2626
export const byteArrayToBase64 = (byteArray: number[]) =>
27-
btoa(String.fromCharCode.apply(null, byteArray));
27+
btoa(String.fromCharCode.apply(null, byteArray)) || '';
2828

2929
export function bytesToChunk(
3030
bytes: number[],

src/values/BytesValue.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import { TypeDefinition } from '../types';
2+
13
import { FormatError } from '../core/Error';
4+
import { NamedEntity } from '../core/NamedEntity';
25
import { Value } from '../core/Value';
36

47
import { isArrayOfNumbers } from '../utils';
58

6-
export class BytesValue extends Value<number[]> {
9+
export class BytesValue extends Value<number[]> implements NamedEntity {
10+
public constructor(public readonly definition: Readonly<Pick<TypeDefinition, 'name' | 'size'>>) {
11+
super();
12+
}
13+
14+
public get name() {
15+
return this.definition.name;
16+
}
17+
718
public encode() {
819
return [...this.getValueForEncoding()];
920
}

src/values/DoubleValue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { base10ToIeee754Double, ieee754DoubleToBase10 } from '../utils';
33
import { FloatValue } from './FloatValue';
44

55
export class DoubleValue extends FloatValue {
6-
public constructor(public readonly size = 8) {
7-
super(size);
6+
public get size() {
7+
return this.definition.size || 8;
88
}
99

1010
public encode() {

src/values/FloatValue.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
import { TypeDefinition } from '../types';
2+
13
import { FormatError } from '../core/Error';
4+
import { NamedEntity } from '../core/NamedEntity';
25
import { Value } from '../core/Value';
36

47
import { base10ToIeee754, ieee754ToBase10, isNumber } from '../utils';
58

6-
export class FloatValue extends Value<number> {
7-
public constructor(public readonly size = 4) {
9+
export class FloatValue extends Value<number> implements NamedEntity {
10+
public constructor(public readonly definition: Readonly<Pick<TypeDefinition, 'name' | 'size'>>) {
811
super();
912
}
1013

14+
public get name() {
15+
return this.definition.name;
16+
}
17+
18+
public get size() {
19+
return this.definition.size || 4;
20+
}
21+
1122
public encode() {
1223
return base10ToIeee754(this.getValueForEncoding(), this.size);
1324
}

src/values/IntegerValue.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
import { TypeDefinition } from '../types';
2+
13
import { FormatError } from '../core/Error';
4+
import { NamedEntity } from '../core/NamedEntity';
25
import { Value } from '../core/Value';
36

47
import { bytesToInt, decimalToHexArray, isInteger } from '../utils';
58

6-
export class IntegerValue extends Value<number> {
7-
public constructor(public readonly size = 1) {
9+
export class IntegerValue extends Value<number> implements NamedEntity {
10+
public constructor(public readonly definition: Readonly<Pick<TypeDefinition, 'name' | 'size'>>) {
811
super();
912
}
1013

14+
public get name() {
15+
return this.definition.name;
16+
}
17+
18+
public get size() {
19+
return this.definition.size || 1;
20+
}
21+
1122
public encode() {
1223
return decimalToHexArray(this.getValueForEncoding(), this.size);
1324
}

src/values/StringValue.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import { TypeDefinition } from '../types';
2+
13
import { FormatError } from '../core/Error';
4+
import { NamedEntity } from '../core/NamedEntity';
25
import { Value } from '../core/Value';
36

47
import { isString, utfStringToByteArray } from '../utils';
58

6-
export class StringValue extends Value<string> {
9+
export class StringValue extends Value<string> implements NamedEntity {
10+
public constructor(public readonly definition: Readonly<Pick<TypeDefinition, 'name' | 'size'>>) {
11+
super();
12+
}
13+
14+
public get name() {
15+
return this.definition.name;
16+
}
17+
718
public encode() {
819
return utfStringToByteArray(this.getValueForEncoding());
920
}

src/values/TimestampValue.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
import { TypeDefinition } from '../types';
2+
13
import { FormatError } from '../core/Error';
4+
import { NamedEntity } from '../core/NamedEntity';
25
import { Value } from '../core/Value';
36

47
import { bytesToInt, decimalToHexArray, isString } from '../utils';
58

6-
export class TimestampValue extends Value<Date, Date | string> {
7-
public constructor(value = new Date()) {
9+
export class TimestampValue extends Value<Date, Date | string> implements NamedEntity {
10+
public constructor(
11+
public readonly definition: Readonly<Pick<TypeDefinition, 'name' | 'size'>>,
12+
value = new Date(),
13+
) {
814
super();
915
this.setValue(value);
1016
}
1117

18+
public get name() {
19+
return this.definition.name;
20+
}
21+
1222
public encode() {
1323
return [...decimalToHexArray(this.getValueForEncoding().getTime(), 8)];
1424
}

src/values/UintValue.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
import { IntegerValue } from './IntegerValue';
22

3-
export class UintValue extends IntegerValue {
4-
public constructor(public readonly size = 1) {
5-
super(size);
6-
}
7-
}
3+
export class UintValue extends IntegerValue {}

src/values/UnitValue.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class UnitValue extends Value<UnitValueData, UnitValueDataSetter> impleme
5151
const [, unitId, ...valueBytes] = bytes;
5252

5353
const unit = this.getUnitDefinition('id', unitId);
54-
const value = new DoubleValue(UnitValue.ValueSize).decode(valueBytes);
54+
const value = this.createDoubleValue().decode(valueBytes);
5555

5656
this._value = {
5757
unit,
@@ -112,7 +112,7 @@ export class UnitValue extends Value<UnitValueData, UnitValueDataSetter> impleme
112112
? this.getUnitDefinition('name', unitIdentifier)
113113
: this.getUnitDefinition('id', unitIdentifier)
114114
: currentUnit || this.getFirstUnitDefinition();
115-
const value = (currentValue || new DoubleValue(UnitValue.ValueSize)).setValue(valueInUnits);
115+
const value = (currentValue || this.createDoubleValue()).setValue(valueInUnits);
116116

117117
this._value = {
118118
unit,
@@ -133,4 +133,11 @@ export class UnitValue extends Value<UnitValueData, UnitValueDataSetter> impleme
133133

134134
return null;
135135
}
136+
137+
protected createDoubleValue() {
138+
return new DoubleValue({
139+
name: this.name,
140+
size: UnitValue.ValueSize,
141+
});
142+
}
136143
}

0 commit comments

Comments
 (0)