Tuesday 30 May 2017

node.js objectmapper string to javascript object

using object-mapper node.js module, to map a object to another.

var objectMapper = require('object-mapper');
var map = {"foo": [{ key:"newfoo", transform: function(value){ return "##"+value;  }}]}
var data =  {"foo":"bar","baz":"blah"}
var dest = objectMapper(data, map);

The variable map is dynamic and would vary based on scenario to scenario. so i could have another translation as

{"foo": [{ key:"newfoo", transform: function(value){ return "$$"+value;  }}]}

I hold all templates by creating a javascript file map_tmpl.js

var tmpl_1= function () {return {"foo": [{ key:"newfoo", transform: function(value){ return "##"+value;  }}]};}
var tmpl_2= function () {return {"foo": [{ key:"newfoo", transform: function(value){ return "##"+value;  }}]};}
module.exports= {tmpl_1,tmpl_2}

I want to save these templates in a database and read from database when needed, If i store this as string and pass it to objectmapper, object-mapper does not work as it is expecting an object, kindly advice on the best way to resolve this.



via tx fun

No comments:

Post a Comment