# @incpad/base-tool

> * base相关 - CreateSymbolTool > const {CreateSymbolTool}=require("@incpad/base-tool")

Latest version **1.0.26** (published 2019-02-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @incpad/base-tool
pnpm add @incpad/base-tool
yarn add @incpad/base-tool
bun add @incpad/base-tool
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.26 |
| Published | 2019-02-18 |
| First published | 2019-02-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 25 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | yixuankeer |

## Links

- npm: https://www.npmjs.com/package/@incpad/base-tool
- npm.io page: https://npm.io/package/@incpad/base-tool

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.1.0
- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2

## Recent versions

- 1.0.26 (latest) — 2019-02-18
- 1.0.25 — 2019-02-13
- 1.0.24 — 2019-02-13
- 1.0.23 — 2019-02-13
- 1.0.22 — 2019-02-13
- 1.0.21 — 2019-02-13

## README

### Usage

* base相关  
    - CreateSymbolTool  
        > const {CreateSymbolTool}=require("@incpad/base-tool")  
        
        用于创建一个symbol对象表来简单隐藏内部私有属性:
        
        ```ecmascript 6
              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中读取环境变量信息，第二个参数可以给予一个默认值，用于处理环境变量不存在的情况
        ```ecmascript 6
              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，依次执行每个函数，每个函数的参数为上一个函数的返回值
        如果中途某个函数抛出错误，则后续函数都不会被执行
        
        ```ecmascript 6
          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模式输出内容
        
        ```ecmascript 6
            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模式输出的设置:
        ```ecmascript 6 
            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输出的间隔，所以也可以用来测量两次操作花费的时间
    ```ecmascript 6
      addNewDebugger("exampleTimeTest");
      const a = getCustomDebugger("exampleTimeTest");
      
      function test() {
          a("asdsadd");
      }
      
      test();
      setTimeout(test, 700);
    //output:
    // exampleTimeTest asdsadd +0ms
    // exampleTimeTest asdsadd +708ms
    ```
    
* wrapper相关:
    - doWrap 包裹传入的函数，返回包装后的函数，为其添加异步的AOP钩子
        ```ecmascript 6
          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的返回值

---
_Source: https://npm.io/package/@incpad/base-tool · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
