funcster

Function serialization and deserialization

funcster

This library contains utilities for serializing and deserializing functions. It provides recursive traversal to discover both serialized and unserialized functions nested within objects and arrays. This is particularly useful for embedding functions into JSON objects.

npm install funcster
serialize(function() { return "Hello world!" });
// -> { __js_function: 'function() { return "Hello world!" }' } 

lib = {
  moduleA: {
    functions: {
      helloWorldfunction() { return "Hello world!" }
    }
  },
  moduleB: {
    functions: {
      goodbyeWorldfunction() { return "Goodbye world!" }
    }
  },
};
 
funcster.deepSerialize(lib);
 
// -> { 
//      moduleA: { 
//        functions: { 
//          helloWorld: { __js_function: 'function() { return "Hello world!" }' } 
//        } 
//      }, 
//      moduleB: { 
//        functions: { 
//          goodbyeWorld: { __js_function: 'function() { return "Goodbye world!" }' } 
//        } 
//      }, 
//    } 

serializedLib = {
  moduleA: {
    functions: {
      helloWorld: { __js_function: 'function() { return "Hello world!" }' }
    }
  },
  moduleB: {
    functions: {
      goodbyeWorld: { __js_function: 'function() { return "Goodbye world!" }' }
    }
  },
};
 
deserializedLib = funcster.deepDeserialize(serializedLib);
deserializedLib.moduleA.functions.helloWorld(); // -> Hello world! 
deserializedLib.moduleB.functions.goodbyeWorld(); // -> Hello world!