https://github.com/lamansky/unarray
[Node.js] Retrieves a single value wrapped in an array.
https://github.com/lamansky/unarray
Last synced: 3 months ago
JSON representation
[Node.js] Retrieves a single value wrapped in an array.
- Host: GitHub
- URL: https://github.com/lamansky/unarray
- Owner: lamansky
- License: mit
- Created: 2018-04-18T09:29:26.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-18T09:35:07.000Z (about 8 years ago)
- Last Synced: 2025-10-05T06:51:28.844Z (9 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# unarray
Retrieves a single value wrapped in an array.
## Installation
Requires [Node.js](https://nodejs.org/) 4.0.0 or above.
```bash
npm i unarray
```
## API
The module exports a single function.
### Parameters
1. `a` (array)
2. Optional: Object argument:
* Optional: `fallback` (any): A value to return if the function fails to retrieve a single value.
* Optional: `recursive` (bool): If `true`, will retrieve a single value wrapped in nested arrays. Defaults to `false`.
### Return Value
A single value if found. On failure, returns `fallback` if provided; otherwise returns the last-searched array.
## Examples
```javascript
const unarray = require('unarray')
unarray([123]) // 123
unarray([123], {fallback: 'abc'}) // 123
unarray([1, 2]) // [1, 2]
unarray([1, 2], {fallback: 3}) // 3
unarray([[123]]) // [123]
unarray([[123]], {recursive: true}) // 123
```