Installation
Simply download the redon-mapper.js file
and include it in your project via ES Modules.
import { RedonMapper } from './lib/redon-mapper.js';
Basic Usage
Defining a template allows you to pick multiple source keys. The mapper will take the first one that exists.
const template = {
username: {
sourceKeys: ["username", "user", "name"],
defaultValue: "Anonymous"
}
};
const rawData = { user: "joseph_m" };
const result = RedonMapper.map(rawData, template);
// Result: { username: "joseph_m" }
Real-World Normalization
Based on the One of One test scenario, here is how you handle inconsistent arrays from different API versions:
const apiResponse = [
{ username: "mike_code", regDate: "2024-02-29T11:15:00Z" },
{ user: "user1", registrationDate: "2024-01-15T09:30:00Z" }
];
const template = {
username: { sourceKeys: ["username", "user", "name"] },
registeredDate: {
sourceKeys: ["regDate", "registrationDate"],
transform: (value) => new Date(value)
}
};
const data = RedonMapper.map(apiResponse, template);
API Reference
.map(source, template)
The main entry point. Automatically detects if
source
is an object or an array and applies
the appropriate mapping logic.
.getValueByPath(obj, path)
Safely retrieves deep nested values using dot notation (e.g., "user.meta.id").
.setNestedValue(obj, path, value)
Sets a value at a nested path, creating intermediate objects if they don't exist.