supermap
invocation
mappedData = supermap(dataToMap, keyMapping, opts);
compiledMap = supermap.compile(keyMapping, opts);
mappedData = compiledMap(dataToMap, opts);
basic usage
const supermap = require('supermapper');
var dataToMap = {
name: "greg",
job: {
type: "programmer",
responsibilities: ["write code", "conference calls"]
}
}
var keyMapping = {
"name": "employee.person.name",
"job.type": "employee.job.title",
"job.responsibilities[0]": "employee.job.skills[0]",
"job.responsibilities[1]": "employee.job.skills[1]",
}
supermap(dataToMap, keyMapping);
altering values with the compose function
var dataToMap = {
name: "greg",
job: {
type: "programmer",
tier: "band 8"
},
}
var keyMapping = {
"name": "employee.person.name",
"job.type": {
key: "employee.job",
compose: (pointer, value, data) => {
return `${data.job.tier} ${value}`
}
},
}
supermap(dataToMap, keyMapping);
using an overriding callback at runtime
var dataToMap = {
name: "greg",
job: {
type: "programmer"
}
}
var keyMapping = {
"name": "employee.person.name",
"job.type": "employee.job",
}
supermap(dataToMap, keyMapping, {
callback: (source, dest, value) => {
return `${value}, awesome`
}
});
creating a value map object to quickly compose strings from an enumerated list
var dataToMap = {
name: "greg",
job: {
type: "programmer"
}
}
var keyMapping = {
"name": "employee.person.name",
"job.type": {
key: "employee.job",
valueMap: {
"programmer": "Engineer",
"_": "Unemployed"
}
}
}
supermap(dataToMap, keyMapping);
creating a value map array to quickly compose any data from an enumerated list
var dataToMap = {
name: "greg",
job: {
type: "programmer"
}
}
var keyMapping = {
"name": "employee.person.name",
"job": {
key: "employee.job",
valueMap: [
[{"type": "programmer"}, {"role": "Engineer"}],
["_", {"role": "Something Else"}],
]
}
}
supermap(dataToMap, keyMapping);
setting a default value if incoming data is null or undefined for the key
var dataToMap = {
name: "greg"
}
var keyMapping = {
"name": "person.name",
"height": {
key: "person.height",
default: "N/A"
}
}
supermap(dataToMap, keyMapping);
copying computed value to additional keys
var dataToMap = {
name: "greg"
}
var keyMapping = {
"name": {
key: "person.name",
compose: (_, val) => {
return val.replace(/^\w/, c => c.toUpperCase())
},
additional: [
"person.firstName",
"person.givenName"
]
},
}
supermap(dataToMap, keyMapping);
value interpolation
enclose the json pointer in {brackets}
to interpolate the value of the incoming data and use as a key.
var dataToMap = {
job: {
type: "programmer",
responsibilities: ["write code", "conference calls"]
}
}
var keyMapping = {
"job.responsibilities": "{job.type}.skills"
}
supermap(dataToMap, keyMapping);
map each in an array
the each
parameter will iterate over objects in an array and map them
let dataToMap = {
company: {
resources: [
{
firstName: "Greg",
role: "Programmer"
},
{
firstName: "Adam",
role: "Admin"
}
]
}
}
let keyMapping = {
"company.resources" : {
key: "people.employees",
each: {
map: {
firstName: "name",
role: "job"
}
}
}
}
supermap(dataToMap, keyMapping);
push to array
passing a number says to treat the target like an array. passing a +
symbol is like passing a number, but it will append to the array.
var dataToMap = {
people: {
job1: "designer",
job2: "programmer",
job3: "admin"
}
}
var keyMapping = {
"people.job1": "positions[0].job",
"people.job2": "positions[1].job",
"people.job3": "positions[+].job"
}
supermap(dataToMap, keyMapping);
runtime opts
{
writeNull: true | false
base: Object
}
compile opts
{
flip: true | false
}