Built in 2020
Skeleton Mapper
Object Mapper Module
This module provides an easy object mapper from a template skeleton.
Made with
APIs rarely return data in the shape you need. The response is deeply nested, the field names are awkward, and half the fields are useless to you. So you write code to reshape it. Then you write the same code again in the next project.
I got tired of that. The pattern never changed. Pick fields from the source, rename them, flatten the nested parts. So I built a small library that does it from a description of the result.
With Skeleton Mapper, you describe the shape you want and you get it. No loops and no repeated assignments.
Describe the shape you want
The library takes a source object and a template. The template says which fields to take and where to put them. The output has the template’s structure and the source’s values.
Simple mappings look like this:
const source = {
name: "Team 1",
shield: "shield.jpg",
university: 5
};
const template = {
teamName: "name",
image: "shield"
};
const result = skeletonMap(source, template);
// { teamName: "Team 1", image: "shield.jpg" }
Nested fields use dot notation, where each dot steps one level deeper. This flattens deep structures:
const source = {
user: {
profile: {
email: "user@example.com"
}
}
};
const template = {
userEmail: "user.profile.email"
};
const result = skeletonMap(source, template);
// { userEmail: "user@example.com" }
The template can also create new nesting. You decide the whole output structure:
const source = {
name: "Angel",
team: {
university: { name: "University 1" }
}
};
const template = {
athlete: { name: "name" },
school: { name: "team.university.name" }
};
const result = skeletonMap(source, template);
// { athlete: { name: "Angel" }, school: { name: "University 1" } }
Arrays work too. Pass an array of objects and you get an array of mapped objects:
const users = [
{ name: "Alice", role: { title: "Admin" } },
{ name: "Bob", role: { title: "User" } }
];
const template = {
username: "name",
permission: "role.title"
};
const result = skeletonMap(users, template);
// [{ username: "Alice", permission: "Admin" }, { username: "Bob", permission: "User" }]
Two small functions inside
Inside, the library uses two functions. One finds values in nested objects. The other applies the template recursively, which means it calls itself for each nested level.
The value finder splits a dot path and walks down the object. Given "user.profile.email", it steps into user, then profile, then returns email. If any step hits undefined, it stops and returns undefined.
The template processor walks through each key of the template. If the value is a string, it uses it as a path to find the source value. If the value is an object, it calls itself with that object as the new template.
This handles any depth on both sides. The source can be deeply nested. So can the output. The mapping stays readable either way.
When a field is missing
By default, missing fields are left out. If a path points to something that does not exist, that key does not appear in the result.
A flag changes this. When addUndefinedFields is true, missing values appear as explicit undefined entries:
const source = { name: "Test" };
const template = { name: "name", age: "age" };
skeletonMap(source, template, false);
// { name: "Test" }
skeletonMap(source, template, true);
// { name: "Test", age: undefined }
This matters when you need to tell “field not mapped” apart from “field mapped but missing.”
One function, no dependencies
I kept the API small on purpose. One function does everything. There are no configuration objects, no method chaining and no special syntax to learn.
The template is plain JavaScript. You can build it at runtime, store it in a config file, or combine templates by spreading objects together.
Type safety was a tradeoff. TypeScript cannot work out the output type from a template that only exists at runtime. The function returns a generic object type. For stricter types, you can cast the result or wrap the call.
The library has no dependencies. It is around 40 lines of code. It does one thing and stays small.
Where it fits
Skeleton Mapper fits when you need to reshape API responses before using them. It also helps when you want a few fields from a complex object, or the same data shape from different sources.
It does not replace full DTO libraries, the tools that define and check data transfer objects. It does no validation and no type conversion. It just maps fields.
For simple reshaping, that is enough.
Getting it from npm
Install it from npm:
npm install skeleton-mapper
Import it and use it:
import { skeletonMap } from 'skeleton-mapper';
const result = skeletonMap(sourceData, yourTemplate);
Small libraries age well
This code has needed almost no maintenance since 2020. Its scope is narrow, so there is little to break.
Writing a generic utility made me think about edge cases early. What happens with undefined? With arrays? With empty objects? Handling them cleanly from the start saves debugging later.
Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build