optional.js
1 "use strict"; 2 Object.defineProperty(exports, "__esModule", { value: true }); 3 exports.flatMapOptional = exports.mapOptional = exports.unsafeUnwrapOptional = exports.unwrapOptional = exports.isSome = exports.isNothing = exports.unsafeUninitialized = void 0; 4 /** 5 * Bypass the protection provided by the `Optional` type 6 * and pretend to produce a value of `Some<T>` while 7 * actually returning `Nothing`. 8 */ 9 function unsafeUninitialized() { 10 return undefined; 11 } 12 exports.unsafeUninitialized = unsafeUninitialized; 13 /** 14 * Test whether an optional does not contain a value. 15 * 16 * @param value - An optional value to test. 17 */ 18 function isNothing(value) { 19 return value === undefined || value === null; 20 } 21 exports.isNothing = isNothing; 22 /** 23 * Test whether an optional contains a value. 24 * @param value - An optional value to test. 25 */ 26 function isSome(value) { 27 return value !== undefined && value !== null; 28 } 29 exports.isSome = isSome; 30 /** 31 * Unwrap the value contained in a given optional, 32 * throwing an error if there is no value. 33 * 34 * @param value - A value to unwrap. 35 */ 36 function unwrapOptional(value) { 37 if (isNothing(value)) { 38 throw new ReferenceError(); 39 } 40 return value; 41 } 42 exports.unwrapOptional = unwrapOptional; 43 /** 44 * Unwrap the value contained in a given optional 45 * without checking if the value exists. 46 * 47 * @param value - A value to unwrap. 48 */ 49 function unsafeUnwrapOptional(value) { 50 return value; 51 } 52 exports.unsafeUnwrapOptional = unsafeUnwrapOptional; 53 function mapOptional(value, body) { 54 if (isSome(value)) { 55 return body(value); 56 } 57 else { 58 return value; 59 } 60 } 61 exports.mapOptional = mapOptional; 62 function flatMapOptional(value, body) { 63 if (isSome(value)) { 64 return body(value); 65 } 66 else { 67 return value; 68 } 69 } 70 exports.flatMapOptional = flatMapOptional; 71 //# sourceMappingURL=optional.js.map