Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/huruji/chain-get

程序员的自我修养:如何优雅地无报错链式取值
https://github.com/huruji/chain-get

chain debug proxy

Last synced: 3 months ago
JSON representation

程序员的自我修养:如何优雅地无报错链式取值

Awesome Lists containing this project

README

        

# chain-get
## 使用Proxy实现的无报错链式优雅取值方法

## 使用
```bash
npm i chain-get -S
```

```js
import chainGet from 'chain-get';

let person = {age: 12, name: 'huruji', sisters: ['a']}

chainGet(person).age() // 12
chainGet(person).sisters[0]() // a
chainGet(person).sisters[1]() // undefined
chainGet(person).brothers[1]() // undefined
```

从此告别以下这类代码

```js
if (person && person.sister && persion.sister[0]) {
const sister = person.sister[0]
}
```