@@ -154,9 +154,96 @@ const asyncStructure = [
154154];
155155
156156const asyncMapper = new AsyncMapper (asyncStructure , {
157- parallelRun: true ,
158- parallelJobsLimit: 3
157+ parallelRun: true ,
158+ parallelJobsLimit: 3 ,
159159});
160160const result = await asyncMapper .map (spaceMissionData );
161161// Result: { vessel: { name: 'Artemis V', certified: true }, flightPlan: { trajectory: 'trans-lunar-injection' }, personnel: { commander: { name: 'Sarah Kim', role: 'commander', experience: 3200, certified: true, missionReady: true } }, assessment: { riskLevel: 'low' }, logistics: { supplies: { food: 44.800000000000004, water: 39.199999999999996, oxygen: 21 } }, category: 'deep-space' }
162162```
163+
164+ ## Mapping to Class Instances
165+
166+ Mapstronaut can map data directly into class instances, preserving the class methods and structure while updating properties from the source data.
167+
168+ ``` ts
169+ // Define a SpaceStation class
170+ class SpaceStation {
171+ constructor (name = " " , crew = 0 , altitude = 0 ) {
172+ this .name = name ;
173+ this .crew = crew ;
174+ this .altitude = altitude ;
175+ this .operational = true ;
176+ }
177+
178+ getStatus() {
179+ return ` ${this .name }: ${this .crew } crew members at ${this .altitude }km altitude ` ;
180+ }
181+
182+ isFullyCrewed() {
183+ return this .crew >= 6 ;
184+ }
185+
186+ updateOperationalStatus() {
187+ this .operational = this .crew > 0 && this .altitude > 300 ;
188+ return this .operational ;
189+ }
190+ }
191+
192+ // Source data from mission control
193+ const missionControlData = {
194+ station: {
195+ identifier: " International Space Station" ,
196+ personnel: 7 ,
197+ orbit: {
198+ height: 408 ,
199+ inclination: 51.6
200+ }
201+ },
202+ lastUpdate: " 2024-03-15T10:30:00Z" ,
203+ systems: {
204+ power: " nominal" ,
205+ communications: " optimal"
206+ }
207+ };
208+
209+ // Mapping structure to populate the class instance
210+ const structure = [
211+ [" station.identifier" , " name" ],
212+ [" station.personnel" , " crew" ],
213+ [" station.orbit.height" , " altitude" ],
214+ {
215+ source: " systems.power" ,
216+ target: " operational" ,
217+ transform : (power ) => power === " nominal" || power === " optimal"
218+ }
219+ ];
220+
221+ // Create a SpaceStation instance as target
222+ const stationInstance = new SpaceStation (" Unknown Station" , 0 , 0 );
223+
224+ // Map the data into the existing class instance
225+ const mapper = new Mapper (structure );
226+ const mappedStation = mapper .map (missionControlData , stationInstance );
227+
228+ // The result is the same instance, now populated with data
229+ console .log (mappedStation === stationInstance ); // true
230+ console .log (mappedStation .getStatus ()); // "International Space Station: 7 crew members at 408km altitude"
231+ console .log (mappedStation .isFullyCrewed ()); // true
232+ console .log (mappedStation .updateOperationalStatus ()); // true
233+
234+ // Class methods are preserved and work with the mapped data
235+ console .log (typeof mappedStation .getStatus ); // "function"
236+ console .log (typeof mappedStation .isFullyCrewed ); // "function"
237+
238+ // The class instance now contains the mapped data
239+ console .log (mappedStation .name ); // "International Space Station"
240+ console .log (mappedStation .crew ); // 7
241+ console .log (mappedStation .altitude ); // 408
242+ console .log (mappedStation .operational ); // true
243+ ```
244+
245+ This approach is particularly useful when:
246+ - You need to maintain class methods and behavior after mapping
247+ - Working with existing class instances that need data updates
248+ - Integrating with object-oriented codebases
249+ - Preserving type information and method bindings
0 commit comments