hierarchy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher A. Kelly

    hierarchy

    i need a script that will take a collection of strings eg:
    inbox
    inbox/folder-1
    inbox/folder-1/Subfolder1
    trash
    sent and turns it into

    root <static, added elsewhere
    |_inbox
    | |_folder-1
    | |_subfolder-1
    |_sent
  • Thomas 'PointedEars' Lahn

    #2
    Re: hierarchy

    Christopher A. Kelly wrote:
    [color=blue]
    > i need a script that will take a collection of strings eg:
    > inbox
    > inbox/folder-1
    > inbox/folder-1/Subfolder1
    > trash
    > sent and turns it into
    >
    > root <static, added elsewhere
    > |_inbox
    > | |_folder-1
    > | |_subfolder-1
    > |_sent[/color]

    Quick hack:

    /**
    * Allows for setting a property of an property.
    *
    * @author
    * (C) 2004 Thomas Lahn <object.js@Poin tedEars.de>
    * Distributed under the terms of the GNU GPL v2.
    * @argument object oRoot
    * Reference to the object which owns the property
    * which property's value should be set.
    * @argument string sPropertyPath
    * @{(value)}-delimited path of property names.
    * @optional _ value = undefined
    * Value to assign to the nested property.
    * @optional string|RegExp delimiter = "/"
    * Delimiter used to split @{(sPropertyPat h)} into
    * path components.
    */
    function setNestedProper ty(oRoot, sPropertyPath, value, delimiter)
    {
    if (!delimiter)
    {
    delimiter = "/";
    }

    var aPropertyPath = sPropertyPath.s plit(delimiter) ;
    for (var i = 0, len = aPropertyPath.l ength - 1; i < len; i++)
    {
    var
    sComponent = aPropertyPath[i],
    t, m = o.hasOwnPropert y,
    bMethod = ((t = typeof m) == "function"
    || (t == "object" && m));

    if ((bMethod && !o.hasOwnProper ty(sComponent)
    || (!bMethod && o[sComponent] == "undefined" ))
    {
    oRoot[sComponent] = {};
    }

    oRoot = oRoot[sComponent];
    }

    oRoot[aPropertyPath[len]] = value;
    }

    var oTree = {};
    for (var aTree = ['inbox', 'inbox/folder-1', 'inbox/folder-1/Subfolder1',
    'trash'],
    i = 0,
    len = aTree.length;
    i < len;
    i++)
    {
    setNestedProper ty(oTree, aTree[i].split('/').join('/ '), {});
    }

    This should create an object with the following structure:

    var oTree = {
    ' inbox': {
    ' folder-1': {
    ' subfolder-1': {}
    }
    },
    ' sent': {}
    }

    The leading space should prevent built-in prototype properties from being
    accidentally overlayed. Can be optimized if only the leaves are iterated
    as iterating a leaf will automagically create its superordinate nodes.

    Displaying this object as a tree is left as an exercise to the reader.


    PointedEars
    --
    This isn't Mission Difficult, Mr. Hunt, it's Mission Impossible

    Comment

    Working...