1.0.1 • Published 7 years ago

tochkak-maybe v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
7 years ago

Implementation of monad Maybe in javascript. It used for working with data wich may not exist in runtime.

class Maybe {
	static of = (value) => new Maybe(value); // method for creating Maybe container

	constructor(value) {
		this.value = value;
	}

	isNothing() { // method for checking that Maybe value exist or not
		return (
			this.value === null ||
			this.value === undefined ||
			Number.isNaN(this.value)
		);
	}

	isJust() { // antonim of isNothing method
		return !this.isNothing();
	}

	map(transform) { // method for safely working with Maybe value
		return this.isNothing() ? Maybe.of(null) : Maybe.of(transform(this.value));
	}

	get() { // method for extract Maybe value
		return this.value;
	}

	getOrElse(defaultValue) { // method for extract Maybe value with default value
		return this.isJust() ? this.value : defaultValue;
	}
}
1.0.1

7 years ago

1.0.0

7 years ago