UNPKG

sangja

Version:

JavaScript data structures library

170 lines (153 loc) 3.97 kB
/** * @class * @memberof sangja */ class Optional { /** * Creates a new Optional. * Value parameter is optional. Undefeined for value is not allowed.(Not work well) * @constructor * @param {*} [value=undefined] - Value to contain */ constructor(value = undefined) { this.value = value; } /** * Returns the containing value. * @returns {*|undefined} The containing value. If it does not contain a value, return undefined */ get() { return this.value; } /** * Returns the containing value. * @returns {*} The containing value. If it does not contain a value, return undefined */ getOrElse(value) { if (this.value === undefined) { return value; } return this.value; } /** * If the optional contains a value, returns 1 else 0. * @returns {number} The number of elements in the optional(0 or 1) */ size() { if (this.value === undefined) { return 0; } return 1; } /** * If the optional is empty, returns true else returns false. * @returns {boolean} True if the optional is empty else false */ isEmpty() { if (this.value === undefined) { return true; } return false; } /** * If the optional contains a value, execute the given function f. * @param {function} f - Function to execute */ forEach(f) { if (this.value === undefined) { return; } f(this.value); } /** * If the optional contains a value, execute the given function f * and returns the new optional containing f(this.value).<br> * If the optional is empty or catch error while executing f, returns empty optional. * @param {function} f - Function to execute with this.value * @return {Optional} Optional(f(this.value)) or Optional() */ map(f) { if (this.value === undefined) { return new Optional(); } try { return new Optional(f(this.value)); } catch (err) { return new Optional(); } } /** * If the optional contains a value, execute the given function f * and flatten the result.<br> * Returns the new optional containing flatten result of f(this.value)<br> * If the optional is empty or catch error while executing f, returns empty optional. * @param {function} f - (this.value) => Optional * @return {Optional} Optional(...f(this.value)) or Optional() */ flatMap(f) { if (this.value === undefined) { return new Optional(); } try { return new Optional(...f(this.value)); } catch (err) { return new Optional(); } } /** * If the contained value satisfies the predicate, return this. * If the optional is empty or given predicate returns false, returns empty optional. * @param {function} f - Predicate * @return {Optional} Optional(this.value) or Optional() */ filter(f) { if (this.value === undefined) { return new Optional(); } if (f(this.value)) { return this; } return new Optional(); } /** * If containing value satisfies given f, return true. * If not satisfy f or not contain any value, return false. * @param {function} f - Predicate * @returns {boolean} */ some(f) { if (this.value === undefined) { return false; } return Boolean(f(this.value)); } /** * If not contain any value or containing value satisfies given f, return true. * If not satisfy f, return false. * @param {function} f - Predicate * @returns {boolean} */ every(f) { if (this.value === undefined) { return true; } return Boolean(f(this.value)); } /** * If contains given value v, return true. * @param {*} v * @returns {boolean} */ includes(v) { if (this.value !== undefined && this.value === v) { return true; } return false; } * [Symbol.iterator]() { if (this.value !== undefined) { yield this.value; } } } module.exports = Optional;