nochoices
Version:
Full featured implementation of options into typescript.
22 lines (21 loc) • 598 B
JavaScript
import { Option } from "./option.js";
/**
* Type safe function to unzip an option of a tuple of 2 into a tuple of 2 options.
* @typeParam A - Any type
* @typeParam B - Any type
*
* @param opt - Option to unzip
* @returns a tuple of 2 options.
*
* @example
* ```ts
* const opt: Option<[number, string]> = Option.Some([10, 'foo'])
* const [optA, optB] =unzip(opt)
* optA.unwrap() === 10 // true
* optB.unwrap() === 'foo' // true
* ```
*/
export function unzip(opt) {
return opt.map(([a, b]) => [Option.Some(a), Option.Some(b)])
.unwrapOr([Option.None(), Option.None()]);
}