@creditkarma/dynamic-config
Version:
Dynamic Config for Node.js backed by Consul and Vault
199 lines • 4.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Nothing = exports.Just = exports.Maybe = void 0;
class Maybe {
static fromNullable(val) {
if (val === null || val === undefined) {
return new Nothing();
}
else {
return new Just(val);
}
}
static nothing() {
return new Nothing();
}
static just(val) {
return new Just(val);
}
static all(...args) {
const values = [];
for (const item of args) {
if (item.isNothing()) {
return new Nothing();
}
else {
values.push(item.get());
}
}
return new Just(values);
}
}
exports.Maybe = Maybe;
class Just extends Maybe {
constructor(val) {
super();
this.value = val;
}
static create(val) {
return new Just(val);
}
toString() {
return `Just(${this.value})`;
}
/**
* join :: Maybe (Maybe a) -> Maybe a
*
* Takes a nested Maybe and removes one level of nesting.
*
* @name join
* @method
* @memberof Maybe#
* @returns {Maybe}
*/
join() {
return this.get();
}
/**
* @name fork
* @method
* @memberof Maybe#
* @param {Function} justFn Function to call with value of Just
* @param {Function} nothingFn Function to call with value of Nothing
* @returns {*} The return value of the matching function
*/
fork(justFn, _) {
return justFn(this.value);
}
/**
* map :: Maybe a -> (a -> b) -> Maybe b
*
* Transforms the value of a Maybe with the given function.
*
* @name map
* @method
* @memberof Maybe#
* @param {Function} mapping Function used to map value of Maybe
* @returns {Maybe}
*/
map(mapping) {
return new Just(mapping(this.value));
}
/**
* chain :: Maybe a -> (a -> Maybe b) -> Maybe b
*
* Takes the value of a Maybe and gives it to a function that returns a new Maybe.
*
* @name chain
* @method
* @memberof Maybe#
* @param {Function} mapping Function used to create new Maybe
* @returns {Maybe}
*/
chain(mapping) {
return this.map(mapping).join();
}
/**
* filter :: Maybe a -> (a -> Boolean) -> Maybe a
*
* Turns a Just into a Nothing if the predicate returns false
*
* @name filter
* @method
* @memberof Maybe#
* @param {Function} predicate Function used to test value
* @returns {Maybe}
*/
filter(predicate) {
if (predicate(this.value)) {
return new Just(this.value);
}
else {
return new Nothing();
}
}
/**
* get :: Maybe a -> a
*
* Extract the value from a Maybe
*
* @name get
* @method
* @memberof Maybe#
* @returns {*}
*/
get() {
return this.value;
}
/**
* getOrElse :: Maybe a -> a -> a
*
* @name getOrElse
* @method
* @memberof Maybe#
* @returns {*}
*/
getOrElse(_) {
return this.value;
}
/**
* isNothing :: Maybe a -> Boolean
*
* @name isNothing
* @method
* @memberof Maybe#
* @returns {Boolean}
*/
isNothing() {
return false;
}
/**
* isJust :: Maybe a -> Boolean
*
* @name isJust
* @method
* @memberof Maybe#
* @returns {Boolean}
*/
isJust() {
return true;
}
}
exports.Just = Just;
class Nothing extends Maybe {
static create() {
return new Nothing();
}
toString() {
return 'Nothing';
}
fork(_, nothingFn) {
return nothingFn();
}
join() {
return new Nothing();
}
map(_) {
return new Nothing();
}
filter(_) {
return new Nothing();
}
chain(_) {
return new Nothing();
}
get() {
throw new Error('Cannot get the value of a Nothing');
}
getOrElse(val) {
return val;
}
isJust() {
return false;
}
isNothing() {
return true;
}
}
exports.Nothing = Nothing;
//# sourceMappingURL=Maybe.js.map