1.0.26 • Published 5 years ago

@incpad/base-tool v1.0.26

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Usage

  • base相关

    • CreateSymbolTool

      const {CreateSymbolTool}=require("@incpad/base-tool")

      用于创建一个symbol对象表来简单隐藏内部私有属性:
            const {CreateSymbolTool}=require("@incpad/base-tool")
            const symbolTable=CreateSymbolTool([      
                "innerProp1",
                "innerMethod1"
              ])
            class A{
                constructor(){
                    this[symbolTable.innerProp1]="xxxx"
                    this.publishProp2="xxxx"
                }
                [symbolTable.innerMethod1](){
                    //xxxx
                }
                publicMethond2(){
                    //xxxx
                }
            }
    • getValueFromEnv

      const {getValueFromEnv}=require("@incpad/base-tool")

      用于从process.env中读取环境变量信息,第二个参数可以给予一个默认值,用于处理环境变量不存在的情况
            const {getValueFromEnv}=require("@incpad/base-tool")
            const PATH=getValueFromEnv("PATH","") //获取环境变量PATH,如果没有就赋予空字符串
    • getRuntimePlatform

      const {getRuntimePlatform}=require("@incpad/base-tool")

      返回当前运行的环境,目前可识别的为Node、Jest、browser、ReactNative(结果不保证一定正确)
    • constant

      const {constant}=require("@incpad/base-tool")

      返回一个对象,包含@incpad项目中所有用到的常量
    • pipeAsyncFuncs

      const {pipeAsyncFuncs}=require("@incpad/base-tool")

      可以传入多个异步函数,每个函数必须返回一个promise,依次执行每个函数,每个函数的参数为上一个函数的返回值 如果中途某个函数抛出错误,则后续函数都不会被执行
        const test = pipeAsyncFuncs(
            console.log.bind(null, 'before'),
            () => {
                return new Promise(resolve => setTimeout(() => {
                    console.log('timer');
                    resolve('timer');
                }, 1000));
            },
            console.log.bind(null, 'now'),
            console.log.bind(null, 'after'),
            )
        ;
        test(1);
        //output:
        //before 1
        //timer
        //now timer
        //after undefined
  • browser相关

    • copyToClipboard

      const {copyToClipboard}=require("@incpad/base-tool/tools/browser") copyToClipboard("test")

      将传入的文本拷贝到用户剪贴板,限浏览器环境
    • scrollToTop

      const {scrollToTop}=require("@incpad/base-tool/tools/browser")

      滚动到页面顶端
    • parseCookie

      const {parseCookie}=require("@incpad/base-tool/tools/browser") parseCookie(document.cookie)

      解析cookie
  • path相关
    • convertPathWithinCustomRootDir

      const {convertPathWithinCustomRootDir}=require("@incpad/base-tool")

      等同于原生的path.resolve
    • checkExist

      const {checkExist}=require("@incpad/base-tool")

      传入一个路径字符串,判断对应路径是否存在或者对应文件是否存在 ps:依赖fs.accessSync,同步,且如果没有读写权限,则默认文件或路径不存在
  • debug相关 ps:debug相关函数全是对于debug库的一个二次封装
    • tap

      const {tap}=require("@incpad/base-tool/tools/debug")

      用法等同于console.log,但是会原样返回输入的参数,所以可以无痛的插入代码链中,且默认只在dev模式输出内容
          const {tap}=require("@incpad/base-tool/tools/debug") 
          function a(c,d){      
                //xxxx
          }
          const e=123,f=otherVar
          a(e,tap(f))
      ps:如果给tap传入多个参数,返回值会合并为数组,需要使用展开运算符展开
    • updateOptions

      const {updateOptions}=require("@incpad/base-tool/tools/debug")

      用于更新debug的一些配置,比如关闭debug相关函数只在dev模式输出的设置:
          const {updateOptions}=require("@incpad/base-tool")
          updateOptions({
              RUN_ONLY_DEBUG:false
          })
    • addNewDebugger & getCustomDebugger

      > const {addNewDebugger,getCustomDebugger}=require("@incpad/base-tool/tools/debug")
      
      对debug的一个封装,新建一个debug对象以及获取一个debug对象
      
      ```ecmascript 6
            const {addNewDebugger,getCustomDebugger}=require("@incpad/base-tool/tools/debug")
            addNewDebugger("test")
            const testDebugger-getCustomDebugger("test")
            testDebugger("some thing to output")
      ```

      由于debug输出默认会显示与上一次相同debugger输出的间隔,所以也可以用来测量两次操作花费的时间

        addNewDebugger("exampleTimeTest");
        const a = getCustomDebugger("exampleTimeTest");
        
        function test() {
            a("asdsadd");
        }
        
        test();
        setTimeout(test, 700);
      //output:
      // exampleTimeTest asdsadd +0ms
      // exampleTimeTest asdsadd +708ms
  • wrapper相关:
    • doWrap 包裹传入的函数,返回包装后的函数,为其添加异步的AOP钩子

        const { doWrap } = require('@incpad/base-tool/tools/wrapper');
        function func(){
            console.log("run")
        }
        const newFunc=doWrap(func, {
                before: console.log.bind(null,"before"),
                after: console.log.bind(null,"after"),
                context,
            })
        newFunc("runData")

      被加入的before和after可以是Promise异步,最后的返回值是func的返回值,三个函数都只会接收到原始参数,即例子中的"runData"

    • doWrapAndPipeData 包裹传入的函数,返回包装后的函数,为其添加异步的AOP钩子

        ```ecmascript 6
          const { doWrapAndPipeData } = require('@incpad/base-tool/tools/wrapper');
          function func(){
              console.log("run")
          }
          const newFunc=doWrapAndPipeData(func, {
                  before: console.log.bind(null,"before"),
                  after: console.log.bind(null,"after"),
                  context,
              })
          newFunc("runData")
        ```
        
        类似doWrap,但是三个函数接受到的参数都是上一个函数的返回值,最后的返回值是after的返回值