Nutritious Pancake Mountain

    nested-set

    1.0.0-rc0 • Public • Published

    nested-set

    Codeship Status for netgenes/nested-set

    Simple helper for nested set. Nest flat or flat nested.

    Install

    npm install nested-set

    Usage

    For nest flat data remember to provide data sorted by left property

    const nestedSetContextFactory = require('nested-set');
    /**
    * default options {
    *   left: 'lft',
    *   right: 'rgt',
    *   children: 'children'
    * }
    */
    const nestedSetContext = nestedSetContextFactory(/*options*/);
    // e.g result from db
    const flat = [
                   {
                     "lft": 1,
                     "rgt": 22,
                     "name": "Clothing"
                   },
                   {
                     "lft": 2,
                     "rgt": 9,
                     "name": "Men's"
                   },
                   {
                     "lft": 3,
                     "rgt": 8,
                     "name": "Suits"
                   },
                   {
                     "lft": 4,
                     "rgt": 5,
                     "name": "Slacks"
                   },
                   {
                     "lft": 6,
                     "rgt": 7,
                     "name": "Jackets"
                   },
                   {
                     "lft": 10,
                     "rgt": 21,
                     "name": "Women's"
                   },
                   {
                     "lft": 11,
                     "rgt": 16,
                     "name": "Dresses"
                   },
                   {
                     "lft": 12,
                     "rgt": 13,
                     "name": "Evening Gowns"
                   },
                   {
                     "lft": 14,
                     "rgt": 15,
                     "name": "Sun Dresses"
                   },
                   {
                     "lft": 17,
                     "rgt": 18,
                     "name": "Skirts"
                   },
                   {
                     "lft": 19,
                     "rgt": 20,
                     "name": "Blouses"
                   }
                 ];
     
    const nested = nestedSetContext.nest(flat);
    /* will produce
     *
     * [
         {
           "name": "Clothing",
           "children": [
             {
               "name": "Men's",
               "children": [
                 {
                   "name": "Suits",
                   "children": [
                     {
                       "name": "Slacks"
                     },
                     {
                       "name": "Jackets"
                     }
                   ]
                 }
               ]
             },
             {
               "name": "Women's",
               "children": [
                 {
                   "name": "Dresses",
                   "children": [
                     {
                       "name": "Evening Gowns"
                     },
                     {
                       "name": "Sun Dresses"
                     }
                   ]
                 },
                 {
                   "name": "Skirts"
                 },
                 {
                   "name": "Blouses"
                 }
               ]
             }
           ]
         }
       ]
     */
    const flatCopy = nestedSetContext.flat(nested);
     
    /* flatCopy == flat*/

    Install

    npm i nested-set

    DownloadsWeekly Downloads

    152

    Version

    1.0.0-rc0

    License

    ISC

    Last publish

    Collaborators

    • jatzek_at_netgenes