|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +exports.None = exports.Some = exports.Option = exports.none = void 0; |
| 4 | +const result_js_1 = require("./result.cjs"); |
| 5 | +/** |
| 6 | + * The primitive None value. |
| 7 | + * |
| 8 | + * _Note: To construct a None variant Option, please use `None()` instead._ |
| 9 | + */ |
| 10 | +exports.none = Symbol("None"); |
| 11 | +/** |
| 12 | + * A Rust-like Option class. |
| 13 | + * |
| 14 | + * _Note: Please use either `Some` or `None` to construct an Option._ |
| 15 | + * |
| 16 | + * @example |
| 17 | + * ``` |
| 18 | + * function divide(left: number, right: number): Option<number> { |
| 19 | + * if (right === 0) return None(); |
| 20 | + * |
| 21 | + * return Some(left / right); |
| 22 | + * } |
| 23 | + * |
| 24 | + * ``` |
| 25 | + */ |
| 26 | +class Option { |
| 27 | + val; |
| 28 | + /** |
| 29 | + * A constructor for an Option. |
| 30 | + * |
| 31 | + * _Note: Please use either `Some` or `None` to construct Options._ |
| 32 | + * |
| 33 | + * @param {T | typeof none} input The value to wrap in an Option. |
| 34 | + */ |
| 35 | + constructor(input) { |
| 36 | + this.val = input; |
| 37 | + } |
| 38 | + /** |
| 39 | + * Converts Option into a String for display purposes. |
| 40 | + */ |
| 41 | + get [Symbol.toStringTag]() { |
| 42 | + return `Option`; |
| 43 | + } |
| 44 | + /** |
| 45 | + * Iterator support for Option. |
| 46 | + * |
| 47 | + * _Note: This method will only yeild if the Option is Some._ |
| 48 | + * @returns {IterableIterator<T>} |
| 49 | + */ |
| 50 | + *[Symbol.iterator]() { |
| 51 | + if (this.isSome()) |
| 52 | + yield this.val; |
| 53 | + } |
| 54 | + /** |
| 55 | + * Returns true if contained value isnt None. |
| 56 | + * @returns {boolean} |
| 57 | + */ |
| 58 | + isSome() { |
| 59 | + return this.val !== exports.none; |
| 60 | + } |
| 61 | + /** |
| 62 | + * Returns true if contained value is None. |
| 63 | + * |
| 64 | + * @returns {boolean} |
| 65 | + */ |
| 66 | + isNone() { |
| 67 | + return this.val === exports.none; |
| 68 | + } |
| 69 | + /** |
| 70 | + * Returns the contained Some value, consuming the Option. |
| 71 | + * Throws an Error with a given message if the contained value is None. |
| 72 | + * |
| 73 | + * @param {string} msg An error message to throw if contained value is None. |
| 74 | + * @returns {T} |
| 75 | + */ |
| 76 | + expect(msg) { |
| 77 | + if (this.isNone()) { |
| 78 | + throw new Error(msg); |
| 79 | + } |
| 80 | + return this.val; |
| 81 | + } |
| 82 | + /** |
| 83 | + * Returns the contained Some value, consuming the Option. |
| 84 | + * Throws an Error if contained value is None. |
| 85 | + * |
| 86 | + * @returns {T} |
| 87 | + */ |
| 88 | + unwrap() { |
| 89 | + if (this.isNone()) { |
| 90 | + throw new Error(`Unwrap called on None`); |
| 91 | + } |
| 92 | + return this.val; |
| 93 | + } |
| 94 | + /** |
| 95 | + * Returns the contained Some value or a provided default. |
| 96 | + * |
| 97 | + * @param {T} fallback A default value to return if contained value is an Option. |
| 98 | + * @returns {T} |
| 99 | + */ |
| 100 | + unwrapOr(fallback) { |
| 101 | + if (this.isNone()) { |
| 102 | + return fallback; |
| 103 | + } |
| 104 | + return this.val; |
| 105 | + } |
| 106 | + /** |
| 107 | + * Returns the contained Some value or computes it from a closure. |
| 108 | + * |
| 109 | + * @param {Function} fn A function that computes a new value. |
| 110 | + * @returns {T} |
| 111 | + */ |
| 112 | + unwrapOrElse(fn) { |
| 113 | + if (this.isNone()) { |
| 114 | + return fn(); |
| 115 | + } |
| 116 | + return this.val; |
| 117 | + } |
| 118 | + /** |
| 119 | + * Maps an Option<T> to Option<U> by applying a function to a contained Some value, leaving None values untouched. |
| 120 | + * |
| 121 | + * @param {Function} fn A mapping function. |
| 122 | + * @returns {Option<U>} |
| 123 | + */ |
| 124 | + map(fn) { |
| 125 | + if (this.isSome()) { |
| 126 | + return new Option(fn(this.val)); |
| 127 | + } |
| 128 | + return this; |
| 129 | + } |
| 130 | + /** |
| 131 | + * Returns the provided fallback (if None), or applies a function to the contained value. |
| 132 | + * |
| 133 | + * @param {U} fallback A defualt value |
| 134 | + * @param {Function} fn A mapping function. |
| 135 | + * @returns {U} |
| 136 | + */ |
| 137 | + mapOr(fallback, fn) { |
| 138 | + if (this.isSome()) { |
| 139 | + return fn(this.val); |
| 140 | + } |
| 141 | + return fallback; |
| 142 | + } |
| 143 | + /** |
| 144 | + * Returns `or` if the Option is None, otherwise returns self. |
| 145 | + * |
| 146 | + * @param {Option<T>} or An alternative Option value |
| 147 | + * @returns {Option<T>} |
| 148 | + */ |
| 149 | + or(or) { |
| 150 | + if (this.isSome()) { |
| 151 | + return this; |
| 152 | + } |
| 153 | + return or; |
| 154 | + } |
| 155 | + /** |
| 156 | + * Transforms the `Option<T>` into a `Result<T, E>`, mapping Some to Ok and None to Err. |
| 157 | + * |
| 158 | + * @param {E} err An error to return if the Option is None. |
| 159 | + * @returns {Result<T, E>} |
| 160 | + * |
| 161 | + * @example |
| 162 | + * ``` |
| 163 | + * const result = Some(2).okOr("Error"); // => Ok(2) |
| 164 | + * ``` |
| 165 | + */ |
| 166 | + okOr(err) { |
| 167 | + if (this.isSome()) { |
| 168 | + return (0, result_js_1.Ok)(this.val); |
| 169 | + } |
| 170 | + else { |
| 171 | + return (0, result_js_1.Err)(err); |
| 172 | + } |
| 173 | + } |
| 174 | + /** |
| 175 | + * Returns contained value for use in matching. |
| 176 | + * |
| 177 | + * _Note: Please only use this to match against in `if` or `swtich` statments._ |
| 178 | + * |
| 179 | + * @returns {T | typeof none} |
| 180 | + * @example |
| 181 | + * ```ts |
| 182 | + * function coolOrNice(input: Option<string>): Option<void> { |
| 183 | + * switch (input.peek()) { |
| 184 | + * case "cool": |
| 185 | + * console.log("Input was the coolest!"); |
| 186 | + * break; |
| 187 | + * case "nice": |
| 188 | + * console.log("Input was was the nicest!"); |
| 189 | + * break |
| 190 | + * default: |
| 191 | + * return None(); |
| 192 | + * } |
| 193 | + * return Some() |
| 194 | + * } |
| 195 | + * ``` |
| 196 | + */ |
| 197 | + peek() { |
| 198 | + return this.val; |
| 199 | + } |
| 200 | + /** |
| 201 | + * Converts from Option<Option<T> to Option<T> |
| 202 | + * @returns Option<T> |
| 203 | + */ |
| 204 | + flatten() { |
| 205 | + if (this.val instanceof Option) { |
| 206 | + return this.val; |
| 207 | + } |
| 208 | + return this; |
| 209 | + } |
| 210 | + /** |
| 211 | + * Run a closure and convert it into an Option. |
| 212 | + * If the function returns `null` or `undefined`, an Option containing None will be reutrned. |
| 213 | + * |
| 214 | + * _Note: Please use `fromAsync` to capture the result of asynchronous closures._ |
| 215 | + * @param {Function} fn The closure to run. |
| 216 | + * @returns {Option<T>} The result of the closure. |
| 217 | + */ |
| 218 | + static from(fn) { |
| 219 | + const result = fn(); |
| 220 | + if (result === null || result === undefined) { |
| 221 | + return new Option(exports.none); |
| 222 | + } |
| 223 | + else { |
| 224 | + return new Option(result); |
| 225 | + } |
| 226 | + } |
| 227 | + /** |
| 228 | + * Run an asynchronous closure and convert it into an Option. |
| 229 | + * If the function returns `null` or `undefined`, an Option containing None will be reutrned. |
| 230 | + * |
| 231 | + * _Note: Please use `from` to capture the result of synchronous closures._ |
| 232 | + * @param {Function} fn The closure to run. |
| 233 | + * @returns {Promise<Option<T>>} The result of the closure. |
| 234 | + */ |
| 235 | + static async fromAsync(fn) { |
| 236 | + const result = await fn(); |
| 237 | + if (result === null || result === undefined) { |
| 238 | + return new Option(exports.none); |
| 239 | + } |
| 240 | + else { |
| 241 | + return new Option(result); |
| 242 | + } |
| 243 | + } |
| 244 | +} |
| 245 | +exports.Option = Option; |
| 246 | +/** |
| 247 | + * Construct an Option from a value other than None. |
| 248 | + * |
| 249 | + * @param {Exclude<T, typeof none>} input a value that isnt None. |
| 250 | + * @returns {Option<T>} |
| 251 | + * @example |
| 252 | + * ```ts |
| 253 | + * function divide(left: number, right: number): Option<number> { |
| 254 | + * if (right === 0) return None(); |
| 255 | + * |
| 256 | + * return Some(left / right); |
| 257 | + * } |
| 258 | + * |
| 259 | + * ``` |
| 260 | + * |
| 261 | + * @example |
| 262 | + * ```ts |
| 263 | + * const foo = Some("Value"); |
| 264 | + * |
| 265 | + * if (foo instanceof Some) { |
| 266 | + * // Do something |
| 267 | + * } |
| 268 | + * ``` |
| 269 | + */ |
| 270 | +function Some(input) { |
| 271 | + return new Option(input); |
| 272 | +} |
| 273 | +exports.Some = Some; |
| 274 | +Object.defineProperty(Some, Symbol.hasInstance, { |
| 275 | + value: (instance) => { |
| 276 | + if (typeof instance !== "object") |
| 277 | + return false; |
| 278 | + return instance?.isSome() || false; |
| 279 | + }, |
| 280 | +}); |
| 281 | +/** |
| 282 | + * Construct the None variant of Option. |
| 283 | + * |
| 284 | + * @returns {Option<T>} |
| 285 | + * @example |
| 286 | + * ```ts |
| 287 | + * function divide(left: number, right: number): Option<number> { |
| 288 | + * if (right === 0) return None(); |
| 289 | + * |
| 290 | + * return Some(left / right); |
| 291 | + * } |
| 292 | + * ``` |
| 293 | + * @example |
| 294 | + * ```ts |
| 295 | + * const foo = None(); |
| 296 | + * |
| 297 | + * if (foo instanceof None) { |
| 298 | + * // Do something |
| 299 | + * } |
| 300 | + * ``` |
| 301 | + */ |
| 302 | +function None() { |
| 303 | + return new Option(exports.none); |
| 304 | +} |
| 305 | +exports.None = None; |
| 306 | +Object.defineProperty(None, Symbol.hasInstance, { |
| 307 | + value: (instance) => { |
| 308 | + if (typeof instance !== "object") |
| 309 | + return false; |
| 310 | + return instance?.isNone() || false; |
| 311 | + }, |
| 312 | +}); |
0 commit comments