dregenor / jsonMapper

simple json mapper
MIT License
31 stars 8 forks source link

function in dict #14

Closed scne closed 7 years ago

scne commented 7 years ago

Is it possible to call JM.ch into dict prcedure?

dregenor commented 7 years ago

not, but you can write your own dict with callFunction

function MyCustomDict (dictionary) {
        return function(key){
            return typeof dictionary[key] === 'function' ? dict[key](key) : dict[key];
        }
    }

and use this

var JM = require('json-mapper');

var converter = JM.makeConverter({
    type: [
        'type' ,
        MyCustomDict({
            1: function () { return 'someFunctionResult' },
            2: 'crop',
            3: 'fit'
        })
    ]
});

console.log('\n\n\n convert with default \n\n', converter({
    'type': 1
})); // { type: "someFunctionResult" }