@incpad/base-tool v1.0.26
Usage
base相关
- CreateSymbolTool
用于创建一个symbol对象表来简单隐藏内部私有属性:const {CreateSymbolTool}=require("@incpad/base-tool")
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
用于从process.env中读取环境变量信息,第二个参数可以给予一个默认值,用于处理环境变量不存在的情况const {getValueFromEnv}=require("@incpad/base-tool")
const {getValueFromEnv}=require("@incpad/base-tool") const PATH=getValueFromEnv("PATH","") //获取环境变量PATH,如果没有就赋予空字符串
- getRuntimePlatform
返回当前运行的环境,目前可识别的为Node、Jest、browser、ReactNative(结果不保证一定正确)const {getRuntimePlatform}=require("@incpad/base-tool")
- constant
返回一个对象,包含@incpad项目中所有用到的常量const {constant}=require("@incpad/base-tool")
- pipeAsyncFuncs
可以传入多个异步函数,每个函数必须返回一个promise,依次执行每个函数,每个函数的参数为上一个函数的返回值 如果中途某个函数抛出错误,则后续函数都不会被执行const {pipeAsyncFuncs}=require("@incpad/base-tool")
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
- CreateSymbolTool
browser相关
- copyToClipboard
将传入的文本拷贝到用户剪贴板,限浏览器环境const {copyToClipboard}=require("@incpad/base-tool/tools/browser") copyToClipboard("test")
- scrollToTop
滚动到页面顶端const {scrollToTop}=require("@incpad/base-tool/tools/browser")
- parseCookie
解析cookieconst {parseCookie}=require("@incpad/base-tool/tools/browser") parseCookie(document.cookie)
- copyToClipboard
- path相关
- convertPathWithinCustomRootDir
等同于原生的path.resolveconst {convertPathWithinCustomRootDir}=require("@incpad/base-tool")
- checkExist
传入一个路径字符串,判断对应路径是否存在或者对应文件是否存在 ps:依赖fs.accessSync,同步,且如果没有读写权限,则默认文件或路径不存在const {checkExist}=require("@incpad/base-tool")
- convertPathWithinCustomRootDir
- debug相关
ps:debug相关函数全是对于debug库的一个二次封装
- tap
用法等同于console.log,但是会原样返回输入的参数,所以可以无痛的插入代码链中,且默认只在dev模式输出内容const {tap}=require("@incpad/base-tool/tools/debug")
ps:如果给tap传入多个参数,返回值会合并为数组,需要使用展开运算符展开const {tap}=require("@incpad/base-tool/tools/debug") function a(c,d){ //xxxx } const e=123,f=otherVar a(e,tap(f))
- updateOptions
用于更新debug的一些配置,比如关闭debug相关函数只在dev模式输出的设置:const {updateOptions}=require("@incpad/base-tool/tools/debug")
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
- tap
- 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的返回值