utilitaire
Version:
Common utils for JavaScript and Typescript development
116 lines • 3.82 kB
JavaScript
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/**
* The Option type encapsulates an optional value.
* A value of type Option<T> either :
* - contains a value of type T,
* - or it is empty (represented as None).
*
* Using Option is a good way to deal with null and undefined.
*/
var Option = (function () {
function Option() {
}
Object.defineProperty(Option.prototype, "value", {
/**
* Returns the option's value.
* @note The option must be nonEmpty and this will return
* undefined if the option is empty.
*/
get: function () { return this._value; },
enumerable: true,
configurable: true
});
Option.prototype.valueOrElse = function (value) {
if (this.isDefined())
return this.value;
else
return value;
};
Option.prototype.valueOrNull = function () {
if (this.isDefined())
return this.value;
else
return null;
};
Option.prototype.isDefined = function () {
return this.value != undefined;
};
Option.prototype.isEmpty = function () {
return !this.isDefined();
};
Option.prototype.flatMap = function (fn) {
if (this.isEmpty())
return exports.None;
else {
var resultOption = fn(this.value);
if (Option.isDefinedValue(resultOption))
return resultOption;
else
return exports.None;
}
};
Option.prototype.map = function (fn) {
if (this.isDefined())
return Option.of(fn(this.value));
else
return exports.None;
};
Option.prototype.forEach = function (fn) {
if (this.isDefined())
fn(this.value);
};
Option.isDefinedValue = function (value) {
return (value != null && value != undefined);
};
Option.of = function (value) {
// If the provided _value is null or undefined, we let the property
// _value undefined else we defining it!
if (Option.isDefinedValue(value))
return new Some(value);
else
return exports.None;
};
Option.empty = function () {
return exports.None;
};
return Option;
}());
exports.Option = Option;
var Some = (function (_super) {
__extends(Some, _super);
function Some(_value) {
_super.call(this);
if (_value == null || _value == undefined)
throw new Error("Some constructor expected non null an non undefined value. " +
_value + "given. " +
"If you are not sure whether your value is defined or not, please " +
"consider the static method Option.of<T>(value:T) that will " +
"deal with the null/undefined case properly.");
this._value = _value;
}
return Some;
}(Option));
exports.Some = Some;
var NoneT = (function (_super) {
__extends(NoneT, _super);
function NoneT() {
_super.apply(this, arguments);
}
return NoneT;
}(Option));
/**
* Here we use the instance of {} as instance of None. Indeed, the '{}' can be
* viewed as an instance of Option because of the Option class definition.
* as we did not defined a '_value' attribute, every call to the isDefined() method
* will always yield false. It is then enough to have a correct working Option instance
* For example see the 'map', and 'foreach' method definition.
*
* @type {Option<typeof undefined>}
*/
exports.None = new NoneT();
//# sourceMappingURL=Option.js.map