ts-data-forge
Version:
[](https://www.npmjs.com/package/ts-data-forge) [](https://www.npmjs.com/package/ts-data-forge) [ • 1.22 kB
JavaScript
import { isSome } from './optional-is-some.mjs';
import { none } from './optional-none.mjs';
import { some } from './optional-some.mjs';
/**
* Combines two `Optional` values into a single `Optional` containing a tuple.
* If either `Optional` is `None`, returns `None`.
*
* @example
*
* ```ts
* const zipped = Optional.zip(Optional.some('left'), Optional.some(1));
*
* assert.isTrue(Optional.isSome(zipped));
*
* if (Optional.isSome(zipped)) {
* const expected: readonly [string, number] = ['left', 1];
*
* assert.deepStrictEqual(zipped.value, expected);
* }
*
* const missing = Optional.zip(
* Optional.some('value'),
* Optional.none as Optional<number>,
* );
*
* assert.deepStrictEqual(missing, Optional.none);
* ```
*
* @template A The value type of the first `Optional`.
* @template B The value type of the second `Optional`.
* @param optionalA The first `Optional`.
* @param optionalB The second `Optional`.
* @returns An `Optional` containing a tuple of both values, or `None`.
*/
const zip = (optionalA, optionalB) => isSome(optionalA) && isSome(optionalB)
? some([optionalA.value, optionalB.value])
: none;
export { zip };
//# sourceMappingURL=optional-zip.mjs.map