Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

More>Object.Extras :: Object.setToPath #264

Open
Ciul opened this Issue Jul 17, 2011 · 0 comments

Comments

Projects
None yet
1 participant

Ciul commented Jul 17, 2011

Hi MooTools pals.

Once time ago I wrote a XML2Js converter, and some of you helped me (at Google Group) with a function I was requiring that time, which I believe should be part of MooTools More Object.Extras.

It is a setToPath function, which basically create object paths from strings and fill it with a value.

Next, it's code:

function setToPath (source, path, val) {
        var parts = key.split('.'),
            source2 = source; // so we can return the object
        for (var i = 0, l = parts.length; i < l; i++) {
            // So when the value does not exist (and is an own property) or is not an object
            if (i < (l - 1) && (!source.hasOwnProperty(parts[i]) || !Type.isObject(source[i]))){
                source[parts[i]] = {};
            }

            if (i == l - 1) source[parts[i]] = val;
            else source = source[parts[i]];
        }
        // Return the modified object
        return source2;
    }

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/359991-more-object-extras-object-settopath?utm_campaign=plugin&utm_content=tracker%2F22069&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F22069&utm_medium=issues&utm_source=github).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment