Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Open
Ciul opened this Issue · 0 comments

1 participant

@Ciul

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! We accept bounties via Bountysource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.