1.0.0 • Published 2 years ago

nv-facutil-follow-match v1.0.0

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

nv-facutil-follow-match

  • simple-util to match follow pattern/validator

install

  • npm install nv-facutil-follow-match

usage

 const {creat_validate,creat_matcher,creat_ptrn} = require("nv-facutil-follow-match")

example

cfg

    var validators = {
        T0 : (o) => o instanceof Array,
        T1 : (o) => (o === null) || (o === undefined),
        T2 : (o) => o%2 ===0,
        T3 : (o) => (typeof(o) === 'function'),
        T4 : (o) => o===true
    }

    var follows = {
        T0 : ["T0","T1","T2","T3","T4"],
        T2 : ["T3","T4"],
        T3 : ["T0","T1","T2","T4"],
        T4 : ["T2"],
    }

    /*
    array CAN-BE-FOLLOWED-BY:
       array | nullish |  even | function | true

    even CAN-BE-FOLLOWED-BY:
        function | true

    function CAN-BE-FOLLOWED-BY:
       array | nullish |  even | function

    true CAN-BE-FOLLOWED-BY:
       even

    */

validate

    var arr = [ [],8,()=>{},[],true,10]
    //creat_ptrn(arr,validators)
    //[ 'T0', 'T2', 'T3', 'T0', 'T4', 'T2' ]
    //array-even-function-array-even  match  T0-T2-T3-T0-T4-T2


    var f = creat_validate(validators,follows)
    > f(arr)
    true
    >

    var arr = [ [],8,null,[],true,10]
    //> creat_ptrn(arr,validators)
    //[ 'T0', 'T2', 'T1', 'T0', 'T4', 'T2' ]
    //>   array-even-nullish....        
    //    NOT match   even(T2) can only be followed-by function | true

    > f(arr)
    false
    >

    /*
    reason
    > f(arr,true)

    > f(arr,true)
    {
      value: null,
      index: 2,
      branches: [ "(o) => (typeof(o) === 'function')", '(o) => o===true' ]
    }
    >


    */

step mode

        var arr = [ [],8,()=>{},[],true,10]
        var matcher = creat_matcher(validators,follows)
        /*
        > var matcher = creat_matcher(validators,follows)
        > matcher.input(arr[0])
        true
        > matcher.input(arr[1])
        true
        > matcher.input(arr[2])
        true
        > matcher.input(arr[3])
        true
        > matcher.input(arr[4])
        true
        > matcher.input(arr[5])
        true
        > matcher.reset()
        undefined
        >
        */

APIS

  • creat_matcher(validators,follows)
  • creat_ptrn(ary,validators)
  • creat_validate(validators,follows)

MATCHER METHODS

    matcher.add_child                               matcher.constructor
    matcher.get_with_tag                            matcher.has
    matcher.input                                   matcher.is_ref
    matcher.is_root                                 matcher.is_validator
    matcher.pls_                                    matcher.reset
    matcher.tags_

    matcher.curr                                    matcher.tag

LICENSE

  • ISC