
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
当函数可以接受多种传参方式时,需要自己解析arguments参数。
flexArgs可以方便地解析输入参数,使函数接受多种传参方式。
npm install 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})
指定数据类型时可以取值:
number,string,function,anyObject,String,Number,Boolean,Function,ArrayflexArgs可以通过位置参数进行匹配,当只有一个参数,且类型为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",{})
FAQs
The npm package flexargs receives a total of 1 weekly downloads. As such, flexargs popularity was classified as not popular.
We found that flexargs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.