@ncodedcode/ncode_react_lib
Version:
ncode react application context library
115 lines • 3.44 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NCOptional = void 0;
var NCOptional = (function () {
function NCOptional(value) {
this.value = value;
}
NCOptional.of = function (value) {
return new NCOptional(value);
};
NCOptional.ofNullable = function (value) {
if (value === undefined || value === null)
return NCOptional.empty();
return new NCOptional(value);
};
NCOptional.try = function (f) {
try {
return NCOptional.of(f());
}
catch (e) {
return NCOptional.empty();
}
};
NCOptional.empty = function () {
return new NCOptional(undefined);
};
NCOptional.prototype.isPresent = function () {
return this.value !== undefined && this.value !== null;
};
NCOptional.prototype.isEmpty = function () {
return this.value === undefined || this.value === null;
};
NCOptional.prototype.ifPresent = function (action) {
if (this.isPresent())
action(this.value);
};
NCOptional.prototype.ifPresentOrElse = function (action, orElse) {
if (this.isPresent()) {
action(this.value);
}
else {
orElse();
}
};
NCOptional.prototype.get = function () {
if (this.isEmpty())
throw Error("value has empty");
return this.value;
};
NCOptional.prototype.orUndefined = function () {
if (this.isEmpty())
return undefined;
return this.get();
};
NCOptional.prototype.orElse = function (other) {
if (this.isEmpty())
return other;
return this.get();
};
NCOptional.prototype.orElseGet = function (other) {
if (this.isEmpty())
return other();
return this.get();
};
NCOptional.prototype.orElseThrow = function (error) {
if (this.isEmpty())
throw error();
return this.get();
};
NCOptional.prototype.do = function (f) {
if (this.isEmpty())
return this;
f(this.get());
return this;
};
NCOptional.prototype.map = function (mapper) {
if (this.isEmpty())
return NCOptional.empty();
return NCOptional.of(mapper(this.get()));
};
NCOptional.prototype.try = function (f) {
if (this.isEmpty())
return NCOptional.empty();
try {
return NCOptional.ofNullable(f(this.get()));
}
catch (e) {
return NCOptional.empty();
}
};
NCOptional.prototype.flatMap = function (mapper) {
if (this.isEmpty())
return NCOptional.empty();
var optV = mapper(this.get());
if (optV.isPresent())
return NCOptional.of(optV.get());
return NCOptional.empty();
};
NCOptional.prototype.filter = function (predicate) {
if (this.isPresent() && predicate(this.get()))
return this;
return NCOptional.empty();
};
NCOptional.prototype.equals = function (other) {
return this.value === other.value;
};
NCOptional.prototype.toString = function () {
if (this.isEmpty())
return "empty";
return "".concat(this.get());
};
return NCOptional;
}());
exports.NCOptional = NCOptional;
//# sourceMappingURL=NCOptional.js.map