parser-combinator
Version:
Parser combinators
103 lines (91 loc) • 2.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*
* Parsec
* https://github.com/d-plaindoux/parsec
*
* Copyright (c) 2016 Didier Plaindoux
* Licensed under the LGPL2 license.
*/
/**
* Private class Option, accessible from someOrNone() or none()
*/
var Option = function () {
function Option(value) {
_classCallCheck(this, Option);
this.value = value;
}
_createClass(Option, [{
key: "isPresent",
value: function isPresent() {
return this.value !== null && this.value !== undefined;
}
}, {
key: "map",
value: function map(bindCall) {
if (this.isPresent()) {
return someOrNone(bindCall(this.value));
} else {
return this;
}
}
}, {
key: "flatmap",
value: function flatmap(bindCall) {
if (this.isPresent()) {
return bindCall(this.value);
} else {
return this;
}
}
}, {
key: "filter",
value: function filter(f) {
if (this.isPresent() && f(this.value)) {
return this;
}
// equivalent of return none() without cyclic creation
// eslint : no-use-before-define
return new Option();
}
}, {
key: "get",
value: function get() {
return this.value;
}
}, {
key: "orElse",
value: function orElse(value) {
if (this.isPresent()) {
return this.value;
} else {
return value;
}
}
}, {
key: "orLazyElse",
value: function orLazyElse(value) {
if (this.isPresent()) {
return this.value;
} else {
return value();
}
}
}]);
return Option;
}();
function someOrNone(value) {
return new Option(value);
}
function none() {
return new Option();
}
exports.default = {
some: someOrNone,
none: none
};
//# sourceMappingURL=option.js.map