Skip to content

Stringifying objects with prototype #4

@cgarciae

Description

@cgarciae

I've modified your code to stringify objects with prototype. Here is my version:

  exports.stringify = function(obj) {

  //Original Stringify
  var partialStringify = function (obj) {

      return JSON.stringify(obj, function (key, value) {

          if (value instanceof Function || typeof value == 'function') {
              return value.toString();
          }
          if (value instanceof RegExp) {
              return '_PxEgEr_' + value;
          }
          return value;
      });
  };

   //Calculate normal return
   var normal = partialStringify(obj);
   //Stringify prototype with recursively
   var proto = obj.__proto__? this.stringify(obj.__proto__): "";

   //If prototype was not {} empty
   if( proto.length > 2 ) {

       //Slide the brackets and put a coma in between
       return normal.slice(0,-1) + ', ' + proto.slice(1);
   }

  return normal;

};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions