1
1
import { Capability as ICapability , Property as IProperty } from '../types' ;
2
+ import { ComponentName } from '../components/classes' ;
2
3
3
4
import {
4
5
bytesToChunks ,
@@ -70,18 +71,19 @@ export abstract class Capability<P extends string = string>
70
71
}
71
72
72
73
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 ) ;
78
75
79
76
if ( dataComponentValue !== undefined ) {
80
77
property . createComponent ( 'data' , dataComponentValue ) ;
81
78
}
82
- this . setProperty ( property ) ;
79
+ return this . setProperty ( property ) ;
80
+ }
83
81
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 ) ) ;
85
87
}
86
88
87
89
public createPropertiesFromExamples ( name : P ) {
@@ -112,7 +114,7 @@ export abstract class Capability<P extends string = string>
112
114
throw new Error ( 'Property components must be an object.' ) ;
113
115
}
114
116
115
- this . createProperty ( name as P ) . fromJSON ( components ) ;
117
+ this . createPropertyFromJSON ( name as P , components ) ;
116
118
}
117
119
} else {
118
120
this . createProperty ( name as P ) ;
@@ -169,6 +171,14 @@ export abstract class Capability<P extends string = string>
169
171
) ;
170
172
}
171
173
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
+
172
182
protected getPropertyDefinition < T extends keyof IProperty > ( field : T , value : IProperty [ T ] ) {
173
183
const definition = [ ...this . definition . properties , ...this . universalProperties ] . find (
174
184
( property ) => property [ field ] === value ,
@@ -193,6 +203,6 @@ export abstract class Capability<P extends string = string>
193
203
this . properties [ name ] = property ;
194
204
}
195
205
196
- return this ;
206
+ return property ;
197
207
}
198
208
}
0 commit comments