0

I am creating a complex object dynamically with nodejs in an effort to write this data to a file for use as a frontend utility library.

Is there any way that I can read/write this object to a file?

Methods I am aware of that fall short:

I'm beginning to think that this is just not possible. Can anyone prove me wrong?

Note: As easy as it is to just read/concat/write files together with workable code for the browser, I would also like to be able to use this as an npm package. The library is set up in a fashion where each bit of reusable code is living in its own file. A build tool allows you to select which files you would like to include in the end result. The complex object I speak of is actually just a global namespace for all of the resulting functionality.

Edit for clarity

Resulting object stored in memory

var obj = {
  foo: 'a string',
  reg: /\s+/,
  num: 100,
  fart: function (name) {
    return name + ' farted and it sounded like pffftt';
  },
  Fart: function (sound) {
    this.sound = sound;
  }
};

And then you write that object character-for-character in a file for use in a browser.

5
  • @Mörre yes, I agree. The question here is if it is possible to read that data (the object in it's entirety) in memory and write it to a file. Commented Oct 27, 2013 at 9:40
  • 1
    No of course not, since you don't have memory level access in this language. You spent too much time with C(++) :-) Commented Oct 27, 2013 at 9:41
  • @Mörre that is an acceptable answer. Commented Oct 27, 2013 at 9:43
  • Sorry to spoil it. But if you really insist, just use JSON stringify and function's toString() - why not? By the way "char for char" does not work simply because that object isn't a linear structure in memory. Each function is another complete object, any variable pointing to an object rather than a primitive JS type is just a (C) pointer to other memory. So sequentially reading would do even LESS than JSON stringify, which catches things like Arrays and sub-objects. Commented Oct 27, 2013 at 9:45
  • so the basic goal is to "save" function in a document? Commented Oct 27, 2013 at 10:28

1 Answer 1

0

Here is a demo, which covers your test case: http://jsfiddle.net/ZFUws/.

function isArray (obj) { return Array.isArray(obj); }

function isObject (obj) { //detect only simple objects
    return obj.__proto_ === Object.prototype;
}

function each (obj, iterator) {
    for (var key in obj) if (obj.hasOwnProperty(key)) iterator(obj[key]);
}

function map (obj, iterator) {
    if (Array.isArray(obj)) return obj.map(iterator);

    var newObj = {};
    for (var key in obj) if (obj.hasOwnProperty(key)) newObj[key] = iterator(obj[key]);
    return newObj;
}

var special = {
    'regexp' : {
        match : function (obj) {
            return obj instanceof RegExp; 
        },
        serialize : function (obj) {
            return {
                $class : 'regexp',
                source : obj.source,
                global : obj.global,
                multiline : obj.multiline,
                lastIndex : obj.lastIndex,
                ignoreCase : obj.ignoreCase
            } 
        },
        deserialize : function (json) {
            var flags = '',
                regexp;

            flags += json.global ? 'g' : '';
            flags += json.multiline ? 'm' : '';
            flags += json.ignoreCase ? 'i' : '';

            regexp = new RegExp(json.source, flags);

            regexp.lastIndex = json.lastIndex

            return regexp;
        }
    },
    'function' : {
        match : function (obj) {
            return obj instanceof Function; 
        },
        serialize : function (obj) {
            return {
                $class : 'function',
                source : obj.toString()
            } 
        },
        deserialize : function (json) {
            return (new Function('return ' + json.source))();
        }
    }
}

function toJSON (obj) {
    return map(obj, function (val) {
        var json;

        each(special, function (desc) {
            if (desc.match(val)) json = desc.serialize(val); 
        });

        if (isArray(val) || isObject (val)) json = toJSON(val);

        return json ? json : val;
    });
}

function fromJSON (json) {
    return map(json, function (val) {
        var obj;

        if (val.$class) {
            obj = special[val.$class].deserialize(val);
        }

        if (isArray(val) || isObject (val)) obj = fromJSON(val);

        return obj ? obj : val;
    });
}

var obj = {
  foo: 'a string',
  reg: /\s+/,
  num: 100,
  fart: function (name) {
    return name + ' farted and it sounded like pffftt';
  },
  Fart: function (sound) {
    this.sound = sound;
  }
};

var serialized = JSON.stringify(toJSON(obj)),
    deserialized = fromJSON(JSON.parse(serialized));

console.log(deserialized.foo);
console.log(deserialized.num);
console.log('11    '.search(deserialized.reg));
console.log(deserialized.fart('John'));
console.log(new deserialized.Fart('pffftt').sound);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your effort! I'll implement this and see how it writes to a file and post the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.