JSMapr is a JavaScript mapping library for modifying a JavaScript object in place. This can be especially useful for API mediation.
var myObj = {
"fName": "Fred",
"lName": "Smith",
"addresses": [
{
"addr": "177 Pine St",
"city": "San Jose",
"state": "CA",
"zip": 95120,
"addressType": "HOME"
},
{
"addr": "10 S Almaden Blvd",
"city": "San Jose",
"state": "CA",
"zip": 95113,
"addressType": "WORK"
}
],
"username": "fsmith",
"child": "Ryan"
};
var myMap = [
// change field names
JSMapr.MOVE("/lName", "/lastName"),
JSMapr.MOVE("/fName", "/firstName"),
JSMapr.MOVE("/child", "/children"),
// change children to an array
JSMapr.MAKEARRAY("/children"),
// force zips to be strings and change street address name
JSMapr.MAPEACH("/addresses",
[
JSMapr.TOSTRING("/zip"),
JSMapr.MOVE("/addr", "/streetAddress")
]
)
];
var mapr = new JSMapr();
mapr.setMapCommands(myMap);
myObj = mapr.map(myObj);
// myObj = {
// "firstName": "Fred",
// "lastName": "Smith",
// "addresses": [
// {
// "streetAddress": "177 Pine St",
// "city": "San Jose",
// "state": "CA",
// "zip": "95120",
// "addressType": "HOME"
// },
// {
// "streetAddress": "10 S Almaden Blvd",
// "city": "San Jose",
// "state": "CA",
// "zip": "95113",
// "addressType": "WORK"
// }
// ],
// "username": "fsmith",
// "children": [ "Ryan" ]
// }
By default, /
is the separator. Locations are specified like /address/city
. In case there are slashes in the object's keys, the location separator can be changed using this function. Location separator must be a single character.
Usage:
mapr.setLocSeparator("|");
By default, there is no logging function, and logging will not be done. The log function should take a single parameter: the string to log.
Usage:
mapr.setLoggingFunc(function(str) { console.log(str); });
Set the command or array of commands to be run on the object that is mapped.
Usage:
mapr.setMapCommands( JSMapr.ADD("/copyrightYear", 2015) );
or
mapr.setMapCommands([
JSMapr.ADD("/copyrightYear", 2015),
JSMapr.DEL("/metadata")
]);
Runs the map commands against the provided object. Return value is the modified object.
Usage:
myObj = mapr.map(myObj);
Add a specified object/value at a specific location.
Delete an element/subtree at a specific location.
Copy the element/subtree at destLoc to srcLoc.
Move the element/subtree at destLoc to srcLoc.
Modify the element at the specified location by running .toString() on it.
Stringify the element/object at the specified location.
If not an array already, convert the specified location to an array. If there is no element there, make it an empty array. If there is a value/object, make it a single element array. If it is already an array, do nothing.
Add to the array at the specified location the object/value. If the specified location is not an array already, add the object/value as a single element array.
Move the element/subtree at srcLoc into the array at destLoc. If destLoc is empty, add the element/subtree at srcLoc as a single element array to destLoc.
Perform a mapping operation at the specified location.
If the specified location is an array, run the mapping operation on each of its elements.
Run a function with optional parameters at the specified location, replacing that location with the return value of the function.
If the specified location is an array, run the function with optional parameters on each of its elements, replacing each element with the return value of the function.
If the specified location is an array, use the supplied sort function to sort the elements of the array.
Run the specified function on the entire object, replacing the object with the return value of the function.
If an element/object exists at loc perform the trueMap mapping operation, otherwise perform the falseMap mapping operation.
If the element at loc is equal to val perform the trueMap mapping operation, otherwise perform the falseMap mapping operation.
If the element at loc is one of the space delimited types in typeStr perform the trueMap mapping operation, otherwise perform the falseMap mapping operation.
If the condition is true perform the trueMap mapping operation and if the condition is false perform the falseMap mapping operation.
Run the specified function with parms and if it returns true perform the trueMap mapping operation otherwise perform the falseMap mapping operation.
Change the location separator for operations going forward.