# flexargs

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

Latest version **1.0.4** (published 2024-10-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install flexargs
pnpm add flexargs
yarn add flexargs
bun add flexargs
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2024-10-09 |
| First published | 2022-02-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | wxzhang |
| Maintainers | zhangfisher |
| Keywords | argument, arg |

## Links

- npm: https://www.npmjs.com/package/flexargs
- Repository: https://gitee.com/zhangfisher/flexargs
- npm.io page: https://npm.io/package/flexargs

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2024-10-09
- 1.0.3 (beta) — 2022-09-03
- 1.0.2 — 2022-09-03
- 1.0.1 — 2022-08-06
- 1.0.0 — 2022-02-12

## README

当函数具备多个函数签名时，`flexArgs`可以方便地解析输入参数，使函数接受多种传参方式。

# 安装方法

```javascript
   npm install flexargs
```

 # 使用方法

```javascript
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`:

```javascript
//  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})    
 
```

同时也支持以下传参：

```javascript
myfunc({name:"jack",count:1})
```

# 数据类型

指定数据类型时可以取值：

- 字符串：`number`，`string`，`function`，`any`
- 对象： `Object`，`String`，`Number`，`Boolean`，`Function`，`Array`

# 字典传参

flexArgs可以通过位置参数进行匹配，当只有一个参数，且类型为`Object`时，采用字典传参匹配模式。

```javascript
myfunc({name:"jack",count:1,options:{},callback:()==>{})
```


# 任意类型

参数类型增加any，可以指定某个参数的类型为`any`。如

```javascript
 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",{})

```

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