Skip to content

Commit f872a3b

Browse files
committed
Adds createPropertyFromJSON method for capabilities
1 parent 9918e5b commit f872a3b

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/core/Capability.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Capability as ICapability, Property as IProperty } from '../types';
2+
import { ComponentName } from '../components/classes';
23

34
import {
45
bytesToChunks,
@@ -70,18 +71,19 @@ export abstract class Capability<P extends string = string>
7071
}
7172

7273
public createProperty(id: P | number, dataComponentValue?: unknown) {
73-
const property = new Property(
74-
typeof id === 'string'
75-
? this.getPropertyDefinition('name', id)
76-
: this.getPropertyDefinition('id', id),
77-
);
74+
const property = this.createPropertyInstance(id);
7875

7976
if (dataComponentValue !== undefined) {
8077
property.createComponent('data', dataComponentValue);
8178
}
82-
this.setProperty(property);
79+
return this.setProperty(property);
80+
}
8381

84-
return property;
82+
public createPropertyFromJSON(
83+
id: P | number,
84+
components: Partial<Record<ComponentName, unknown>>,
85+
) {
86+
return this.setProperty(this.createPropertyInstance(id).fromJSON(components));
8587
}
8688

8789
public createPropertiesFromExamples(name: P) {
@@ -112,7 +114,7 @@ export abstract class Capability<P extends string = string>
112114
throw new Error('Property components must be an object.');
113115
}
114116

115-
this.createProperty(name as P).fromJSON(components);
117+
this.createPropertyFromJSON(name as P, components);
116118
}
117119
} else {
118120
this.createProperty(name as P);
@@ -169,6 +171,14 @@ export abstract class Capability<P extends string = string>
169171
);
170172
}
171173

174+
protected createPropertyInstance(id: P | number): Property {
175+
return new Property(
176+
typeof id === 'string'
177+
? this.getPropertyDefinition('name', id)
178+
: this.getPropertyDefinition('id', id),
179+
);
180+
}
181+
172182
protected getPropertyDefinition<T extends keyof IProperty>(field: T, value: IProperty[T]) {
173183
const definition = [...this.definition.properties, ...this.universalProperties].find(
174184
(property) => property[field] === value,
@@ -193,6 +203,6 @@ export abstract class Capability<P extends string = string>
193203
this.properties[name] = property;
194204
}
195205

196-
return this;
206+
return property;
197207
}
198208
}

0 commit comments

Comments
 (0)