to-json-callback

0.1.1 • Public • Published

to-json-callback

build status Coverage Status

Did you know that some function in JS can't be serialized as string and be evaluated as such somewhere else?

If you did, you are in the right project, if you didn't though, you still are in the right project.

what does this solve

const obj = {method() {}};

const str = String(obj.method);
// method() {}

eval(str);
// Uncaught SyntaxError: Unexpected token '{'

now ... let's try this again:

import toJSONCallback from 'to-json-callback';

const obj = {method() {}};

const str = toJSONCallback(obj.method);
// function method() {}

eval(str);
// no error whatsoever

This is also compatible with all async variant of functions, as 100% code-coverage test proves.

And that's all folks!

Package Sidebar

Install

npm i to-json-callback

Weekly Downloads

5,973

Version

0.1.1

License

ISC

Unpacked Size

3.42 kB

Total Files

7

Last publish

Collaborators

  • webreflection