npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@incpad/base-tool

v1.0.26

Published

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

Downloads

5

Readme

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对象

            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的返回值