1.0.7 • Published 1 year ago

nv-top-down-group-tree v1.0.7

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

nv-top-down-group-tree

  • simple 4-tuple tree
  • to test syntax-based-eval-authorization in nvlang, normally USELESS

install

  • npm install nv-top-down-group-tree

usage

  const {creat,load_from_json }   = require("nv-top-down-group-tree");

example

        var rt = creat(100000/*max supported node count*/)


        // .creat_group(path, data:Object)
        //  path must begin with "/"
        //  path-tail must match  <<ele>/>+
        //  ele must match ^[_a-z][_a-z0-9]+$

        var [cond,lstnd,grpnds] = rt.creat_group("/a/b/c/d/",{xx:123,yy:456})

        /*

        > grpnds
        [
          [
            '/a/',
            Node(3) [2 %dlgaelfz:2% ] [ [Forest], 2, false, data: [Object] ]
          ],
          [
            '/a/b/',
            Node(3) [3 %dlgaelfz:3% ] [ [Forest], 3, false, data: [Object] ]
          ],
          [
            '/a/b/c/',
            Node(3) [4 %dlgaelfz:4% ] [ [Forest], 4, false, data: [Object] ]
          ],
          [
            '/a/b/c/d/',
            Node(3) [5 %dlgaelfz:5% ] [ [Forest], 5, false, data: [Object] ]
          ]
        ]
        >


        > lstnd.path_
        '/a/b/c/d/'
        >
        lstnd.tag_
        'd'
        lstnd.data
        {xx:123,yy:456}

        */

        var [cond,nd] = rt.pget("/a")
        /*
        > rt.pget("/a")
        [
          true,
          Node(3) [2 %dlgaelfz:2% ] [
            Forest {
              fid: 'dlgaelfzcbgbwhewjmocuymjfxqdghiisehu',
              max_size: 100000,
              idpool: [Object]
            },
            2,
            false,
            data: { xx: 123, yy: 456 }
          ]
        ]
        >
        */


        var [cond,deleted] = rt.dele_group("/a",(data)=>[data.xx===123,"ok"])

        /*
        > deleted
        [
          [ '/a/b/c/d/', { xx: 123, yy: 456 } ],
          [ '/a/b/c/', { xx: 123, yy: 456 } ],
          [ '/a/b/', { xx: 123, yy: 456 } ],
          [ '/a/', { xx: 123, yy: 456 } ]
        ]
        >

        */

        var [cond,lstnd,grpnds] = rt.creat_group("/a/b/c/d/",{xx:123,yy:456})
        var [cond,lstnd,grpnds] = rt.creat_group("/a/b/c/d/e/",{xx:123,yy:789})

        /*
        > lstnd.tag_
        'e'
        > lstnd.path_
        '/a/b/c/d/e/'
        >
        */


        var [cond,r] = rt.join_group('/a/b/c/d/e/','session0',{uname:'abc'});

        /*
        > cond
        true
        > r
        Node(3) [11 %fmhyqada:11% ] [
          Forest {
            fid: 'fmhyqadaxcxcdizfgpdfahrrigynebgrjtdr',
            max_size: 100000,
            idpool: { minid_: 1, maxid_: 100000, used_: 7, lefted_: 99993 }
          },
          11,
          false,
          data: { uname: 'abc' }
        ]
        > r.tag_
        'session0'
        > r.is_leaf()
        true
        >
        > r.$parent_.get_child_with_tag("session0") === r
        true
        >
        */

        var [cond,r] = rt.join_group('/a/b/c/d/e/','session1',{uname:'def'});
        var [cond,r] = rt.join_group('/a/b/c/','session2',{uname:'ghi'});


        var [cond,groupe] = rt.pget("/a/b/c/d/e/")
        var [cond,groupc] = rt.pget("/a/b/c/")


        groupe.ch_paths_
        /*
        >
        [ '/a/b/c/d/e/session0', '/a/b/c/d/e/session1' ]
        */
        groupc.ch_paths_
        groupc.des_paths_
        /*
        > groupc.ch_paths_
        [ '/a/b/c/session2' ]
        >
        > groupc.des_paths_
        [
          '/a/b/c/d/e/session0',
          '/a/b/c/d/e/session1',
          '/a/b/c/session2'
        ]
        >
        */


        var [cond,sess0] = rt.pget("/a/b/c/d/e/session0");

        /*
        > sess0.ppath_
        '/a/b/c/d/e/'
        > sess0.path_
        '/a/b/c/d/e/session0'
        */

        var [cond,r] = rt.leave_group('/a/b/c/d/e/',"session0")
        /*
         > r
        [ 'session0', { uname: 'abc' } ]
        >
        > groupc.ch_paths_
        [ '/a/b/c/session2' ]
        >
        > groupc.des_paths_
        [ '/a/b/c/d/e/session1', '/a/b/c/session2' ]
        >
        > groupe.des_paths_
        [ '/a/b/c/d/e/session1' ]
        > groupe.ch_paths_
        [ '/a/b/c/d/e/session1' ]
        >
        */


        var j = rt.dump2j();  // ttac format:  [ <tag>,<type>,<data>, [Array<ttac>]]
        /*
            [
              [
                'a',2,{ xx: 123, yy: 456 },[
                  [
                    'b',2,{ xx: 123, yy: 456 },[
                      [
                        'c', 2,{ xx: 123, yy: 456 },[
                          [
                            'd',2,{ xx: 123, yy: 456 },[
                              [
                                'e',2,{ xx: 123, yy: 789 },[ 
                                    [ 'session1', 1, { uname: 'def' }, [] ] 
                                ]
                              ]
                            ]
                          ],
                          [ 'session2', 1, { uname: 'ghi' }, [] ]
                        ]
                      ]
                    ]
                  ]
                ]
              ]
            ]
        */


        var rt2 = load_from_json(j)


        /*
        > rt.des_paths_
        [ '/a/b/c/d/e/session1', '/a/b/c/session2' ]
        >
        */
        var rt2 = load_from_json(j)

        /*
        > rt2.des_paths_
        [ '/a/b/c/d/e/session1', '/a/b/c/session2' ]
        >
        */

METHODS

    rt.ch_paths_                               rt.des_paths_                              rt.get_child_with_tag                      rt.is_leaf
    rt.is_nonleaf                              rt.is_rt                                   rt.path_                                   rt.ppath_
    rt.set_as_leaf                             rt.set_as_nonleaf                          rt.set_as_rt                               rt.set_tag
    rt.tag_

    rt.creat_group                             rt.dele_group                              rt.dump2j
    rt.join_group                              rt.leave_group                             rt.pget

    rt.data    

APIS

LICENSE

  • ISC
1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago