Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/eryouhao/safe-get-plus

Securely get attributes from objects and can provide default value
https://github.com/eryouhao/safe-get-plus

javascript npm-module

Last synced: about 4 hours ago
JSON representation

Securely get attributes from objects and can provide default value

Awesome Lists containing this project

README

        

# safe-get-plus
## Install
``` sh
npm install safe-get-plus --save
```
## Start

``` javascript
const safeGet = require('safe-get-plus')

const response = {
data: {
user: {
name: 'eryou',
age: 18,
friends: ['xiaoming', 'xiaohong'],
},
list: [1, 2, 3],
}
}

const data = safeGet(response, 'data.user.friends', []) // ['xiaoming', 'xiaohong']

const data = safeGet(response, 'data.user.address', '') // ''

```

## API
``` javascript
const safeGet = require('safe-get-plus')
safeGet(Object, [Property dot string], [Default Value])
```
**Arguments**

`Object`: The object to be valued.
`[Property dot string]`: A string that gets a value with a point.
`[Default Value]|null`: Default value when no value is taken.