unarray
v1.0.0
Published
Retrieves a single value wrapped in an array.
Downloads
17
Readme
unarray
Retrieves a single value wrapped in an array.
Installation
Requires Node.js 4.0.0 or above.
npm i unarray
API
The module exports a single function.
Parameters
a
(array)- Optional: Object argument:
- Optional:
fallback
(any): A value to return if the function fails to retrieve a single value. - Optional:
recursive
(bool): Iftrue
, will retrieve a single value wrapped in nested arrays. Defaults tofalse
.
- Optional:
Return Value
A single value if found. On failure, returns fallback
if provided; otherwise returns the last-searched array.
Examples
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