

GitHub - mikechabot/maybe-baby: Minimize defensive coding. A JavaScript implemen...
source link: https://github.com/mikechabot/maybe-baby
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

maybe-baby
2.x will be maintained, however, if possible for your repository, you should opt to use TypeScript's optional chaining, which was introduced in 3.7
Install
npm install --save maybe-baby
yarn add maybe-baby
Getting Started
What if we need the zipCode
of the user below, which lives on the address
object?
const user = { email: '[email protected]', address: null, name: { first: 'John', last: null, middle: null } };
Accessing it via dot notation will result in an error:
const zipCode = user.address.zipCode; // Uncaught TypeError: Cannot read property 'zipCode' of undefined
Possible Solutions?
- Write some ugly null checks that don't scale well:
const getZipCode = (user) => { if (user !== null && user !== undefined) { if (user.address !== null && user.address !== undefined) { return user.address.zipCode } } }
-
Use
_.get()
or something similar, but these libraries have large footprints, and most likely won't be implementing the monadic structure. -
Wait for optional chaining to be approved in ECMA, or use babel-plugin-transform-optional-chaining.
-
Use TypeScript's optional chaining
A Better Solution?
- Use
maybe-baby
to minimize defensive coding:
import Maybe from 'maybe-baby'; // Use a function getter const getZipCode = (user) => Maybe.of(() => user.address.zipCode).join();
Now we can safely get the zipCode
without worrying about the shape of the object, or encountering TypeErrors
:
const zipCode = getZipCode(user); console.log(zipCode); // undefined
Docs
Documentation generated via JSDoc.
Migrating from 1.x to 2.x
Breaking changes were introduced in 2.0.0; the following redundant function were removed.
Maybe.of() can replicate the behavior of
prop
,props
, andpath
.
prop(val: string | number)
const obj = Maybe.of({ foo: { bar: [123, 456] } }); // Incorrect const bar = obj.prop("foo").join(); // { bar: [123, 456] } // Correct Maybe.of(() => obj.join().foo).join(); // { bar: [123, 456] } // Incorrect obj .prop("foo") .prop("bar") .prop(1) .join(); // 456 // Correct Maybe.of(() => obj.join().foo.bar[1]).join() // 456
props(...args)
const obj = Maybe.of({ foo: 'bar', baz: [1,2,3] }); // Incorrect obj.props('baz', 0).join(); // 1 // Correct Maybe.of(() => obj.join().baz[0]).join(); // 1
props(val: string)
const obj = Maybe.of({ foo: 'bar', baz: [1,2,3] }); // Incorrect obj.path('baz.0').join() // 1 // Correct Maybe.of(() => obj.join().baz[0]).join(); // 1
API
Check out the API below, or the complete documentation.
of(val: unknown | OfTypeFunc<T>)
Accepts a value of any type, and returns a monad:
const str = Maybe.of('foo'); const num = Maybe.of(123); const bool = Maybe.of(true); const obj = Maybe.of({}); const arr = Maybe.of([]); const empty = Maybe.of(null); const undef = Maybe.of(undefined);
Accepts a function, and sets the function's return value as the monad's value, returns a monad.
If the function results in an error, the monad's value is set to
undefined
.
type OfTypeFunc<T> = () => T; const user = {}; const mZipCode = Maybe.of(() => user.address.zipCode); console.log(mZipCode.join()); // undefined
isJust(): boolean
Returns true
if the value is not null
or undefined
:
Maybe.of(123).isJust(); // true Maybe.of(null).isJust(); // false
isNothing(): boolean
Returns true
if the value is null
or undefined
:
Maybe.of(123).isNothing(); // false Maybe.of(null).isNothing(); // true
join(): T
Returns the value:
Maybe.of(123).join(); // 123 Maybe.of(null).join(); // null
orElse(defaultValue: unknown): Maybe
Chain to the end of a monad to return as the default value if isNothing()
is true
:
Maybe.of(undefined) .orElse('No Value') .join(); // 'No Value'
map(transform: (val: T) => T | Maybe<T>): Maybe
Apply a transformation to the monad, and return a new monad:
const val = 1; const newVal = Maybe.of(val).map(val => val + 1); newVal.join(); // 2;
chain(chain: (val: T) => Maybe<T>): Maybe
Chain together functions that return Maybe monads:
function addOne (val) { return Maybe.of(val + 1); } const three = Maybe.of(1) .chain(addOne) .chain(addOne); three.join(); // 3
Credit
Credit to James Sinclair for writing The Marvellously Mysterious JavaScript Maybe Monad.
Recommend
-
74
Some software has higher than usual requirements for availability, safety or security. Very often in such projects, people practice pragmatic paranoia and coding style called Defensive Programming…
-
36
README.md Verifier "Defend against the impossible, because the impossible will happen."
-
37
README.md Don't roave/dont is a small PHP package aimed at enforcing good practices when it comes to designing
-
36
PHP Strict Casts Strict type casting for super defensive PHP Installation $ composer require ntzm/strict-casts Usage This package provides the following strict ca...
-
7
A guide to implementing dark modes on websites Saturday, 28 September 2019 ∙ 6 minute read Previously I wrote about how to decide to ad...
-
6
# Mahamudra.Cryptography.Notation [Photo by Tezos on Unsplash] EncodeBase58 What is Base58 encoding? Base58 is a binary-to-text encoding created by Satoshi Nakamoto for Bitcoin addresses. The original Bitcoin...
-
10
How soon is too soon to report progress from a C++/WinRT coroutine that implements a Windows Runtime asynchronous operation with progress?
-
6
BFFS - Backend for Frontend Shield BFFS is a simple project built with Laravel that implements the...
-
5
Defensive coding by Mark Seemann This post examines the advantages and disadvantages of defensive coding. One of my readers, Barry Giles, recently wrote me and asked a q...
-
7
Download source code - 84.2 KB Introduction Decision trees are a popular machine learning algorithm used for both...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK