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

flexargs

v1.0.4

Published

解析函数输入参数,简化对多种传参的支持

Downloads

16

Readme

当函数具备多个函数签名时,flexArgs可以方便地解析输入参数,使函数接受多种传参方式。

安装方法

   npm install flexargs

使用方法

import flexArgs from "flexargs"

 function myfunc(){
    // 对参数进行自动匹配和解析
    let { name, callback ,count,options } = flexArgs(
    [                                       // 定义5种传参方式
        {name:String},
        {count:Number},
        {name:String,options:Object},
        {name:String,callback:Function},
        {name:String,callback:Function,options:Object} 
    ],
    arguments,                              // 必须的
    {                                       // 可选的默认参数
        name:"tom",
        count:0,
        options:{x:2,z:9}
    })
    .....
 }

以上示例声明了myfunc函数支持5种传参方式,因此就可以非常任性也以如下方式调用myfunc:

//  name="aaa",count=0,callback=undefined,options:{x:2,z:9}
myfunc("aaa")                  
//  name="tom",count=1,callback=undefined,options:{x:2,z:9}
myfunc(1)                     
//  name="bbb",count=0,callback=undefined,options:{x:1,y:2,z:9}
myfunc("bbb",{x:1,y:2})        
//  name="ccc",count=0,callback=<function>,options:{x:2,z:9}
myfunc("ccc",function(){})     
//  name="ddd",count=0,callback=<function>,options:{x:1,y:2,z:9} 
myfunc("ddd",function(){},{x:1,y:2})    
 

同时也支持以下传参:

myfunc({name:"jack",count:1})

数据类型

指定数据类型时可以取值:

  • 字符串:numberstringfunctionany
  • 对象: ObjectStringNumberBooleanFunctionArray

字典传参

flexArgs可以通过位置参数进行匹配,当只有一个参数,且类型为Object时,采用字典传参匹配模式。

myfunc({name:"jack",count:1,options:{},callback:()==>{})

任意类型

参数类型增加any,可以指定某个参数的类型为any。如

 function myfunc(){
    let { name, payload,count } = flexArgs([              
        {name:String,payload:'any'}
        {name:String,count:Boolean}
    ],arguments)
 }
// 以下第三个参数,可以是任意类型
myfunc("a",1)
myfunc("a",true)
myfunc("a","wxzhang")
myfunc("a",{})