UNPKG

@masala/parser

Version:
109 lines (95 loc) 3 kB
'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. */ /** * Need a reference object in memory for uniqueness. Symbols causes troubles. */ var empty = { __MASALA_EMPTY__: 'empty' }; /** * 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 !== empty; } }, { 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(empty); } }, { 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(empty); } exports.default = { some: someOrNone, none: none }; //# sourceMappingURL=option.js.map