Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

supermapper

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

supermapper

map any object to any schema.

  • 1.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

supermap

invocation

mappedData = supermap(dataToMap, keyMapping, opts);

//compile your mapping for much faster runtime

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);

//result
// {
//     "employee": {
//         "person": {
//             "name": "greg"
//         },
//         "job": {
//             "title": "programmer",
//             "skills": [
//                 "write code",
//                 "conference calls"
//             ]
//         }
//     }
// }

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);

//result
// {
//     "employee": {
//         "person": {
//             "name": "greg"
//         },
//         "job": "band 8 programmer"
//     }
// }

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`
	}
});

//result
// {
//     "employee": {
//         "person": {
//             "name": "greg, awesome"
//         },
//         "job": "programmer, 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" //Default Value if not found in map
		}
	}
}

supermap(dataToMap, keyMapping);

//result
// {
//     "employee": {
//         "person": {
//             "name": "greg"
//         },
//         "job": "Engineer"
//     }
// }

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"}], //Default Value if not found in array
		]
	}
}

supermap(dataToMap, keyMapping);

// {
//     "employee": {
//         "person": {
//             "name": "greg"
//         },
//         "job": {
//             "role": "Engineer"
//         }
//     }
// }

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);

//result
// {
//     "person": {
//         "name": "greg",
//         "height": "N/A"
//     }
// }



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()) //capitalize first name
		},
		additional: [
			"person.firstName",
			"person.givenName"
		]
	},
}
supermap(dataToMap, keyMapping);

//result
// {
//     "person": {
//         "name": "Greg",
//         "firstName": "Greg",
//         "givenName": "Greg"
//     }
// }



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);

//result
// {
//     "programmer": {
//         "skills": [
//             "write code",
//             "conference calls"
//         ]
//     }
// }


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);

// result
// {
//     "people": {
//         "employees": [
//             {
//                 "name": "Greg",
//                 "job": "Programmer"
//             },
//             {
//                 "name": "Adam",
//                 "job": "Admin"
//             }
//         ]
//     }
// }

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);

//result
// {
//     "positions": [
//         {
//             "job": "designer"
//         },
//         {
//             "job": "programmer"
//         },
//         {
//             "job": "admin"
//         }
//     ]
// }



runtime opts


{
	writeNull: true | false // (default: false) if the incoming data value is null or undefined, create the key in mapped object 
	base: Object // rather than creating a new object for the map, pass a mutable object to be altered
}

compile opts


{
	flip: true | false // (default: false) flip the direction of keys and values in mapping
}

FAQs

Package last updated on 09 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc