1.0.1 • Published 2 years ago

nv-state-duration v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

nv-state-duration

  • nv-state-duration
  • for handle state-include-duration
  • its a simple version of nvbtn

install

  • npm install nv-state-duration

usage

   const {creat_cls} = require("nv-state-duration")
   const Duration = creat_cls(['down','up','left','right','front','back'],5)

example

    var o = new Duration()

    /*
    > o
    Duration(2) [
      FixedArray(5) [
        undefined,
        undefined,
        undefined,
        undefined,
        undefined
      ],
      undefined
    ]
    >

    */

down->up->front->front->right->right->right->stop

    o.down()

    /*
    > o.down()
    State(3) [down:undefined  1651999459820->undefined] [
      'down',
      1651999459820,
      undefined
    ]
    > o.curr
    State(3) [down:undefined  1651999459820->undefined] [
      'down',
      1651999459820,
      undefined
    ]
    > o.hist
    FixedArray(5) [ undefined, undefined, undefined, undefined, undefined ]
    >

    */
    o.up()

    /*
    > o.up()
    State(3) [up:undefined  1651999494108->undefined] [
      'up',
      1651999494108,
      undefined
    ]
    > o.curr
    State(3) [up:undefined  1651999494108->undefined] [
      'up',
      1651999494108,
      undefined
    ]
    > o.hist
    FixedArray(5) [
      State(3) [down:34288  1651999459820->1651999494108] [
        'down',
        1651999459820,
        1651999494108
      ],
      undefined,
      undefined,
      undefined,
      undefined
    ]
    >

    */



    o.front()
    o.front()

    /*
    > o.hist
    FixedArray(5) [
      State(3) [down:34288  1651999459820->1651999494108] [
        'down',
        1651999459820,
        1651999494108
      ],
      State(3) [up:19691  1651999494108->1651999513799] [
        'up',
        1651999494108,
        1651999513799
      ],
      State(3) [front:1349  1651999513799->1651999515148] [
        'front',
        1651999513799,
        1651999515148
      ],
      undefined,
      undefined
    ]
    >
    > o.curr
    State(3) [front:undefined  1651999515148->undefined] [
      'front',
      1651999515148,
      undefined
    ]
    >

    */



    o.right()
    o.right()
    o.right()
    o.stop()

    /*
    > o.right()
    State(3) [right:undefined  1651999565893->undefined] [
      'right',
      1651999565893,
      undefined
    ]
    > o.right()
    State(3) [right:undefined  1651999565894->undefined] [
      'right',
      1651999565894,
      undefined
    ]
    > o.right()
    State(3) [right:undefined  1651999565897->undefined] [
      'right',
      1651999565897,
      undefined
    ]
    > o.stop()
    FixedArray(5) [
      State(3) [front:1349  1651999513799->1651999515148] [
        'front',
        1651999513799,
        1651999515148
      ],
      State(3) [front:50745  1651999515148->1651999565893] [
        'front',
        1651999515148,
        1651999565893
      ],
      State(3) [right:1  1651999565893->1651999565894] [
        'right',
        1651999565893,
        1651999565894
      ],
      State(3) [right:3  1651999565894->1651999565897] [
        'right',
        1651999565894,
        1651999565897
      ],
      State(3) [right:916  1651999565897->1651999566813] [
        'right',
        1651999565897,
        1651999566813
      ]
    ]
    >
    > o.curr
    undefined
    > o.hist
    FixedArray(5) [
      State(3) [front:1349  1651999513799->1651999515148] [
        'front',
        1651999513799,
        1651999515148
      ],
      State(3) [front:50745  1651999515148->1651999565893] [
        'front',
        1651999515148,
        1651999565893
      ],
      State(3) [right:1  1651999565893->1651999565894] [
        'right',
        1651999565893,
        1651999565894
      ],
      State(3) [right:3  1651999565894->1651999565897] [
        'right',
        1651999565894,
        1651999565897
      ],
      State(3) [right:916  1651999565897->1651999566813] [
        'right',
        1651999565897,
        1651999566813
      ]
    ]
    >
    */

        o.clear_history()

        /*
        > o
        Duration(2) [
          FixedArray(5) [
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
          ],
          undefined
        ]
        >

        */


        /*
        > o.up()
        State(3) [up:undefined  1651999655950->undefined] [
          'up',
          1651999655950,
          undefined
        ]
        > o
        Duration(2) [
          FixedArray(5) [
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
          ],
          State(3) [up:undefined  1651999655950->undefined] [
            'up',
            1651999655950,
            undefined
          ]
        ]
        > o.down()
        State(3) [down:undefined  1651999667758->undefined] [
          'down',
          1651999667758,
          undefined
        ]
        > o.stop()
        FixedArray(5) [
          State(3) [up:11808  1651999655950->1651999667758] [
            'up',
            1651999655950,
            1651999667758
          ],
          State(3) [down:4607  1651999667758->1651999672365] [
            'down',
            1651999667758,
            1651999672365
          ],
          undefined,
          undefined,
          undefined
        ]
        >
        */

        /*
        > o.hist[0]
        State(3) [up:11808  1651999655950->1651999667758] [
          'up',
          1651999655950,
          1651999667758
        ]
        > o.hist[0].duration_
        11808
        >
        */

METHODS

common

  getter:
      curr               :   current state+duration
      hist               :   history sequence

  method:
      stop
      clear_history
      reset
      is_hist_ptrn_match(ptrns:Ptrns):Boolean
      
          MatchPtrn: {state:/*state-key name*/,ge:/*duration >= */,le:/*duration <= */}
          Ptrns : Array<MatchPtrn> 

special

  same as state_names of creat_cls(state_names) 

  
   

API

  • creat_cls(state_names,history_size=7)

LICENSE

  • ISC