Skip to content

Commit 6407674

Browse files
committed
test: add tests for mapping from and into javascript classes
1 parent 0f5ddfa commit 6407674

3 files changed

Lines changed: 531 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Mapstronaut is a lightweight and flexible JavaScript/TypeScript library for tran
44

55
[![npm version](https://img.shields.io/npm/v/mapstronaut)](https://www.npmjs.com/package/mapstronaut)
66
[![MIT License](https://img.shields.io/github/license/jprevo/mapstronaut)](https://github.com/jprevo/mapstronaut/blob/main/LICENSE)
7-
[![Tests 288/288](https://img.shields.io/badge/tests-288/288-green)](https://github.com/jprevo/mapstronaut/tree/main/test)
7+
[![Tests 302/302](https://img.shields.io/badge/tests-302/302-green)](https://github.com/jprevo/mapstronaut/tree/main/test)
88
[![Coverage 99%](https://img.shields.io/badge/coverage-99%25-green)](https://github.com/jprevo/mapstronaut/tree/main/test)
99

1010
## Why Mapstronaut?

docs/advanced.md

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,96 @@ const asyncStructure = [
154154
];
155155

156156
const asyncMapper = new AsyncMapper(asyncStructure, {
157-
parallelRun: true,
158-
parallelJobsLimit: 3
157+
parallelRun: true,
158+
parallelJobsLimit: 3,
159159
});
160160
const 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

Comments
 (0)