krakenjs/shortstop

can we store function using shortstop?

ohpyupi opened this issue · 4 comments

can we store function using shortstop?
I am curious if that is possible.

As in create a protocol handler that makes a value the result of invoking a function? Or just sets the value to a function?

In any case, yes.

const shortstop = require('shortstop');

const functionResolver = (func) => new Function(`return ${func}`)(); 

const json = {
  "greeting": "function:(firstName, lastName) => { console.log('Hello ' + firstName + ' ' + lastName) }"
};

const resolver = shortstop.create();
resolver.use('function', functionResolver);

resolver.resolve(json, (err, data) => {
  if (err) return console.error(err);
  data.greeting('Jack', 'Domino');
});

Hi @tlivings thanks for your comment. I've tried to make my own implementation in the above example.
Do you think it could be a workable solution to my question?

IMO why not just export the function and use the require protocol handler to pull it in?

Are you wanting to inline code in the config?

@tlivings yeah. I was looking for a way to store functions dynamically in json.