Skip to main content

Function: get()

get<Value>(iterable): Value

Returns the only value in iterable if it contains exactly one value. Otherwise, throws an error.

Type Parameters

Value

Parameters

iterable

Iterable<Value, any, any>

Returns

Value

Example

import { get } from 'lfi'

console.log(get([`sloth`]))
//=> sloth

try {
console.log(get([]))
} catch {
console.log(`Oh no! It was empty...`)
}
//=> Oh no! It was empty...

try {
console.log(get([`sloth`, `lazy`, `sleep`]))
} catch {
console.log(`Oh no! It had more than one value...`)
}
//=> Oh no! It had more than one value...
Playground

Since

v0.0.1

Defined in

optionals.d.ts:190