Javascript heredoc

后端 未结 14 1597
盖世英雄少女心
盖世英雄少女心 2020-12-02 12:55

I need something like heredoc in JavaScript. Do you have any ideas for this? I need cross-browser functionality.

I found this:

heredoc = \'\\
14条回答
  •  眼角桃花
    2020-12-02 13:33

    // js heredoc - http://stackoverflow.com/a/32915549/466363
    // a function with comment with eval-able string, use it just like regular string
    
    function extractFuncCommentString(func,comments) {
      var matches = func.toString().match(/function\s*\(\)\s*\{\s*\/\*\!?\s*([\s\S]+?)\s*\*\/\s*\}/);
      if (!matches) return undefined;
      var str=matches[1];
    
       // i have made few flavors of comment removal add yours if you need something special, copy replacement lines from examples below, mix them
      if(comments===1 )
      {
       // keep comments, in order to keep comments  you need to convert /**/ to / * * / to be able to put them inside /**/ like /*    / * * /    */
       return (
        str
       .replace(/\/\s\*([\s\S]*?)\*\s\//g,"/*$1*/") //       change   / * text * /  to   /* text */ 
       )
      }
      else if(comments===2)
      {
       // keep comments and replace singleline comment to multiline comment
       return (
        str
       .replace(/\/\s\*([\s\S]*?)\*\s\//g,"/*$1*/") //       change   / * text * /  to   /* text */ 
       .replace(/\/\/(.*)/g,"/*$1*/")          //           change   //abc to  /*abc*/
       )
      }
      else if(comments===3)
      {
       // remove comments
       return (
          str
          .replace(/\/\s\*([\s\S]*?)\*\s\//g,"") //       match / * abc * /
          .replace(/\/\/(.*)/g,"")             // match //abc
         )
      }
      else if(comments===4)
      {
       // remove comments and trim and replace new lines with escape codes
       return (
          str
          .replace(/\/\s\*([\s\S]*?)\*\s\//g,"") //       match / * abc * /
          .replace(/\/\/(.*)/g,"")             // match //abc
          .trim() // after removing comments trim and:
          .replace(/\n/g,'\\n').replace(/\r/g,'\\r') // replace new lines with escape codes. allows further eval() of the string, you put in the comment function: a quoted text but with new lines
         )
      }
      else if(comments===5)
      {
       // keep comments comments and replace strings, might not suit when there are spaces or comments before and after quotes 
       // no comments allowed before quotes of the string
       return (
          str
          .replace(/\/\s\*([\s\S]*?)\*\s\//g,"/*$1*/") //       change   / * text * /  to   /* text */
          .replace(/\/\/(.*)/g,"/*$1*/")          //           change   //abc to  /*abc*/
          .trim() // trim space around quotes to not escape it and:
          .replace(/\n/g,'\\n').replace(/\r/g,'\\r') // replace new lines with escape codes. allows further eval() of the string, you put in the comment function: a quoted text but with new lines
         )
      }
      else 
      return str
    }
    

    example

    var week=true,b=123;
    var q = eval(extractFuncCommentString(function(){/*!
    
    // this is a comment     
    
    
    'select 
    
    / * this
    is a multiline 
    comment * /
    
     a
    ,b  // this is a comment  
    ,c
    from `table`
    where b='+b+' and monthweek="'+(week?'w':'m')+'" 
    //+' where  a=124
    order by a asc
    '
    */},4));
    

    with cache: - make a simple template function, and save the function:(second time works fast)

    var myfunction_sql1;
    function myfunction(week,a){
    
    
        if(!myfunction_sql1) eval('myfunction_sql1=function(week,a){return ('+extractFuncCommentString(function(){/*!
    'select 
    
    / * this
    is a multiline 
    comment * /
    
     a
    ,b  // this is a comment  
    ,c
    from `table`
    where b='+b+' and monthweek="'+(week?'w':'m')+'" 
    //+' where  a=124
    order by a asc
    
    '*/},4)+')}');
        q=myfunction_sql1(week,a);
        console.log(q)
    }
    myfunction(true,1234)
    

提交回复
热议问题