1.0.1 • Published 2 years ago

nv-ctx-cflow v1.0.1

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

nv-ctx-cflow

  • nv-ctx-cflow
  • several very simple small functions to simulate control-flow
  • for test using in nvlang, normally USELESS
  • nvlang normally USE graph-syntax to impl WHILE / IF; for backward compatible it provide serveral OPERS

  • to avoid using while/if in app/dsl-layer-code(make the app-layer-code easy to parse/unparse/validate/authorize)

  • similiar to nv-facutil-simple-ctx+nv-facutil-loop+nv-facutil-simple-continue, just for compatible with nvlang-syntax

why

 IN nvlang dsl-layer-AND-app-layer, parse/unparse code IS frequently operation:
     when do auto-generate/static-stracing for app-layer-code: 
         the lexical-block such as while/if make things complicated(
                COZ if-branch normally can ONLY  be decided in runtime
         ) which SLOW-DOWN  the parse/unparse (
                IF your dsl/app frequently do parse/unparse operation
         ) 
SO nvlang SUGGEST only write if/while in ir-layer, make things simple

install

  • npm install nv-ctx-cflow

usage

    const {creat_ctx}           = require("nv-ctx-basic");
    const {whil,elif,ifif,nmif} = require("nv-ctx-cflow");

    whil.ss(ctx,cond_func,handle_func)               (cond_func IS  sync)  AND  (handle_func IS sync)
    whil.as(ctx,cond_func,handle_func)               (cond_func IS async)  AND  (handle_func IS sync)
    whil.sa(ctx,cond_func,handle_func)               (cond_func IS  sync)  AND  (handle_func IS async)
    whil.sa(ctx,cond_func,handle_func)               (cond_func IS async)  AND  (handle_func IS async)

    whil.whil(
       ctx:Ctx,
       cond_func:     async@optional (ctx:Ctx):Ctx => {...},
       handle_func:   async@optional (ctx:Ctx):Ctx => {...},
    )


    // if elif ....elif else, fst-match
    elif.ss(ctx,cond_handle_entries,else_handle)
    elif.sa(ctx,cond_handle_entries,else_handle)
    elif.as(ctx,cond_handle_entries,else_handle)
    elif.aa(ctx,cond_handle_entries,else_handle) 

    // if(){} if(){} ... if(){}, exec-all-matches   

    ifif.ss(ctx,cond_handle_entries)
    ifif.sa(ctx,cond_handle_entries)
    ifif.as(ctx,cond_handle_entries)
    ifif.aa(ctx,cond_handle_entries)


    //break if more than max_match_cnt matched 
    nmif.ss(ctx,max_match_cnt,cond_handle_entries)
    nmif.sa(ctx,max_match_cnt,cond_handle_entries)
    nmif.as(ctx,max_match_cnt,cond_handle_entries)
    nmif.aa(ctx,max_match_cnt,cond_handle_entries)

example

whil

    > var ctx = {a:0,sum:0}
    > var condf = (ctx)=>ctx.a<5
    > var handle = (ctx)=> {ctx.sum=ctx.sum+ctx.a;ctx.a=ctx.a+1;}
    > whil.ss(ctx,condf,handle)
    { a: 5, sum: 10 }
    > await whil.whil(ctx,condf,handle)
    { a: 5, sum: 10 }
    >    

elif

    var ctx = {a:31,rslt:undefined}

    var entries = [
       [(ctx)=>ctx.a<10,(ctx)=>ctx.rslt="lt(10)"],
       [(ctx)=>ctx.a<20,(ctx)=>ctx.rslt="ge(10) AND lt(20)"],
       [(ctx)=>ctx.a<30,(ctx)=>ctx.rslt="ge(20) AND lt(30)"]
    ]

    var else_handle = (ctx)=>ctx.rslt="ge(30)"



    > await elif.elif(ctx,entries,else_handle)
    { a: 31, rslt: 'ge(30)' }
    > 

ifif

    var ctx = {a:30,rslt:[]}

    var entries = [
       [(ctx)=>ctx.a%2===0,(ctx)=>ctx.rslt.push(2)],
       [(ctx)=>ctx.a%3===0,(ctx)=>ctx.rslt.push(3)],
       [(ctx)=>ctx.a%5===0,(ctx)=>ctx.rslt.push(5)]
    ]


    await ifif.ifif(ctx,entries)
	/*
	{ a: 30, rslt: [ 2, 3, 5 ] }
	*/

nmif

    var ctx = {a:30,rslt:[]}

    var entries = [
       [(ctx)=>ctx.a%2===0,(ctx)=>ctx.rslt.push(2)],
       [(ctx)=>ctx.a%3===0,(ctx)=>ctx.rslt.push(3)],
       [(ctx)=>ctx.a%5===0,(ctx)=>ctx.rslt.push(5)]
    ]


    await nmif.nmif(ctx,2,entries)
	/*
	{ a: 30, rslt: [ 2, 3 ] }
	*/

METHODS

API

    {
      whil: {
        whil: [AsyncFunction: whil],
        ss: [Function: ss],
        sa: [AsyncFunction: sa],
        as: [AsyncFunction: as],
        aa: [AsyncFunction: aa]
      },
      elif: {
        elif: [AsyncFunction: elif],
        ss: [Function: ss],
        sa: [AsyncFunction: sa],
        as: [AsyncFunction: as],
        aa: [AsyncFunction: aa]
      },
      ifif: {
        ifif: [AsyncFunction: ifif],
        ss: [Function: ss],
        sa: [AsyncFunction: sa],
        as: [AsyncFunction: as],
        aa: [AsyncFunction: aa]
      },
      nmif: {
        nmif: [AsyncFunction: nmif],
        ss: [Function: ss],
        sa: [AsyncFunction: sa],
        as: [AsyncFunction: as],
        aa: [AsyncFunction: aa]
      }
    }

LICENSE

  • ISC