maybell v0.2.0
maybell is a lightweight library that provides functions to fight against undefined.
Installation
$ npm install maybellAPI
fmap
fmap :: (a -> b) -> Maybe a -> Maybe bTakes a function, applies the function to the input value, returns the result if the input value is not undefined, or returns undefined if the value is undefined.
Example:
fmap(function(x) {
return x + 1;
})(undefined);
//=> undefined
fmap(function(x) {
return x + 1;
})(2);
//=> 3Maybe a here means a value that could be either undefined or any other type. It could be undefined or "foo", in which case Type "a" is String. Or it could be undefined or 1, then Type "a" is Number. But it can't be undefined or "foo" or 1.
Maybe b means its value could be undefined or any other type which doesn't have to be the same type as Type "a".
Other use case:
function getUserName(user) {
return user.name;
}
getUserName();
//=> !TypeError: Cannot read property 'name' of undefined
// Workaround
function getUserName(user) {
if(!user) {
return ;
}
return user.name;
}
getUserName();
//=> undefined
// Better
var getUserNameFromMaybeUser = fmap(getUserName);
getUserNameFromMaybeUser();
//=> undefined
getUserNameFromMaybeUser({ name: 'Leo' });
//=> 'Leo'empty
empty :: * -> Maybe *returns 'undefined'
empty()
//=> undefinedisNothing
isNothing :: Maybe a -> BooleanChecks if the input is undefined or null
isNothing(undefined);
//=> true
isNothing(null);
//=> true
isNothing(0)
//=> false
isNothing(false)
//=> falseisJust
isJust :: Maybe a -> BooleanChecks if the input is not either undefined or null
isJust(undefined);
//=> false
isJust(null);
//=> false
isJust(0);
//=> true
isJust(false);
//=> truelift
lift :: (a -> b -> ... -> n -> x) -> Maybe a -> Maybe b -> ... -> Maybe n -> Maybe x"lifts" a function and applies the input values to the function if none of them is 'undefined', otherwise returns 'undefined'.
var sumValue = function(a, b) {
return a.value + b.value;
};
lift(sumValue)({ value: 1 }, { value: 2 });
//=> 3;
lift(sumValue)(undefined, { value: 2 });
//=> undefined
lift(sumValue)();
//=> undefinedsequence
sequence :: Array Maybe a -> Maybe Array aTakes a list of maybe value and returns the list if none of them is 'undefined'. Otherwise returns 'undefined' if any of list item is 'undefined'.
sequence([undefined, 1, 2, 3]);
//=> undefined
sequence([1, 2, 3, 4]);
//=> [1, 2, 3, 4]
sequence([])
//=> []traverse
traverse :: (a -> Maybe b) -> Array a -> Maybe Array bMaps map a function, which takes a value and returns a maybe value, over a list of value, and use sequence to transform the list of maybe value into a list of value.
function gt3(x) {
if (x > 3) {
return x;
} else {
return undefined;
}
}
traverse(gt3)([5, 6, 7]);
//=> [5, 6, 7]
traverse(gt3)([0, 1, 2]);
//=> undefined
traverse(gt3)([5, 6, 1]);
//=> undefined
traverse(gt3)([]);
//=> []